xiaoyong931011
2023-04-08 e75b2614c89004ba604c1e4f7ec700f1f422c171
src/main/java/cc/mrbird/febs/mall/service/impl/AgentServiceImpl.java
@@ -5,6 +5,8 @@
import cc.mrbird.febs.mall.entity.*;
import cc.mrbird.febs.mall.mapper.*;
import cc.mrbird.febs.mall.service.*;
import cc.mrbird.febs.mall.vo.AdminAgentLevelVo;
import cc.mrbird.febs.mall.vo.AdminAgentMemberVo;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
@@ -49,30 +51,39 @@
        List<MallMember> parentMembers = memberMapper.selectByInviteIds(ids);
        for (MallMember parent : parentMembers) {
            // 未激活用户无法升级
            if (AgentLevelEnum.ZERO_LEVEL.name().equals(member.getLevel())) {
//            if (AgentLevelEnum.ZERO_LEVEL.name().equals(member.getLevel())) {
//                continue;
//            }
            /**
             * 升级到董事就无法升级了
             */
            String nextLevelName = MemberLevelEnum.NORMAL.getNextLevel(parent.getLevel());
            int levelCode = MemberLevelEnum.NORMAL.getLevelCode(nextLevelName);
            if(levelCode >= 7){
                continue;
            }
            DataDictionaryCustom nextLevel = dataDictionaryCustomMapper.selectNextAgentLevelInfo(parent.getLevel());
            if (nextLevel == null) {
                log.info("当前层级无下一级:{}", parent.getLevel());
//            DataDictionaryCustom nextLevel = dataDictionaryCustomMapper.selectNextAgentLevelInfo(parent.getLevel());
//            if (nextLevel == null) {
//                log.info("当前层级无下一级:{}", parent.getLevel());
//                continue;
//            }
            DataDictionaryCustom newLevelDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode("AGENT_PERK_SET", nextLevelName);
            AdminAgentInfo adminAgentInfo = JSONObject.parseObject(newLevelDic.getValue(), AdminAgentInfo.class);
            if (!directMemberCnt(parent, adminAgentInfo)) {
                continue;
            }
            AgentInfo agentInfo = JSONObject.parseObject(nextLevel.getValue(), AgentInfo.class);
            if (!directMemberCnt(parent, agentInfo)) {
//            if (!agentCntFinish(parent, adminAgentInfo)) {
//                continue;
//            }
            if (!teamIncome(parent, adminAgentInfo)) {
                continue;
            }
            if (!agentCntFinish(parent, agentInfo)) {
                continue;
            }
            if (!teamIncome(parent, agentInfo)) {
                continue;
            }
            parent.setLevel(nextLevel.getCode());
            parent.setLevel(newLevelDic.getCode());
            memberMapper.updateById(parent);
        }
    }
@@ -83,7 +94,7 @@
     * @param member
     * @return
     */
    private boolean directMemberCnt(MallMember member, AgentInfo agentInfo) {
    private boolean directMemberCnt(MallMember member, AdminAgentInfo agentInfo) {
        List<MallMember> childs = memberMapper.selectByRefererId(member.getInviteId());
        if (CollUtil.isEmpty(childs)) {
            return false;
@@ -93,7 +104,7 @@
            return true;
        }
        log.info("用户:{}直推数量未达标, 当前等级:{}, 当前数量:{}, 目标数量:{}", member.getPhone(), member.getLevel(), childs.size(), agentInfo.getLastAgentCnt());
        log.info("用户:{}直推数量未达标, 当前等级:{}, 当前数量:{}, 目标数量:{}", member.getPhone(), member.getLevel(), childs.size(), agentInfo.getDirectCnt());
        return false;
    }
@@ -132,20 +143,36 @@
    /**
     * 团队业绩是否达标
     *  除去直属的最大的一个业绩团队,剩余的所有业绩之和
     *
     * @param
     * @param agentInfo
     * @return
     */
    private boolean teamIncome(MallMember member, AgentInfo agentInfo) {
        BigDecimal totalIncome = memberMapper.selectAchieveByMemberId(member.getInviteId(), 2);
        BigDecimal targetIncome = agentInfo.getTeamIncome().multiply(new BigDecimal("10000"));
        if (totalIncome.compareTo(targetIncome) >= 0) {
            return true;
    private boolean teamIncome(MallMember member, AdminAgentInfo agentInfo) {
        BigDecimal teamIncome = agentInfo.getTeamIncome();
        //业绩集合
        List<BigDecimal> list = new ArrayList<>();
        //总业绩
        BigDecimal teamIncomeMax = BigDecimal.ZERO;
        //所有直推团队
        List<MallMember> mallMembers = mallMemberMapper.selectByRefererId(member.getInviteId());
        List<String> mallMemberInviteIds = mallMembers.stream().map(MallMember::getInviteId).collect(Collectors.toList());
        for(String inviteId : mallMemberInviteIds){
            BigDecimal totalIncomeMember = memberMapper.selectAllAchieveByInviteId(inviteId);
            teamIncomeMax = teamIncomeMax.add(totalIncomeMember);
            list.add(totalIncomeMember);
        }
        log.info("用户:{}团队业绩未达标, 当前等级:{}, 当前数量:{}, 目标数量:{}", member.getPhone(), member.getLevel(), totalIncome, targetIncome);
//        BigDecimal targetIncome = agentInfo.getTeamIncome().multiply(new BigDecimal("10000"));
//        if (totalIncome.compareTo(targetIncome) >= 0) {
//            return true;
//        }
//
//        log.info("用户:{}团队业绩未达标, 当前等级:{}, 当前数量:{}, 目标数量:{}", member.getPhone(), member.getLevel(), totalIncome, targetIncome);
        return false;
    }
@@ -158,6 +185,7 @@
         * 3、不同代理级别获取不用的比例的现金和积分
         * 4、董事享受全公司入单分红2%加权(现金)
         * 5、合伙人享全公司入单分红5%加权(现金)
         * 6、入单后,50%入现金池 35%的积分入积分池,更新价格
         */
        //获取当前订单子表信息
        List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectbyOrderId(orderId);
@@ -188,6 +216,7 @@
                amountTC,
                "score",
                MoneyFlowTypeEnum.BUY_SET_MEAL_PERK.getValue(),
                memberId,
                FlowTypeEnum.SCORE.getValue(),
                orderNo);
@@ -205,6 +234,7 @@
                    amountTC,
                    "balance",
                    MoneyFlowTypeEnum.DIRECT_CASH_PERK.getValue(),
                    memberId,
                    FlowTypeEnum.BALANCE.getValue(),
                    orderNo);
@@ -213,6 +243,7 @@
                    amountTC,
                    "score",
                    MoneyFlowTypeEnum.DIRECT_SCORE_PERK.getValue(),
                    memberId,
                    FlowTypeEnum.SCORE.getValue(),
                    orderNo);
        }
@@ -227,6 +258,7 @@
                    amountTC,
                    "balance",
                    MoneyFlowTypeEnum.DIRECTOR_CASH_PERK.getValue(),
                    memberId,
                    FlowTypeEnum.BALANCE.getValue(),
                    orderNo);
        }
