From 951c94ad11acdfdd8d174fe0840a9c11d55f4cc7 Mon Sep 17 00:00:00 2001
From: KKSU <15274802129@163.com>
Date: Thu, 13 Feb 2025 12:32:19 +0800
Subject: [PATCH] feat(runVip): 添加线上充值字段并更新相关页面和数据映射

---
 src/main/java/cc/mrbird/febs/mall/service/impl/RunVipServiceImpl.java |  543 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 530 insertions(+), 13 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/mall/service/impl/RunVipServiceImpl.java b/src/main/java/cc/mrbird/febs/mall/service/impl/RunVipServiceImpl.java
index 635c78d..9fa2d82 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/impl/RunVipServiceImpl.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/impl/RunVipServiceImpl.java
@@ -6,8 +6,10 @@
 import cc.mrbird.febs.common.enumerates.RunVipMoneyFlowTypeEnum;
 import cc.mrbird.febs.common.enumerates.YesOrNoEnum;
 import cc.mrbird.febs.common.exception.FebsException;
+import cc.mrbird.febs.common.utils.AppContants;
 import cc.mrbird.febs.common.utils.LoginUserUtil;
 import cc.mrbird.febs.common.utils.MallUtils;
+import cc.mrbird.febs.common.utils.RedisUtils;
 import cc.mrbird.febs.mall.conversion.RunVipConversion;
 import cc.mrbird.febs.mall.dto.*;
 import cc.mrbird.febs.mall.entity.*;
@@ -30,11 +32,15 @@
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ThreadLocalRandom;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 
@@ -52,6 +58,10 @@
     private final MallMemberMapper mallMemberMapper;
     private final IApiMallMemberWalletService walletService;
     private final IMallMoneyFlowService mallMoneyFlowService;
+    private final RunVipGrowMapper runVipGrowMapper;
+
+    private final RedisUtils redisUtils;
+
     @Override
     public List<ApiRunVipVo> vipInfo() {
         Long memberId = LoginUserUtil.getLoginUser().getId();
@@ -86,12 +96,28 @@
             throw new FebsException("资金密码错误");
         }
         Long runVipId = apiGoChargeDto.getRunVipId();
