Helius
2020-05-25 6ac527e204fbcf9bc62f280eb925cddab291fe5b
src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java
@@ -2,16 +2,13 @@
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcong.excoin.common.LoginUserUtils;
import com.xcong.excoin.common.enumerates.CoinTypeEnum;
@@ -21,6 +18,7 @@
import com.xcong.excoin.modules.coin.dao.platform.CnyUsdtExchangeDao;
import com.xcong.excoin.modules.coin.entity.CnyUsdtExchange;
import com.xcong.excoin.modules.coin.entity.MemberAccountMoneyChange;
import com.xcong.excoin.modules.coin.parameter.vo.MemberWalletCoinInfoVo;
import com.xcong.excoin.modules.coin.parameter.vo.MemberWalletCoinVo;
import com.xcong.excoin.modules.coin.service.CoinService;
import com.xcong.excoin.modules.member.dao.MemberWalletAgentDao;
@@ -30,8 +28,8 @@
import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity;
import com.xcong.excoin.utils.MessageSourceUtils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
@Service
@@ -63,10 +61,23 @@
         
         if(!StrUtil.isEmpty(memberId.toString())) {
            
            List<MemberWalletCoinEntity> MemberWalletCoinlist = memberWalletCoinDao.selectMemberWalletCoinsByMemberId(memberId);
            List<MemberWalletCoinEntity> memberWalletCoinlist = memberWalletCoinDao.selectMemberWalletCoinsByMemberId(memberId);
            
            if(CollUtil.isNotEmpty(MemberWalletCoinlist)) {
               for(MemberWalletCoinEntity walletCoin : MemberWalletCoinlist) {
            List<MemberWalletCoinInfoVo> memberWalletCoinInfoVolist = new ArrayList<MemberWalletCoinInfoVo>();
            MemberWalletCoinInfoVo memberWalletCoinInfoVo = new MemberWalletCoinInfoVo();
            if(ObjectUtil.isNotEmpty(memberWalletCoinlist)) {
               for(MemberWalletCoinEntity memberWalletCoinEntity : memberWalletCoinlist) {
                  memberWalletCoinInfoVo.setAvailableBalance(memberWalletCoinEntity.getAvailableBalance());
                  memberWalletCoinInfoVo.setFrozenBalance(memberWalletCoinEntity.getFrozenBalance());
                  memberWalletCoinInfoVo.setMemberId(memberWalletCoinEntity.getMemberId());
                  memberWalletCoinInfoVo.setTotalBalance(memberWalletCoinEntity.getTotalBalance());
                  memberWalletCoinInfoVolist.add(memberWalletCoinInfoVo);
               }
            }
            if(CollUtil.isNotEmpty(memberWalletCoinlist)) {
               for(MemberWalletCoinEntity walletCoin : memberWalletCoinlist) {
                  if(MemberWalletCoinEnum.WALLETCOINCODE.getValue().equals(walletCoin.getWalletCode())) {   
                     totalUsdt = walletCoin.getAvailableBalance().add(walletCoin.getFrozenBalance());
                     BigDecimal totalCny = totalUsdt.multiply(cnyUsdt);
@@ -83,34 +94,35 @@
            MemberWalletCoinVo memberWalletCoinVo = new MemberWalletCoinVo();
            memberWalletCoinVo.setTotalUsdt(totalUsdt.setScale(4, BigDecimal.ROUND_DOWN));
            memberWalletCoinVo.setTotalCny(totalUsdt.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
            memberWalletCoinVo.setWalletCoinEntities(MemberWalletCoinlist);
            memberWalletCoinVo.setMemberWalletCoinInfoVo(memberWalletCoinInfoVolist);
            return Result.ok(memberWalletCoinVo);
         }else {
            List<MemberWalletCoinEntity> MemberWalletCoinlist = new ArrayList<MemberWalletCoinEntity>();
            MemberWalletCoinEntity coin = new MemberWalletCoinEntity();
            List<MemberWalletCoinInfoVo> memberWalletCoinlist = new ArrayList<MemberWalletCoinInfoVo>();
            MemberWalletCoinInfoVo coin = new MemberWalletCoinInfoVo();
            coin.setAvailableBalance(BigDecimal.ZERO);
            coin.setTotalBalance(BigDecimal.ZERO);
            coin.setFrozenBalance(BigDecimal.ZERO);
            coin.setWalletCode(CoinTypeEnum.BTC.toString());
            MemberWalletCoinlist.add(coin);
            memberWalletCoinlist.add(coin);
            coin.setWalletCode(CoinTypeEnum.ETH.toString());
            MemberWalletCoinlist.add(coin);
            memberWalletCoinlist.add(coin);
            coin.setWalletCode(CoinTypeEnum.LTC.toString());
            MemberWalletCoinlist.add(coin);
            memberWalletCoinlist.add(coin);
            coin.setWalletCode(CoinTypeEnum.BCH.toString());
            MemberWalletCoinlist.add(coin);
            memberWalletCoinlist.add(coin);
            coin.setWalletCode(CoinTypeEnum.USDT.toString());
            MemberWalletCoinlist.add(coin);
            memberWalletCoinlist.add(coin);
            coin.setWalletCode(CoinTypeEnum.EOS.toString());
            MemberWalletCoinlist.add(coin);
            memberWalletCoinlist.add(coin);
            coin.setWalletCode(CoinTypeEnum.XRP.toString());
            MemberWalletCoinlist.add(coin);
            memberWalletCoinlist.add(coin);
            coin.setWalletCode(CoinTypeEnum.ETC.toString());
            MemberWalletCoinlist.add(coin);
            memberWalletCoinlist.add(coin);
            MemberWalletCoinVo memberWalletCoinVo = new MemberWalletCoinVo();
            memberWalletCoinVo.setTotalUsdt(totalUsdt.setScale(4, BigDecimal.ROUND_DOWN));
            memberWalletCoinVo.setTotalCny(totalUsdt.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
            memberWalletCoinVo.setWalletCoinEntities(MemberWalletCoinlist);
            memberWalletCoinVo.setMemberWalletCoinInfoVo(memberWalletCoinlist);;
            return Result.ok(memberWalletCoinVo);
         }
      } catch (Exception e) {
@@ -150,14 +162,48 @@
   @Override
   public Result getWalletContractById() {
      
         return Result.fail(MessageSourceUtils.getString("member_service_0002"));
         //获取用户ID
         Long memberId = LoginUserUtils.getAppLoginUser().getId();
         CnyUsdtExchange cnyUsdtExchange =cnyUsdtExchangeDao.getCNYAndUSDTOne();
         BigDecimal cnyUsdt = cnyUsdtExchange.getValue();
         String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
         MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, walletCode);
         if(ObjectUtil.isEmpty(walletContract)) {
            return Result.fail(MessageSourceUtils.getString("member_service_0001"));
         }
         /**
          * todo
          */
         //获取当前的合约持仓
         //获取实时盈亏
         BigDecimal profitAndLoss = new BigDecimal("50.000");
         //获取总付款
         BigDecimal totalPayment = new BigDecimal("50.000");
         walletContract.setFrozenBalance(totalPayment.setScale(4, BigDecimal.ROUND_DOWN));
         BigDecimal lastTotalBalance = new BigDecimal("0");
         BigDecimal totalBalance = walletContract.getTotalBalance();
         lastTotalBalance = totalBalance.add(profitAndLoss);
         if(lastTotalBalance.compareTo(BigDecimal.ZERO) < 0) {
            lastTotalBalance = new BigDecimal("0");
         }
         walletContract.setAvailableBalance(walletContract.getAvailableBalance());
         walletContract.setTotalBalance(lastTotalBalance.setScale(4, BigDecimal.ROUND_DOWN));
         Map<String,Object> map = new HashMap<String, Object>();
         map.put(MemberWalletCoinEnum.WALLETCOINUSDT.getValue(), lastTotalBalance.setScale(4, BigDecimal.ROUND_DOWN));
         map.put(MemberWalletCoinEnum.WALLETCONTRACT.getValue(), walletContract);
         map.put(MemberWalletCoinEnum.WALLETCOINCNY.getValue(), lastTotalBalance.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
         return Result.ok(map);
   }
   @Override
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   public Result coinWalletTransferToContract(BigDecimal balance, String symbol) {
      try {
         if(balance.compareTo(BigDecimal.ZERO) <= 0) {
            return Result.fail(MessageSourceUtils.getString("member_service_0004"));
         }
@@ -200,31 +246,25 @@
            MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange();
            memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTTOCONTRACT.getValue());
            memberAccountRecord.setMemberId(memberId);
            memberAccountRecord.setPrice(balance.negate().setScale(4, BigDecimal.ROUND_DOWN));
            memberAccountRecord.setAmount(balance.negate().setScale(4, BigDecimal.ROUND_DOWN));
            memberAccountRecord.setStatus(MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER);
            memberAccountRecord.setSymbol(MemberWalletCoinEnum.WALLETCOINCODE.getValue());
            memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_COIN);
            memberAccountRecord.setTime(new Date());
            memberAccountMoneyChangeDao.insert(memberAccountRecord);
            
            //添加合约资金划转历史记录
            memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTFROMWALLETCOIN.getValue());
            memberAccountRecord.setSymbol(MemberWalletCoinEnum.WALLETCOINCODE.getValue());
            memberAccountRecord.setPrice(balance.setScale(4, BigDecimal.ROUND_DOWN));
            memberAccountRecord.setAmount(balance.setScale(4, BigDecimal.ROUND_DOWN));
            memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_CONTRACT);
            memberAccountMoneyChangeDao.insert(memberAccountRecord);
         }
      } catch (Exception e) {
         e.printStackTrace();
         return Result.fail(MessageSourceUtils.getString("member_service_0096"));
      }
      return Result.ok(MessageSourceUtils.getString("member_service_0006"));
   }
   @Override
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   public Result contractTransferToWalletCoin(BigDecimal balance, String symbol) {
      try {
         if(balance.compareTo(BigDecimal.ZERO)<=0){
            return Result.fail(MessageSourceUtils.getString("member_service_0004"));
         }
@@ -251,12 +291,12 @@
         // 加币
         MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, walletCode);
         BigDecimal walletCoinAvailableBalance = walletCoin.getAvailableBalance();
         walletCoinAvailableBalance.add(balance);
         BigDecimal CoinAvailableBalance = walletCoinAvailableBalance.add(balance);
         BigDecimal walletCoinTotalBalance = walletCoin.getTotalBalance();
         walletCoinTotalBalance.add(balance);
         BigDecimal CoinTotalBalance = walletCoinTotalBalance.add(balance);
         
         walletCoin.setAvailableBalance(walletCoinAvailableBalance.setScale(4, BigDecimal.ROUND_DOWN));
         walletCoin.setTotalBalance(walletCoinTotalBalance.setScale(4, BigDecimal.ROUND_DOWN));
         walletCoin.setAvailableBalance(CoinAvailableBalance.setScale(4, BigDecimal.ROUND_DOWN));
         walletCoin.setTotalBalance(CoinTotalBalance.setScale(4, BigDecimal.ROUND_DOWN));
         int updateById = memberWalletCoinDao.updateById(walletCoin);
         if(updateById < 1) {
            return Result.fail(MessageSourceUtils.getString("member_service_0096"));
@@ -266,32 +306,29 @@
         MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange();
         memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTTOWALLETCOIN.getValue());
         memberAccountRecord.setMemberId(memberId);
         memberAccountRecord.setPrice(balance.negate().setScale(4, BigDecimal.ROUND_DOWN));
         memberAccountRecord.setAmount(balance.negate().setScale(4, BigDecimal.ROUND_DOWN));
         memberAccountRecord.setStatus(MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER);
         memberAccountRecord.setSymbol(walletCode);
         memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_CONTRACT);
         memberAccountRecord.setTime(new Date());
         memberAccountMoneyChangeDao.insert(memberAccountRecord);
         
         //添加资金划转历史记录
         memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTFROMCONTRACT.getValue());
         memberAccountRecord.setSymbol(walletCode);
         memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_COIN);
         memberAccountRecord.setPrice(balance.setScale(4, BigDecimal.ROUND_DOWN));
         memberAccountRecord.setAmount(balance.setScale(4, BigDecimal.ROUND_DOWN));
         memberAccountMoneyChangeDao.insert(memberAccountRecord);
      } catch (Exception e) {
         e.printStackTrace();
         return Result.fail(MessageSourceUtils.getString("member_service_0096"));
      }
      return Result.ok(MessageSourceUtils.getString("member_service_0006"));
   }
   @Override
   public Result findWalletContractBySymbol(String symbol) {
      /**
       * ---todo
       */
      return null;
   public Result findWalletContractBySymbol() {
      //获取用户ID
      Long memberId = LoginUserUtils.getAppLoginUser().getId();
      String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
      MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, walletCode);
      return Result.ok(walletContract);
   }
   @Override
