xiaoyong931011
2023-04-06 c5b60fbff8008c95cef0236ea520041a43151d19
src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallOrderInfoServiceImpl.java
@@ -78,6 +78,7 @@
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    private final MallAchieveRecordMapper mallAchieveRecordMapper;
    private final MallMemberWalletMapper mallMemberWalletMapper;
    private final MallMqRecordMapper mallMqRecordMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -206,25 +207,19 @@
        /**
         * 使用积分折扣现金
         */
        DataDictionaryCustom scorePercentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.SCORE_PERCENT.getType(),
                DataDictionaryEnum.SCORE_PERCENT.getCode()
        );
        DataDictionaryCustom scoreChangeDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.SCORE_CHANGE.getType(),
                DataDictionaryEnum.SCORE_CHANGE.getCode()
        );
        BigDecimal score = ObjectUtil.isEmpty(addOrderDto.getScore()) ? BigDecimal.ZERO : addOrderDto.getScore();
        orderInfo.setScoreCnt(score);
        if(ObjectUtil.isNotEmpty(scorePercentDic)){
            BigDecimal scoreChange = BigDecimal.ONE;
            if(ObjectUtil.isNotEmpty(scoreChangeDic)){
                scoreChange = new BigDecimal(scoreChangeDic.getValue()).abs().setScale(2,BigDecimal.ROUND_DOWN);
            }
            //折扣现金
            BigDecimal scorePercentAmount = score.divide(scoreChange,BigDecimal.ROUND_DOWN).setScale(2,BigDecimal.ROUND_DOWN);
            orderInfo.setScoreAmount(scorePercentAmount);
        BigDecimal scoreChange = BigDecimal.ONE;
        if(ObjectUtil.isNotEmpty(scoreChangeDic)){
            scoreChange = new BigDecimal(scoreChangeDic.getValue()).abs().setScale(2,BigDecimal.ROUND_DOWN);
        }
        //折扣现金
        BigDecimal scorePercentAmount = score.divide(scoreChange,BigDecimal.ROUND_DOWN).setScale(2,BigDecimal.ROUND_DOWN);
        orderInfo.setScoreAmount(scorePercentAmount);
        orderInfo.setAmount(total);
        if(2 == deliverType){
            orderInfo.setDeliverType(2);
@@ -347,8 +342,18 @@
                orderInfo.setStatus(OrderStatusEnum.WAIT_SHIPPING.getValue());
                orderInfo.setPayTime(new Date());
                orderInfo.setPayResult("1");
                this.baseMapper.updateById(orderInfo);
                changeWallet(orderInfo.getId());
                /**
                 * 插入一条待处理记录
                 * mq处理之后,更新状态
                 */
                MallMqRecord mallMqRecord = new MallMqRecord();
                mallMqRecord.setOrderId(orderInfo.getId());
                mallMqRecord.setState(2);
                mallMqRecord.setRetryTimes(2);
                mallMqRecordMapper.insert(mallMqRecord);
                //发送补贴消息
                agentProducer.sendPerkMoneyMsg(orderInfo.getId());
                //发送代理自动升级消息
@@ -444,6 +449,7 @@
         * 所有合伙人补贴至消费礼包的三倍额度需要复购一次。
         */
        BigDecimal scoreSum = BigDecimal.ZERO;
        BigDecimal achieveSum = BigDecimal.ZERO;
        List<MallOrderItem> orderItems = this.baseMapper.getMallOrderItemByOrderId(orderInfo.getId());
        if (CollUtil.isNotEmpty(orderItems)) {
            for (MallOrderItem orderItem : orderItems) {
@@ -454,11 +460,41 @@
                 */
                if (mallGoods.getIsNormal() == 2) {
                    hasTc = true;
                    BigDecimal star = mallGoods.getStar()==null ? BigDecimal.ZERO : mallGoods.getStar();
                    BigDecimal star = (mallGoods.getStar()==null ? BigDecimal.ZERO : mallGoods.getStar())
                            .multiply(new BigDecimal(orderItem.getCnt()));
                    starSum = starSum.add(star);
//                    scoreSum = scoreSum.add(
//                            new BigDecimal(mallGoods.getPresentPrice())
//                                    .multiply(new BigDecimal(3))
//                                    .multiply(new BigDecimal(orderItem.getCnt()))
//                                    .abs()
//                                    .setScale(2,BigDecimal.ROUND_DOWN));
                    String goodsName = mallGoods.getGoodsName();
                    /**
                     * 1-2档投资级别,2.5倍
                     * 3-4档投资级别,2.8倍
                     * 5-6档投资级别,3倍
                     * 目前紧急修改成按照商品名称
                     */
                    BigDecimal basePercent = new BigDecimal("3");
                    if(goodsName.equals("礼包一") || goodsName.equals("礼包二")){
                        basePercent = new BigDecimal("2.5");
                    }
                    if(goodsName.equals("礼包三") || goodsName.equals("礼包四")){
                        basePercent = new BigDecimal("2.8");
                    }
                    if(goodsName.equals("礼包五") || goodsName.equals("礼包六")){
                        basePercent = new BigDecimal("3");
                    }
                    scoreSum = scoreSum.add(
                            new BigDecimal(mallGoods.getPresentPrice())
                                    .multiply(new BigDecimal(3))
                                    .multiply(new BigDecimal(orderItem.getCnt()))
                                    .multiply(basePercent)
                                    .abs()
                                    .setScale(2,BigDecimal.ROUND_DOWN));
                    achieveSum = achieveSum.add(
                            new BigDecimal(mallGoods.getPresentPrice())
                                    .multiply(new BigDecimal(orderItem.getCnt()))
                                    .abs()
                                    .setScale(2,BigDecimal.ROUND_DOWN));
                }
@@ -485,7 +521,8 @@
                                    starSum,
                                    MoneyFlowTypeEnum.STAR.getValue(),
                                    orderInfo.getOrderNo(),
                                    FlowTypeEnum.STAR.getValue());
                                    FlowTypeEnum.STAR.getValue(),
                                    2);
                        }
                        if(starGet.compareTo(star) > 0){
                            BigDecimal subtract = starGet.subtract(star);
@@ -498,7 +535,8 @@
                                    starSum,
                                    MoneyFlowTypeEnum.STAR.getValue(),
                                    orderInfo.getOrderNo(),
                                    FlowTypeEnum.STAR.getValue());
                                    FlowTypeEnum.STAR.getValue(),
                                    2);
                        }
                    }else{
                        mallMemberWalletMapper.addStarByMemberId(starSum.intValue(), memberId);
@@ -507,7 +545,8 @@
                                starSum,
                                MoneyFlowTypeEnum.STAR.getValue(),
                                orderInfo.getOrderNo(),
                                FlowTypeEnum.STAR.getValue());
                                FlowTypeEnum.STAR.getValue(),
                                2);
                    }
                }
            }
