From 48731a1c7023dc75fe7357fac5736adcf084e0c2 Mon Sep 17 00:00:00 2001 From: wzy <wzy19931122ai@163.com> Date: Sat, 27 Aug 2022 15:02:10 +0800 Subject: [PATCH] fix --- src/main/java/cc/mrbird/febs/dapp/service/impl/DappWalletServiceImpl.java | 130 +++++++++++++++++++++++------------------- 1 files changed, 71 insertions(+), 59 deletions(-) diff --git a/src/main/java/cc/mrbird/febs/dapp/service/impl/DappWalletServiceImpl.java b/src/main/java/cc/mrbird/febs/dapp/service/impl/DappWalletServiceImpl.java index 379f1aa..c0258d4 100644 --- a/src/main/java/cc/mrbird/febs/dapp/service/impl/DappWalletServiceImpl.java +++ b/src/main/java/cc/mrbird/febs/dapp/service/impl/DappWalletServiceImpl.java @@ -13,11 +13,11 @@ import cc.mrbird.febs.dapp.dto.*; import cc.mrbird.febs.dapp.entity.*; import cc.mrbird.febs.dapp.mapper.*; -import cc.mrbird.febs.dapp.service.DappSystemService; import cc.mrbird.febs.dapp.service.DappWalletService; import cc.mrbird.febs.dapp.utils.BoxUtil; import cc.mrbird.febs.dapp.vo.ActiveNftListVo; import cc.mrbird.febs.dapp.vo.WalletInfoVo; +import cc.mrbird.febs.rabbit.producer.ChainProducer; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateField; import cn.hutool.core.date.DateUnit; @@ -52,8 +52,9 @@ private final DappAccountMoneyChangeDao dappAccountMoneyChangeDao; private final RedisUtils redisUtils; private final DataDictionaryCustomMapper dataDictionaryCustomMapper; - private final DappSystemService dappSystemService; private final DappNftActivationDao dappNftActivationDao; + + private final ChainProducer chainProducer; @Override public WalletInfoVo walletInfo() { @@ -63,11 +64,16 @@ WalletInfoVo walletInfo = new WalletInfoVo(); List<DappMemberEntity> direct = dappMemberDao.selectChildMemberDirectOrNot(member.getInviteId(), 1); DappWalletCoinEntity walletCoin = dappWalletCoinDao.selectByMemberId(member.getId()); + DappWalletMineEntity walletMine = dappWalletMineDao.selectByMemberId(member.getId()); walletInfo.setDirectCnt(direct.size()); walletInfo.setInviteId(member.getInviteId()); walletInfo.setBalance(walletCoin.getAvailableAmount()); walletInfo.setHasBuy(memberInfo.getActiveStatus()); + walletInfo.setOutCnt(memberInfo.getOutCnt()); + walletInfo.setProfit(dappFundFlowDao.selectProfitAmountByMemberId(member.getId())); + walletInfo.setTFCbalance(walletMine.getAvailableAmount()); + return walletInfo; } @@ -105,36 +111,6 @@ DappAccountMoneyChangeEntity ethChange = new DappAccountMoneyChangeEntity(member.getId(), preEthAmount, walletOperateDto.getAmount(), walletMine.getAvailableAmount(), "ETH兑换USDT-ETH, 兑换价格为:" + ethUsdtPrice, AppContants.MONEY_TYPE_CHANGE); DappAccountMoneyChangeEntity usdtChange = new DappAccountMoneyChangeEntity(member.getId(), preUsdtAmount, usdt, walletCoin.getAvailableAmount(), "ETH兑换USDT-USDT, 兑换价格为:" + ethUsdtPrice, AppContants.MONEY_TYPE_CHANGE); dappAccountMoneyChangeDao.insert(ethChange); - dappAccountMoneyChangeDao.insert(usdtChange); - } - - @Override - @Transactional(rollbackFor = Exception.class) - public void withdraw(WalletOperateDto walletOperateDto) { - DappMemberEntity member = LoginUserUtil.getAppUser(); - - List<DappFundFlowEntity> fundFlows = dappFundFlowDao.selectListForMemberAndDay(member.getId(), 2); - if (CollUtil.isNotEmpty(fundFlows)) { - throw new FebsException("一天只能提现一次"); - } - - DappWalletCoinEntity walletCoin = dappWalletCoinDao.selectByMemberId(member.getId()); - if (walletOperateDto.getAmount().compareTo(walletCoin.getAvailableAmount()) > 0) { - throw new FebsException("可用金额不足"); - } - - Integer fee = (Integer) redisUtils.get(AppContants.REDIS_KEY_CHANGE_FEE); - - //TODO 并发加悲观锁 - BigDecimal preAmount = walletCoin.getAvailableAmount(); - walletCoin.setAvailableAmount(walletCoin.getAvailableAmount().subtract(walletOperateDto.getAmount())); - walletCoin.setFrozenAmount(walletCoin.getFrozenAmount().add(walletOperateDto.getAmount())); - dappWalletCoinDao.updateById(walletCoin); - - DappFundFlowEntity fund = new DappFundFlowEntity(member.getId(), walletOperateDto.getAmount(), AppContants.MONEY_TYPE_WITHDRAWAL, 1, new BigDecimal(fee)); - dappFundFlowDao.insert(fund); - - DappAccountMoneyChangeEntity usdtChange = new DappAccountMoneyChangeEntity(member.getId(), preAmount, walletOperateDto.getAmount(), walletCoin.getAvailableAmount(), "USDT申请提现", AppContants.MONEY_TYPE_WITHDRAWAL); dappAccountMoneyChangeDao.insert(usdtChange); } @@ -223,13 +199,21 @@ if (transferDto.getAmount().compareTo(walletCoin.getAvailableAmount()) > 0) { throw new FebsException("Balance Not Enough"); } - walletCoin.setTotalAmount(walletCoin.getTotalAmount().subtract(transferDto.getAmount())); - walletCoin.setAvailableAmount(walletCoin.getAvailableAmount().subtract(transferDto.getAmount())); - dappWalletCoinDao.updateById(walletCoin); + updateWalletCoinWithLock(transferDto.getAmount().negate(), member.getId()); + + DappFundFlowEntity fundFlow = new DappFundFlowEntity(member.getId(), transferDto.getAmount().negate(), 1, 2, transferDto.getFee(), transferDto.getTxHash()); + dappFundFlowDao.insert(fundFlow); + + chainProducer.sendAchieveTreeMsg(member.getId()); } else { + int type = 1; + // 1-认购 2-充值tfc + if (transferDto.getType() == 2) { + type = 6; + } if (transferDto.getId() == null) { - DappFundFlowEntity fundFlow = new DappFundFlowEntity(member.getId(), transferDto.getAmount(), transferDto.getType(), 1, transferDto.getFee(), transferDto.getTxHash()); + DappFundFlowEntity fundFlow = new DappFundFlowEntity(member.getId(), transferDto.getAmount(), type, 1, transferDto.getFee(), transferDto.getTxHash()); dappFundFlowDao.insert(fundFlow); return fundFlow.getId(); } @@ -250,30 +234,11 @@ @Override public Map<String, BigDecimal> calPrice(PriceDto priceDto) { - DappMemberEntity member = LoginUserUtil.getAppUser(); + String priceStr = redisUtils.getString(AppContants.REDIS_KEY_TFC_NEW_PRICE); - if (!dappSystemService.systemHasStart()) { - HashMap<String, BigDecimal> map = new HashMap<>(); - map.put("x", new BigDecimal("0.05")); - map.put("y", new BigDecimal("0.05")); - return map; - } - - ContractChainService tfcInstance = ChainService.getInstance(ChainEnum.BSC_TFC.name()); - // u剩余数量 - BigDecimal sourceU = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOf(ChainEnum.BSC_USDT_SOURCE.getAddress()); - // 源池代币剩余数量 - BigDecimal sourceCoin = tfcInstance.balanceOf(ChainEnum.BSC_USDT_SOURCE.getAddress()); - // 用户卖出数量 - BigDecimal coin = priceDto.getAmount(); - BigDecimal x = sourceU.divide(sourceCoin, tfcInstance.decimals(), RoundingMode.HALF_UP); - BigDecimal y = sourceU.divide(sourceCoin.add(coin), tfcInstance.decimals(), RoundingMode.HALF_UP); - - log.info("购买价格:{}, 出卖价格:{}", x, y); - HashMap<String, BigDecimal> map = new HashMap<>(); - map.put("x", x); - map.put("y", y); - return map; + Map<String, BigDecimal> data = new HashMap<>(); + data.put("fee", new BigDecimal(priceStr)); + return data; } @Override @@ -394,6 +359,53 @@ @Override public void withdraw(WithdrawDto withdrawDto) { + DappMemberEntity member = LoginUserUtil.getAppUser(); + updateWalletCoinWithLock(withdrawDto.getAmount().negate(), member.getId()); + updateWalletMineWithLock(withdrawDto.getFee(), member.getId()); + + DappFundFlowEntity feeFlow = new DappFundFlowEntity(member.getId(), withdrawDto.getFee().negate(), 7, 2, null, null); + dappFundFlowDao.insert(feeFlow); + + DappFundFlowEntity fundFlow = new DappFundFlowEntity(member.getId(), withdrawDto.getAmount().negate(), 5, 2, withdrawDto.getFee(), null); + dappFundFlowDao.insert(fundFlow); + + String hash = ChainService.getInstance(ChainEnum.BSC_USDT.name()).transfer(member.getAddress(), withdrawDto.getAmount()); + fundFlow.setToHash(hash); + dappFundFlowDao.updateById(fundFlow); + + chainProducer.sendTfcFee(withdrawDto.getFee().toPlainString()); + } + + @Override + public void updateWalletCoinWithLock(BigDecimal amount, Long memberId) { + boolean isSuccess = false; + while(!isSuccess) { + DappWalletCoinEntity walletCoin = dappWalletCoinDao.selectByMemberId(memberId); + + walletCoin.setTotalAmount(walletCoin.getTotalAmount().add(amount)); + walletCoin.setAvailableAmount(walletCoin.getAvailableAmount().add(amount)); + + int i = dappWalletCoinDao.updateWithLock(walletCoin); + if (i > 0) { + isSuccess = true; + } + } + } + + @Override + public void updateWalletMineWithLock(BigDecimal amount, Long memberId) { + boolean isSuccess = false; + while(!isSuccess) { + DappWalletCoinEntity walletCoin = dappWalletCoinDao.selectByMemberId(memberId); + + walletCoin.setTotalAmount(walletCoin.getTotalAmount().add(amount)); + walletCoin.setAvailableAmount(walletCoin.getAvailableAmount().add(amount)); + + int i = dappWalletCoinDao.updateWithLock(walletCoin); + if (i > 0) { + isSuccess = true; + } + } } } -- Gitblit v1.9.1