xiaoyong931011
2023-04-06 e6667c792be898c41cda7d850dda02a403ef79c2
src/main/java/cc/mrbird/febs/mall/service/impl/AgentServiceImpl.java
@@ -47,6 +47,7 @@
    private final SqlSessionTemplate sqlSessionTemplate;
    private final IApiMallMemberWalletService memberWalletService;
    private final MallMoneyFlowMapper mallMoneyFlowMapper;
    private final MallMqRecordMapper mallMqRecordMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -215,6 +216,8 @@
                //减少补贴额度
                mallMemberWalletMapper.reduceTotalScoreById(sharePerkAmount, mallMemberWallet.getId());
                reduceStar(mallMemberUp.getId(),sharePerkAmount);
                mallMemberWalletMapper.addBalanceById(sharePerkAmount, mallMemberWallet.getId());
                mallMoneyFlowService.addMoneyFlow(
@@ -222,7 +225,12 @@
                        sharePerkAmount,
                        MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(),
                        mallOrderInfo.getOrderNo(),
                        FlowTypeEnum.BALANCE.getValue());
                        "直推奖",
                        "补贴额度:"+totalScore,
                        memberId,
                        2,
                        FlowTypeEnum.BALANCE.getValue(),
                        1);
            }
        }
        /**
@@ -264,6 +272,11 @@
        long end = System.currentTimeMillis();
        log.info("============订单:{},时间:{}===========",mallOrderInfo.getOrderNo(),(end - start)/1000);
        MallMqRecord mallMqRecord = mallMqRecordMapper.selectByOrderId(orderId);
        if(ObjectUtil.isNotEmpty(mallMqRecord)){
            mallMqRecord.setState(1);
            mallMqRecordMapper.updateById(mallMqRecord);
        }
    }
    /**
@@ -340,9 +353,17 @@
        if(CollUtil.isNotEmpty(mallMemberStars)){
            List<Long> mallMemberStarIds = mallMemberStars.stream().map(MallMember::getId).collect(Collectors.toList());
            //排除掉本身
            mallMemberStarIds.remove(memberId);
            if(CollUtil.isEmpty(mallMemberStarIds)){
                return mallMemberStars;
            }
            //当前等级的总贡献点
            List<MallMemberWallet> mallMemberWallets = mallMemberWalletMapper.selectMemberWalletsByIds(mallMemberStarIds);
            Integer starSum = mallMemberWallets.stream().mapToInt(MallMemberWallet::getStar).sum();
            if(starSum <= 0){
                return mallMemberStars;
            }
            //星级补贴比例
            DataDictionaryCustom starPerkDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                    "AGENT_LEVEL_REQUIRE",
@@ -387,8 +408,11 @@
                        mallMoneyFlow.setOrderNo(orderNo);
                        mallMoneyFlow.setRtMemberId(memberId);
                        mallMoneyFlow.setStatus(2);
                        mallMoneyFlow.setRemark("补贴额度:"+mallMemberWallet.getTotalScore());
                        mallMoneyFlow.setFlowType(FlowTypeEnum.BALANCE.getValue());
                        mallMoneyFlows.add(mallMoneyFlow);
                        reduceStar(mallMemberWallet.getMemberId(),starPerkAmount);
                    }
                    count = count + 1;
                    if (count % 1000 == 0 || count == mallMemberWallets.size()) {
@@ -463,8 +487,12 @@
                mallMoneyFlow.setOrderNo(orderNo);
                mallMoneyFlow.setRtMemberId(memberId);
                mallMoneyFlow.setStatus(2);
                mallMoneyFlow.setRemark("补贴额度:"+mallMemberWallet.getTotalScore());
                mallMoneyFlow.setFlowType(FlowTypeEnum.BALANCE.getValue());
                mallMoneyFlows.add(mallMoneyFlow);
                reduceStar(mallMemberWallet.getMemberId(),amount);
            }
            count = count + 1;
            if (count % 1000 == 0 || count == mallMemberWallets.size()) {
@@ -501,5 +529,56 @@
        }
    }
    @Override
    public void reduceStar(Long memberId,BigDecimal totalScore) {
        /**
         *获取用户的全部未用完的补贴额度流水
         */
        MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
        List<MallMoneyFlow> mallMoneyFlows = mallMoneyFlowMapper.selectMoneyFlowByMemberIdAndIsRetrun(memberId,2);
        //如果补贴额度全部用完则更新用户的贡献点为零
        if(CollUtil.isEmpty(mallMoneyFlows)){
            mallMemberWalletMapper.reduceStarByMemberId(mallMemberWallet.getStar(), memberId);
            return;
        }
        for(MallMoneyFlow mallMoneyFlow : mallMoneyFlows){
            String remark = mallMoneyFlow.getRemark();
            BigDecimal remarkNum = new BigDecimal(remark);
            //如果补贴额度小于记录的剩余补贴额度
            if(totalScore.compareTo(remarkNum) < 0){
                remarkNum = remarkNum.subtract(totalScore).setScale(2,BigDecimal.ROUND_DOWN);
                mallMoneyFlowMapper.updateRemarkById(remarkNum.toString(),mallMoneyFlow.getId());
                break;
            }
            //如果补贴额度等于等于记录的剩余补贴额度
            if(totalScore.compareTo(remarkNum) == 0){
                remarkNum = remarkNum.subtract(totalScore).setScale(2,BigDecimal.ROUND_DOWN);
                mallMoneyFlowMapper.updateRemarkAndIsReturnById(remarkNum.toString(),mallMoneyFlow.getId());
                //减少用户的贡献点
                String orderNo = mallMoneyFlow.getOrderNo();
                MallMoneyFlow mallMoneyFlowStar = mallMoneyFlowMapper.selectByOrderNoAndMemberId(orderNo,memberId,2);
                BigDecimal amount = mallMoneyFlowStar.getAmount();
                mallMemberWalletMapper.reduceStarByMemberId(amount.intValue(), memberId);
                //更新记录的返利状态为已返利
                mallMoneyFlowMapper.updateRemarkAndIsReturnById("0",mallMoneyFlowStar.getId());
                break;
            }
            //如果补贴额度大于记录的剩余补贴额度
            if(totalScore.compareTo(remarkNum) > 0){
                mallMoneyFlowMapper.updateRemarkAndIsReturnById("0",mallMoneyFlow.getId());
                //减少用户的贡献点
                String orderNo = mallMoneyFlow.getOrderNo();
                MallMoneyFlow mallMoneyFlowStar = mallMoneyFlowMapper.selectByOrderNoAndMemberId(orderNo,memberId,2);
                BigDecimal amount = mallMoneyFlowStar.getAmount();
                mallMemberWalletMapper.reduceStarByMemberId(amount.intValue(), memberId);
                //更新记录的返利状态为已返利
                mallMoneyFlowMapper.updateRemarkAndIsReturnById("0",mallMoneyFlowStar.getId());
                totalScore = totalScore.subtract(remarkNum);
            }
        }
    }
}