@@ -241,6 +273,7 @@
                    amountTC,
                    "balance",
                    MoneyFlowTypeEnum.PARTNER_CASH_PERK.getValue(),
                    memberId,
                    FlowTypeEnum.BALANCE.getValue(),
                    orderNo);
        }
@@ -309,6 +342,7 @@
                                    teamPerkMemberId,
                                    "balance",
                                    MoneyFlowTypeEnum.AGENT_CASH_PERK.getValue(),
                                    memberId,
                                    FlowTypeEnum.BALANCE.getValue(),
                                    orderNo);
                        }
@@ -317,6 +351,7 @@
                                    teamPerkMemberId,
                                    "score",
                                    MoneyFlowTypeEnum.AGENT_SCORE_PERK.getValue(),
                                    memberId,
                                    FlowTypeEnum.SCORE.getValue(),
                                    orderNo);
                        }
@@ -325,6 +360,62 @@
                        scorePercentNormal = scorePercent;
                    }
                }
            }
        }
        /**
         * 6、入单后,50%入现金池 35%的积分入积分池,更新价格
         */
        DataDictionaryCustom poolCashPercentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                PerkEnum.POOL_CASH_PERCENT.getType(),
                PerkEnum.POOL_CASH_PERCENT.getCode());
        DataDictionaryCustom poolScorePercentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                PerkEnum.POOL_SCORE_PERCENT.getType(),
                PerkEnum.POOL_SCORE_PERCENT.getCode());
        if(ObjectUtil.isNotEmpty(poolCashPercentDic)){
            BigDecimal poolCashPercent = new BigDecimal(poolCashPercentDic.getValue()).multiply(AppContants.PERCENTAGE);
            //新增现金数量
            BigDecimal poolCashAdd = amountTC.multiply(poolCashPercent);
            DataDictionaryCustom poolCashDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                    PerkEnum.POOL_CASH.getType(),
                    PerkEnum.POOL_CASH.getCode());
            //当前现金池数量
            BigDecimal poolCash = new BigDecimal(poolCashDic.getValue());
            poolCash = poolCash.add(poolCashAdd).setScale(2,BigDecimal.ROUND_DOWN);
            dataDictionaryCustomMapper.updateDicValueByTypeAndCode(
                    PerkEnum.POOL_CASH.getType(),
                    PerkEnum.POOL_CASH.getCode(),
                    poolCash.toString()
            );
            DataDictionaryCustom poolScorePriceDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                    PerkEnum.POOL_SCORE_PRICE.getType(),
                    PerkEnum.POOL_SCORE_PRICE.getCode());
            BigDecimal poolScorePrice = new BigDecimal(poolScorePriceDic.getValue() == null ? "1" : poolScorePriceDic.getValue());
            BigDecimal poolScorePercent = new BigDecimal(poolScorePercentDic.getValue()).multiply(AppContants.PERCENTAGE);
            //新增积分数量
            BigDecimal poolScoreAdd = amountTC.multiply(poolScorePercent);
            poolScoreAdd = poolScoreAdd.divide(poolScorePrice,2,BigDecimal.ROUND_DOWN);
            DataDictionaryCustom poolScoreDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                    PerkEnum.POOL_SCORE.getType(),
                    PerkEnum.POOL_SCORE.getCode());
            //当前积分池数量
            BigDecimal poolScore = new BigDecimal(poolScoreDic.getValue());
            poolScore = poolScore.add(poolScoreAdd).setScale(2,BigDecimal.ROUND_DOWN);
            dataDictionaryCustomMapper.updateDicValueByTypeAndCode(
                    PerkEnum.POOL_SCORE.getType(),
                    PerkEnum.POOL_SCORE.getCode(),
                    poolScore.toString()
            );
            if(poolScore.compareTo(BigDecimal.ZERO) > 0 && poolCash.compareTo(BigDecimal.ZERO) > 0){
                BigDecimal divide = poolCash.divide(poolScore, 2, BigDecimal.ROUND_DOWN);
                dataDictionaryCustomMapper.updateDicValueByTypeAndCode(
                        PerkEnum.POOL_SCORE_PRICE.getType(),
                        PerkEnum.POOL_SCORE_PRICE.getCode(),
                        divide.toString()
                );
            }
        }
