From c119feb821bdb1e6ef407f55056173f752c01c32 Mon Sep 17 00:00:00 2001
From: zainali5120 <512061637@qq.com>
Date: Thu, 16 Jul 2020 17:11:20 +0800
Subject: [PATCH] 订单止盈止损类提交

---
 src/main/java/com/xcong/excoin/quartz/job/BlockCoinUpdateJob.java                  |    5 +
 src/main/java/com/xcong/excoin/modules/blackchain/service/EosService.java          |   84 +++++++++++++++++++++
 src/main/java/com/xcong/excoin/modules/blackchain/model/EosResult.java             |   28 +++++++
 src/main/java/com/xcong/excoin/modules/coin/service/impl/BlockCoinServiceImpl.java |   66 ++++++++++++++++
 src/main/java/com/xcong/excoin/modules/coin/service/BlockCoinService.java          |    2 
 5 files changed, 185 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/blackchain/model/EosResult.java b/src/main/java/com/xcong/excoin/modules/blackchain/model/EosResult.java
new file mode 100644
index 0000000..ca0bd19
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/modules/blackchain/model/EosResult.java
@@ -0,0 +1,28 @@
+package com.xcong.excoin.modules.blackchain.model;
+
+import lombok.Data;
+
+/**
+ *  数据样例
+ *  EosResult(quantity=2.0000 EOS, memo=aa, from=okbtothemoon, to=biueeostoken, accountActionSeq=2)
+ */
+@Data
+public class EosResult {
+    /**
+     *  转账数量
+     */
+    private String quantity;
+
+    /**
+     * 转账标记
+     */
+    private String memo;
+    private String from;
+    private String to;
+
+    /**
+     * 这笔转账对应于这个账户的序号
+     */
+    private Integer accountActionSeq;
+
+}
diff --git a/src/main/java/com/xcong/excoin/modules/blackchain/service/EosService.java b/src/main/java/com/xcong/excoin/modules/blackchain/service/EosService.java
new file mode 100644
index 0000000..4aad013
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/modules/blackchain/service/EosService.java
@@ -0,0 +1,84 @@
+package com.xcong.excoin.modules.blackchain.service;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.xcong.excoin.modules.blackchain.model.EosResult;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class EosService {
+    // 正式网络 https://api-v2.eosasia.one https://proxy.eosnode.tools
+    // 测试网络 https://api-kylin.eosasia.one
+    // "eosqxyx11111", "5JJB2oiCXwASK9fumjyTgbtHcwDVedVRaZda1kBhFuQcmjnrDWB"
+    //private final static String account = "huobideposit";
+    public final static String ACCOUNT = "biueeostoken";
+    // private String account;
+    private final static String EOS_NAME = "EOS";
+    public static final String K_mnemonic = "mnemonic";
+    public static final String K_privateKey = "privateKey";
+    public static final String K_publicKey = "publicKey";
+    private static final String EOS_TOKEN = "eosio.token";
+    private static final String ACTION_TRANSFER = "transfer";
+
+
+    public static void main(String[] args) throws Exception {
+
+        List<EosResult> actions = getActions(0, 3);
+        for(EosResult eosResult:actions){
+            System.out.println(eosResult.toString());
+            String quantity = eosResult.getQuantity();
+            String amountStr = quantity.split("")[0];
+            System.out.println(quantity);
+            System.out.println(new BigDecimal(amountStr));
+        }
+    }
+
+
+    /**
+     *
+     * @param pos 从第几条开始(包括pos)
+     * @param offset 查询条数
+     * @return
+     */
+    public static List<EosResult> getActions(int pos, int offset) {
+        JSONObject param = new JSONObject();
+        param.put("account_name", ACCOUNT);
+        param.put("pos", pos);
+        param.put("offset", offset);
+        Map<String, String> header = new HashMap<String, String>();
+        header.put("content-type", "application/json");
+        String res = HttpUtil.post("https://api.eosflare.io/v1/eosflare/get_actions", null, param.toJSONString(), header);
+        System.out.println(res);
+        List<EosResult> results = new ArrayList<>();
+        try {
+
+            JSONObject jsonObject = JSONObject.parseObject(res);
+            JSONArray actions = jsonObject.getJSONArray("actions");
+            if (actions != null && actions.size() > 0) {
+                int size = actions.size();
+                EosResult eosResult = null;
+                for (int i = 0; i < size; i++) {
+                    JSONObject jsonObject1 = actions.getJSONObject(i);
+                    // 当前交易序号
+                    Object account_action_seq = jsonObject1.get("account_action_seq");
+                    JSONObject action_trace = jsonObject1.getJSONObject("action_trace");
+                    JSONObject act = action_trace.getJSONObject("act");
+                    Object account = act.get("account");
+                    if (!EOS_TOKEN.equals(account)) {
+                        continue;
+                    }
+                    eosResult = JSONObject.parseObject(act.getString("data"), EosResult.class);
+                    eosResult.setAccountActionSeq((Integer) account_action_seq);
+                    results.add(eosResult);
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return results;
+    }
+}
diff --git a/src/main/java/com/xcong/excoin/modules/coin/service/BlockCoinService.java b/src/main/java/com/xcong/excoin/modules/coin/service/BlockCoinService.java
index 4944af5..9a78edb 100644
--- a/src/main/java/com/xcong/excoin/modules/coin/service/BlockCoinService.java
+++ b/src/main/java/com/xcong/excoin/modules/coin/service/BlockCoinService.java
@@ -10,4 +10,6 @@
 
     public void updateBtc();
 
+    public void updateEos();
+
 }
diff --git a/src/main/java/com/xcong/excoin/modules/coin/service/impl/BlockCoinServiceImpl.java b/src/main/java/com/xcong/excoin/modules/coin/service/impl/BlockCoinServiceImpl.java
index 0fbce89..1eae8af 100644
--- a/src/main/java/com/xcong/excoin/modules/coin/service/impl/BlockCoinServiceImpl.java
+++ b/src/main/java/com/xcong/excoin/modules/coin/service/impl/BlockCoinServiceImpl.java
@@ -5,7 +5,9 @@
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.StrUtil;
 import com.xcong.excoin.common.enumerates.CoinTypeEnum;
+import com.xcong.excoin.modules.blackchain.model.EosResult;
 import com.xcong.excoin.modules.blackchain.service.BtcService;
+import com.xcong.excoin.modules.blackchain.service.EosService;
 import com.xcong.excoin.modules.blackchain.service.EthService;
 import com.xcong.excoin.modules.blackchain.service.UsdtService;
 import com.xcong.excoin.modules.coin.service.BlockCoinService;
@@ -18,11 +20,14 @@
 import com.xcong.excoin.modules.member.entity.MemberEntity;
 import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
 import com.xcong.excoin.utils.LogRecordUtils;
+import com.xcong.excoin.utils.RedisUtils;
 import com.xcong.excoin.utils.ThreadPoolUtils;
 import com.xcong.excoin.utils.dingtalk.DingTalkUtils;
 import com.xcong.excoin.utils.mail.Sms106Send;
 import com.xcong.excoin.utils.mail.SubMailSend;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -48,6 +53,11 @@
     private MemberCoinChargeDao memberCoinChargeDao;
     @Resource
     private MemberWalletCoinDao memberWalletCoinDao;
+
+    @Resource
+    private RedisUtils redisUtils;
+
+    private final static String EOS_SEQ_KEY="eos_seq_key";
 
     @Transactional(rollbackFor = Exception.class)
     @Override
@@ -250,6 +260,62 @@
         }
     }
 