-        RunVip runVip = this.baseMapper.selectById(runVipId);
-        if(runVip.getState() != YesOrNoEnum.YES.getValue()){
+        RunVip runVipNext = this.baseMapper.selectById(runVipId);
+        if(runVipNext.getState() != YesOrNoEnum.YES.getValue()){
             throw new FebsException("会员套餐已下架");
         }
         //价格
-        BigDecimal presentAmount = runVip.getPresentPrice().multiply(new BigDecimal(apiGoChargeDto.getVipCnt())).setScale(2,BigDecimal.ROUND_DOWN);
+        BigDecimal presentAmount = runVipNext.getPresentPrice().multiply(new BigDecimal(apiGoChargeDto.getVipCnt())).setScale(2,BigDecimal.ROUND_DOWN);
+        //减免价格
+        RunVip runVip = this.baseMapper.selectOne(new LambdaQueryWrapper<RunVip>().eq(RunVip::getVipCode, mallMember.getLevel()));
+        BigDecimal reduceAmount = runVip.getPresentPrice();
+        RunVipGrow runVipGrow = runVipGrowMapper.selectList(
+                new LambdaQueryWrapper<RunVipGrow>()
+                        .eq(RunVipGrow::getMemberId, memberId)
+                        .eq(RunVipGrow::getLevelNow, mallMember.getLevel())
+                        .orderByDesc(RunVipGrow::getId)
+        ).stream().findFirst().orElse(null);
+        if(runVipGrow != null){
+            reduceAmount = runVipGrow.getAmountNow();
+        }
+        presentAmount = presentAmount.subtract(reduceAmount);
+        if(BigDecimal.ZERO.compareTo(presentAmount) >= 0){
+            throw new FebsException("支付异常,请刷新页面重试");
+        }
 
         Long addressId = apiGoChargeDto.getAddressId();
         MallMemberPayment mallMemberPayment = mallMemberPaymentMapper.selectById(addressId);
@@ -129,17 +155,21 @@
         mallCharge.setType(mallMemberPayment.getBankNo());
         mallCharge.setAddress(mallMemberPayment.getBank());
         mallCharge.setAmount(presentAmount);
+
+        BigDecimal amountReal = processAmount(presentAmount,memberId);
+        mallCharge.setAmountReal(amountReal);
+
         mallCharge.setFailTime(failTime);
         mallCharge.setSysAddress(sysAddress);
-        mallCharge.setVipCode(runVip.getVipCode());
-        mallCharge.setVipName(runVip.getVipName());
+        mallCharge.setVipCode(runVipNext.getVipCode());
+        mallCharge.setVipName(runVipNext.getVipName());
         mallCharge.setVipCnt(apiGoChargeDto.getVipCnt());
         mallChargeMapper.insert(mallCharge);
 
         ApiGoChargeVo apiGoChargeVo = new ApiGoChargeVo();
         apiGoChargeVo.setFailTime(mallCharge.getFailTime());
         apiGoChargeVo.setAddress(mallCharge.getAddress());
-        apiGoChargeVo.setAmount(mallCharge.getAmount());
+        apiGoChargeVo.setAmount(mallCharge.getAmountReal());
         apiGoChargeVo.setSysAddress(mallCharge.getSysAddress());
         apiGoChargeVo.setSysAddressType(mallCharge.getType());
 
@@ -156,6 +186,7 @@
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public FebsResponse balanceBuy(ApiBalanceBuyDto apiBalanceBuyDto) {
         Long memberId = LoginUserUtil.getLoginUser().getId();
         MallMember mallMember = mallMemberMapper.selectById(memberId);
@@ -167,12 +198,30 @@
             throw new FebsException("资金密码错误");
         }
         Long runVipId = apiBalanceBuyDto.getRunVipId();
-        RunVip runVip = this.baseMapper.selectById(runVipId);
-        if(runVip.getState() != YesOrNoEnum.YES.getValue()){
+        RunVip runVipNext = this.baseMapper.selectById(runVipId);
+        if(runVipNext.getState() != YesOrNoEnum.YES.getValue()){
             throw new FebsException("会员套餐已下架");
         }
+
         //价格
-        BigDecimal presentAmount = runVip.getPresentPrice().setScale(2,BigDecimal.ROUND_DOWN);
+        BigDecimal presentAmount = runVipNext.getPresentPrice().setScale(2,BigDecimal.ROUND_DOWN);
+        //减免价格
+        RunVip runVip = this.baseMapper.selectOne(new LambdaQueryWrapper<RunVip>().eq(RunVip::getVipCode, mallMember.getLevel()));
+        BigDecimal reduceAmount = runVip.getPresentPrice();
+        RunVipGrow runVipGrow = runVipGrowMapper.selectList(
+                new LambdaQueryWrapper<RunVipGrow>()
+                        .eq(RunVipGrow::getMemberId, memberId)
+                        .eq(RunVipGrow::getLevelNow, mallMember.getLevel())
+                        .orderByDesc(RunVipGrow::getId)
+        ).stream().findFirst().orElse(null);
+        if(runVipGrow != null){
+            reduceAmount = runVipGrow.getAmountNow();
+        }
+        presentAmount = presentAmount.subtract(reduceAmount);
+        if(BigDecimal.ZERO.compareTo(presentAmount) >= 0){
+            throw new FebsException("支付异常,请刷新页面重试");
+        }
+
         MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
         if(mallMemberWallet.getCommission().compareTo(BigDecimal.ZERO) <= 0){
             throw new FebsException("余额不足");
@@ -189,7 +238,7 @@
                 orderNo,
                 FlowTypeEnum.COMMISSION.getValue(),
                 RunVipMoneyFlowTypeEnum.COMMISSION_PAY.getValue(),
-                presentAmount,
+                presentAmount.negate(),
                 StrUtil.format(RunVipMoneyFlowTypeEnum.COMMISSION_PAY.getDescription(),presentAmount),
                 YesOrNoEnum.YES.getValue()
         );
@@ -201,8 +250,8 @@
         mallCharge.setType(StrUtil.format(RunVipMoneyFlowTypeEnum.COMMISSION_PAY.getDescription(),presentAmount));
         mallCharge.setAddress(mallMember.getPhone());
         mallCharge.setAmount(presentAmount);
-        mallCharge.setVipCode(runVip.getVipCode());
-        mallCharge.setVipName(runVip.getVipName());
+        mallCharge.setVipCode(runVipNext.getVipCode());
+        mallCharge.setVipName(runVipNext.getVipName());
         mallCharge.setVipCnt(apiBalanceBuyDto.getVipCnt());
         mallChargeMapper.insert(mallCharge);
 
@@ -227,7 +276,7 @@
         }
         apiGoChargeVo.setFailTime(mallCharge.getFailTime());
         apiGoChargeVo.setAddress(mallCharge.getAddress());
-        apiGoChargeVo.setAmount(mallCharge.getAmount());
+        apiGoChargeVo.setAmount(mallCharge.getAmountReal());
         apiGoChargeVo.setSysAddress(mallCharge.getSysAddress());
         apiGoChargeVo.setSysAddressType(mallCharge.getType());
         return new FebsResponse().success().data(apiGoChargeVo);
@@ -384,4 +433,472 @@
         }
         return apiRunHealthVo;
     }
