xiaoyong931011
2023-04-10 66fdac21a55b328a963a149e38252f9cc02143d0
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;
@@ -48,31 +50,37 @@
        List<String> ids = StrUtil.split(member.getReferrerIds(), ',');
        List<MallMember> parentMembers = memberMapper.selectByInviteIds(ids);
        for (MallMember parent : parentMembers) {
            // 未激活用户无法升级
            if (AgentLevelEnum.ZERO_LEVEL.name().equals(member.getLevel())) {
            /**
             * 升级到董事就无法升级了
             */
            String nextLevelName = MemberLevelEnum.NORMAL.getNextLevel(parent.getLevel());
            int levelCode = MemberLevelEnum.NORMAL.getLevelCode(nextLevelName);
            if(levelCode == 8){
                //level为v6,升级董事,则看团队下有没有两个V6
                List<MallMember> mallMemberTeamMembers = memberMapper.selectAllChildAgentListByInviteId(parent.getInviteId());
                List<MallMember> levelV6List = mallMemberTeamMembers.stream()
                        .filter(teamMember -> teamMember.getLevel().equals(MemberLevelEnum.V6.getType()))
                        .collect(Collectors.toList());
                if(CollUtil.isNotEmpty(levelV6List) && levelV6List.size() >= 2){
                    parent.setLevel(MemberLevelEnum.V_DIRECTOR.getType());
                    parent.setDirector(AppContants.OPTION_YES);
                    memberMapper.updateById(parent);
                }
                continue;
            }
            DataDictionaryCustom nextLevel = dataDictionaryCustomMapper.selectNextAgentLevelInfo(parent.getLevel());
            if (nextLevel == null) {
                log.info("当前层级无下一级:{}", parent.getLevel());
            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 (!teamIncome(parent, adminAgentInfo)) {
                continue;
            }
            if (!agentCntFinish(parent, agentInfo)) {
                continue;
            }
            if (!teamIncome(parent, agentInfo)) {
                continue;
            }
            parent.setLevel(nextLevel.getCode());
            parent.setLevel(nextLevelName);
            memberMapper.updateById(parent);
        }
    }
@@ -83,7 +91,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 +101,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,21 +140,51 @@
    /**
     * 团队业绩是否达标
     *  除去直属的最大的一个业绩团队,剩余的所有业绩之和
     *
     * @param
     * @param agentInfo
     * @return
     */
    private boolean teamIncome(MallMember member, AgentInfo agentInfo) {
        BigDecimal totalIncome = memberMapper.selectAchieveByMemberId(member.getInviteId(), 2);
    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);
        }
        //去掉一个最大区的业绩
        BigDecimal bigMax = list.stream().max(BigDecimal::compareTo).get();
        teamIncomeMax = teamIncomeMax.subtract(bigMax);
        BigDecimal targetIncome = agentInfo.getTeamIncome().multiply(new BigDecimal("10000"));
        if (totalIncome.compareTo(targetIncome) >= 0) {
        if (teamIncomeMax.compareTo(teamIncome) >= 0) {
            return true;
        }
        log.info("用户:{}团队业绩未达标, 当前等级:{}, 当前数量:{}, 目标数量:{}", member.getPhone(), member.getLevel(), totalIncome, targetIncome);
        log.info("用户:{}团队业绩未达标, 当前等级:{}, 当前业绩:{}, 目标业绩:{}", member.getPhone(), member.getLevel(), teamIncomeMax, teamIncome);
        return false;
    }
    public static void main(String[] args) {
        BigDecimal q = new BigDecimal("100");
        BigDecimal q1 = new BigDecimal("99");
        BigDecimal q2 = new BigDecimal("100");
        BigDecimal q3= new BigDecimal("88");
        List<BigDecimal> list = new ArrayList<>();
        list.add(q);
        list.add(q1);
        list.add(q2);
        list.add(q3);
        BigDecimal bigMax = list.stream().max(BigDecimal::compareTo).get();
        System.out.println(bigMax);
    }
    @Override
@@ -158,6 +196,7 @@
         * 3、不同代理级别获取不用的比例的现金和积分
         * 4、董事享受全公司入单分红2%加权(现金)
         * 5、合伙人享全公司入单分红5%加权(现金)
         * 6、入单后,50%入现金池 35%的积分入积分池,更新价格
         */
        //获取当前订单子表信息
        List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectbyOrderId(orderId);
@@ -188,6 +227,7 @@
                amountTC,
                "score",
                MoneyFlowTypeEnum.BUY_SET_MEAL_PERK.getValue(),
                memberId,
                FlowTypeEnum.SCORE.getValue(),
                orderNo);
@@ -205,6 +245,7 @@
                    amountTC,
                    "balance",
                    MoneyFlowTypeEnum.DIRECT_CASH_PERK.getValue(),
                    memberId,
                    FlowTypeEnum.BALANCE.getValue(),
                    orderNo);
@@ -213,6 +254,7 @@
                    amountTC,
                    "score",
                    MoneyFlowTypeEnum.DIRECT_SCORE_PERK.getValue(),
                    memberId,
                    FlowTypeEnum.SCORE.getValue(),
                    orderNo);
        }
@@ -227,6 +269,7 @@
                    amountTC,
                    "balance",
                    MoneyFlowTypeEnum.DIRECTOR_CASH_PERK.getValue(),
                    memberId,
                    FlowTypeEnum.BALANCE.getValue(),
                    orderNo);
        }
@@ -241,6 +284,7 @@
                    amountTC,
                    "balance",
                    MoneyFlowTypeEnum.PARTNER_CASH_PERK.getValue(),
                    memberId,
                    FlowTypeEnum.BALANCE.getValue(),
                    orderNo);
        }
@@ -309,6 +353,7 @@
                                    teamPerkMemberId,
                                    "balance",
                                    MoneyFlowTypeEnum.AGENT_CASH_PERK.getValue(),
                                    memberId,
                                    FlowTypeEnum.BALANCE.getValue(),
                                    orderNo);
                        }
@@ -317,6 +362,7 @@
                                    teamPerkMemberId,
                                    "score",
                                    MoneyFlowTypeEnum.AGENT_SCORE_PERK.getValue(),
                                    memberId,
                                    FlowTypeEnum.SCORE.getValue(),
                                    orderNo);
                        }
@@ -325,6 +371,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 +442,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 +459,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 +473,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 +482,7 @@
                amount,
                type,
                orderNo,
                rtMemberId,
                flowType,
                AppContants.IS_RETURN_YES);
    }
@@ -394,7 +497,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 +510,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);
                }
            }
        }