fix
Helius
2022-06-16 02184fd86a8ef854d087d5c919151bbdf092efef
src/main/java/cc/mrbird/febs/mall/service/impl/MemberProfitServiceImpl.java
@@ -8,6 +8,7 @@
import cc.mrbird.febs.mall.entity.*;
import cc.mrbird.febs.mall.mapper.*;
import cc.mrbird.febs.mall.service.IApiMallMemberWalletService;
import cc.mrbird.febs.mall.service.IMallAchieveService;
import cc.mrbird.febs.mall.service.IMallMoneyFlowService;
import cc.mrbird.febs.mall.service.IMemberProfitService;
import cn.hutool.core.collection.CollUtil;
@@ -25,8 +26,7 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.*;
/**
 * @author wzy
@@ -45,6 +45,7 @@
    private final IMallMoneyFlowService moneyFlowService;
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    private final MallMoneyFlowMapper mallMoneyFlowMapper;
    private final IMallAchieveService mallAchieveService;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -56,14 +57,21 @@
        }
        Date profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
        BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D");
        // 套餐业绩
        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
        // 普通商品业绩
        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
        BigDecimal totalIncome = tcIncome.add(normalIncome);
        if (totalIncome.compareTo(BigDecimal.ZERO) == 0) {
            return;
        }
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.STATIC_BONUS.getType(), DataDictionaryEnum.STATIC_BONUS.getCode());
        BigDecimal perProfit = totalIncome.divide(new BigDecimal(dic.getValue()), 2, RoundingMode.HALF_UP).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
        BigDecimal perProfit = totalIncome.multiply(new BigDecimal(dic.getValue()).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP));
        Map<Long, List<Map<String, Object>>> map = new HashMap<>();
        Map<Long, BigDecimal> goodsProp = new HashMap<>();
        for (MallMember member : members) {
            List<MallGoods> goodsList = mallGoodsMapper.selectOrderGoodsList(member.getId(), profitDate);
@@ -72,18 +80,51 @@
            }
            for (MallGoods goods : goodsList) {
                BigDecimal goodsProfit = goods.getStaticProp().multiply(perProfit);
                int reduce = walletService.reduce(goodsProfit, member.getId(), "score");
                if (reduce == 2) {
                    break;
                if (goods.getIsNormal() == 1) {
                    continue;
                }
                walletService.add(goodsProfit, member.getId(), "commission");
                moneyFlowService.addMoneyFlow(member.getId(), goodsProfit, MoneyFlowTypeEnum.STATIC_BONUS.getValue(), goods.getOrderNo(), FlowTypeEnum.COMMISSION.getValue());
                moneyFlowService.addMoneyFlow(member.getId(), goodsProfit.negate(), MoneyFlowTypeEnum.STATIC_BONUS.getValue(), goods.getOrderNo(), FlowTypeEnum.SCORE.getValue());
                List<Map<String, Object>> list = map.get(goods.getId());
                if (CollUtil.isEmpty(list)) {
                    list = new ArrayList<>();
                }
                Map<String, Object> listItem = new HashMap<>();
                listItem.put("memberId", member.getId());
                listItem.put("orderNo", goods.getOrderNo());
                list.add(listItem);
                map.put(goods.getId(), list);
                goodsProp.put(goods.getId(), goods.getStaticProp());
            }
        }
        if (!map.isEmpty()) {
            for (Map.Entry<Long, List<Map<String, Object>>> entry : map.entrySet()) {
                List<Map<String, Object>> list = entry.getValue();
                BigDecimal staticProfit = goodsProp.get(entry.getKey()).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP).multiply(perProfit);
                BigDecimal preStaticProfit = staticProfit.divide(new BigDecimal(list.size()), 2, RoundingMode.HALF_UP);
                for (Map<String, Object> item : list) {
                    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());
                }
            }
        }
    }
    @Override
    public void dynamicProfit(Long orderId) {
        dynamicProfit(orderId, 2);
    }
    /**
@@ -94,7 +135,7 @@
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dynamicProfit(Long orderId) {
    public void dynamicProfit(Long orderId, Integer isNormal) {
        log.info("######直推奖励, 订单ID:{}######", orderId);
        MallOrderInfo orderInfo = mallOrderInfoMapper.selectById(orderId);
        if (orderInfo.getOrderType() == 2) {
@@ -107,73 +148,113 @@
        if (StrUtil.isBlank(member.getReferrerId())) {
            return;
        }
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.DYNAMIC_BONUS.getType(), DataDictionaryEnum.DYNAMIC_BONUS.getCode());
        DataDictionaryCustom indrectDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.INDRECT_BONUS.getType(), DataDictionaryEnum.INDRECT_BONUS.getCode());
        String parent = member.getReferrerId();
        // 直推奖励字典
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.DYNAMIC_BONUS.getType(), DataDictionaryEnum.DYNAMIC_BONUS.getCode());
        // 直接父级
        MallMember parent = mallMemberMapper.selectInfoByInviteId(member.getReferrerId());
        List<MallOrderItem> items = mallOrderInfoMapper.getMallOrderItemByOrderId(orderId);
        for (MallOrderItem item : items) {
            // 减去成本后算收益
            BigDecimal amount = item.getPrice().subtract(item.getCostPrice()).multiply(BigDecimal.valueOf(item.getCnt()));
            if (amount.compareTo(BigDecimal.ZERO) < 1) {
                continue;
            }
            BigDecimal dynamicProfit = amount.divide(new BigDecimal(dic.getValue()), 2, RoundingMode.HALF_UP);
            // 判断套餐或者普通商品,结算对应商品的动态分红
            if (!Objects.equals(item.getIsNormal(), isNormal)) {
                continue;
            }
            List<String> parents = StrUtil.split(member.getReferrerIds(), ',');
            // =======直推返利== start =====
            // 直接奖励收益
            BigDecimal dynamicProfit = amount.multiply(new BigDecimal(dic.getValue()).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP));
            if (dynamicProfit.compareTo(BigDecimal.ZERO) < 1) {
                continue;
            }
            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());
            // =======直推返利== end =====
            // =======隔代奖== start =====
            if (StrUtil.isBlank(parent.getReferrerId())) {
                continue;
            }
            List<String> parents = StrUtil.split(parent.getReferrerIds(), ',');
            List<MallMember> members = mallMemberMapper.selectByInviteIds(parents);
            if (CollUtil.isEmpty(members)) {
                return;
            }
            // 隔代比例
            BigDecimal indrectDicProp = new BigDecimal(indrectDic.getValue());
            // 隔代推荐奖
            BigDecimal direct = dynamicProfit.divide(indrectDicProp, 2, RoundingMode.HALF_UP);
            BigDecimal direct = dynamicProfit;
            for (MallMember parentMember : members) {
                if (parent.equals(parentMember.getInviteId())) {
                if (parent.getInviteId().equals(parentMember.getInviteId())) {
                    continue;
                }
                if (direct.compareTo(BigDecimal.ONE) < 1) {
                    break;
                // 直推数量
                Integer directCnt = mallMemberMapper.selectOwnCntByInviteId(member.getReferrerId());
                List<DataDictionaryCustom> dataDices = dataDictionaryCustomMapper.selectDicByType(DataDictionaryEnum.INDIRECT_BONUS_SETTING.getType());
                directCnt = directCnt == null ? 0 :directCnt;
                // 隔代比例
                BigDecimal indrectDicProp = BigDecimal.ZERO;
                for (DataDictionaryCustom dataDic : dataDices) {
                    JSONObject jsonObject = JSONObject.parseObject(dataDic.getValue());
                    if (directCnt >= jsonObject.getInteger("pushCnt")) {
                        indrectDicProp = jsonObject.getBigDecimal("prop");
                    }
                }
                int reduce =  walletService.reduce(direct, parentMember.getId(), "score");
                if (reduce == 2) {
                // 隔代推荐奖 收益
                direct = direct.divide(indrectDicProp, 2, RoundingMode.HALF_UP);
                // direct 收益小于1,则跳出
                if (direct.compareTo(BigDecimal.ONE) < 1) {
                    continue;
                }
                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());
                direct = direct.divide(indrectDicProp, 2, RoundingMode.HALF_UP);
            }
            // =======隔代奖== end =====
            if (dynamicProfit.compareTo(BigDecimal.ZERO) < 1) {
                continue;
            }
            int reduce = walletService.reduce(dynamicProfit, member.getId(), "score");
            if (reduce == 2) {
                continue;
            }
            walletService.add(dynamicProfit, member.getId(), "commission");
            moneyFlowService.addMoneyFlow(member.getId(), dynamicProfit, MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue());
            moneyFlowService.addMoneyFlow(member.getId(), dynamicProfit.negate(), MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue());
            item.setHasSettle(1);
            mallOrderItemMapper.updateById(item);
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void agentProfit(Date profitDate) {
        log.info("#####==代理分红==start==#####");
        if (profitDate == null) {
            profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
        }
        BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D");
        // 套餐业绩
        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
        // 普通商品业绩
        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
        BigDecimal totalIncome = tcIncome.add(normalIncome);
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.AGENT_BONUS.getType(), DataDictionaryEnum.AGENT_BONUS.getCode());
        BigDecimal profit = totalIncome.divide(new BigDecimal(dic.getValue()), 2, RoundingMode.HALF_UP);
@@ -224,13 +305,17 @@
            return;
        }
        BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M");
        // 套餐业绩
        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M", 2);
        // 普通商品业绩
        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M", 1);
        BigDecimal totalIncome = tcIncome.add(normalIncome);
        List<DataDictionaryCustom> rankAward = dataDictionaryCustomMapper.selectDicByType("RANK_AWARD");
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.RANK_BONUS.getType(), DataDictionaryEnum.RANK_BONUS.getCode());
        BigDecimal rankBonusTotal = new BigDecimal(dic.getValue()).divide(totalIncome, 2, RoundingMode.HALF_UP);
        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);
        int i = 0;
@@ -251,7 +336,7 @@
    }
    /**
     * 拿推荐人收益(代理,直推,排名)的10%加权平分给下面直推
     * 拿推荐人的收益(代理,直推,排名)的10%加权平分给下面直推
     */
    @Override
    public void thankfulProfit() {
@@ -273,18 +358,24 @@
                continue;
            }
            BigDecimal preProfit = income.multiply(prop).divide(BigDecimal.valueOf(children.size()), 2, RoundingMode.HALF_DOWN);
            BigDecimal reduceProfit = income.multiply(prop);
            if (reduceProfit.compareTo(BigDecimal.ZERO) < 1) {
                continue;
            }
            int reduce = walletService.reduce(preProfit.negate(), mallMember.getId(), "commission");
            int reduce = walletService.reduce(reduceProfit, mallMember.getId(), "commission");
            if (reduce == 2) {
                continue;
            }
            moneyFlowService.addMoneyFlow(mallMember.getId(), income.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
            moneyFlowService.addMoneyFlow(mallMember.getId(), reduceProfit.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
            for (MallMember child : children) {
                int reduce1 = walletService.reduce(preProfit.negate(), child.getId(), "score");
                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;
                }
@@ -296,4 +387,65 @@
        }
        log.info("######==感恩奖==end==####");
    }
    @Override
    public void storeAndDirectorProfit(Date profitDate) {
        log.info("#####==店补/董事==start==######");
        if (profitDate == null) {
            profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
        }
        // 套餐业绩
        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
        // 普通商品业绩
        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
        BigDecimal totalIncome = tcIncome.add(normalIncome);
        // 董事
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.DIRECTOR_BONUS.getType(), DataDictionaryEnum.DIRECTOR_BONUS.getCode());
        List<MallMember> directors = mallMemberMapper.selectDirectorsOrStoreMaster(1);
        if (CollUtil.isNotEmpty(directors)) {
            BigDecimal total = totalIncome.multiply(new BigDecimal(dic.getValue()).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP));
            BigDecimal pre = total.divide(new BigDecimal(directors.size()), 2, RoundingMode.HALF_UP);
            for (MallMember director : directors) {
                if (pre.compareTo(BigDecimal.ZERO) < 1) {
                    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());
            }
        }
        // 店补
        dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.COMMUNITY_BONUS.getType(), DataDictionaryEnum.COMMUNITY_BONUS.getCode());
        List<MallMember> storeMasters = mallMemberMapper.selectDirectorsOrStoreMaster(2);
        if (CollUtil.isNotEmpty(storeMasters)) {
            BigDecimal total = totalIncome.multiply(new BigDecimal(dic.getValue()).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP));
            BigDecimal pre = total.divide(new BigDecimal(storeMasters.size()), 2, RoundingMode.HALF_UP);
            for (MallMember storeMaster : storeMasters) {
                if (pre.compareTo(BigDecimal.ZERO) < 1) {
                    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());
            }
        }
        log.info("#####==店补/董事==end==######");
    }
}