wzy
2020-12-10 068b277fe6e9fb588adf5118799db9198e1d5249
src/main/java/com/xcong/excoin/modules/member/service/impl/MemberServiceImpl.java
@@ -1,7 +1,9 @@
package com.xcong.excoin.modules.member.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
@@ -9,26 +11,12 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcong.excoin.common.entity.FebsResponse;
import com.xcong.excoin.common.entity.QueryRequest;
import com.xcong.excoin.common.utils.RedisUtils;
import com.xcong.excoin.common.utils.TRC20ApiUtils;
import com.xcong.excoin.modules.Sms106Send;
import com.xcong.excoin.modules.member.dto.MemberDetailConfirmDto;
import com.xcong.excoin.modules.member.entity.AgentFriendRelationEntity;
import com.xcong.excoin.modules.member.entity.MemberAccountMoneyChangeEntity;
import com.xcong.excoin.modules.member.entity.MemberAuthenticationEntity;
import com.xcong.excoin.modules.member.entity.MemberCoinAddressEntity;
import com.xcong.excoin.modules.member.entity.MemberCoinChargeEntity;
import com.xcong.excoin.modules.member.entity.MemberCoinWithdrawEntity;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.member.entity.MemberQuickBuySaleEntity;
import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
import com.xcong.excoin.modules.member.mapper.AgentFriendRelationMapper;
import com.xcong.excoin.modules.member.mapper.MemberAccountMoneyChangeMapper;
import com.xcong.excoin.modules.member.mapper.MemberAuthenticationMapper;
import com.xcong.excoin.modules.member.mapper.MemberCoinAddressMapper;
import com.xcong.excoin.modules.member.mapper.MemberCoinChargeMapper;
import com.xcong.excoin.modules.member.mapper.MemberCoinWithdrawMapper;
import com.xcong.excoin.modules.member.mapper.MemberMapper;
import com.xcong.excoin.modules.member.mapper.MemberQuickBuySaleMapper;
import com.xcong.excoin.modules.member.mapper.MemberWalletCoinMapper;
import com.xcong.excoin.modules.member.entity.*;
import com.xcong.excoin.modules.member.mapper.*;
import com.xcong.excoin.modules.member.service.EthService;
import com.xcong.excoin.modules.member.service.IMemberService;
import com.xcong.excoin.modules.member.service.RocService;
@@ -85,6 +73,12 @@
   private final MemberAuthenticationMapper memberAuthenticationMapper;
   
   private final AgentFriendRelationMapper agentFriendRelationMapper;
   private final TdFinancialReordDao tdFinancialReordDao;
   private final RedisUtils redisUtils;
   private final TrcAddressDao trcAddressDao;
   
   @Override
   public IPage<AgentFriendRelationEntity> findAgentInfoListInPage(AgentFriendRelationEntity agentFriendRelationEntity,
@@ -201,7 +195,11 @@
              String firstName = (memberEntitys.getFirstName() == null ? "" : memberEntitys.getFirstName());
              String secondName = (memberEntitys.getSecondName() == null ? "" : memberEntitys.getSecondName());
              memberEntitys.setRealName(firstName+secondName);
           }
            HashMap<String, Object> coinMap = memberWalletCoinMapper.selectMemberWalletCoinByMemberId(memberEntitys.getId());
            memberEntitys.setUsdtAmount((BigDecimal) coinMap.get("usdt"));
            memberEntitys.setRfncAmount((BigDecimal) coinMap.get("rfnc"));
         }
        }
        return selectMemberListInPage;
    }
