Helius
2022-06-17 f61c65658100a64f64cb14c64f27934702d1812e
src/main/java/cc/mrbird/febs/mall/service/impl/MemberProfitServiceImpl.java
@@ -45,7 +45,7 @@
    private final IMallMoneyFlowService moneyFlowService;
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    private final MallMoneyFlowMapper mallMoneyFlowMapper;
    private final IMallAchieveService mallAchieveService;
    private final MallAchieveRecordMapper mallAchieveRecordMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -58,10 +58,10 @@
        Date profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
        // 套餐业绩
        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
//        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
        // 普通商品业绩
        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
        BigDecimal totalIncome = tcIncome.add(normalIncome);
//        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
        BigDecimal totalIncome = mallAchieveRecordMapper.selectAchieveTotal("D", profitDate);
        if (totalIncome.compareTo(BigDecimal.ZERO) == 0) {
            return;
@@ -109,14 +109,15 @@
                    Long memberId = (Long) item.get("memberId");
                    String orderNo = (String) item.get("orderNo");
                    int reduce = walletService.reduce(preStaticProfit, memberId, "score");
                    if (reduce == 2) {
                        break;
                    }
                    walletService.add(preStaticProfit, memberId, "commission");
                    moneyFlowService.addMoneyFlow(memberId, preStaticProfit, MoneyFlowTypeEnum.STATIC_BONUS.getValue(), orderNo, FlowTypeEnum.COMMISSION.getValue());
                    moneyFlowService.addMoneyFlow(memberId, preStaticProfit.negate(), MoneyFlowTypeEnum.STATIC_BONUS.getValue(), orderNo, FlowTypeEnum.SCORE.getValue());
//                    int reduce = walletService.reduce(preStaticProfit, memberId, "score");
//                    if (reduce == 2) {
//                        break;
//                    }
//
//                    walletService.add(preStaticProfit, memberId, "commission");
//                    moneyFlowService.addMoneyFlow(memberId, preStaticProfit, MoneyFlowTypeEnum.STATIC_BONUS.getValue(), orderNo, FlowTypeEnum.COMMISSION.getValue());
//                    moneyFlowService.addMoneyFlow(memberId, preStaticProfit.negate(), MoneyFlowTypeEnum.STATIC_BONUS.getValue(), orderNo, FlowTypeEnum.SCORE.getValue());
                    changeScoreAndCommission(memberId, preStaticProfit, MoneyFlowTypeEnum.STATIC_BONUS.getValue(), orderNo);
                }
            }
        }
@@ -157,6 +158,9 @@
        List<MallOrderItem> items = mallOrderInfoMapper.getMallOrderItemByOrderId(orderId);
        for (MallOrderItem item : items) {
            item.setHasSettle(1);
            mallOrderItemMapper.updateById(item);
            // 减去成本后算收益
            BigDecimal amount = item.getPrice().subtract(item.getCostPrice()).multiply(BigDecimal.valueOf(item.getCnt()));
            if (amount.compareTo(BigDecimal.ZERO) < 1) {
@@ -176,15 +180,18 @@
                continue;
            }
            int reduce = walletService.reduce(dynamicProfit, parent.getId(), "score");
            if (reduce == 2) {
//            int reduce = walletService.reduce(dynamicProfit, parent.getId(), "score");
//            if (reduce == 2) {
//                continue;
//            }
//            walletService.add(dynamicProfit, parent.getId(), "commission");
//            moneyFlowService.addMoneyFlow(parent.getId(), dynamicProfit, MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue());
//            moneyFlowService.addMoneyFlow(parent.getId(), dynamicProfit.negate(), MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue());
            dynamicProfit = changeScoreAndCommission(parent.getId(), dynamicProfit, MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo());
            if (dynamicProfit.compareTo(BigDecimal.ZERO) < 1) {
                continue;
            }
            walletService.add(dynamicProfit, parent.getId(), "commission");
            moneyFlowService.addMoneyFlow(parent.getId(), dynamicProfit, MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue());
            moneyFlowService.addMoneyFlow(parent.getId(), dynamicProfit.negate(), MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue());
            // =======直推返利== end =====
            // =======隔代奖== start =====
@@ -205,10 +212,10 @@
                }
                // 直推数量
                Integer directCnt = mallMemberMapper.selectOwnCntByInviteId(member.getReferrerId());
                Integer directCnt = mallMemberMapper.selectOwnCntByInviteId(parentMember.getInviteId());
                List<DataDictionaryCustom> dataDices = dataDictionaryCustomMapper.selectDicByType(DataDictionaryEnum.INDIRECT_BONUS_SETTING.getType());
                directCnt = directCnt == null ? 0 :directCnt;
                directCnt = directCnt == null ? 0 : directCnt;
                // 隔代比例
                BigDecimal indrectDicProp = BigDecimal.ZERO;
@@ -220,26 +227,23 @@
                }
                // 隔代推荐奖 收益
                direct = direct.divide(indrectDicProp, 2, RoundingMode.HALF_UP);
                direct = direct.multiply(indrectDicProp.divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP));
                // direct 收益小于1,则跳出
                if (direct.compareTo(BigDecimal.ONE) < 1) {
                    continue;
                    break;
                }
                int reduceResult =  walletService.reduce(direct, parentMember.getId(), "score");
                if (reduceResult == 2) {
                    continue;
                }
                walletService.add(direct, parentMember.getId(), "commission");
                moneyFlowService.addMoneyFlow(parentMember.getId(), direct, MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue());
                moneyFlowService.addMoneyFlow(parentMember.getId(), direct.negate(), MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue());
