|  |  | 
 |  |  | 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.model.XrpTransResult; | 
 |  |  | import com.xcong.excoin.modules.blackchain.model.XrpTransactions; | 
 |  |  | import com.xcong.excoin.modules.blackchain.model.XrpTx; | 
 |  |  | import com.xcong.excoin.modules.blackchain.model.*; | 
 |  |  | import com.xcong.excoin.modules.blackchain.service.*; | 
 |  |  | import com.xcong.excoin.modules.coin.service.BlockCoinService; | 
 |  |  | import com.xcong.excoin.modules.member.dao.MemberCoinAddressDao; | 
 |  |  | 
 |  |  | import java.text.ParseException; | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | import java.util.Date; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * @author wzy | 
 |  |  | 
 |  |  |     @Resource | 
 |  |  |     private RedisUtils redisUtils; | 
 |  |  |  | 
 |  |  |     private final static String EOS_SEQ_KEY="eos_seq_key"; | 
 |  |  |     private final static String EOS_SEQ_KEY = "eos_seq_key"; | 
 |  |  |  | 
 |  |  |     private final static String xrp_update_key = "xrp_update_key"; | 
 |  |  |  | 
 |  |  |     private final static String trc20_update_key = "trc20_update_key"; | 
 |  |  |  | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     @Override | 
 |  |  | 
 |  |  |                         BigDecimal newBalance = balance.subtract(early); | 
 |  |  |                         memberWalletCoinDao.updateBlockBalance(walletCoinEntity.getId(), newBalance, balance, 0); | 
 |  |  |  | 
 |  |  |                         String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.USDT.name(), "ERC20", balance); | 
 |  |  |                         String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.USDT.name(), "ERC20", balance,null); | 
 |  |  |                         // 插入财务记录 | 
 |  |  |                         LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", newBalance, CoinTypeEnum.USDT.name(), 1, 1); | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |                         BigDecimal newBalance = balance.subtract(early); | 
 |  |  |                         memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), newBalance, balance, 0); | 
 |  |  |                         String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.ETH.name(), null, balance); | 
 |  |  |                         String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.ETH.name(), null, balance,null); | 
 |  |  |  | 
 |  |  |                         // 插入财务记录 | 
 |  |  |                         LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", newBalance, CoinTypeEnum.ETH.name(), 1, 1); | 
 |  |  | 
 |  |  |                         BigDecimal newBalance = balance.subtract(early); | 
 |  |  |  | 
 |  |  |                         memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), newBalance, balance, 0); | 
 |  |  |                         String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.USDT.name(), "OMNI", balance); | 
 |  |  |                         String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.USDT.name(), "OMNI", balance,null); | 
 |  |  |  | 
 |  |  |                         ThreadPoolUtils.sendDingTalk(5); | 
 |  |  |                         MemberEntity member = memberDao.selectById(memberId); | 
 |  |  | 
 |  |  |                         BigDecimal newBalance = balance.subtract(early); | 
 |  |  |                         memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), newBalance, balance, 0); | 
 |  |  |  | 
 |  |  |                         String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.BTC.name(), null, balance); | 
 |  |  |                         String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.BTC.name(), null, balance,null); | 
 |  |  |                         LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", newBalance, CoinTypeEnum.BTC.name(), 1, 1); | 
 |  |  |  | 
 |  |  |                         ThreadPoolUtils.sendDingTalk(5); | 
 |  |  | 
 |  |  |     @Override | 
 |  |  |     public void updateEos() { | 
 |  |  |         // 获取上次读取的序号 | 
 |  |  |         int pos= 0; | 
 |  |  |         int pos = 0; | 
 |  |  |         //每次获取的条数 | 
 |  |  |         int offset = 10; | 
 |  |  |  | 
 |  |  |         String eosSeq = redisUtils.getString(EOS_SEQ_KEY); | 
 |  |  |         if(StringUtils.isNotBlank(eosSeq)){ | 
 |  |  |         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){ | 
 |  |  |         if (CollectionUtils.isNotEmpty(actions)) { | 
 |  |  |             for (EosResult eosResult : actions) { | 
 |  |  |                 String to = eosResult.getTo(); | 
 |  |  |                 Integer accountActionSeq = eosResult.getAccountActionSeq(); | 
 |  |  |                 if(accountActionSeq>seq){ | 
 |  |  |                 if (accountActionSeq > seq) { | 
 |  |  |                     seq = accountActionSeq; | 
 |  |  |                 } | 
 |  |  |                 if(!EosService.ACCOUNT.equals(to)){ | 
 |  |  |                 if (!EosService.ACCOUNT.equals(to)) { | 
 |  |  |                     // 判断是否是收款 | 
 |  |  |                     continue; | 
 |  |  |                 } | 
 |  |  |                 // 处理收款 | 
 |  |  |                 String quantity = eosResult.getQuantity(); | 
 |  |  |                 String memo = eosResult.getMemo(); | 
 |  |  |                 if(StringUtils.isBlank(memo)){ | 
 |  |  |                 if (StringUtils.isBlank(memo)) { | 
 |  |  |                     // 没有标记的跳过 | 
 |  |  |                     continue; | 
 |  |  |                 } | 
 |  |  |                 if(StringUtils.isNotBlank(quantity)){ | 
 |  |  |                 if (StringUtils.isNotBlank(quantity)) { | 
 |  |  |                     // 转账额 | 
 |  |  |                     String amountStr = quantity.split("")[0]; | 
 |  |  |                     BigDecimal amount = new BigDecimal(amountStr); | 
 |  |  |                     List<MemberCoinAddressEntity> memberCoinAddress = memberCoinAddressDao.selectAllBlockAddressBySymbolAndTag(CoinTypeEnum.EOS.name(), memo); | 
 |  |  |                     if(CollectionUtils.isNotEmpty(memberCoinAddress)){ | 
 |  |  |                     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(),amount,BigDecimal.ZERO,0); | 
 |  |  |                         if (memberCoinAddressEntity != null) { | 
 |  |  |                             memberWalletCoinDao.updateBlockBalance(memberWalletCoinEntity.getId(), amount, BigDecimal.ZERO, 0); | 
 |  |  |                             // 添加冲币记录 | 
 |  |  |                             String orderNo = insertCoinCharge(EosService.ACCOUNT,memberId,amount,CoinTypeEnum.EOS.name(),memo,BigDecimal.ZERO); | 
 |  |  |                             String orderNo = insertCoinCharge(EosService.ACCOUNT, memberId, amount, CoinTypeEnum.EOS.name(), memo, BigDecimal.ZERO,null); | 
 |  |  |                             LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", amount, CoinTypeEnum.EOS.name(), 1, 1); | 
 |  |  |  | 
 |  |  |                             ThreadPoolUtils.sendDingTalk(5); | 
 |  |  | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         // 最后更新seq 即下次查询的起始位置 在本次最大的基础上加一 | 
 |  |  |         if(seq>0 && seq>pos){ | 
 |  |  |             redisUtils.set(EOS_SEQ_KEY,seq+1); | 
 |  |  |         if (seq > 0 && seq > pos) { | 
 |  |  |             redisUtils.set(EOS_SEQ_KEY, seq + 1); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |         // 首先去查redis上的上次同步时间 | 
 |  |  |         Object lastUpdateTime = redisUtils.get(xrp_update_key); | 
 |  |  |         SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss"); | 
 |  |  |         Date start =null; | 
 |  |  |         if(lastUpdateTime==null) { | 
 |  |  |         Date start = null; | 
 |  |  |         if (lastUpdateTime == null) { | 
 |  |  |             // 没有 说明是第一次同步 此时从第一天开始同步2020 0716开始 | 
 |  |  |             try { | 
 |  |  |                 start = format.parse("2020-07-16 12:00:00"); | 
 |  |  |             } catch (ParseException e) { | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } | 
 |  |  |         }else { | 
 |  |  |         } else { | 
 |  |  |             // 有上次时间 | 
 |  |  |             try { | 
 |  |  |                 start = format.parse(lastUpdateTime.toString()); | 
 |  |  | 
 |  |  |         redisUtils.set(xrp_update_key, updateTime); | 
 |  |  |         // 判断有无 | 
 |  |  |         List<XrpTransactions> list = result.getTransactions(); | 
 |  |  |         if(CollectionUtils.isNotEmpty(list)) { | 
 |  |  |         if (CollectionUtils.isNotEmpty(list)) { | 
 |  |  |             // 不为空 说明有转入记录 | 
 |  |  |             for(XrpTransactions item:list) { | 
 |  |  |             for (XrpTransactions item : list) { | 
 |  |  |                 XrpTx data = item.getTx(); | 
 |  |  |                 Integer amountOld = data.getAmount(); | 
 |  |  |                 // 除以1000000 | 
 |  |  |                 BigDecimal amount = new BigDecimal(amountOld).divide(new BigDecimal(1000000)); | 
 |  |  |                 Integer memoInt = data.getDestinationTag(); | 
 |  |  |                 // 没有标签直接返回 | 
 |  |  |                 if(memoInt==null){ | 
 |  |  |                 if (memoInt == null) { | 
 |  |  |                     continue; | 
 |  |  |                 } | 
 |  |  |                 String memo = memoInt.toString(); | 
 |  |  |                 String destination = data.getDestination(); | 
 |  |  |                 // 判断收款人是不是系统账号 | 
 |  |  |                 if(!XrpService.ACCOUNT.equals(destination)){ | 
 |  |  |                 if (!XrpService.ACCOUNT.equals(destination)) { | 
 |  |  |                     continue; | 
 |  |  |                 } | 
 |  |  |                 List<MemberCoinAddressEntity> memberCoinAddress = memberCoinAddressDao.selectAllBlockAddressBySymbolAndTag(CoinTypeEnum.XRP.name(), memo); | 
 |  |  |                 if(CollectionUtils.isNotEmpty(memberCoinAddress)){ | 
 |  |  |                 if (CollectionUtils.isNotEmpty(memberCoinAddress)) { | 
 |  |  |                     MemberCoinAddressEntity memberCoinAddressEntity = memberCoinAddress.get(0); | 
 |  |  |                     // 用户ID | 
 |  |  |                     Long memberId = memberCoinAddressEntity.getMemberId(); | 
 |  |  |                     MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.XRP.name()); | 
 |  |  |                     if(memberCoinAddressEntity!=null){ | 
 |  |  |                         memberWalletCoinDao.updateBlockBalance(memberWalletCoinEntity.getId(),amount,BigDecimal.ZERO,0); | 
 |  |  |                     if (memberCoinAddressEntity != null) { | 
 |  |  |                         memberWalletCoinDao.updateBlockBalance(memberWalletCoinEntity.getId(), amount, BigDecimal.ZERO, 0); | 
 |  |  |                         // 添加冲币记录 | 
 |  |  |                         String orderNo = insertCoinCharge(XrpService.ACCOUNT,memberId,amount,CoinTypeEnum.XRP.name(),memo,BigDecimal.ZERO); | 
 |  |  |                         String orderNo = insertCoinCharge(XrpService.ACCOUNT, memberId, amount, CoinTypeEnum.XRP.name(), memo, BigDecimal.ZERO,null); | 
 |  |  |                         LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", amount, CoinTypeEnum.XRP.name(), 1, 1); | 
 |  |  |  | 
 |  |  |                         ThreadPoolUtils.sendDingTalk(5); | 
 |  |  | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void updateTrc20() { | 
 |  |  |         // 首先去查redis上的上次同步时间 | 
 |  |  |         Object lastUpdateTime = redisUtils.get(trc20_update_key); | 
 |  |  |         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"); | 
 |  |  |         String start = null; | 
 |  |  |         if (lastUpdateTime == null) { | 
 |  |  |             // 没有 说明是第一次同步 此时从第一天开始同步2020 0905开始 | 
 |  |  |             start = "2020-09-05'T'00:00"; | 
 |  |  |         } else { | 
 |  |  |             // 有上次时间 | 
 |  |  |             start = lastUpdateTime.toString(); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 去查询上次同步时间后的所有记录 | 
 |  |  |         //Trc20Service.getAddressTransactions() | 
 |  |  |         // 写入本次更新时间 | 
 |  |  |         String updateTime = format.format(new Date()); | 
 |  |  |         redisUtils.set(trc20_update_key, updateTime); | 
 |  |  |         // 判断有无 | 
 |  |  |         List<MemberCoinAddressEntity> addressList = memberCoinAddressDao.selectAllBlockAddressBySymbolAndTag(CoinTypeEnum.USDT.name(), "TRC20"); | 
 |  |  |         if (CollectionUtils.isNotEmpty(addressList)) { | 
 |  |  |             Map<String, Object> hashParam = new HashMap<>(); | 
 |  |  |             for (MemberCoinAddressEntity coinAddressEntity : addressList) { | 
 |  |  |                 String address = coinAddressEntity.getAddress(); | 
 |  |  |                 List<Trc20TransactionsData> addressTransactions = Trc20Service.getAddressTransactions(address, start); | 
 |  |  |                 if (CollectionUtils.isNotEmpty(addressTransactions)) { | 
 |  |  |                     for (Trc20TransactionsData trc20TransactionsData : addressTransactions) { | 
 |  |  |                         String transactionId = trc20TransactionsData.getTransaction_id(); | 
 |  |  |                         String value = trc20TransactionsData.getValue(); | 
 |  |  |                         // 本次转账金额 | 
 |  |  |                         BigDecimal amount = new BigDecimal(value).divide(new BigDecimal("1000000")); | 
 |  |  |                         // 校验token是否为trc20USD | 
 |  |  |                         if (trc20TransactionsData.getToken_info() != null && trc20TransactionsData.getToken_info().containsKey("address")) { | 
 |  |  |                             String tokenTrc = trc20TransactionsData.getToken_info().get("address").toString(); | 
 |  |  |                             if (!"TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t".equals(tokenTrc)) { | 
 |  |  |                                 continue; | 
 |  |  |                             } | 
 |  |  |                         } else { | 
 |  |  |                             continue; | 
 |  |  |                         } | 
 |  |  |                         // 校验hash是否已同步过 | 
 |  |  |                         hashParam.put("hash", transactionId); | 
 |  |  |                         List<MemberCoinChargeEntity> memberCoinChargeEntities = memberCoinChargeDao.selectByMap(hashParam); | 
 |  |  |                         if (CollectionUtils.isNotEmpty(memberCoinChargeEntities)) { | 
 |  |  |                             // 若已同步过 | 
 |  |  |                             continue; | 
 |  |  |                         } | 
 |  |  |                         // 添加钱包余额 | 
 |  |  |                         // 用户ID | 
 |  |  |                         Long memberId = coinAddressEntity.getMemberId(); | 
 |  |  |                         MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.USDT.name()); | 
 |  |  |  | 
 |  |  |                         memberWalletCoinDao.updateBlockBalance(memberWalletCoinEntity.getId(), amount, BigDecimal.ZERO, 0); | 
 |  |  |                         // 添加冲币记录 | 
 |  |  |                         String orderNo = insertCoinCharge(address, memberId, amount, CoinTypeEnum.USDT.name(), "TRC20", BigDecimal.ZERO,transactionId); | 
 |  |  |                         LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", amount, CoinTypeEnum.USDT.name(), 1, 1); | 
 |  |  |  | 
 |  |  |                         ThreadPoolUtils.sendDingTalk(5); | 
 |  |  |                         MemberEntity member = memberDao.selectById(memberId); | 
 |  |  |                         if (StrUtil.isNotBlank(member.getPhone())) { | 
 |  |  |                             //String amountEos = amount + "XRP"; | 
 |  |  |                             Sms106Send.sendRechargeMsg(member.getPhone(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); | 
 |  |  |                         } else { | 
 |  |  |                             SubMailSend.sendRechargeMail(member.getEmail(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); | 
 |  |  |                         } | 
 |  |  |  | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private String generateNo() { | 
 |  |  |         // 生成订单号 | 
 |  |  |         Long timestamp = System.currentTimeMillis(); | 
 |  |  | 
 |  |  |         return String.valueOf(timestamp).substring(2) + random; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public String insertCoinCharge(String address, Long memberId, BigDecimal newBalance, String symbol, String tag, BigDecimal lastAmount) { | 
 |  |  |     public String insertCoinCharge(String address, Long memberId, BigDecimal newBalance, String symbol, String tag, BigDecimal lastAmount,String hash) { | 
 |  |  |         MemberCoinChargeEntity memberCoinChargeEntity = new MemberCoinChargeEntity(); | 
 |  |  |         memberCoinChargeEntity.setAddress(address); | 
 |  |  |         memberCoinChargeEntity.setMemberId(memberId); | 
 |  |  | 
 |  |  |         memberCoinChargeEntity.setTag(tag); | 
 |  |  |         memberCoinChargeEntity.setStatus(1); | 
 |  |  |         memberCoinChargeEntity.setLastAmount(lastAmount); | 
 |  |  |         memberCoinChargeEntity.setHash(hash); | 
 |  |  |         String orderNo = generateNo(); | 
 |  |  |         memberCoinChargeEntity.setOrderCode(orderNo); | 
 |  |  |         memberCoinChargeDao.insert(memberCoinChargeEntity); |