+
+    @Override
+    public FebsResponse growInfo() {
+        Long memberId = LoginUserUtil.getLoginUser().getId();
+        ApiGrowInfoVo apiGrowInfoVo = new ApiGrowInfoVo();
+        MallMember mallMember = mallMemberMapper.selectById(memberId);
+        String memberLevel = mallMember.getLevel();
+
+        RunVipGrow runVipGrow = runVipGrowMapper.selectOne(
+                new LambdaQueryWrapper<RunVipGrow>()
+                        .eq(RunVipGrow::getMemberId, memberId)
+                        .orderByDesc(RunVipGrow::getId)
+                        .last("limit 1")
+        );
+
+        //有升级权益的记录、当前等级和最新的升级记录是同一个
+        if(runVipGrow != null && memberLevel.equals(runVipGrow.getLevelNow())){
+            apiGrowInfoVo.setLevelNow(runVipGrow.getLevelNow());
+            apiGrowInfoVo.setAmountNow(runVipGrow.getAmountNow());
+            apiGrowInfoVo.setLevelNext(runVipGrow.getLevelNext());
+            apiGrowInfoVo.setAmountAll(runVipGrow.getAmountAll());
+        }else{
+            List<RunVip> runVips = this.baseMapper.selectList(null);
+            Map<String, RunVip> runVipMap = runVips.stream()
+                    .collect(Collectors.toMap(RunVip::getVipCode, runVip -> runVip));
+            RunVip runVipNow = runVipMap.get(memberLevel);
+            apiGrowInfoVo.setLevelNow(runVipNow.getVipCode());
+            apiGrowInfoVo.setAmountNow(runVipNow.getPresentPrice());
+
+
+            //获取runVips中的orderNumber为runVipNow.getOrderNumber() + 1的那个元素
+            RunVip runVipNext = runVips.stream().filter(runVip -> runVip.getOrderNumber() == runVipNow.getOrderNumber() + 1).findFirst().orElse(null);
+            if(runVipNext != null){
+                apiGrowInfoVo.setLevelNext(runVipNext.getVipCode());
+                apiGrowInfoVo.setAmountAll(runVipNext.getPresentPrice());
+            }
+        }
+        return new FebsResponse().success().data(apiGrowInfoVo);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public FebsResponse growBuy(ApiGrowBuyDto apiGrowBuyDto) {
+        Long memberId = LoginUserUtil.getLoginUser().getId();
+
+        BigDecimal amount = apiGrowBuyDto.getAmount();
+        if(BigDecimal.ZERO.compareTo(amount) >= 0){
+            throw new FebsException("金额异常");
+        }
+
+        MallMember mallMember = mallMemberMapper.selectById(memberId);
+        if (StrUtil.isBlank(mallMember.getTradePassword())) {
+            throw new FebsException("未设置资金密码");
+        }
+
+        if (!mallMember.getTradePassword().equals(SecureUtil.md5(apiGrowBuyDto.getTradeWord()))) {
+            throw new FebsException("资金密码错误");
+        }
+
+        MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
+        if(mallMemberWallet.getCommission().compareTo(amount) < 0){
+            throw new FebsException("余额不足");
+        }
+
+        String memberLevel = mallMember.getLevel();
+        String levelNext = apiGrowBuyDto.getLevelNext();
+
+        List<RunVip> runVips = this.baseMapper.selectList(null);
+        Map<String, RunVip> runVipMap = runVips.stream()
+                .collect(Collectors.toMap(RunVip::getVipCode, runVip -> runVip));
+        RunVip runVip = runVipMap.get(memberLevel);
+        RunVip runVipNext = runVipMap.get(levelNext);
+        if(runVipNext == null){
+            throw new FebsException("无法升级会员权益");
+        }
+        if(runVip.getOrderNumber() >= runVipNext.getOrderNumber()){
+            throw new FebsException("用户无法升级");
+        }
+
+        walletService.reduceCommission(amount,memberId);
+        String orderNo = MallUtils.getOrderNum("QY");
+        mallMoneyFlowService.runVipMoneyFlowAdd(
+                memberId,
+                memberId,
+                orderNo,
+                FlowTypeEnum.COMMISSION.getValue(),
+                RunVipMoneyFlowTypeEnum.COMMISSION_PAY_GROW.getValue(),
+                amount.negate(),
+                StrUtil.format(RunVipMoneyFlowTypeEnum.COMMISSION_PAY_GROW.getDescription(),amount),
+                YesOrNoEnum.YES.getValue()
+        );
+
+        RunVipGrow runVipGrow = runVipGrowMapper.selectOne(
+                new LambdaQueryWrapper<RunVipGrow>()
+                        .eq(RunVipGrow::getMemberId, memberId)
+                        .eq(RunVipGrow::getLevelNow, memberLevel)
+                        .eq(RunVipGrow::getLevelNext, levelNext)
+        );
+
+        MallCharge mallCharge = new MallCharge();
+        mallCharge.setMemberId(memberId);
+        mallCharge.setOrderNo(orderNo);
+        mallCharge.setState(YesOrNoEnum.YES.getValue());
+        mallCharge.setType(StrUtil.format(RunVipMoneyFlowTypeEnum.COMMISSION_PAY_GROW.getDescription(),amount));
+        mallCharge.setAddress(mallMember.getPhone());
+        mallCharge.setAmount(amount);
+        mallCharge.setVipCnt(1);
+        mallCharge.setVipName(runVip.getVipName()+"权益");
+        mallCharge.setVipCode(runVip.getVipCode());
+        //有升级权益的记录
+        if(runVipGrow != null){
+            //判断金额是否满足升级条件
+            BigDecimal amountNow = runVipGrow.getAmountNow();
+            BigDecimal amountAll = runVipGrow.getAmountAll();
+            BigDecimal subtract = amountAll.subtract(amountNow);
+
+            if(subtract.compareTo(amount) <= 0){
+                mallCharge.setVipCode(runVipGrow.getLevelNext());
+            }else{
+                mallCharge.setVipCode(memberLevel);
+            }
+        }else{
+
+            BigDecimal presentPrice = runVip.getPresentPrice();
+            BigDecimal add = presentPrice.add(amount);
+
+            BigDecimal presentPriceNext = runVipNext.getPresentPrice();
+            if(presentPriceNext.compareTo(add) <= 0){
+                mallCharge.setVipCode(runVipNext.getVipCode());
+            }
+        }
+        mallChargeMapper.insert(mallCharge);
+
+        agentProducer.sendBuyVipSuccessMsg(mallCharge.getId());
+
+        return new FebsResponse().success().message("操作成功");
+    }
+
+    @Override
+    public FebsResponse growCharge(ApiGrowChargeDto apiGrowChargeDto) {
+        Long memberId = LoginUserUtil.getLoginUser().getId();
+
+        ApiGoChargeVo apiGoChargeVo = new ApiGoChargeVo();
+
+        BigDecimal amount = apiGrowChargeDto.getAmount();
+        if(BigDecimal.ZERO.compareTo(amount) >= 0){
+            throw new FebsException("金额异常");
+        }
+
+        MallMember mallMember = mallMemberMapper.selectById(memberId);
+        if (StrUtil.isBlank(mallMember.getTradePassword())) {
+            throw new FebsException("未设置资金密码");
+        }
+
+        if (!mallMember.getTradePassword().equals(SecureUtil.md5(apiGrowChargeDto.getTradeWord()))) {
+            throw new FebsException("资金密码错误");
+        }
+
+        String memberLevel = mallMember.getLevel();
+        String levelNext = apiGrowChargeDto.getLevelNext();
+
+        List<RunVip> runVips = this.baseMapper.selectList(null);
+        Map<String, RunVip> runVipMap = runVips.stream()
+                .collect(Collectors.toMap(RunVip::getVipCode, runVip -> runVip));
+        RunVip runVip = runVipMap.get(memberLevel);
+        RunVip runVipNext = runVipMap.get(levelNext);
+        if(runVipNext == null){
+            throw new FebsException("无法升级会员权益");
+        }
+        if(runVip.getOrderNumber() >= runVipNext.getOrderNumber()){
+            throw new FebsException("用户无法升级");
+        }
+
+        Long addressId = apiGrowChargeDto.getAddressId();
+        MallMemberPayment mallMemberPayment = mallMemberPaymentMapper.selectById(addressId);
+        if(ObjectUtil.isEmpty(mallMemberPayment)){
+            return new FebsResponse().fail().message("请先绑定你的地址");
+        }
+
+        //判断系统的充值地址
+        String trcType = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
+                RunVipDataDictionaryEnum.CHARGE_TYPE_TRC.getType(),
+                RunVipDataDictionaryEnum.CHARGE_TYPE_TRC.getCode()
+        ).getValue();
+        String sysAddress = "";
+        if(trcType.equals(mallMemberPayment.getBankNo())){
+            sysAddress = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
+                    RunVipDataDictionaryEnum.CHARGE_SYS_ADDRESS_TRC.getType(),
+                    RunVipDataDictionaryEnum.CHARGE_SYS_ADDRESS_TRC.getCode()
+            ).getValue();
+        }else{
+            sysAddress = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
+                    RunVipDataDictionaryEnum.CHARGE_SYS_ADDRESS_BSC.getType(),
+                    RunVipDataDictionaryEnum.CHARGE_SYS_ADDRESS_BSC.getCode()
+            ).getValue();
+        }
+        String failMinutes = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
+                RunVipDataDictionaryEnum.CHARGE_SYS_FAIL_TIME.getType(),
+                RunVipDataDictionaryEnum.CHARGE_SYS_FAIL_TIME.getCode()
+        ).getValue();
+        DateTime failTime = DateUtil.offsetMinute(DateUtil.date(), Integer.parseInt(failMinutes));
+
+        MallCharge mallCharge = new MallCharge();
+        String orderNo = MallUtils.getOrderNum("QY");
+        mallCharge.setMemberId(memberId);
+        mallCharge.setOrderNo(orderNo);
+        mallCharge.setState(YesOrNoEnum.ING.getValue());
+        mallCharge.setType(mallMemberPayment.getBankNo());
+        mallCharge.setAddress(mallMemberPayment.getBank());
+        mallCharge.setFailTime(failTime);
+        mallCharge.setSysAddress(sysAddress);
+
+        mallCharge.setAmount(amount);
+
+        BigDecimal amountReal = processAmount(amount,memberId);
+        mallCharge.setAmountReal(amountReal);
+
+        mallCharge.setVipCnt(1);
+        mallCharge.setVipName(runVip.getVipName()+"权益");
+        mallCharge.setVipCode(runVip.getVipCode());
+
+        RunVipGrow runVipGrow = runVipGrowMapper.selectOne(
+                new LambdaQueryWrapper<RunVipGrow>()
+                        .eq(RunVipGrow::getMemberId, memberId)
+                        .eq(RunVipGrow::getLevelNow, memberLevel)
+                        .eq(RunVipGrow::getLevelNext, levelNext)
+        );
+
+        //有升级权益的记录、当前等级和最新的升级记录是同一个
+        if(runVipGrow != null){
+            //判断金额是否满足升级条件
+            BigDecimal amountNow = runVipGrow.getAmountNow();
+            BigDecimal amountAll = runVipGrow.getAmountAll();
+            BigDecimal subtract = amountAll.subtract(amountNow);
+
+            if(subtract.compareTo(amount) <= 0){
+                mallCharge.setVipCode(runVipGrow.getLevelNext());
+            }
+        }else{
+            BigDecimal add = runVip.getPresentPrice().add(amount);
+            if(runVipNext.getPresentPrice().compareTo(add) <= 0){
+                mallCharge.setVipCode(runVipNext.getVipCode());
+            }
+        }
+
+        mallChargeMapper.insert(mallCharge);
+
+        apiGoChargeVo.setFailTime(mallCharge.getFailTime());
+        apiGoChargeVo.setAddress(mallCharge.getAddress());
+        apiGoChargeVo.setAmount(mallCharge.getAmountReal());
+        apiGoChargeVo.setSysAddress(mallCharge.getSysAddress());
+        apiGoChargeVo.setSysAddressType(mallCharge.getType());
+
+        /**
+         * 充值接口调用后,发送一个延时队列
+         *  功能:延迟时间为【failMinutes】后,更新充值记录为失败。
+         */
+        ApiMemberChargeFailDto apiMemberChargeFailDto = new ApiMemberChargeFailDto();
+        apiMemberChargeFailDto.setChargeId(mallCharge.getId());
+        apiMemberChargeFailDto.setFailTime(Integer.parseInt(failMinutes) * 60L* 1000L);
+        agentProducer.sendMemberChargeFailMsg(apiMemberChargeFailDto);
+        return new FebsResponse().success().data(apiGoChargeVo);
+    }
+
+    @Override
+    public FebsResponse reduceAmount() {
+        Long memberId = LoginUserUtil.getLoginUser().getId();
+        ApiReduceAmountVo apiReduceAmountVo = new ApiReduceAmountVo();
+        MallMember mallMember = mallMemberMapper.selectById(memberId);
+        //减免价格
+        RunVip runVip = this.baseMapper.selectOne(new LambdaQueryWrapper<RunVip>().eq(RunVip::getVipCode, mallMember.getLevel()));
+        BigDecimal reduceAmount = runVip.getPresentPrice();
+        RunVipGrow runVipGrow = runVipGrowMapper.selectList(
+                new LambdaQueryWrapper<RunVipGrow>()
+                        .eq(RunVipGrow::getMemberId, memberId)
+                        .eq(RunVipGrow::getLevelNow, mallMember.getLevel())
+                        .orderByDesc(RunVipGrow::getId)
+        ).stream().findFirst().orElse(null);
+        if(runVipGrow != null){
+            BigDecimal amountNow = runVipGrow.getAmountNow();
+            reduceAmount = amountNow;
+        }
+        apiReduceAmountVo.setReduceAmount(reduceAmount);
+        return new FebsResponse().success().data(apiReduceAmountVo);
+    }
+
+    @Override
+    public FebsResponse goChargeUSDT(ApiGoChargeUSDTDto apiGoChargeDto) {
+
+        Long memberId = LoginUserUtil.getLoginUser().getId();
+
+        ApiGoChargeVo apiGoChargeVo = new ApiGoChargeVo();
+
+        BigDecimal amount = apiGoChargeDto.getAmount();
+        if(BigDecimal.ZERO.compareTo(amount) >= 0){
+            throw new FebsException("金额异常");
+        }
+
+        MallMember mallMember = mallMemberMapper.selectById(memberId);
+        if (StrUtil.isBlank(mallMember.getTradePassword())) {
+            throw new FebsException("未设置资金密码");
+        }
+
+        if (!mallMember.getTradePassword().equals(SecureUtil.md5(apiGoChargeDto.getTradeWord()))) {
+            throw new FebsException("资金密码错误");
+        }
+
+        Long addressId = apiGoChargeDto.getAddressId();
+        MallMemberPayment mallMemberPayment = mallMemberPaymentMapper.selectById(addressId);
+        if(ObjectUtil.isEmpty(mallMemberPayment)){
+            return new FebsResponse().fail().message("请先绑定你的地址");
+        }
+
+        //判断系统的充值地址
+        String trcType = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
+                RunVipDataDictionaryEnum.CHARGE_TYPE_TRC.getType(),
+                RunVipDataDictionaryEnum.CHARGE_TYPE_TRC.getCode()
+        ).getValue();
+        String sysAddress = "";
+        if(trcType.equals(mallMemberPayment.getBankNo())){
+            sysAddress = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
+                    RunVipDataDictionaryEnum.CHARGE_SYS_ADDRESS_TRC.getType(),
+                    RunVipDataDictionaryEnum.CHARGE_SYS_ADDRESS_TRC.getCode()
+            ).getValue();
+        }else{
+            sysAddress = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
+                    RunVipDataDictionaryEnum.CHARGE_SYS_ADDRESS_BSC.getType(),
+                    RunVipDataDictionaryEnum.CHARGE_SYS_ADDRESS_BSC.getCode()
+            ).getValue();
+        }
+        String failMinutes = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
+                RunVipDataDictionaryEnum.CHARGE_SYS_FAIL_TIME.getType(),
+                RunVipDataDictionaryEnum.CHARGE_SYS_FAIL_TIME.getCode()
+        ).getValue();
+        DateTime failTime = DateUtil.offsetMinute(DateUtil.date(), Integer.parseInt(failMinutes));
+
+        MallCharge mallCharge = new MallCharge();
+        String orderNo = MallUtils.getOrderNum("C");
+        mallCharge.setMemberId(memberId);
+        mallCharge.setOrderNo(orderNo);
+        mallCharge.setState(YesOrNoEnum.ING.getValue());
+        mallCharge.setType(mallMemberPayment.getBankNo());
+        mallCharge.setAddress(mallMemberPayment.getBank());
+        mallCharge.setFailTime(failTime);
+        mallCharge.setSysAddress(sysAddress);
+
+        mallCharge.setAmount(amount);
+        mallCharge.setVipName(RunVipMoneyFlowTypeEnum.COMMISSION_PAY_CHARGE.getTypeDec());
+
+        BigDecimal amountReal = processAmount(amount,memberId);
+        mallCharge.setAmountReal(amountReal);
+
+        mallChargeMapper.insert(mallCharge);
+
+        apiGoChargeVo.setFailTime(mallCharge.getFailTime());
+        apiGoChargeVo.setAddress(mallCharge.getAddress());
+        apiGoChargeVo.setAmount(mallCharge.getAmountReal());
+        apiGoChargeVo.setSysAddress(mallCharge.getSysAddress());
+        apiGoChargeVo.setSysAddressType(mallCharge.getType());
+
+        /**
+         * 充值接口调用后,发送一个延时队列
+         *  功能:延迟时间为【failMinutes】后,更新充值记录为失败。
+         */
+        ApiMemberChargeFailDto apiMemberChargeFailDto = new ApiMemberChargeFailDto();
+        apiMemberChargeFailDto.setChargeId(mallCharge.getId());
+        apiMemberChargeFailDto.setFailTime(Integer.parseInt(failMinutes) * 60L* 1000L);
+        agentProducer.sendMemberChargeFailMsg(apiMemberChargeFailDto);
+        return new FebsResponse().success().data(apiGoChargeVo);
+    }
+
+    @Override
+    public FebsResponse goChargeUSDTInfo(ApiGoChargeInfoDto apiGoChargeInfoDto) {
+        Long memberId = LoginUserUtil.getLoginUser().getId();
+        String orderNo = apiGoChargeInfoDto.getOrderNo();
+
+        LambdaQueryWrapper<MallCharge> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        objectLambdaQueryWrapper.eq(MallCharge::getMemberId,memberId);
+        objectLambdaQueryWrapper.eq(MallCharge::getOrderNo,orderNo);
+        MallCharge mallCharge = mallChargeMapper.selectOne(objectLambdaQueryWrapper);
+
+        ApiGoChargeVo apiGoChargeVo = new ApiGoChargeVo();
+        if(null == mallCharge){
+            throw new FebsException("记录不存在");
+        }
+        apiGoChargeVo.setFailTime(mallCharge.getFailTime());
+        apiGoChargeVo.setAddress(mallCharge.getAddress());
+        apiGoChargeVo.setAmount(mallCharge.getAmountReal());
+        apiGoChargeVo.setSysAddress(mallCharge.getSysAddress());
+        apiGoChargeVo.setSysAddressType(mallCharge.getType());
+        return new FebsResponse().success().data(apiGoChargeVo);
+    }
+
+    /**
+     * 处理金额并生成唯一编号
+     * 此方法用于根据给定的金额和会员ID生成一个唯一的数字
+     *
+     * @param amount 金额,必须大于0
+     * @param memberId 会员ID,不能为空
+     * @return 生成的唯一数字
+     * @throws NullPointerException 如果金额或会员ID为null
+     */
+    public BigDecimal processAmount(BigDecimal amount, Long memberId) {
+        Objects.requireNonNull(amount, "金额不能为空");
+        Objects.requireNonNull(memberId, "会员ID不能为空");
+
+        return generateUniqueNumber(amount,memberId);
+    }
+
+    /**
+     * 生成唯一数字
+     * 通过随机数和给定的金额计算生成一个唯一数字,并确保这个数字在Redis中是唯一的
+     *
+     * @param amount 金额
+     * @param memberId 会员ID
+     * @return 生成的唯一数字
+     * @throws RuntimeException 如果在指定次数内无法生成唯一数字
+     */
+    private BigDecimal generateUniqueNumber(BigDecimal amount,Long memberId) {
+        int maxAttempts = 50;
+        int attempts = 0;
+        int num;
+        BigDecimal bigDecimal;
+
+        do {
+            if (attempts++ >= maxAttempts) {
+                throw new RuntimeException("无法生成唯一数字");
+            }
+            num = ThreadLocalRandom.current().nextInt(1, 51);
+
+            bigDecimal = calculateAmount(amount, num);
+
+        } while (!setRedisValue(bigDecimal, memberId)); // 原子操作检查并写入
+
+        return bigDecimal;
+    }
+
+    /**
+     * 计算调整后的金额
+     * 根据给定的金额和一个随机数计算新的金额,用于生成唯一数字
+     *
+     * @param amount 原始金额
+     * @param num 随机生成的数字
+     * @return 调整后的金额
+     */
+    private BigDecimal calculateAmount(BigDecimal amount, int num) {
+        BigDecimal multiplier = new BigDecimal("0.01");
+        BigDecimal increment = new BigDecimal(num).multiply(multiplier);
+        return amount.add(increment).setScale(2, RoundingMode.DOWN);
+    }
+
+    /**
+     * 将生成的数字设置到Redis中
+     * 此方法确保生成的数字是唯一的,通过在Redis中设置值并使用NX参数来实现
+     *
+     * @param amountReal 生成的唯一数字
+     * @param memberId 会员ID
+     * @return 如果设置成功则返回true,否则返回false
+     */
+    private boolean setRedisValue(BigDecimal amountReal, Long memberId) {
+        String failMinutes = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
+                RunVipDataDictionaryEnum.CHARGE_SYS_FAIL_TIME.getType(),
+                RunVipDataDictionaryEnum.CHARGE_SYS_FAIL_TIME.getCode()
+        ).getValue();
+        String key = AppContants.CHARGE_AMOUNT_PROFIX + amountReal; // 添加前缀避免键冲突
+        return redisUtils.setNotExist(key, memberId, 60L * Integer.parseInt(failMinutes));
+    }
+
 }

--
Gitblit v1.9.1