+    @Override
+    public void updateEos() {
+        // 获取上次读取的序号
+        int pos= 0;
+        //每次获取的条数
+        int offset = 10;
+
+        String eosSeq = redisUtils.getString(EOS_SEQ_KEY);
+        if(StringUtils.isNotBlank(eosSeq)){
+            pos = Integer.valueOf(eosSeq);
+        }
+        // 记录最大的seq
+        int seq = pos;
+        List<EosResult> actions = EosService.getActions(pos, offset);
+        if(CollectionUtils.isNotEmpty(actions)){
+            for(EosResult eosResult:actions){
+                String to = eosResult.getTo();
+                Integer accountActionSeq = eosResult.getAccountActionSeq();
+                if(accountActionSeq>seq){
+                    seq = accountActionSeq;
+                }
+                if(!EosService.ACCOUNT.equals(to)){
+                    // 判断是否是收款
+                    continue;
+                }
+                // 处理收款
+                String quantity = eosResult.getQuantity();
+                String memo = eosResult.getMemo();
+                if(StringUtils.isBlank(memo)){
+                    // 没有标记的跳过
+                    continue;
+                }
+                if(StringUtils.isNotBlank(quantity)){
+                    // 转账额
+                    String amountStr = quantity.split("")[0];
+                    List<MemberCoinAddressEntity> memberCoinAddress = memberCoinAddressDao.selectAllBlockAddressBySymbolAndTag(CoinTypeEnum.EOS.name(), memo);
+                    if(CollectionUtils.isNotEmpty(memberCoinAddress)){
+                        MemberCoinAddressEntity memberCoinAddressEntity = memberCoinAddress.get(0);
+                        // 用户ID
+                        Long memberId = memberCoinAddressEntity.getMemberId();
+                        MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.EOS.name());
+                        if(memberCoinAddressEntity!=null){
+                            memberWalletCoinDao.updateBlockBalance(memberWalletCoinEntity.getId(),new BigDecimal(amountStr),BigDecimal.ZERO,0);
+                            // 添加冲币记录
+                            insertCoinCharge(EosService.ACCOUNT,memberId,new BigDecimal(amountStr),CoinTypeEnum.EOS.name(),memo,BigDecimal.ZERO);
+                        }
+                    }
+                }
+            }
+        }
+        // 最后更新seq 即下次查询的起始位置 在本次最大的基础上加一
+        if(seq>0 && seq>pos){
+            redisUtils.set(EOS_SEQ_KEY,seq+1);
+        }
+    }
+
     private String generateNo() {
         // 生成订单号
         Long timestamp = System.currentTimeMillis();
diff --git a/src/main/java/com/xcong/excoin/quartz/job/BlockCoinUpdateJob.java b/src/main/java/com/xcong/excoin/quartz/job/BlockCoinUpdateJob.java
index e68dd4c..dca19aa 100644
--- a/src/main/java/com/xcong/excoin/quartz/job/BlockCoinUpdateJob.java
+++ b/src/main/java/com/xcong/excoin/quartz/job/BlockCoinUpdateJob.java
@@ -50,4 +50,9 @@
         blockCoinService.updateBtc();
     }
 
+    @Scheduled(cron = "0 4/20 * * * ? ")
+    public void eosUpdate() {
+        blockCoinService.updateEos();
+    }
+
 }

--
Gitblit v1.9.1