xiaoyong931011
2022-02-21 6258b45b69d75325b7d6df7edf2761c22a87369b
src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java
@@ -22,6 +22,7 @@
import com.xcong.excoin.modules.coin.entity.OrderCoinsDealEntity;
import com.xcong.excoin.modules.coin.mapper.MemberAccountMoneyChangeMapper;
import com.xcong.excoin.modules.coin.parameter.dto.RecordsPageDto;
import com.xcong.excoin.modules.coin.parameter.vo.AllWalletCoinVo;
import com.xcong.excoin.modules.coin.parameter.vo.MemberAccountMoneyChangeInfoVo;
import com.xcong.excoin.modules.coin.parameter.vo.MemberAgentIntoInfoVo;
import com.xcong.excoin.modules.coin.parameter.vo.MemberWalletAgentInfoVo;
@@ -510,4 +511,145 @@
           return Result.ok(pageEntityToPageVo);
   }
   @Override
   public Result getAllWalletCoin() {
      //获取【币币】
        Long memberId = LoginUserUtils.getAppLoginUser().getId();
        PlatformCnyUsdtExchangeEntity cnyUsdtExchange = cnyUsdtExchangeDao.getCNYAndUSDTOne();
        BigDecimal cnyUsdt = cnyUsdtExchange.getValue();
        AllWalletCoinVo allWalletCoinVo = new AllWalletCoinVo();
        BigDecimal totalUsdts = BigDecimal.ZERO;
        if (!StrUtil.isEmpty(memberId.toString())) {
            List<MemberWalletCoinEntity> memberWalletCoinlist = memberWalletCoinDao.selectMemberWalletCoinsByMemberId(memberId);
            List<MemberWalletCoinInfoVo> memberWalletCoinInfoVolist = new ArrayList<MemberWalletCoinInfoVo>();
            if (CollUtil.isNotEmpty(memberWalletCoinlist)) {
                for (MemberWalletCoinEntity memberWalletCoinEntity : memberWalletCoinlist) {
                    MemberWalletCoinInfoVo memberWalletCoinInfoVo = new MemberWalletCoinInfoVo();
                    memberWalletCoinInfoVo.setAvailableBalance(memberWalletCoinEntity.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN));
                    memberWalletCoinInfoVo.setFrozenBalance(memberWalletCoinEntity.getFrozenBalance().setScale(4, BigDecimal.ROUND_DOWN));
                    memberWalletCoinInfoVo.setMemberId(memberWalletCoinEntity.getMemberId());
                    memberWalletCoinInfoVo.setTotalBalance(memberWalletCoinEntity.getTotalBalance().setScale(4, BigDecimal.ROUND_DOWN));
                    memberWalletCoinInfoVo.setWalletCode(memberWalletCoinEntity.getWalletCode());
                    memberWalletCoinInfoVolist.add(memberWalletCoinInfoVo);
                }
            }
            if (CollUtil.isNotEmpty(memberWalletCoinInfoVolist)) {
                for (MemberWalletCoinInfoVo walletCoin : memberWalletCoinInfoVolist) {
                    if (MemberWalletCoinEnum.WALLETCOINCODE.getValue().equals(walletCoin.getWalletCode())) {
                        BigDecimal totalUsdt = BigDecimal.ZERO;
                        totalUsdt = walletCoin.getAvailableBalance().add(walletCoin.getFrozenBalance());
                        totalUsdts = totalUsdts.add(totalUsdt);
                    } else {
                        BigDecimal amount = walletCoin.getAvailableBalance().add(walletCoin.getFrozenBalance());
                        // 获取最新价
                        BigDecimal closePrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(walletCoin.getWalletCode()+"/USDT")));
                        BigDecimal totalUsdt = BigDecimal.ZERO;
                        totalUsdt = totalUsdt.add(amount.multiply(closePrice));
                        totalUsdts = totalUsdts.add(totalUsdt);
                    }
                }
            }
        }
        allWalletCoinVo.setWalletUsdt(totalUsdts.setScale(4, BigDecimal.ROUND_DOWN));
      //获取【合约】
        String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
        MemberWalletContractEntity walletContract = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberId, walletCode);
        if (ObjectUtil.isEmpty(walletContract)) {
            return Result.fail(MessageSourceUtils.getString("member_service_0001"));
        }
        allWalletCoinVo.setContractUsdt(walletContract.getTotalBalance().setScale(4, BigDecimal.ROUND_DOWN));
        totalUsdts = totalUsdts.add(walletContract.getTotalBalance());
      //获取【代理】
        MemberWalletAgentEntity walletAgent = memberWalletAgentDao.selectWalletAgentBymIdAndCode(memberId, walletCode);
        BigDecimal availableBalance = walletAgent.getAvailableBalance();
        allWalletCoinVo.setAgentUsdt(availableBalance.setScale(4, BigDecimal.ROUND_DOWN));
        totalUsdts = totalUsdts.add(availableBalance);
        allWalletCoinVo.setTotalUsdt(totalUsdts.setScale(4, BigDecimal.ROUND_DOWN));
        allWalletCoinVo.setTotalCny(totalUsdts.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
        return Result.ok(allWalletCoinVo);
   }
    @Override
    public void updateWalletBalance(Long id, BigDecimal availableBalance,  BigDecimal totalBalance,BigDecimal frozenBalance) {
        if(id==null){
            return;
        }
        // 这里需要加锁 保证同一个时间只有一个线程操作一个钱包
        String key = "UPDATE_WALLET_COIN_"+id;
        while (true){
            boolean b = redisUtils.setNotExist(key, 1, 2);
            if(b){
                //System.out.println("我拿到了锁");
                // 拿到了锁才能扣
                memberWalletCoinDao.updateWalletBalance(id,availableBalance,totalBalance,frozenBalance);
                // 扣完释放锁
                redisUtils.del(key);
                break;
            }else {
            }
        }
    }
    @Override
    public Result usdtToGusd(BigDecimal balance, Integer transfertype) {
        //获取用户ID
        Long memberId = LoginUserUtils.getAppLoginUser().getId();
        if (balance.compareTo(BigDecimal.ZERO) <= 0) {
            return Result.fail(MessageSourceUtils.getString("member_service_0004"));
        }
        // 扣币
        String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
        MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, walletCode);
        BigDecimal availableBalance = memberWalletCoinEntity.getAvailableBalance();
        BigDecimal totalBalance = memberWalletCoinEntity.getTotalBalance();
        BigDecimal available = availableBalance.subtract(balance);
        if (available.compareTo(BigDecimal.ZERO) < 0) {
            return Result.fail(MessageSourceUtils.getString("member_service_0008"));
        }
        BigDecimal total = totalBalance.subtract(balance);
        if (total.compareTo(BigDecimal.ZERO) < 0) {
            return Result.fail(MessageSourceUtils.getString("member_service_0008"));
        }
        memberWalletCoinEntity.setAvailableBalance(available);
        memberWalletCoinEntity.setTotalBalance(total);
        int i = memberWalletCoinDao.updateById(memberWalletCoinEntity);
        if (i < 1) {
            return Result.fail(MessageSourceUtils.getString("member_service_0095"));
        }
        //添加资金划转历史记录
        MemberAccountMoneyChange memberAccountRecord = new MemberAccountMoneyChange();
        //获取usdt兑换gusd的兑换比例
        String gusdName = CoinTypeEnum.GUSD.name();
        MemberWalletCoinEntity gusdMemberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, gusdName);
        BigDecimal gusdAvailableBalance = gusdMemberWalletCoinEntity.getAvailableBalance();
        BigDecimal gusdTotalBalance = gusdMemberWalletCoinEntity.getTotalBalance();
        gusdMemberWalletCoinEntity.setAvailableBalance(gusdAvailableBalance.add(balance));
        gusdMemberWalletCoinEntity.setTotalBalance(gusdTotalBalance.add(balance));
        int updateById = memberWalletCoinDao.updateById(gusdMemberWalletCoinEntity);
        if (updateById < 1) {
            return Result.fail(MessageSourceUtils.getString("member_service_0095"));
        }
        //添加资金划转历史记录
        memberAccountRecord.setMemberId(memberId);
        memberAccountRecord.setStatus(MemberAccountMoneyChange.STATUS_SUCCESS_INTEGER);
        memberAccountRecord.setSymbol(walletCode);
        memberAccountRecord.setContent(MemberWalletCoinEnum.ZHIYATOGUSD.getValue());
        memberAccountRecord.setType(MemberAccountMoneyChange.TYPE_WALLET_COIN);
        memberAccountRecord.setAmount(balance);
        memberAccountMoneyChangeDao.insert(memberAccountRecord);
        return Result.ok(MessageSourceUtils.getString("member_service_0006"));
    }
}