@@ -471,9 +469,9 @@
      }
      // 转币 需要扣除手续费
      amount = amount.subtract(selectById.getFeeAmount());
      if("ROC".equals(symbol) && !"Y".equals(selectById.getIsInside())){
      if("RFNC".equals(symbol) && !"Y".equals(selectById.getIsInside())){
         // 如果是ROC  则自动转
         String transfer = RocService.transfer(amount, address, "ROC");
         String transfer = RocService.transfer(amount, address, "RFNC");
         if(!"success".equals(transfer)){
            FebsResponse febsResponse = new FebsResponse();
            return  febsResponse.fail().message(transfer);
@@ -490,36 +488,94 @@
      memberWalletCoinMapper.updateById(walletCoin);
      
      if ("Y".equals(selectById.getIsInside())) {
         Map<String, Object> columnMap = new HashMap<>();
         columnMap.put("symbol", symbol);
         columnMap.put("address", address);
         // 如果是内部转账 则需要将币加到内部地址
         List<MemberCoinAddressEntity> selectByMap = memberCoinAddressMapper.selectByMap(columnMap);
         if(selectByMap == null || selectByMap.isEmpty()) {
            return new FebsResponse().fail().message("地址有误,请拒绝!");
         // 查询算力系统中是否存在该地址
         TdCoinWallet tdCoinWallet = memberWalletCoinMapper.selectTdCoinWalletByAddress(address, "USDT");
         TrcAddressEntity trcAddressEntity = trcAddressDao.selectTrcAddressByAddress(address);
         // 判断是否为TRC20地址
         if (trcAddressEntity == null) {
            if (tdCoinWallet != null) {
               // 更新算力系统中用户钱包余额
               memberWalletCoinMapper.updateTdCoinWalletAvaliable(amount, address);
               TdFinancialReord tdFinancialReord = new TdFinancialReord();
               tdFinancialReord.setAmount(amount);
               tdFinancialReord.setCreateTime(new Date());
               tdFinancialReord.setMemId(tdCoinWallet.getMemId());
               tdFinancialReord.setSymbol("USDT");
               tdFinancialReord.setTitle("USDT交易所转账");
               tdFinancialReord.setContent("USDT交易所转账");
               // 插入算力系统中财务记录
               tdFinancialReordDao.insert(tdFinancialReord);
            } else {
               Map<String, Object> columnMap = new HashMap<>();
               columnMap.put("symbol", symbol);
               columnMap.put("address", address);
               // 如果是内部转账 则需要将币加到内部地址
               List<MemberCoinAddressEntity> selectByMap = memberCoinAddressMapper.selectByMap(columnMap);
               if (selectByMap == null || selectByMap.isEmpty()) {
                  return new FebsResponse().fail().message("地址有误,请拒绝!");
               }
               Long aimMemberId = selectByMap.get(0).getMemberId();
               MemberWalletCoinEntity aimWalletCoin = memberWalletCoinMapper.findWalletCoinByMemberIdAndWalletCode(aimMemberId, symbol);
               BigDecimal addTotal = aimWalletCoin.getTotalBalance().add(amount);
               BigDecimal addAvailable = aimWalletCoin.getAvailableBalance().add(amount);
               aimWalletCoin.setTotalBalance(addTotal);
               aimWalletCoin.setAvailableBalance(addAvailable);
               memberWalletCoinMapper.updateById(aimWalletCoin);
               MemberAccountMoneyChangeEntity memberAccountMoneyChangeEntity = new MemberAccountMoneyChangeEntity();
               memberAccountMoneyChangeEntity.setContent("收款");
               memberAccountMoneyChangeEntity.setMemberId(aimMemberId);
               memberAccountMoneyChangeEntity.setAmount(amount);
               memberAccountMoneyChangeEntity.setStatus(MemberAccountMoneyChangeEntity.STATUS_SUCCESS_INTEGER);
               memberAccountMoneyChangeEntity.setSymbol(selectById.getSymbol());
               memberAccountMoneyChangeEntity.setType(MemberAccountMoneyChangeEntity.TYPE_WALLET_COIN);
               memberAccountMoneyChangeEntity.setCreateBy(selectById.getCreateBy());
               memberAccountMoneyChangeEntity.setCreateTime(new Date());
               memberAccountMoneyChangeEntity.setUpdateBy(selectById.getCreateBy());
               memberAccountMoneyChangeEntity.setUpdateTime(new Date());
               memberAccountMoneyChangeMapper.insert(memberAccountMoneyChangeEntity);
            }
         } else {
            if (trcAddressEntity.getSystemFlag().equals(TrcAddressEntity.SYSTEM_FLAG_SL)) {
               memberWalletCoinMapper.updateTdCoinWalletTrc20(amount, trcAddressEntity.getMemberId());
               TdFinancialReord tdFinancialReord = new TdFinancialReord();
               tdFinancialReord.setAmount(amount);
               tdFinancialReord.setCreateTime(new Date());
               tdFinancialReord.setMemId(trcAddressEntity.getMemberId());
               tdFinancialReord.setSymbol("USDT");
               tdFinancialReord.setTitle("USDT交易所转账");
               tdFinancialReord.setContent("USDT交易所转账");
               // 插入算力系统中财务记录
               tdFinancialReordDao.insert(tdFinancialReord);
            } else {
               Long aimMemberId = trcAddressEntity.getMemberId();
               MemberWalletCoinEntity aimWalletCoin = memberWalletCoinMapper.findWalletCoinByMemberIdAndWalletCode(aimMemberId, symbol);
               BigDecimal addTotal = aimWalletCoin.getTotalBalance().add(amount);
               BigDecimal addAvailable = aimWalletCoin.getAvailableBalance().add(amount);
               aimWalletCoin.setTotalBalance(addTotal);
               aimWalletCoin.setAvailableBalance(addAvailable);
               memberWalletCoinMapper.updateById(aimWalletCoin);
               MemberAccountMoneyChangeEntity memberAccountMoneyChangeEntity = new MemberAccountMoneyChangeEntity();
               memberAccountMoneyChangeEntity.setContent("收款");
               memberAccountMoneyChangeEntity.setMemberId(aimMemberId);
               memberAccountMoneyChangeEntity.setAmount(amount);
               memberAccountMoneyChangeEntity.setStatus(MemberAccountMoneyChangeEntity.STATUS_SUCCESS_INTEGER);
               memberAccountMoneyChangeEntity.setSymbol(selectById.getSymbol());
               memberAccountMoneyChangeEntity.setType(MemberAccountMoneyChangeEntity.TYPE_WALLET_COIN);
               memberAccountMoneyChangeEntity.setCreateBy(selectById.getCreateBy());
               memberAccountMoneyChangeEntity.setCreateTime(new Date());
               memberAccountMoneyChangeEntity.setUpdateBy(selectById.getCreateBy());
               memberAccountMoneyChangeEntity.setUpdateTime(new Date());
               memberAccountMoneyChangeMapper.insert(memberAccountMoneyChangeEntity);
            }
         }
         Long aimMemberId = selectByMap.get(0).getMemberId();
         MemberWalletCoinEntity aimWalletCoin = memberWalletCoinMapper.findWalletCoinByMemberIdAndWalletCode(aimMemberId, symbol);
         BigDecimal addTotal = aimWalletCoin.getTotalBalance().add(amount);
         BigDecimal addAvailable = aimWalletCoin.getAvailableBalance().add(amount);
         aimWalletCoin.setTotalBalance(addTotal);
         aimWalletCoin.setAvailableBalance(addAvailable);
         memberWalletCoinMapper.updateById(aimWalletCoin);
         MemberAccountMoneyChangeEntity memberAccountMoneyChangeEntity = new MemberAccountMoneyChangeEntity();
         memberAccountMoneyChangeEntity.setContent("收款");
         memberAccountMoneyChangeEntity.setMemberId(aimMemberId);
         memberAccountMoneyChangeEntity.setAmount(amount);
         memberAccountMoneyChangeEntity.setStatus(MemberAccountMoneyChangeEntity.STATUS_SUCCESS_INTEGER);
         memberAccountMoneyChangeEntity.setSymbol(selectById.getSymbol());
         memberAccountMoneyChangeEntity.setType(MemberAccountMoneyChangeEntity.TYPE_WALLET_COIN);
         memberAccountMoneyChangeEntity.setCreateBy(selectById.getCreateBy());
           memberAccountMoneyChangeEntity.setCreateTime(new Date());
           memberAccountMoneyChangeEntity.setUpdateBy(selectById.getCreateBy());
           memberAccountMoneyChangeEntity.setUpdateTime(new Date());
         memberAccountMoneyChangeMapper.insert(memberAccountMoneyChangeEntity);
      }
      
      Map<String, Object> columnMaps = new HashMap<>();
@@ -533,13 +589,29 @@
      
      selectById.setStatus(MemberCoinWithdrawEntity.IS_STATUS_Y);
      memberCoinWithdrawMapper.updateById(selectById);
      if (!"Y".equals(selectById.getIsInside())) {
         if ("TRC20".equals(selectById.getLabel())) {
            Thread thread = new Thread(new Runnable() {
               @Override
               public void run() {
                  log.info("远程调用TRC执行:{}", selectById.getId());
                  String orderNo = generateOrderNo(memberId);
                  TRC20ApiUtils.coinApply(orderNo, memberId.toString(), selectById.getSymbol(), selectById.getAmount().subtract(selectById.getFeeAmount()).toPlainString(), selectById.getAddress());
                  selectById.setTag(orderNo);
                  memberCoinWithdrawMapper.updateById(selectById);
               }
            });
            thread.start();
         }
      }
      MemberEntity memberEntity = memberMapper.selectById(memberId);
      String phone = memberEntity.getPhone();
      //String email = memberEntity.getEmail();
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time = format.format(new Date()); // 将当前时间袼式化为指定的格式
      log.info("---->{}", phone);
      if(StrUtil.isNotBlank(phone)) {
         Sms106Send.sendWithdrawalCoinMsg(phone, time);
      }
@@ -572,6 +644,29 @@
         return new FebsResponse().success();
   }
   public String generateOrderNo(Long mid) {
      StringBuilder orderNo = new StringBuilder();
      String date = DateUtil.format(new Date(), "yyyyMMdd");
      orderNo.append(date);
      orderNo.append(mid);
      orderNo.append(RandomUtil.randomNumbers(2));
      Object countObj = redisUtils.get(date);
      if (countObj == null) {
         countObj = 0;
      }
      int count = (int) countObj;
      count++;
      redisUtils.set(date, count, 24 * 60 * 60);
      int size = 4;
      for (int i = 0; i < size - String.valueOf(count).length(); i++) {
         orderNo.append("0");
      }
      orderNo.append(count);
      return orderNo.toString();
   }
   @Override
   @Transactional(rollbackFor = Exception.class)
   public FebsResponse memberWithdrawCoinCancel(@NotNull(message = "{required}") Long id) {