@@ -515,13 +554,29 @@
             * 增加补贴额度
             */
            if (scoreSum.compareTo(BigDecimal.ZERO) > 0) {
//                MallMember mallMember = memberMapper.selectById(memberId);
//                if(MemberLevelEnum.SECOND_LEVEL.getLevelCode(mallMember.getLevel()) >= 5){
//                    scoreSum = scoreSum.multiply(new BigDecimal(3));
//                }
//                if(MemberLevelEnum.SECOND_LEVEL.getLevelCode(mallMember.getLevel()) >= 3
//                &&MemberLevelEnum.SECOND_LEVEL.getLevelCode(mallMember.getLevel()) < 5){
//                    scoreSum = scoreSum.multiply(new BigDecimal(2.8));
//                }
//                if(MemberLevelEnum.SECOND_LEVEL.getLevelCode(mallMember.getLevel()) < 3){
//                    scoreSum = scoreSum.multiply(new BigDecimal(2.5));
//                }
                memberWalletService.add(scoreSum, memberId, "totalScore");
                mallMoneyFlowService.addMoneyFlow(
                        memberId,
                        scoreSum,
                        MoneyFlowTypeEnum.TOTAL_SCORE.getValue(),
                        orderInfo.getOrderNo(),
                        FlowTypeEnum.TOTAL_SCORE.getValue());
                        "",
                        scoreSum.toString(),
                        memberId,
                        1,
                        FlowTypeEnum.TOTAL_SCORE.getValue(),
                        2);
            }
        }
