KKSU
2025-02-13 580fe843a333628795d619c5744a8353c44eb8ed
feat(mall): 实现充值金额处理和唯一标识生成

- 新增金额处理逻辑,生成包含随机增量的唯一金额
- 使用 Redis 存储唯一金额,确保并发安全性
- 修改充值相关代码,支持新生成的唯一金额
- 优化日志记录和异常处理
5 files modified
155 ■■■■■ changed files
src/main/java/cc/mrbird/febs/common/utils/AppContants.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/mall/chain/ercCoin/BscUsdtContractEvent.java 21 ●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/mall/chain/trcCoin/quartz/ChainTrcListenerJob.java 19 ●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/mall/entity/MallCharge.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/mall/service/impl/RunVipServiceImpl.java 111 ●●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/common/utils/AppContants.java
@@ -21,6 +21,8 @@
    public static final String PC_LOGIN_PREFIX = "pc_";
    public static final String CHARGE_AMOUNT_PROFIX = "uniqueAmount:";
    public static final String FP_TOKEN_HEADER_TYPE = "WECHATPAY2-SHA256-RSA2048 ";
    public static final String REDIS_KEY_BLOCK_ETH_NEWEST_NUM = "BLOCK_ETH_NEWEST_NUM";
src/main/java/cc/mrbird/febs/mall/chain/ercCoin/BscUsdtContractEvent.java
@@ -2,6 +2,8 @@
import cc.mrbird.febs.common.enumerates.RunVipDataDictionaryEnum;
import cc.mrbird.febs.common.enumerates.YesOrNoEnum;
import cc.mrbird.febs.common.utils.AppContants;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.mall.entity.MallCharge;
import cc.mrbird.febs.mall.mapper.DataDictionaryCustomMapper;
import cc.mrbird.febs.mall.mapper.MallChargeMapper;
@@ -29,6 +31,8 @@
    private MallChargeMapper mallChargeMapper;
    @Resource
    private AgentProducer agentProducer;
    @Resource
    private RedisUtils redisUtils;
    @Override
    public void sdmUSDT(EthUsdtContract.TransferEventResponse e) {
@@ -70,11 +74,22 @@
            BigInteger tokens = e.tokens;
            BigDecimal amount = new BigDecimal(tokens.toString())
                    .divide(BigDecimal.TEN.pow(decimals), decimals, RoundingMode.HALF_DOWN)
                    .setScale(2,BigDecimal.ROUND_DOWN);
                    .setScale(2,RoundingMode.DOWN);
            String chargeRedis = redisUtils.getString(AppContants.CHARGE_AMOUNT_PROFIX + amount);
            if (StrUtil.isBlank(chargeRedis)) {
                log.info("Redis未扫描到充值金额:{}",transactionHash);
                return;
            }else{
                redisUtils.del(AppContants.CHARGE_AMOUNT_PROFIX + amount);
                log.info("Redis扫描到充值记录:{},{}",transactionHash,chargeRedis);
            }
            LambdaQueryWrapper<MallCharge> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(MallCharge::getAddress, fromAddress);
            queryWrapper.eq(MallCharge::getSysAddress, sysAddress);
            queryWrapper.eq(MallCharge::getAmount, amount);
            queryWrapper.eq(MallCharge::getAmountReal, amount);
            queryWrapper.eq(MallCharge::getMemberId, Long.parseLong(chargeRedis));
            queryWrapper.eq(MallCharge::getType, type);
            queryWrapper.eq(MallCharge::getState, YesOrNoEnum.ING.getValue());
            List<MallCharge> mallCharges = mallChargeMapper.selectList(queryWrapper);
src/main/java/cc/mrbird/febs/mall/chain/trcCoin/quartz/ChainTrcListenerJob.java
@@ -2,6 +2,8 @@
import cc.mrbird.febs.common.enumerates.RunVipDataDictionaryEnum;
import cc.mrbird.febs.common.enumerates.YesOrNoEnum;
import cc.mrbird.febs.common.utils.AppContants;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.mall.chain.trcCoin.OkHttpUtil2;
import cc.mrbird.febs.mall.chain.trcCoin.OklinkDataPageDetailModel;
import cc.mrbird.febs.mall.chain.trcCoin.OklinkModel;
@@ -20,6 +22,7 @@
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
@@ -37,6 +40,8 @@
    private MallChargeMapper mallChargeMapper;
    @Resource
    private AgentProducer agentProducer;
    @Resource
    private RedisUtils redisUtils;
    /**
     * 五分钟  毫秒
@@ -125,11 +130,19 @@
                    continue;
                }
                BigDecimal amount = new BigDecimal(tokenTransfer.getAmount());
                BigDecimal amount = new BigDecimal(tokenTransfer.getAmount()).setScale(2, RoundingMode.DOWN);
                String chargeRedis = redisUtils.getString(AppContants.CHARGE_AMOUNT_PROFIX + amount);
                if (StrUtil.isBlank(chargeRedis)) {
                    log.info("Redis未扫描到充值金额:{}",transactionId);
                    return;
                }else{
                    redisUtils.del(AppContants.CHARGE_AMOUNT_PROFIX + amount);
                    log.info("Redis扫描到充值记录:{},{}",transactionId,chargeRedis);
                }
                LambdaQueryWrapper<MallCharge> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MallCharge::getAddress, fromAddress);
                queryWrapper.eq(MallCharge::getSysAddress, sysAddress);
                queryWrapper.eq(MallCharge::getAmount, amount);
                queryWrapper.eq(MallCharge::getAmountReal, amount);
                queryWrapper.eq(MallCharge::getMemberId, Long.parseLong(chargeRedis));
                queryWrapper.eq(MallCharge::getType, type);
                queryWrapper.eq(MallCharge::getState, YesOrNoEnum.ING.getValue());
                List<MallCharge> mallCharges = mallChargeMapper.selectList(queryWrapper);
src/main/java/cc/mrbird/febs/mall/entity/MallCharge.java
@@ -25,6 +25,8 @@
    private String vipName;
    private Integer vipCnt;
    private BigDecimal amountReal;
    @TableField(exist = false)
    private String account;
src/main/java/cc/mrbird/febs/mall/service/impl/RunVipServiceImpl.java
@@ -1,10 +1,15 @@
package cc.mrbird.febs.mall.service.impl;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.enumerates.*;
import cc.mrbird.febs.common.enumerates.FlowTypeEnum;
import cc.mrbird.febs.common.enumerates.RunVipDataDictionaryEnum;
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,9 +35,12 @@
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;
@@ -51,6 +59,9 @@
    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();
@@ -144,6 +155,10 @@
        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(runVipNext.getVipCode());
@@ -154,7 +169,7 @@
        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());
@@ -261,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);
@@ -631,6 +646,10 @@
        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());
@@ -663,7 +682,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());
@@ -762,11 +781,15 @@
        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.getAmount());
        apiGoChargeVo.setAmount(mallCharge.getAmountReal());
        apiGoChargeVo.setSysAddress(mallCharge.getSysAddress());
        apiGoChargeVo.setSysAddressType(mallCharge.getType());
@@ -797,9 +820,85 @@
        }
        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);
    }
    /**
     * 处理金额并生成唯一编号
     * 此方法用于根据给定的金额和会员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));
    }
}