@@ -340,7 +431,7 @@
     * @param flowType 流水分类
     * @param orderNo 订单编号
     */
    private void perkMember(Long memberId,PerkEnum perkEnum,BigDecimal amountTC,String walletField,int type,int flowType,String orderNo){
    private void perkMember(Long memberId,PerkEnum perkEnum,BigDecimal amountTC,String walletField,int type,Long rtMemberId,int flowType,String orderNo){
        DataDictionaryCustom perkEnumDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                perkEnum.getType(),
                perkEnum.getCode());
@@ -357,7 +448,7 @@
                perkEnumDicPercentPerk = perkEnumDicPercentPerk.divide(poolScorePrice, 2 ,BigDecimal.ROUND_DOWN);
            }
            if(perkEnumDicPercentPerk.compareTo(BigDecimal.ZERO) > 0){
                addWalletInfoAndMoneyFlow(perkEnumDicPercentPerk,memberId,walletField,type,flowType,orderNo);
                addWalletInfoAndMoneyFlow(perkEnumDicPercentPerk,memberId,walletField,type,rtMemberId,flowType,orderNo);
            }
        }
    }
@@ -371,7 +462,7 @@
     * @param flowType
     * @param orderNo
     */
    private void addWalletInfoAndMoneyFlow(BigDecimal amount,Long memberId,String walletField,int type,int flowType,String orderNo){
    private void addWalletInfoAndMoneyFlow(BigDecimal amount,Long memberId,String walletField,int type,Long rtMemberId,int flowType,String orderNo){
        //增加
        iApiMallMemberWalletService.add(amount,memberId,walletField);
        //增加一个流水记录
@@ -380,6 +471,7 @@
                amount,
                type,
                orderNo,
                rtMemberId,
                flowType,
                AppContants.IS_RETURN_YES);
    }
@@ -394,7 +486,7 @@
     * @param flowType  流水分类
     * @param orderNo   订单编号
     */
    private void perkMembersLevel(List<MallMember> mallMembers,PerkEnum perkEnum,BigDecimal amountTC,String walletField,int type,int flowType,String orderNo){
    private void perkMembersLevel(List<MallMember> mallMembers,PerkEnum perkEnum,BigDecimal amountTC,String walletField,int type,Long rtMemberId,int flowType,String orderNo){
        DataDictionaryCustom perkEnumDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                perkEnum.getType(),
                perkEnum.getCode());
@@ -407,7 +499,7 @@
            if(perkEnumDicPercentPerk.compareTo(BigDecimal.ZERO) > 0){
                for(MallMember mallMember : mallMembers){
                    Long memberId = mallMember.getId();
                    addWalletInfoAndMoneyFlow(perkEnumDicPercentPerk,memberId,walletField,type,flowType,orderNo);
                    addWalletInfoAndMoneyFlow(perkEnumDicPercentPerk,memberId,walletField,type,rtMemberId,flowType,orderNo);
                }
            }
        }