@@ -560,13 +615,15 @@
        /**
         * 产生一条业绩记录
         */
        MallAchieveRecord mallAchieveRecord = new MallAchieveRecord();
        mallAchieveRecord.setMemberId(memberId);
        mallAchieveRecord.setAchieveTime(new Date());
        mallAchieveRecord.setAmount(orderInfo.getAmount().subtract(orderInfo.getScoreAmount()));
        mallAchieveRecord.setOrderId(orderId);
        mallAchieveRecord.setPayTime(orderInfo.getPayTime());
        mallAchieveRecordMapper.insert(mallAchieveRecord);
        if(achieveSum.compareTo(BigDecimal.ZERO) > 0){
            MallAchieveRecord mallAchieveRecord = new MallAchieveRecord();
            mallAchieveRecord.setMemberId(memberId);
            mallAchieveRecord.setAchieveTime(new Date());
            mallAchieveRecord.setAmount(achieveSum.subtract(orderInfo.getScoreAmount()));
            mallAchieveRecord.setOrderId(orderId);
            mallAchieveRecord.setPayTime(orderInfo.getPayTime());
            mallAchieveRecordMapper.insert(mallAchieveRecord);
        }
    }
    public static void main(String[] args) {
@@ -851,21 +908,39 @@
        }
        boolean hasTc = false;
        //总的折扣积分数量
        BigDecimal total = BigDecimal.ZERO;
        for (AddOrderItemDto item : addOrderDto.getItems()) {
                MallGoodsSku sku = mallGoodsSkuMapper.selectSkuInfoById(item.getSkuId());
                MallGoods mallGoods = mallGoodsMapper.selectById(sku.getGoodsId());
                /**
                 * 贡献点判断
            MallGoodsSku sku = mallGoodsSkuMapper.selectSkuInfoById(item.getSkuId());
            if(ObjectUtil.isEmpty(sku)){
                throw new FebsException("商品已更新,请重新下单");
            }
            MallGoods mallGoods = mallGoodsMapper.selectById(sku.getGoodsId());
            if(ObjectUtil.isEmpty(mallGoods)){
                throw new FebsException("商品已更新,请重新下单");
            }
            /**
             * 贡献点判断
             */
            if (mallGoods.getIsNormal() == 2) {
                hasTc = true;
            }
                /*
                获取单个商品的折扣积分数量
                    = 现价 * 数量 * 折扣现金比例
                 */
                if (mallGoods.getIsNormal() == 2) {
                    hasTc = true;
                }
                BigDecimal amount = sku.getPresentPrice().multiply(BigDecimal.valueOf(item.getCnt()));
                total = total.add(amount);
            BigDecimal scorePercent = new BigDecimal(mallGoods.getScorePercent()).multiply(new BigDecimal(0.01));
            BigDecimal amount = sku.getPresentPrice().multiply(BigDecimal.valueOf(item.getCnt())).multiply(scorePercent);
            total = total.add(amount);
        }
        BigDecimal score = ObjectUtil.isEmpty(addOrderDto.getScore()) ? BigDecimal.ZERO : addOrderDto.getScore();
        MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
        BigDecimal prizeScore = mallMemberWallet.getPrizeScore();
        if(prizeScore.compareTo(score) < 0){
            throw new FebsException("积分不足");
        }
        if(hasTc){
            //系统设置的个人贡献点最大值
            DataDictionaryCustom starGetDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
@@ -874,43 +949,29 @@
            if(ObjectUtil.isNotEmpty(starGetDic)){
                String starGetStr = StrUtil.isEmpty(starGetDic.getValue()) ? "0" : starGetDic.getValue();
                BigDecimal starGet = new BigDecimal(starGetStr);
                MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
                BigDecimal star = new BigDecimal(mallMemberWallet.getStar());
                if(starGet.compareTo(BigDecimal.ZERO) != 0 && starGet.compareTo(star) <= 0){
                    throw new FebsException("无法购买套餐");
                }
                BigDecimal prizeScore = mallMemberWallet.getPrizeScore();
                if(prizeScore.compareTo(score) < 0){
                    throw new FebsException("积分不足");
                }
            }
        }
        /**
         * 使用积分折扣现金
         */
        DataDictionaryCustom scorePercentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.SCORE_PERCENT.getType(),
                DataDictionaryEnum.SCORE_PERCENT.getCode()
        );
        DataDictionaryCustom scoreChangeDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.SCORE_CHANGE.getType(),
                DataDictionaryEnum.SCORE_CHANGE.getCode()
        );
        if(ObjectUtil.isNotEmpty(scorePercentDic)){
            //商品的现金折扣比例
            BigDecimal scorePercent = new BigDecimal(scorePercentDic.getValue()).multiply(BigDecimal.valueOf(0.01));
            //最大折扣金额对应需要的总积分数目
            BigDecimal scorePercentMaxAmount = total.multiply(scorePercent);
            BigDecimal scoreChange = BigDecimal.ONE;
            if(ObjectUtil.isNotEmpty(scoreChangeDic)){
                scoreChange = new BigDecimal(scoreChangeDic.getValue()).abs().setScale(2,BigDecimal.ROUND_DOWN);
            }
            scorePercentMaxAmount = scorePercentMaxAmount.multiply(scoreChange).setScale(2,BigDecimal.ROUND_DOWN);
            if(scorePercentMaxAmount.compareTo(score) < 0){
                throw new FebsException("最多可使用"+scorePercentMaxAmount+"积分");
            }
        //最大折扣金额对应需要的总积分数目
        BigDecimal scorePercentMaxAmount = total;
        BigDecimal scoreChange = BigDecimal.ONE;
        if(ObjectUtil.isNotEmpty(scoreChangeDic)){
            scoreChange = new BigDecimal(scoreChangeDic.getValue()).abs().setScale(2,BigDecimal.ROUND_DOWN);
        }
        scorePercentMaxAmount = scorePercentMaxAmount.multiply(scoreChange).setScale(2,BigDecimal.ROUND_DOWN);
        if(scorePercentMaxAmount.compareTo(score) < 0){
            throw new FebsException("最多可使用"+scorePercentMaxAmount+"积分");
        }
        return new FebsResponse().success();
    }