KKSU
2025-02-13 580fe843a333628795d619c5744a8353c44eb8ed
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.*;
@@ -37,6 +39,8 @@
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;
@@ -55,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();
@@ -105,10 +112,12 @@
                        .orderByDesc(RunVipGrow::getId)
        ).stream().findFirst().orElse(null);
        if(runVipGrow != null){
            BigDecimal amountNow = runVipGrow.getAmountNow();
            reduceAmount = reduceAmount.add(amountNow);
            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);
@@ -146,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());
@@ -156,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());
@@ -202,10 +215,12 @@
                        .orderByDesc(RunVipGrow::getId)
        ).stream().findFirst().orElse(null);
        if(runVipGrow != null){
            BigDecimal amountNow = runVipGrow.getAmountNow();
            reduceAmount = reduceAmount.add(amountNow);
            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){
@@ -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);
@@ -493,7 +508,7 @@
        if(runVipNext == null){
            throw new FebsException("无法升级会员权益");
        }
        if(runVip.getOrderNumber() < runVipNext.getOrderNumber()){
        if(runVip.getOrderNumber() >= runVipNext.getOrderNumber()){
            throw new FebsException("用户无法升级");
        }
@@ -535,40 +550,32 @@
            BigDecimal subtract = amountAll.subtract(amountNow);
            if(subtract.compareTo(amount) <= 0){
                runVipGrow.setAmountNow(amountAll);
                mallCharge.setVipCode(runVipGrow.getLevelNext());
            }else{
                runVipGrow.setAmountNow(amountNow.add(amount).setScale(2, RoundingMode.DOWN));
                mallCharge.setVipCode(memberLevel);
            }
            runVipGrow.setAmount(amount);
            runVipGrowMapper.updateById(runVipGrow);
        }else{
            runVipGrow = new RunVipGrow();
            runVipGrow.setMemberId(memberId);
            runVipGrow.setLevelNow(memberLevel);
            runVipGrow.setLevelNext(runVipNext.getVipCode());
            runVipGrow.setAmountAll(runVipNext.getPresentPrice());
            runVipGrow.setAmount(amount);
            runVipGrow.setAmountNow(amount);
            runVipGrowMapper.insert(runVipGrow);
            if(runVipGrow.getAmountAll().compareTo(amount) <= 0){
                mallCharge.setVipCode(runVipGrow.getLevelNext());
            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().data("操作成功");
        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){
@@ -595,7 +602,7 @@
        if(runVipNext == null){
            throw new FebsException("无法升级会员权益");
        }
        if(runVip.getOrderNumber() < runVipNext.getOrderNumber()){
        if(runVip.getOrderNumber() >= runVipNext.getOrderNumber()){
            throw new FebsException("用户无法升级");
        }
@@ -639,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());
@@ -661,13 +672,29 @@
                mallCharge.setVipCode(runVipGrow.getLevelNext());
            }
        }else{
            if(runVipNext.getPresentPrice().compareTo(amount) <= 0){
            BigDecimal add = runVip.getPresentPrice().add(amount);
            if(runVipNext.getPresentPrice().compareTo(add) <= 0){
                mallCharge.setVipCode(runVipNext.getVipCode());
            }
        }
        mallChargeMapper.insert(mallCharge);
        return new FebsResponse().success().data("操作成功");
        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
@@ -686,9 +713,192 @@
        ).stream().findFirst().orElse(null);
        if(runVipGrow != null){
            BigDecimal amountNow = runVipGrow.getAmountNow();
            reduceAmount = reduceAmount.add(amountNow);
            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));
    }
}