//                int reduceResult = walletService.reduce(direct, parentMember.getId(), "score");
//                if (reduceResult == 2) {
//                    continue;
//                }
//
//                walletService.add(direct, parentMember.getId(), "commission");
//                moneyFlowService.addMoneyFlow(parentMember.getId(), direct, MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue());
//                moneyFlowService.addMoneyFlow(parentMember.getId(), direct.negate(), MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue());
                changeScoreAndCommission(parentMember.getId(), direct, MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo());
            }
            // =======隔代奖== end =====
            item.setHasSettle(1);
            mallOrderItemMapper.updateById(item);
        }
    }
@@ -251,43 +255,61 @@
            profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
        }
        // 套餐业绩
        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
//        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
        // 普通商品业绩
        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
        BigDecimal totalIncome = tcIncome.add(normalIncome);
//        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
        BigDecimal totalIncome = mallAchieveRecordMapper.selectAchieveTotal("D", profitDate);
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.AGENT_BONUS.getType(), DataDictionaryEnum.AGENT_BONUS.getCode());
        BigDecimal profit = totalIncome.divide(new BigDecimal(dic.getValue()), 2, RoundingMode.HALF_UP);
        BigDecimal preProfit = profit.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
        BigDecimal profit = totalIncome.multiply(new BigDecimal(dic.getValue()).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP));
        List<MallMember> agentMembers = mallMemberMapper.selectByIdAndNoLevel(null, AgentLevelEnum.FIRST_LEVEL.name());
        if (CollUtil.isEmpty(agentMembers)) {
            return;
        }
        for (MallMember agentMember : agentMembers) {
            DataDictionaryCustom agentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(AppContants.AGENT_LEVEL_REQUIRE, agentMember.getLevel());
            if (agentDic == null) {
                continue;
            }
        List<DataDictionaryCustom> dics = dataDictionaryCustomMapper.selectDicByType(AppContants.AGENT_LEVEL_REQUIRE);
        for (DataDictionaryCustom agentDic : dics) {
            JSONObject jsonObject = JSONObject.parseObject(agentDic.getValue());
            BigDecimal profitProp = jsonObject.getBigDecimal("profitProp");
            BigDecimal income = preProfit.multiply(profitProp);
            BigDecimal profitProp = jsonObject.getBigDecimal("profitProp").divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
            int reduce = walletService.reduce(income, agentMember.getId(), "score");
            if (reduce == 2) {
            List<MallMember> agentMemberList = mallMemberMapper.selectMemberWithLevel(agentDic.getCode());
            if (CollUtil.isEmpty(agentMemberList)) {
                continue;
            }
            walletService.add(income, agentMember.getId(), "commission");
            BigDecimal income = profit.multiply(profitProp).divide(BigDecimal.valueOf(agentMemberList.size()), 2, RoundingMode.HALF_UP);
            moneyFlowService.addMoneyFlow(agentMember.getId(), income, MoneyFlowTypeEnum.AGENT_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
            moneyFlowService.addMoneyFlow(agentMember.getId(), income.negate(), MoneyFlowTypeEnum.AGENT_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue());
            for (MallMember agentMember : agentMemberList) {
//                int reduce = walletService.reduce(income, agentMember.getId(), "score");
//                if (reduce == 2) {
//                    continue;
//                }
//
//                walletService.add(income, agentMember.getId(), "commission");
//
//                moneyFlowService.addMoneyFlow(agentMember.getId(), income, MoneyFlowTypeEnum.AGENT_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
//                moneyFlowService.addMoneyFlow(agentMember.getId(), income.negate(), MoneyFlowTypeEnum.AGENT_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue());
                changeScoreAndCommission(agentMember.getId(), income, MoneyFlowTypeEnum.AGENT_BONUS.getValue(), null);
            }
        }
        log.info("#####==代理分红==end==#####");
    }
    private BigDecimal changeScoreAndCommission(Long memberId, BigDecimal income, Integer type, String orderNo) {
        Map<String, BigDecimal> map = new HashMap<>();
        map.put("amount", income);
        int reduce = walletService.reduce(income, memberId, "score", map);
        if (reduce == 2) {
            return BigDecimal.ZERO;
        }
        log.info("#####==代理分红==end==#####");
        income = map.get("amount");
        walletService.add(income, memberId, "commission");
        moneyFlowService.addMoneyFlow(memberId, income, type, orderNo, FlowTypeEnum.COMMISSION.getValue());
        moneyFlowService.addMoneyFlow(memberId, income.negate(), type, orderNo, FlowTypeEnum.SCORE.getValue());
        return income;
    }
    @Override
@@ -306,32 +328,32 @@
        }
        // 套餐业绩
        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M", 2);
//        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M", 2);
        // 普通商品业绩
        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M", 1);
        BigDecimal totalIncome = tcIncome.add(normalIncome);
//        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M", 1);
        BigDecimal totalIncome = mallAchieveRecordMapper.selectAchieveTotal("D", profitDate);
        List<DataDictionaryCustom> rankAward = dataDictionaryCustomMapper.selectDicByType("RANK_AWARD");
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.RANK_BONUS.getType(), DataDictionaryEnum.RANK_BONUS.getCode());
        BigDecimal rankBonusTotal = totalIncome.multiply(new BigDecimal(dic.getValue()).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP));
        BigDecimal preBonus = rankBonusTotal.divide(BigDecimal.valueOf(100),2 , RoundingMode.HALF_UP);
        BigDecimal preBonus = rankBonusTotal.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
        int i = 0;
        for (MallMember member : rankList) {
            DataDictionaryCustom rank = rankAward.get(i);
            i++;
            BigDecimal bonus = preBonus.multiply(new BigDecimal(rank.getValue()));
            int reduce = walletService.reduce(bonus, member.getId(), "score");
            if (reduce == 2) {
                continue;
            }
//            int reduce = walletService.reduce(bonus, member.getId(), "score");
//            if (reduce == 2) {
//                continue;
//            }
//            walletService.add(bonus, member.getId(), "commission");
//            moneyFlowService.addMoneyFlow(member.getId(), bonus, MoneyFlowTypeEnum.RANK_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
//            moneyFlowService.addMoneyFlow(member.getId(), bonus.negate(), MoneyFlowTypeEnum.RANK_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue());
            walletService.add(bonus, member.getId(), "commission");
            moneyFlowService.addMoneyFlow(member.getId(), bonus, MoneyFlowTypeEnum.RANK_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
            moneyFlowService.addMoneyFlow(member.getId(), bonus.negate(), MoneyFlowTypeEnum.RANK_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue());
            changeScoreAndCommission(member.getId(), bonus, MoneyFlowTypeEnum.RANK_BONUS.getValue(), null);
        }
    }