@@ -334,9 +371,8 @@
   }
   @Override
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   public Result agentTransferToWalletCoin(BigDecimal balance, Integer transfertype) {
      try {
         if(balance.compareTo(BigDecimal.ZERO) <= 0) {
            return Result.fail(MessageSourceUtils.getString("member_service_0004"));
         }
@@ -386,8 +422,7 @@
            memberAccountRecord.setSymbol(walletCode);
            memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTFROMAGENT.getValue());
            memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_COIN);
            memberAccountRecord.setTime(new Date());
            memberAccountRecord.setPrice(balance.setScale(4, BigDecimal.ROUND_DOWN));
            memberAccountRecord.setAmount(balance.setScale(4, BigDecimal.ROUND_DOWN));
            memberAccountMoneyChangeDao.insert(memberAccountRecord);
            memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTTOWALLETCOIN.getValue());
            
@@ -411,20 +446,14 @@
            memberAccountRecord.setSymbol(walletCode);
            memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTFROMAGENT.getValue());
            memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_CONTRACT);
            memberAccountRecord.setTime(new Date());
            memberAccountRecord.setPrice(balance.setScale(4, BigDecimal.ROUND_DOWN));
            memberAccountRecord.setAmount(balance.setScale(4, BigDecimal.ROUND_DOWN));
            memberAccountMoneyChangeDao.insert(memberAccountRecord);
            memberAccountRecord.setContent(MemberWalletCoinEnum.CONTENTTOCONTRACT.getValue());
         }
         memberAccountRecord.setPrice(balance.negate().setScale(4, BigDecimal.ROUND_DOWN));
         memberAccountRecord.setAmount(balance.negate().setScale(4, BigDecimal.ROUND_DOWN));
         memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_AGENT);
         memberAccountRecord.setTime(new Date());
         memberAccountMoneyChangeDao.insert(memberAccountRecord);
         
      } catch (Exception e) {
         e.printStackTrace();
         return Result.fail(MessageSourceUtils.getString("member_service_0096"));
      }
      return Result.ok(MessageSourceUtils.getString("member_service_0006"));
   }
@@ -445,7 +474,7 @@
      walletAgent.setAvailableBalance(availableBalance);
      Map<String,Object> map = new HashMap<String, Object>();
      map.put(MemberWalletCoinEnum.WALLETCOINUSDT.getValue(), availableBalance.setScale(4, BigDecimal.ROUND_DOWN));
      map.put(MemberWalletCoinEnum.WALLETAGENTCNY.getValue(), walletAgent);
      map.put(MemberWalletCoinEnum.WALLETAGENT.getValue(), walletAgent);
      map.put(MemberWalletCoinEnum.WALLETCOINCNY.getValue(),  multiply.setScale(4, BigDecimal.ROUND_DOWN));
      return Result.ok(map);
   }