KKSU
2025-01-03 86cc20a154707c8ebc3d26b090d43ec01d550e61
src/main/java/cc/mrbird/febs/mall/service/impl/RunVipServiceImpl.java
@@ -30,8 +30,10 @@
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;
@@ -52,6 +54,7 @@
    private final MallMemberMapper mallMemberMapper;
    private final IApiMallMemberWalletService walletService;
    private final IMallMoneyFlowService mallMoneyFlowService;
    private final RunVipGrowMapper runVipGrowMapper;
    @Override
    public List<ApiRunVipVo> vipInfo() {
        Long memberId = LoginUserUtil.getLoginUser().getId();
@@ -86,12 +89,26 @@
            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){
            BigDecimal amountNow = runVipGrow.getAmountNow();
            reduceAmount = reduceAmount.add(amountNow);
        }
        presentAmount = presentAmount.subtract(reduceAmount);
        Long addressId = apiGoChargeDto.getAddressId();
        MallMemberPayment mallMemberPayment = mallMemberPaymentMapper.selectById(addressId);
@@ -131,8 +148,8 @@
        mallCharge.setAmount(presentAmount);
        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);
@@ -156,6 +173,7 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse balanceBuy(ApiBalanceBuyDto apiBalanceBuyDto) {
        Long memberId = LoginUserUtil.getLoginUser().getId();
        MallMember mallMember = mallMemberMapper.selectById(memberId);
@@ -167,12 +185,28 @@
            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){
            BigDecimal amountNow = runVipGrow.getAmountNow();
            reduceAmount = reduceAmount.add(amountNow);
        }
        presentAmount = presentAmount.subtract(reduceAmount);
        MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
        if(mallMemberWallet.getCommission().compareTo(BigDecimal.ZERO) <= 0){
            throw new FebsException("余额不足");
@@ -189,7 +223,7 @@
                orderNo,
                FlowTypeEnum.COMMISSION.getValue(),
                RunVipMoneyFlowTypeEnum.COMMISSION_PAY.getValue(),
                presentAmount,
                presentAmount.negate(),
                StrUtil.format(RunVipMoneyFlowTypeEnum.COMMISSION_PAY.getDescription(),presentAmount),
                YesOrNoEnum.YES.getValue()
        );
@@ -201,8 +235,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);
@@ -384,4 +418,254 @@
        }
        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){
                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());
            }
        }
        mallChargeMapper.insert(mallCharge);
        agentProducer.sendBuyVipSuccessMsg(mallCharge.getId());
        return new FebsResponse().success().data("操作成功");
    }
    @Override
    public FebsResponse growCharge(ApiGrowChargeDto apiGrowChargeDto) {
        Long memberId = LoginUserUtil.getLoginUser().getId();
        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);
        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{
            if(runVipGrow.getAmountAll().compareTo(amount) <= 0){
                mallCharge.setVipCode(runVipGrow.getLevelNext());
            }
        }
        mallChargeMapper.insert(mallCharge);
        return new FebsResponse().success().data("操作成功");
    }
}