@@ -367,22 +389,22 @@
            if (reduce == 2) {
                continue;
            }
            moneyFlowService.addMoneyFlow(mallMember.getId(), reduceProfit.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
            for (MallMember child : children) {
                BigDecimal preProfit = income.multiply(prop).divide(BigDecimal.valueOf(children.size()), 2, RoundingMode.HALF_DOWN);
                if (preProfit.compareTo(BigDecimal.ZERO) < 1) {
                    continue;
                }
                int reduce1 = walletService.reduce(preProfit, child.getId(), "score");
                if (reduce1 == 2) {
                    continue;
                }
                walletService.add(preProfit, child.getId(), "commission");
                moneyFlowService.addMoneyFlow(child.getId(), preProfit, MoneyFlowTypeEnum.THANKFUL.getValue(), null, mallMember.getId(), FlowTypeEnum.COMMISSION.getValue());
                moneyFlowService.addMoneyFlow(child.getId(), preProfit.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, mallMember.getId(), FlowTypeEnum.SCORE.getValue());
//                int reduce1 = walletService.reduce(preProfit, child.getId(), "score");
//                if (reduce1 == 2) {
//                    continue;
//                }
//                walletService.add(preProfit, child.getId(), "commission");
//                moneyFlowService.addMoneyFlow(child.getId(), preProfit, MoneyFlowTypeEnum.THANKFUL.getValue(), null, mallMember.getId(), FlowTypeEnum.COMMISSION.getValue());
//                moneyFlowService.addMoneyFlow(child.getId(), preProfit.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, mallMember.getId(), FlowTypeEnum.SCORE.getValue());
                changeScoreAndCommission(child.getId(), preProfit, MoneyFlowTypeEnum.THANKFUL.getValue(), null);
            }
        }
        log.info("######==感恩奖==end==####");
@@ -395,10 +417,10 @@
            profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
        }
        // 套餐业绩
        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
//        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
        // 普通商品业绩
        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
        BigDecimal totalIncome = tcIncome.add(normalIncome);
//        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
        BigDecimal totalIncome = mallAchieveRecordMapper.selectAchieveTotal("D", profitDate);
        // 董事
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.DIRECTOR_BONUS.getType(), DataDictionaryEnum.DIRECTOR_BONUS.getCode());
@@ -412,14 +434,14 @@
                    continue;
                }
                int reduce = walletService.reduce(pre, director.getId(), "score");
                if (reduce == 2) {
                    continue;
                }
                walletService.add(pre, director.getId(), "commission");
                moneyFlowService.addMoneyFlow(director.getId(), pre, MoneyFlowTypeEnum.DIRECTOR_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
                moneyFlowService.addMoneyFlow(director.getId(), pre.negate(), MoneyFlowTypeEnum.DIRECTOR_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue());
//                int reduce = walletService.reduce(pre, director.getId(), "score");
//                if (reduce == 2) {
//                    continue;
//                }
//                walletService.add(pre, director.getId(), "commission");
//                moneyFlowService.addMoneyFlow(director.getId(), pre, MoneyFlowTypeEnum.DIRECTOR_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
//                moneyFlowService.addMoneyFlow(director.getId(), pre.negate(), MoneyFlowTypeEnum.DIRECTOR_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue());
                changeScoreAndCommission(director.getId(), pre, MoneyFlowTypeEnum.DIRECTOR_BONUS.getValue(), null);
            }
        }
@@ -435,17 +457,16 @@
                    continue;
                }
                int reduce = walletService.reduce(pre, storeMaster.getId(), "score");
                if (reduce == 2) {
                    continue;
                }
                walletService.add(pre, storeMaster.getId(), "commission");
                moneyFlowService.addMoneyFlow(storeMaster.getId(), pre, MoneyFlowTypeEnum.COMMUNITY_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
                moneyFlowService.addMoneyFlow(storeMaster.getId(), pre.negate(), MoneyFlowTypeEnum.COMMUNITY_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue());
//                int reduce = walletService.reduce(pre, storeMaster.getId(), "score");
//                if (reduce == 2) {
//                    continue;
//                }
//                walletService.add(pre, storeMaster.getId(), "commission");
//                moneyFlowService.addMoneyFlow(storeMaster.getId(), pre, MoneyFlowTypeEnum.COMMUNITY_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
//                moneyFlowService.addMoneyFlow(storeMaster.getId(), pre.negate(), MoneyFlowTypeEnum.COMMUNITY_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue());
                changeScoreAndCommission(storeMaster.getId(), pre, MoneyFlowTypeEnum.COMMUNITY_BONUS.getValue(), null);
            }
        }
        log.info("#####==店补/董事==end==######");
    }
}