package com.hzs.bonus.bonus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.activity.pick.IAcPickServiceApi;
import com.hzs.bonus.base.service.ICuMemberService;
import com.hzs.bonus.bonus.param.LeXueRangeParam;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.constant.msg.BonusMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.activity.pick.AcPickMemberLog;
import com.hzs.common.domain.activity.pick.ext.AcPickExt;
import com.hzs.common.domain.bonus.BdBonusDeduct;
import com.hzs.common.domain.bonus.risk.CuMemberRiskControl;
import com.hzs.common.domain.member.base.CuAwardsControl;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.bonus.CuMemberBonus;
import com.hzs.common.domain.member.bonus.CuMemberBonusExpand;
import com.hzs.common.domain.member.bonus.CuMemberBonusPush;
import com.hzs.common.domain.member.bonus.CuMemberBonusRange;
import com.hzs.common.domain.member.detail.CuMemberAssess;
import com.hzs.common.domain.member.ext.CuBonusExpandExt;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.sale.ext.SaOrderItemsExt;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.domain.system.config.BdGrade;
import com.hzs.sale.order.dto.SaOrderRangeDto;
import com.hzs.system.config.dto.BonusConfigDTO;
import com.hzs.system.config.dto.BonusExpandDTO;
import com.hzs.system.config.dto.BonusRepurchasePushDTO;
import com.hzs.system.config.dto.RangeDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 计算复购的奖金处理类
 */
@Component
public class BonusSettlePurchaseHandle extends BonusSettleHandle {

    @DubboReference
    IAcPickServiceApi acPickServiceApi;

    @Autowired
    private BonusSettleFirstPurchaseHandle bonusSettleFirstPurchaseHandle;

    @Autowired
    private ICuMemberService cuMemberService;

    /*
     * 计算复购直推 血缘10代根据比例
     **/
    List<CuMemberBonusPush> calculateRepurchasePushBonus(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, String settleTableName, String settleDateStr, BonusConfigDTO bonusConfigDTO,
                                                         List<SaOrderExt> purchaseOrderList, Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap) {
        List<CuMemberBonusPush> cuMemberBonusPushList = new ArrayList<>();
        if (purchaseOrderList.size() > 0) {
            Set<Long> expandMemberSet = new HashSet<>();
            purchaseOrderList.forEach(saOrderExt -> expandMemberSet.add(saOrderExt.getPkMember()));
            List<CuMemberSettleExt> cuMemberSettleExtList = cuMemberTreeService.batchQueryCuMemberSettleParent(settleTableName, expandMemberSet);
            getMemberSettle(cuMemberSettleExtMap, cuMemberSettleExtList);
            Map<Long, CuMemberAssess> cuMemberAssessMap = getCuMemberAssessMap(cuMemberSettleExtMap, settleDateStr, cuMemberSettleExtList);
            for (SaOrderExt saOrderExt : purchaseOrderList) {
                // 国内没有复购直推
                if (saOrderExt.getPkCountry().equals(CountryConstants.CHINA_COUNTRY)) {
                    continue;
                }
                if (EOrderType.REPURCHASE_ORDER.getValue() == saOrderExt.getOrderType() && ComputeUtil.compareEqual(saOrderExt.getOrderAmount(), BigDecimal.valueOf(158000))) {
                    continue;
                }
                List<CuMemberBonusPush> newMemberBonusPushList = calculateRepurchasePushBonusOne(cuMemberSettleExtMap, bonusConfigDTO, cuMemberAssessMap, period, cuMemberBonusMap, saOrderExt);
                if (newMemberBonusPushList.size() > 0) {
                    cuMemberBonusPushList.addAll(newMemberBonusPushList);
                }
            }
        }
        return cuMemberBonusPushList;
    }

    List<CuMemberBonusPush> calculateRepurchasePushBonusOne(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, BonusConfigDTO bonusConfigDTO, Map<Long, CuMemberAssess> cuMemberAssessMap, Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap, SaOrderExt saOrderExt) {
        CuMemberSettleExt sourceSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
        List<CuMemberBonusPush> cuMemberBonusPushList = new ArrayList<>();
        if (sourceSettleExt == null) {
            return cuMemberBonusPushList;
        }
        BigDecimal orderAchieve = saOrderExt.getOrderAchieve();
//        if(ComputeUtil.compareValue(saOrderExt.getOrderAmount(), BigDecimal.valueOf(170000))){
//            for (SaOrderItemsExt saOrderItemsExt : saOrderExt.getOrderItemsExtList()) {
//                if(Objects.equals(13825, saOrderItemsExt.getPkWares())){
//                    orderAchieve = ComputeUtil.computeSubtract(orderAchieve, saOrderItemsExt.getAchievement());
//                }
//            }
//            if(!ComputeUtil.compareValue(orderAchieve)){
//                return cuMemberBonusPushList;
//            }
//        }
        int i = MagicNumberConstants.BLOOD_GENERATION_START;
        Long pkParent = sourceSettleExt.getPkParent();
        while (i <= MagicNumberConstants.BLOOD_GENERATION_END) {
            CuMemberSettleExt targetMemberSettleExt = cuMemberSettleExtMap.get(pkParent);
            if (targetMemberSettleExt == null) {
                break;
            }
//            if(MagicNumberConstants.ACHIEVE_SETTLE_MEMBER.equals(sourceSettleExt.getMemberCode()) ||
//                    MagicNumberConstants.ACHIEVE_SETTLE_MEMBER.equals(targetMemberSettleExt.getMemberCode()) ){
//                if(cuMemberTreeService.queryCuMemberUnderUmbrella(settleName, saOrderExt.getPkMember())) {
//                    break;
//                }
//            }
            // 死点没有奖金
            if (ECategory.NORMAL.getValue() != targetMemberSettleExt.getCategory() ||
                    EAccountStatus.STOP_INCOME.getValue() == targetMemberSettleExt.getAccountStatus()) {
                pkParent = targetMemberSettleExt.getPkParent();
                continue;
            }
            if (validatePkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.HELP_EACH_INCOME.getValue())) {
                pkParent = targetMemberSettleExt.getPkParent();
                continue;
            }
            // 正常状态的辅导奖需要判断复购考核，复购考核不通过的状态变为烧伤
            // 海粉推荐的直接考核通过
            if (!Objects.equals(MagicNumberConstants.CHINA_HA_FUN, targetMemberSettleExt.getPkGrade())) {
                if (EYesNo.NO.getIntValue() == targetMemberSettleExt.getPurchaseStatus()) {
                    // 需要复购考核的
                    CuMemberAssess cuMemberAssess = cuMemberAssessMap.get(targetMemberSettleExt.getPkMember());
                    if (cuMemberAssess == null) {
                        pkParent = targetMemberSettleExt.getPkParent();
                        continue;
                    }
                }
            }
            CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, targetMemberSettleExt);
            // 直推奖金明细
            int pkBonusItem = getPkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.HELP_EACH_INCOME.getValue());
            CuMemberBonusPush cuMemberBonusPush = bonusSettleFirstPurchaseHandle.getCuMemberBonusPush(saOrderExt, targetMemberSettleExt, cuMemberBonus, pkBonusItem);
            Map<Integer, BonusRepurchasePushDTO> repurchasePushMap = bonusConfigDTO.getBonusRepurchasePushMap().get(targetMemberSettleExt.getPkSettleCountry());
            if (!repurchasePushMap.containsKey(i)) {
                break;
            }
            // 复购直推需要修改
            BonusRepurchasePushDTO bonusRepurchasePushDTO = repurchasePushMap.get(i);
            cuMemberBonusPush.setCalType(bonusRepurchasePushDTO.getCalType());
            cuMemberBonusPush.setRound(0);
            cuMemberBonusPush.setSecond(0);
            // 比例
            if (ECalType.RATIO.getValue() == bonusRepurchasePushDTO.getCalType()) {
                cuMemberBonusPush.setCalValue(bonusRepurchasePushDTO.getPushValue());
                cuMemberBonusPush.setPretaxIncome(ComputeUtil.computeBonusMultiply(orderAchieve, bonusRepurchasePushDTO.getPushValue()));
            } else {
                cuMemberBonusPush.setCalValue(BigDecimal.ZERO);
                cuMemberBonusPush.setPretaxIncome(bonusRepurchasePushDTO.getPushValue());
            }
            BdBonusDeduct bdBonusDeduct = setBonusDetailDeduct(bonusConfigDTO, cuMemberBonusPush, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
            if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusPush.getIncomeStatus()) {
                cuMemberBonus.setRepurPushIncome(ComputeUtil.computeAdd(cuMemberBonus.getRepurPushIncome(), cuMemberBonusPush.getPretaxIncome()));
                setPurchaseTotal(bdBonusDeduct, cuMemberBonus, cuMemberBonusPush);
            }
            repurchasePushRemark(saOrderExt, cuMemberBonusPush, sourceSettleExt, targetMemberSettleExt, BonusMsgConstants.REPURCHASE_PUSH);
            cuMemberBonusPushList.add(cuMemberBonusPush);
            pkParent = targetMemberSettleExt.getPkParent();
            i++;
        }
        return cuMemberBonusPushList;
    }

    /**
     * 计算复购拓展收益
     *
     * @param settleTableName   结算表
     * @param period            期间
     * @param bonusConfigDTO    参数
     * @param purchaseOrderList 首单
     * @param cuMemberBonusMap  所有计算出来的奖金汇总
     */
    List<CuMemberBonusExpand> calculateRepurchaseExpandBonus(Map<Long, CuMemberRiskControl> riskControlMap, Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, String settleTableName, String settleDateStr, BonusConfigDTO bonusConfigDTO, Map<String, String> systemConfigMap,
                                                             List<SaOrderExt> purchaseOrderList, Integer period, Map<Long, CuMemberBonus> weekMemberBonusMap, Map<Long, CuMemberBonus> cuMemberBonusMap, Map<Long, CuBonusExpandExt> cuBonusExpandExtMap) {
        List<CuMemberBonusExpand> cuMemberBonusExpandList = new ArrayList<>();
        // 获取所有订单的上级，根据订单往上一步步计算
        Map<Long, List<CuMemberSettleExt>> memberSettleMap = cuMemberTreeService.batchQueryCuMemberSettlePlaceParent(settleTableName, purchaseOrderList);
        List<CuMemberSettleExt> memberSettleExtList = new ArrayList<>();
        for (List<CuMemberSettleExt> cuMemberSettleExt : memberSettleMap.values()) {
            memberSettleExtList.addAll(cuMemberSettleExt);
        }
        getMemberSettle(cuMemberSettleExtMap, memberSettleExtList);
        Map<Long, CuMemberAssess> cuMemberAssessMap = getCuMemberAssessMap(cuMemberSettleExtMap, settleDateStr, memberSettleExtList);
        for (SaOrderExt saOrderExt : purchaseOrderList) {
            // 所有需要计算的父节点，只计算新增
            List<CuMemberSettleExt> cuMemberSettleExtList = memberSettleMap.get(saOrderExt.getPkId());
            BigDecimal addTouch = BigDecimal.ZERO;
            BigDecimal expandRatio = BigDecimal.ZERO;
            int expandNumber = 0;
            for (int i = cuMemberSettleExtList.size() - 1; i >= 0; i--) {
                CuMemberSettleExt cuMemberSettleExt = cuMemberSettleExtList.get(i);
                if (cuBonusExpandExtMap.containsKey(cuMemberSettleExt.getPkMember())) {
                    addTouch = cuBonusExpandExtMap.get(cuMemberSettleExt.getPkMember()).getAddTouch();
                    expandRatio = cuBonusExpandExtMap.get(cuMemberSettleExt.getPkMember()).getExpandRatio();
                    expandNumber = cuBonusExpandExtMap.get(cuMemberSettleExt.getPkMember()).getExpandNumber();
                    break;
                }
            }
            List<CuMemberBonusExpand> newMemberBonusExpandList = calculateRepurchaseExpandBonusOne(riskControlMap, settleTableName, cuMemberSettleExtMap, bonusConfigDTO, systemConfigMap, period,
                    weekMemberBonusMap, cuMemberBonusMap, cuMemberAssessMap, saOrderExt, cuMemberSettleExtList, addTouch, expandRatio, expandNumber);
            if (newMemberBonusExpandList.size() > 0) {
                cuMemberBonusExpandList.addAll(newMemberBonusExpandList);
            }
        }
        // 根据订单往上穿业绩，只计算新增
        return cuMemberBonusExpandList;
    }

    /**
     * @Description: 秒接复购量奖
     * @return: List<CuMemberBonusExpand>
     * @Author: sui q
     * @Date: 2023/3/20 16:10
     */
    protected List<CuMemberBonusExpand> calculateRepurchaseExpandSecondBonus(Map<Long, CuMemberRiskControl> riskControlMap, String settleTableName, Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, BonusConfigDTO bonusConfigDTO,
                                                                             Map<String, String> systemConfigMap, Integer period,
                                                                             Map<Long, CuMemberBonus> weekMemberBonusMap,
                                                                             Map<Long, CuMemberBonus> cuMemberBonusMap, SaOrderExt saOrderExt,
                                                                             List<CuMemberSettleExt> cuMemberSettleExtList, String settleDateStr, BigDecimal addTouch, BigDecimal expandRatio, Integer expandNumber) {
        Map<Long, CuMemberAssess> cuMemberAssessMap = getCuMemberAssessMap(cuMemberSettleExtMap, settleDateStr, cuMemberSettleExtList);
        // 复购订单，计算复购极差，复购拓展
        return calculateRepurchaseExpandBonusOne(riskControlMap, settleTableName, cuMemberSettleExtMap, bonusConfigDTO, systemConfigMap, period, weekMemberBonusMap, cuMemberBonusMap,
                cuMemberAssessMap, saOrderExt, cuMemberSettleExtList, addTouch, expandRatio, expandNumber);
    }

    /**
     * @Description: 秒接复购量奖
     * @return: List<CuMemberBonusExpand>
     * @Author: sui q
     * @Date: 2023/3/20 16:10
     */
    protected List<CuMemberBonusPush> calculateRepurchasePushSecondBonus(String settleTableName, Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, BonusConfigDTO bonusConfigDTO, Integer period,
                                                                         Map<Long, CuMemberBonus> cuMemberBonusMap, SaOrderExt saOrderExt,
                                                                         List<CuMemberSettleExt> cuMemberSettleExtList, String settleDateStr) {
        // 根据订单获取轮次，要回写
        Map<Long, CuMemberAssess> cuMemberAssessMap = getCuMemberAssessMap(cuMemberSettleExtMap, settleDateStr, cuMemberSettleExtList);
        return calculateRepurchasePushBonusOne(cuMemberSettleExtMap, bonusConfigDTO, cuMemberAssessMap, period, cuMemberBonusMap, saOrderExt);
    }


    private List<CuMemberBonusExpand> calculateRepurchaseExpandBonusOne(Map<Long, CuMemberRiskControl> riskControlMap, String settleName, Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, BonusConfigDTO bonusConfigDTO, Map<String, String> systemConfigMap, Integer period,
                                                                        Map<Long, CuMemberBonus> weekMemberBonusMap, Map<Long, CuMemberBonus> cuMemberBonusMap, Map<Long, CuMemberAssess> cuMemberAssessMap,
                                                                        SaOrderExt saOrderExt, List<CuMemberSettleExt> cuMemberSettleExtList, BigDecimal addTouch, BigDecimal expandRatio, Integer expandNumber) {
        Map<Integer, BonusExpandDTO> bdBonusExpandMap = bonusConfigDTO.getBonusExpandMap();
        List<CuMemberBonusExpand> cuMemberBonusExpandList = new ArrayList<>();
        CuMemberSettleExt settleExt = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
        if (settleExt == null) {
            return cuMemberBonusExpandList;
        }
        settleExt.setRepurchaseAmount(ComputeUtil.computeAdd(settleExt.getRepurchaseAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
        settleExt.setRepurchasePv(ComputeUtil.computeAdd(settleExt.getRepurchasePv(), saOrderExt.getOrderAchieve()));
        settleExt.setRepurchaseNewAmount(ComputeUtil.computeAdd(settleExt.getRepurchaseNewAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
        settleExt.setRepurchaseNewPv(ComputeUtil.computeAdd(settleExt.getRepurchaseNewPv(), saOrderExt.getOrderAchieve()));
        // 先计算新增，根据新增计算碰次
        // 先进行初始化所有的安置人，存储需要修改的值,cuMemberSettleExtMap最终修改的
//        getMemberSettle(cuMemberSettleExtMap, cuMemberSettleExtList);
        // 先准备值，准备好值后，开始计算，计算碰次
        int touchNum = calculateTouchNum(riskControlMap, settleName, cuMemberAssessMap, cuMemberSettleExtMap, saOrderExt, cuMemberSettleExtList, bonusConfigDTO);
        // 有碰次，计算拓展奖
        if (touchNum > 0) {
            // 判断是否>3 是否需要启动4-N，4-N的参数按照伞上各安置人结算国家进行获取
            // 先计算前3碰
            int startTouch = touchNum;
            // 后面碰次
            int endTouch = 0;
            BonusExpandDTO bdBonusExpand = bdBonusExpandMap.get(saOrderExt.getPkCountry());
            int realExpandNumber = expandNumber > 0 ? expandNumber : bdBonusExpand.getExpandNumber();
            if (touchNum > realExpandNumber) {
                startTouch = realExpandNumber;
                endTouch = touchNum - startTouch;
            }
            // 正常的拓展奖计算
            // 计算拓展奖
            for (int i = 1; i <= startTouch; i++) {
                for (CuMemberSettleExt sourceMemberSettleExt : cuMemberSettleExtList) {
                    CuMemberSettleExt targetMemberSettleExt = cuMemberSettleExtMap.get(sourceMemberSettleExt.getPkPlaceParent());
                    if (targetMemberSettleExt == null || ECategory.NORMAL.getValue() != targetMemberSettleExt.getCategory() ||
                            EAccountStatus.STOP_INCOME.getValue() == targetMemberSettleExt.getAccountStatus()) {
                        continue;
                    }
                    if (validatePkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.CONSUMPTION_EXPANSION_INCOME.getValue())) {
                        continue;
                    }
                    // 判断左右区是否有值，找到有值的就是有碰次，碰完左区或右区肯定有变为0的
                    // 判断左右区是否有值，找到有值的就是有碰次，碰完左区或右区肯定有变为0的
                    if ((targetMemberSettleExt.getPkSettleCountry().equals(CountryConstants.CHINA_COUNTRY) && ComputeUtil.compareValue(targetMemberSettleExt.getABalance()) && ComputeUtil.compareValue(targetMemberSettleExt.getBBalance()))
                            || (ComputeUtil.compareValue(targetMemberSettleExt.getRepABalance()) && ComputeUtil.compareValue(targetMemberSettleExt.getRepBBalance()))) {
                        // 根据会员等级获取对应的配置
                        BdGrade bdGrade = getGradeExpand(bonusConfigDTO, targetMemberSettleExt);
                        BigDecimal countryExpandRatio = bdGrade.getRepurchaseRatio();
                        Integer pkBonusItems = getPkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.CONSUMPTION_EXPANSION_INCOME.getValue());
                        calculateRepurchaseExpandBonusByAchieve(period, weekMemberBonusMap, cuMemberBonusMap, bonusConfigDTO, systemConfigMap, cuMemberBonusExpandList, saOrderExt, touchNum, i, sourceMemberSettleExt, targetMemberSettleExt, bdGrade, countryExpandRatio, pkBonusItems, cuMemberAssessMap);
                        break;
                    }
                }
            }
            // 4-N计算
            if (endTouch > 0) {
                // 获得每一碰的比例, 找到剩下的碰次的国家
                for (int i = 1; i <= endTouch; i++) {
                    for (CuMemberSettleExt sourceMemberSettleExt : cuMemberSettleExtList) {
                        CuMemberSettleExt targetMemberSettleExt = cuMemberSettleExtMap.get(sourceMemberSettleExt.getPkPlaceParent());
                        if (targetMemberSettleExt == null || ECategory.NORMAL.getValue() != targetMemberSettleExt.getCategory() ||
                                EAccountStatus.STOP_INCOME.getValue() == targetMemberSettleExt.getAccountStatus()) {
                            continue;
                        }
                        if (validatePkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.CONSUMPTION_EXPANSION_INCOME.getValue())) {
                            continue;
                        }
                        // 判断左右区是否有值，找到有值的就是有碰次，碰完左区或右区肯定有变为0的
                        if ((targetMemberSettleExt.getPkSettleCountry().equals(CountryConstants.CHINA_COUNTRY) && ComputeUtil.compareValue(targetMemberSettleExt.getABalance()) && ComputeUtil.compareValue(targetMemberSettleExt.getBBalance()))
                                || (ComputeUtil.compareValue(targetMemberSettleExt.getRepABalance()) && ComputeUtil.compareValue(targetMemberSettleExt.getRepBBalance()))) {
                            // 根据会员等级获取对应的配置
                            BdGrade bdGrade = getGradeExpand(bonusConfigDTO, targetMemberSettleExt);
                            // 获取国家4-n的配置
                            BigDecimal countryExpandRatio = getCountryRepurchaseExpand(bonusConfigDTO, targetMemberSettleExt, endTouch, bdGrade, addTouch, expandRatio);
                            Integer pkBonusItems = getPkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.CONSUMPTION_EXPANSION_INCOME.getValue());
                            calculateRepurchaseExpandBonusByAchieve(period, weekMemberBonusMap, cuMemberBonusMap, bonusConfigDTO, systemConfigMap, cuMemberBonusExpandList, saOrderExt, touchNum, i + startTouch, sourceMemberSettleExt, targetMemberSettleExt, bdGrade, countryExpandRatio, pkBonusItems, cuMemberAssessMap);
                            break;
                        }
                    }
                }
            }
        }
        return cuMemberBonusExpandList;
    }

    private int calculateTouchNum(Map<Long, CuMemberRiskControl> riskControlMap, String settleName, Map<Long, CuMemberAssess> cuMemberAssessMap,
                                  Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, SaOrderExt saOrderExt, List<CuMemberSettleExt> cuMemberSettleExtList, BonusConfigDTO bonusConfigDTO) {
        int touchNum = 0;
        boolean flag = Boolean.TRUE;
        CuMemberSettleExt firstMemberSettleExt = cuMemberSettleExtList.get(0);
        for (CuMemberSettleExt cuMemberSettleExt : cuMemberSettleExtList) {
            // 数据已经准备好了，肯定有值，map中是实际的值，先计算新增，然后根据新增结算计算碰次，list是为了存储碰次的顺序
            CuMemberSettleExt targetMemberSettleExt = cuMemberSettleExtMap.get(cuMemberSettleExt.getPkPlaceParent());
            if (targetMemberSettleExt == null || ECategory.NORMAL.getValue() != targetMemberSettleExt.getCategory() ||
                    EAccountStatus.STOP_INCOME.getValue() == targetMemberSettleExt.getAccountStatus() ||
                    EGrade.START_UP.getValue() == targetMemberSettleExt.getGradeValue()) {
                continue;
            }
//            if(MagicNumberConstants.ACHIEVE_SETTLE_MEMBER.equals(targetMemberSettleExt.getMemberCode())){
//                if(cuMemberTreeService.queryCuMemberUnderUmbrella(settleName, saOrderExt.getPkMember())) {
//                    flag = Boolean.FALSE;
//                }
//            }
            // 验证是否计算奖金
            BdGrade bdGrade = getGradeExpand(bonusConfigDTO, targetMemberSettleExt);
            if (bdGrade.getRepurchaseCalculate() != null && EYesNo.NO.getIntValue() == bdGrade.getRepurchaseCalculate()) {
                continue;
            }
            if (targetMemberSettleExt.getPkSettleCountry().equals(CountryConstants.CHINA_COUNTRY)) {
                if (EPlaceDept.LEFT_DEPT.getValue() == cuMemberSettleExt.getPlaceDept()) {
                    // 左区
                    // K值设置
                    if (riskControlMap.containsKey(targetMemberSettleExt.getPkMember())) {
                        CuMemberRiskControl cuMemberRiskControl = riskControlMap.get(targetMemberSettleExt.getPkMember());
                        targetMemberSettleExt.setANewAmount(ComputeUtil.computeAdd(targetMemberSettleExt.getANewAmount(), ComputeUtil.computeBonusMultiply(convertLocalCurrency(saOrderExt, bonusConfigDTO), cuMemberRiskControl.getAchieveRatio())));
                        targetMemberSettleExt.setANewPv(ComputeUtil.computeAdd(targetMemberSettleExt.getANewPv(), ComputeUtil.computeBonusMultiply(saOrderExt.getUploadAchieve(), cuMemberRiskControl.getAchieveRatio())));
                        targetMemberSettleExt.setABalance(ComputeUtil.computeAdd(targetMemberSettleExt.getABalance(), ComputeUtil.computeBonusMultiply(saOrderExt.getUploadAchieve(), cuMemberRiskControl.getAchieveRatio())));
                    } else {
                        targetMemberSettleExt.setANewAmount(ComputeUtil.computeAdd(targetMemberSettleExt.getANewAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
                        targetMemberSettleExt.setANewPv(ComputeUtil.computeAdd(targetMemberSettleExt.getANewPv(), saOrderExt.getUploadAchieve()));
                        targetMemberSettleExt.setABalance(ComputeUtil.computeAdd(targetMemberSettleExt.getABalance(), saOrderExt.getUploadAchieve()));
                    }
                    targetMemberSettleExt.setRABalance(ComputeUtil.computeAdd(targetMemberSettleExt.getRABalance(), saOrderExt.getUploadAchieve()));
//                }
                } else {
                    // 右区
                    if (riskControlMap.containsKey(targetMemberSettleExt.getPkMember())) {
                        CuMemberRiskControl cuMemberRiskControl = riskControlMap.get(targetMemberSettleExt.getPkMember());
                        targetMemberSettleExt.setBNewAmount(ComputeUtil.computeAdd(targetMemberSettleExt.getBNewAmount(), ComputeUtil.computeBonusMultiply(convertLocalCurrency(saOrderExt, bonusConfigDTO), cuMemberRiskControl.getAchieveRatio())));
                        targetMemberSettleExt.setBNewPv(ComputeUtil.computeAdd(targetMemberSettleExt.getBNewPv(), ComputeUtil.computeBonusMultiply(saOrderExt.getUploadAchieve(), cuMemberRiskControl.getAchieveRatio())));
                        targetMemberSettleExt.setBBalance(ComputeUtil.computeAdd(targetMemberSettleExt.getBBalance(), ComputeUtil.computeBonusMultiply(saOrderExt.getUploadAchieve(), cuMemberRiskControl.getAchieveRatio())));
                    } else {
                        targetMemberSettleExt.setBNewAmount(ComputeUtil.computeAdd(targetMemberSettleExt.getBNewAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
                        targetMemberSettleExt.setBNewPv(ComputeUtil.computeAdd(targetMemberSettleExt.getBNewPv(), saOrderExt.getUploadAchieve()));
                        targetMemberSettleExt.setBBalance(ComputeUtil.computeAdd(targetMemberSettleExt.getBBalance(), saOrderExt.getUploadAchieve()));
                    }
                    targetMemberSettleExt.setRBBalance(ComputeUtil.computeAdd(targetMemberSettleExt.getRBBalance(), saOrderExt.getUploadAchieve()));
                }
                // 判断左右区是否有值，左右区有值有碰次
                if (ComputeUtil.compareValue(targetMemberSettleExt.getABalance()) && ComputeUtil.compareValue(targetMemberSettleExt.getBBalance())) {
                    // 国内量奖修改为考核不通过的不占用4-N，不算奖金
                    // 验证是否验证考核
                    touchNum++;
                }
                // 判断左右区是否有值，左右区有值有碰次
                if (ComputeUtil.compareValue(targetMemberSettleExt.getRABalance()) && ComputeUtil.compareValue(targetMemberSettleExt.getRBBalance())) {
                    BigDecimal minAchieve = ComputeUtil.obtainMinAchieve(targetMemberSettleExt.getRABalance(), targetMemberSettleExt.getRBBalance());
                    targetMemberSettleExt.setRABalance(ComputeUtil.computeSubtractAbs(targetMemberSettleExt.getRABalance(), minAchieve));
                    targetMemberSettleExt.setRBBalance(ComputeUtil.computeSubtractAbs(targetMemberSettleExt.getRBBalance(), minAchieve));
                }
            } else {
                // 嗨粉、海粉没有量奖
                if (EPlaceDept.LEFT_DEPT.getValue() == cuMemberSettleExt.getPlaceDept()) {
                    // 左区
                    targetMemberSettleExt.setRepANewAmount(ComputeUtil.computeAdd(targetMemberSettleExt.getRepANewAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
                    targetMemberSettleExt.setRepANewPv(ComputeUtil.computeAdd(targetMemberSettleExt.getRepANewPv(), saOrderExt.getUploadAchieve()));
                    targetMemberSettleExt.setRepABalance(ComputeUtil.computeAdd(targetMemberSettleExt.getRepABalance(), saOrderExt.getUploadAchieve()));
                    targetMemberSettleExt.setRRepABalance(ComputeUtil.computeAdd(targetMemberSettleExt.getRRepABalance(), saOrderExt.getUploadAchieve()));
                } else {
                    // 右区
                    targetMemberSettleExt.setRepBNewAmount(ComputeUtil.computeAdd(targetMemberSettleExt.getRepBNewAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
                    targetMemberSettleExt.setRepBNewPv(ComputeUtil.computeAdd(targetMemberSettleExt.getRepBNewPv(), saOrderExt.getUploadAchieve()));
                    targetMemberSettleExt.setRepBBalance(ComputeUtil.computeAdd(targetMemberSettleExt.getRepBBalance(), saOrderExt.getUploadAchieve()));
                    targetMemberSettleExt.setRRepBBalance(ComputeUtil.computeAdd(targetMemberSettleExt.getRRepBBalance(), saOrderExt.getUploadAchieve()));
                }
                // 判断左右区是否有值，左右区有值有碰次
                if (ComputeUtil.compareValue(targetMemberSettleExt.getRepABalance()) && ComputeUtil.compareValue(targetMemberSettleExt.getRepBBalance())) {
                    touchNum++;
                }
                // 判断左右区是否有值，左右区有值有碰次
                if (ComputeUtil.compareValue(targetMemberSettleExt.getRRepABalance()) && ComputeUtil.compareValue(targetMemberSettleExt.getRRepBBalance())) {
                    BigDecimal minAchieve = ComputeUtil.obtainMinAchieve(targetMemberSettleExt.getRRepABalance(), targetMemberSettleExt.getRRepBBalance());
                    targetMemberSettleExt.setRRepABalance(ComputeUtil.computeSubtractAbs(targetMemberSettleExt.getRRepABalance(), minAchieve));
                    targetMemberSettleExt.setRRepBBalance(ComputeUtil.computeSubtractAbs(targetMemberSettleExt.getRRepBBalance(), minAchieve));
                }
            }
        }
        return touchNum;
    }

    private Map<Long, CuMemberAssess> getCuMemberAssessMap(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, String settleDateStr, List<CuMemberSettleExt> memberSettleExtList) {
        List<CuMemberSettleExt> queryMemberList = getQueryMemberSettleExtList(cuMemberSettleExtMap, memberSettleExtList);
        return getLongCuMemberAssessMap(settleDateStr, queryMemberList);
    }

    /**
     * 计算复购极差
     *
     * @param period            期间
     * @param bonusConfigDTO    参数
     * @param purchaseOrderList 复购订单
     * @param cuMemberBonusMap  所有计算出来的奖金汇总
     */
    List<CuMemberBonusRange> calculateRepurchaseRangeBonus(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, String settleTableName, String settleDate, Map<String, RangeDTO> rangeDtoMap, Map<String, BdAwards> awardsMap,
                                                           BonusConfigDTO bonusConfigDTO, List<SaOrderExt> purchaseOrderList,
                                                           Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap, Map<Long, CuAwardsControl> cuAwardsControlMap, LeXueRangeParam leXueRangeParam) {
        // 计算复购极差的订单
//        List<SaOrderExt> chinaRangeOrderList = new ArrayList<>();
        //        if (chinaRangeOrderList.size() > 0) {
//            Map<String, List<SaOrderRangeDto>> saOrderRangeDtoMap = saOrderServiceApi.queryOrderRangeVO(chinaRangeOrderList).getData();
//            Map<String, RangeDTO> rangePkDtoMap = new HashMap<>(ComputeUtil.mapInitCapacity(rangeDtoMap.size()));
//            rangeDtoMap.forEach((key, rangeDto) ->
//                    rangePkDtoMap.put(rangeDto.getPkCountry().toString() + rangeDto.getPkRange(), rangeDto));
//            chinaRangeOrderList.forEach(saOrderExt -> {
//                List<SaOrderRangeDto> orderRangeVOList = saOrderRangeDtoMap.get(saOrderExt.getOrderCode());
//                if (orderRangeVOList == null || orderRangeVOList.size() == 0) {
//                    return;
//                }
//                if (!validateSecondOrderExistBonus(saOrderExt, EBonusItems.REPURCHASE_RANGE_INCOME.getValue())) {
//                    return;
//                }
//                Map<Long, List<SaOrderRangeDto>> orderRangeDtoMap = getRangeDtoMap(orderRangeVOList);
//                for(Long pkItem :orderRangeDtoMap.keySet()){
//                    List<SaOrderRangeDto> saOrderRangeDtoList = orderRangeDtoMap.get(pkItem);
//                    // 一定要发完，从推荐人开始发
//                    executeChinaOrderRange(cuMemberSettleExtMap, rangeDtoMap, awardsMap, bonusConfigDTO, period, cuMemberBonusMap,
//                            cuAwardsControlMap, cuMemberBonusRangeList, cuMemberAssessMap, rangePkDtoMap, saOrderExt, saOrderRangeDtoList);
//                }
//            });
//            List<SaOrderExt> saOrderExtList = new ArrayList<>();
//            for (SaOrderExt saOrderExt : chinaRangeOrderList) {
//                if(ComputeUtil.compareValue(saOrderExt.getOrderAmount(), BigDecimal.valueOf(170000))){
//                    for (SaOrderItemsExt saOrderItemsExt : saOrderExt.getOrderItemsExtList()) {
//                        if(Objects.equals(13825, saOrderItemsExt.getPkWares())){
//                            saOrderExt.setOrderAchieve(saOrderItemsExt.getAchievement());
//                            saOrderExtList.add(saOrderExt);
//                        }
//                    }
//                }
//            }
//            if(saOrderExtList.size() >0) {
//                for (SaOrderExt saOrderExt : saOrderExtList) {
//                    CuMemberSettleExt sourceMemberTree = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
//                    while (true) {
//                        if (sourceMemberTree == null) {
//                            break;
//                        }
//                        CuMemberSettleExt parentMemberTree = cuMemberSettleExtMap.get(sourceMemberTree.getPkParent());
//                        parentMemberTree = getRangeCuMemberTree(cuMemberSettleExtMap, rangeDtoMap, awardsMap, bonusConfigDTO, sourceMemberTree, parentMemberTree, cuAwardsControlMap, cuMemberAssessMap);
//                        if (parentMemberTree == null) {
//                            break;
//                        }
//                        RangeDTO parentRangeDto = getRangeDto(rangeDtoMap, awardsMap, parentMemberTree, cuAwardsControlMap);
//                        RangeDTO sourceRangeDto = getRangeDto(rangeDtoMap, awardsMap, sourceMemberTree, cuAwardsControlMap);
//                        BigDecimal achieveRatio = ComputeUtil.computeSubtract(parentRangeDto.getAchieveRatio(), sourceRangeDto.getAchieveRatio());
//                        // 发放奖金
//                        CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, parentMemberTree);
//                        CuMemberBonusRange cuMemberBonusRange = getCuMemberBonusRange(bonusConfigDTO, saOrderExt, parentMemberTree, cuMemberBonus);
//                        cuMemberBonusRange.setCalAchieve(saOrderExt.getOrderAchieve());
////                        BigDecimal repurchaseRangeBonus = ComputeUtil.computeMultiply(ComputeUtil.computeSubtract(orderAchieve, saOrderRangeDto.getRangeAchieve()), saOrderRangeDto.getQuantity());
//                        if (ComputeUtil.compareValue(BigDecimal.ZERO, achieveRatio)) {
//                            sourceMemberTree = cuMemberSettleExtMap.get(parentMemberTree.getPkMember());
//                            continue;
//                        }
//                        cuMemberBonusRange.setCalValue(BigDecimal.ZERO);
//                        BigDecimal repurchaseRangeBonus = ComputeUtil.computeBonusMultiply(saOrderExt.getOrderAchieve(), achieveRatio);
//                        cuMemberBonusRange.setPretaxIncome(repurchaseRangeBonus);
//                        cuMemberBonusRange.setPkRange(parentRangeDto.getPkRange());
//                        cuMemberBonusRange.setPkBeforeRange(sourceRangeDto.getPkRange());
//                        cuMemberBonusRange.setPkOrderItems(0L);
//                        rangeRemark(saOrderExt, cuMemberBonusRange, sourceMemberTree, parentMemberTree, parentRangeDto, sourceRangeDto);
//                        packageRepurchaseRange(bonusConfigDTO, cuMemberBonusRangeList, saOrderExt, cuMemberBonus, cuMemberBonusRange, repurchaseRangeBonus);
//                        if (ERangeType.AWARDS.getValue() == parentRangeDto.getRangeType() && EAwards.GLOBAL_CHAIRMAN.getValue() == parentRangeDto.getRangeValue()) {
//                            break;
//                        }
//                        sourceMemberTree = cuMemberSettleExtMap.get(parentMemberTree.getPkMember());
//                    }
//                }
//            }
//        }
        return new ArrayList<>();
    }

    private void executeOverseasOrderRange(String settleName, Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, Map<String, RangeDTO> rangeDtoMap, Map<String, BdAwards> awardsMap, BonusConfigDTO bonusConfigDTO, Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap, Map<Long, CuAwardsControl> cuAwardsControlMap, List<CuMemberBonusRange> cuMemberBonusRangeList, Map<Long, CuMemberAssess> cuMemberAssessMap, Map<String, RangeDTO> rangePkDtoMap, SaOrderExt saOrderExt, List<SaOrderRangeDto> orderRangeVOList) {
        CuMemberSettleExt sourceCuMemberTree = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
        CuMemberSettleExt parentMemberTree = cuMemberSettleExtMap.get(sourceCuMemberTree.getPkParent());
        BigDecimal orderPrice = orderRangeVOList.get(0).getOrderPrice();
//                BigDecimal orderAchieve = orderRangeVOList.get(0).getOrderAchieve();
        int pkRange = orderRangeVOList.get(0).getPkRange();
        for (SaOrderRangeDto saOrderRangeDto : orderRangeVOList) {
            parentMemberTree = getCuMemberTree(cuMemberSettleExtMap, rangeDtoMap, rangePkDtoMap, awardsMap, bonusConfigDTO, parentMemberTree, saOrderRangeDto, cuAwardsControlMap, cuMemberAssessMap, saOrderExt);
            if (parentMemberTree == null) {
                break;
            }
//            if(MagicNumberConstants.ACHIEVE_SETTLE_MEMBER.equals(sourceCuMemberTree.getMemberCode()) ||
//                    MagicNumberConstants.ACHIEVE_SETTLE_MEMBER.equals(parentMemberTree.getMemberCode()) ){
//                if(cuMemberTreeService.queryCuMemberUnderUmbrella(settleName, saOrderExt.getPkMember())) {
//                    break;
//                }
//            }
            RangeDTO rangeDto = getRangeDto(rangeDtoMap, awardsMap, parentMemberTree, cuAwardsControlMap);
            if (saOrderRangeDto.getRangeValue() != null && rangeDto.getRangeValue() != null &&
                    rangeDto.getRangeValue().equals(saOrderRangeDto.getRangeValue())) {
                CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, parentMemberTree);
                CuMemberBonusRange cuMemberBonusRange = getCuMemberBonusRange(bonusConfigDTO, saOrderExt, parentMemberTree, cuMemberBonus);
                BigDecimal repurchaseRangeBonus = ComputeUtil.computeMultiply(ComputeUtil.computeSubtract(orderPrice, saOrderRangeDto.getRangePrice()), saOrderRangeDto.getQuantity());
                if (ComputeUtil.compareValue(BigDecimal.ZERO, repurchaseRangeBonus)) {
                    break;
                }
                cuMemberBonusRange.setCalValue(BigDecimal.ZERO);
                cuMemberBonusRange.setPretaxIncome(repurchaseRangeBonus);
                cuMemberBonusRange.setPkRange(rangeDto.getPkRange());
                cuMemberBonusRange.setPkBeforeRange(pkRange);
                cuMemberBonusRange.setPkOrderItems(saOrderRangeDto.getPkOrderItems());
                rangeRemark(saOrderExt, cuMemberBonusRange, sourceCuMemberTree, parentMemberTree, rangePkDtoMap);
                packageRepurchaseRange(bonusConfigDTO, cuMemberBonusRangeList, saOrderExt, cuMemberBonus, cuMemberBonusRange, repurchaseRangeBonus);
                if (ComputeUtil.compareEqual(orderPrice, saOrderRangeDto.getRangePrice())) {
                    break;
                }
                orderPrice = saOrderRangeDto.getRangePrice();
                pkRange = saOrderRangeDto.getPkRange();
            }
        }
    }

    private Map<Long, List<SaOrderRangeDto>> getRangeDtoMap(List<SaOrderRangeDto> orderRangeVOList) {
        Map<Long, List<SaOrderRangeDto>> orderRangeDtoMap = new HashMap<>();
        for (SaOrderRangeDto saOrderRangeDto : orderRangeVOList) {
            if (orderRangeDtoMap.containsKey(saOrderRangeDto.getPkOrderItems())) {
                orderRangeDtoMap.get(saOrderRangeDto.getPkOrderItems()).add(saOrderRangeDto);
            } else {
                List<SaOrderRangeDto> saOrderRangeDtoList = new ArrayList<>();
                saOrderRangeDtoList.add(saOrderRangeDto);
                orderRangeDtoMap.put(saOrderRangeDto.getPkOrderItems(), saOrderRangeDtoList);
            }
        }
        return orderRangeDtoMap;
    }

    private void executeChinaOrderRange(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, Map<String, RangeDTO> rangeDtoMap, Map<String, BdAwards> awardsMap, BonusConfigDTO bonusConfigDTO, Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap, Map<Long, CuAwardsControl> cuAwardsControlMap, List<CuMemberBonusRange> cuMemberBonusRangeList, Map<Long, CuMemberAssess> cuMemberAssessMap, Map<String, RangeDTO> rangePkDtoMap, SaOrderExt saOrderExt, List<SaOrderRangeDto> orderRangeVOList) {
        CuMemberSettleExt sourceCuMemberTree = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
        if (sourceCuMemberTree == null) {
            return;
        }
        CuMemberSettleExt parentMemberTree = cuMemberSettleExtMap.get(sourceCuMemberTree.getPkParent());
        BigDecimal orderAchieve = orderRangeVOList.get(0).getOrderAchieve();
        int pkRange = orderRangeVOList.get(0).getPkRange();
        for (SaOrderRangeDto saOrderRangeDto : orderRangeVOList) {
            parentMemberTree = getCuMemberTree(cuMemberSettleExtMap, rangeDtoMap, rangePkDtoMap, awardsMap, bonusConfigDTO,
                    parentMemberTree, saOrderRangeDto, cuAwardsControlMap, cuMemberAssessMap, saOrderExt);
            if (parentMemberTree == null) {
                break;
            }
            RangeDTO rangeDto = getRangeDto(rangeDtoMap, awardsMap, parentMemberTree, cuAwardsControlMap);
            if (saOrderRangeDto.getRangeValue() != null && rangeDto.getRangeValue() != null &&
                    rangeDto.getRangeValue().equals(saOrderRangeDto.getRangeValue())) {
                CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, parentMemberTree);
                CuMemberBonusRange cuMemberBonusRange = getCuMemberBonusRange(bonusConfigDTO, saOrderExt, parentMemberTree, cuMemberBonus);
                cuMemberBonusRange.setCalAchieve(saOrderExt.getOrderAmount());
                BigDecimal repurchaseRangeBonus = ComputeUtil.computeMultiply(ComputeUtil.computeSubtract(orderAchieve, saOrderRangeDto.getRangeAchieve()), saOrderRangeDto.getQuantity());
                if (ComputeUtil.compareValue(BigDecimal.ZERO, repurchaseRangeBonus)) {
                    break;
                }
                cuMemberBonusRange.setCalValue(BigDecimal.ZERO);
                cuMemberBonusRange.setPretaxIncome(repurchaseRangeBonus);
                cuMemberBonusRange.setPkRange(rangeDto.getPkRange());
                cuMemberBonusRange.setPkBeforeRange(pkRange);
                cuMemberBonusRange.setPkOrderItems(saOrderRangeDto.getPkOrderItems());
                rangeRemark(saOrderExt, cuMemberBonusRange, sourceCuMemberTree, parentMemberTree, rangePkDtoMap);
                packageRepurchaseRange(bonusConfigDTO, cuMemberBonusRangeList, saOrderExt, cuMemberBonus, cuMemberBonusRange, repurchaseRangeBonus);
                if (ComputeUtil.compareEqual(orderAchieve, saOrderRangeDto.getRangeAchieve())) {
                    break;
                }
                orderAchieve = saOrderRangeDto.getRangeAchieve();
                pkRange = saOrderRangeDto.getPkRange();
            }
        }
    }

    private void packageRepurchaseRange(BonusConfigDTO bonusConfigDTO, List<CuMemberBonusRange> cuMemberBonusRangeList, SaOrderExt saOrderExt, CuMemberBonus cuMemberBonus, CuMemberBonusRange cuMemberBonusRange, BigDecimal repurchaseRangeBonus) {
        BdBonusDeduct bdBonusDeduct = setBonusDetailDeduct(bonusConfigDTO, cuMemberBonusRange, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
        if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusRange.getIncomeStatus()) {
            cuMemberBonus.setRepurRangeIncome(ComputeUtil.computeAdd(cuMemberBonus.getRepurRangeIncome(), repurchaseRangeBonus));
            setPurchaseTotal(bdBonusDeduct, cuMemberBonus, cuMemberBonusRange);
        }
        cuMemberBonusRangeList.add(cuMemberBonusRange);
    }

    private void calculateLeXueRangeBonus(String settleTableName, Map<String, RangeDTO> rangeDtoMap, BonusConfigDTO bonusConfigDTO,
                                          Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap, LeXueRangeParam leXueRangeParam,
                                          List<SaOrderExt> specialOrderList, List<CuMemberBonusRange> cuMemberBonusRangeList) {
        // 计算乐学易考特殊复购极差，找第一个vip，svip，找到有代金券，代金券-1，奖金给金额，第一个是svip后，就不给
        if (specialOrderList.size() > 0) {
            // 查找订单血缘
            Set<Long> expandMemberSet = specialOrderList.stream().distinct().map(SaOrderExt::getPkMember).collect(Collectors.toSet());
            List<CuMemberSettleExt> specialMemberSettleExtList = cuMemberTreeService.batchQueryCuMemberSettleParentForSpecial(settleTableName, expandMemberSet);
            // 重算的时候首先先把数量还回来，在重新计算，查询订单扣除的记录
            List<AcPickMemberLog> acPickMemberLogList = acPickServiceApi.queryPickMemberLog(specialOrderList).getData();
            Map<String, AcPickMemberLog> pickMemberLogMap = new HashMap<>(ComputeUtil.mapInitCapacity(acPickMemberLogList.size()));
            if (CollectionUtil.isNotEmpty(acPickMemberLogList)) {
                acPickMemberLogList.forEach(acPickMemberLog ->
                        pickMemberLogMap.put(acPickMemberLog.getPkMember().toString() + acPickMemberLog.getPkId(), acPickMemberLog));
                leXueRangeParam.setAcPickMemberLogList(acPickMemberLogList);
            }
            // 初始化父类网体的值
            Map<Long, CuMemberSettleExt> specialmeberSettleExtMap = new HashMap<>();
            getMemberSettle(specialmeberSettleExtMap, specialMemberSettleExtList);
            // 还原数量
            specialmeberSettleExtMap.forEach((pkMember, cumemberSettleExt) -> {
                String key = pkMember.toString() + cumemberSettleExt.getApPkId();
                if (pickMemberLogMap.containsKey(key)) {
                    cumemberSettleExt.setUsableQuantity(cumemberSettleExt.getUsableQuantity() + pickMemberLogMap.get(key).getPickQuantity());
                }
            });
            List<AcPickExt> acPickExtList = new ArrayList<>();
            for (SaOrderExt saOrderExt : specialOrderList) {
                for (SaOrderItemsExt saOrderItemsExt : saOrderExt.getOrderItemsExtList()) {
                    if (EYesNo.YES.getIntValue() == saOrderItemsExt.getIsGift() || ComputeUtil.compareEqual(saOrderItemsExt.getPrice(), BigDecimal.ZERO) ||
                            ComputeUtil.compareEqual(saOrderItemsExt.getAchievement(), BigDecimal.ZERO)) {
                        continue;
                    }
                    CuMemberSettleExt sourceMemberSettleExt = specialmeberSettleExtMap.get(saOrderExt.getPkMember());
                    if (sourceMemberSettleExt == null) {
                        continue;
                    }
                    // 找不到父级了或者找到第一个svip直接跳出
                    boolean flag = Boolean.TRUE;
                    Long pkParent = sourceMemberSettleExt.getPkParent();
                    int gradeValue = sourceMemberSettleExt.getGradeValue();
                    while (true) {
                        CuMemberSettleExt targetMemberSettleExt = specialmeberSettleExtMap.get(pkParent);
                        if (targetMemberSettleExt == null) {
                            break;
                        }
                        if (gradeValue < targetMemberSettleExt.getGradeValue() && EGrade.VIP.getValue() == targetMemberSettleExt.getGradeValue() && flag) {
                            // 获得奖金
                            // 验证是否有奖金
                            if (validatePkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.REPURCHASE_RANGE_INCOME.getValue())) {
                                pkParent = targetMemberSettleExt.getPkParent();
                                sourceMemberSettleExt = targetMemberSettleExt;
                                continue;
                            }
                            AcPickExt acPickExt = calculateRangeBonus(rangeDtoMap, bonusConfigDTO, period, cuMemberBonusMap, saOrderExt, saOrderItemsExt, sourceMemberSettleExt,
                                    targetMemberSettleExt, cuMemberBonusRangeList);
                            if (acPickExt != null) {
                                acPickExtList.add(acPickExt);
                            }
                            pkParent = targetMemberSettleExt.getPkParent();
                            sourceMemberSettleExt = targetMemberSettleExt;
                            flag = Boolean.FALSE;
                        } else if (gradeValue < targetMemberSettleExt.getGradeValue() && EGrade.S_VIP.getValue() == targetMemberSettleExt.getGradeValue()) {
                            // 获得奖金
                            if (validatePkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.REPURCHASE_RANGE_INCOME.getValue())) {
                                break;
                            }
                            AcPickExt acPickExt = calculateRangeBonus(rangeDtoMap, bonusConfigDTO, period, cuMemberBonusMap, saOrderExt, saOrderItemsExt, sourceMemberSettleExt,
                                    targetMemberSettleExt, cuMemberBonusRangeList);
                            if (acPickExt != null) {
                                acPickExtList.add(acPickExt);
                            }
                            break;
                        } else {
                            pkParent = targetMemberSettleExt.getPkParent();
                            sourceMemberSettleExt = targetMemberSettleExt;
                        }
                    }
                }
            }
            if (acPickExtList.size() > 0) {
                leXueRangeParam.setAcPickExtList(acPickExtList);
            }
        }
    }

    private AcPickExt calculateRangeBonus(Map<String, RangeDTO> rangeDtoMap, BonusConfigDTO bonusConfigDTO, Integer period,
                                          Map<Long, CuMemberBonus> cuMemberBonusMap, SaOrderExt saOrderExt, SaOrderItemsExt saOrderItemsExt,
                                          CuMemberSettleExt sourceMemberSettleExt,
                                          CuMemberSettleExt targetMemberSettleExt, List<CuMemberBonusRange> cuMemberBonusRangeList) {
        if (targetMemberSettleExt.getUsableQuantity() != null && targetMemberSettleExt.getUsableQuantity() > 0) {
            CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, targetMemberSettleExt);
            CuMemberBonusRange cuMemberBonusRange = getCuMemberBonusRange(bonusConfigDTO, saOrderExt, targetMemberSettleExt, cuMemberBonus);
            cuMemberBonusRange.setCalAchieve(saOrderItemsExt.getAchievement());
            cuMemberBonusRange.setCalValue(BigDecimal.ZERO);
            cuMemberBonusRange.setPretaxIncome(ComputeUtil.computeMultiply(BigDecimal.valueOf(saOrderItemsExt.getWaresQuantity()),
                    ComputeUtil.computeDivide(targetMemberSettleExt.getRetailPrice(), BigDecimal.ONE)));
            String key = targetMemberSettleExt.getPkSettleCountry().toString() + ERangeType.GRADE.getValue() + targetMemberSettleExt.getPkGrade();
            cuMemberBonusRange.setPkRange(rangeDtoMap.get(key).getPkRange());
            String beforeKey = targetMemberSettleExt.getPkSettleCountry().toString() + ERangeType.GRADE.getValue() + targetMemberSettleExt.getPkGrade();
            cuMemberBonusRange.setPkBeforeRange(rangeDtoMap.get(beforeKey).getPkRange());
            cuMemberBonusRange.setIncomeRatio(ComputeUtil.computeDivide(ComputeUtil.computeMultiply(cuMemberBonusRange.getPretaxIncome(), BigDecimal.ONE), saOrderExt.getOrderAmount()));
            cuMemberBonusRange.setPkOrderItems(0L);
            // 计算比例
            cuMemberBonusRange.setPkBonusItems(getPkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.REPURCHASE_RANGE_INCOME.getValue()));
            // 收益占比
            BdBonusDeduct bdBonusDeduct = setBonusDetailDeduct(bonusConfigDTO, cuMemberBonusRange, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
            xueRangeRemark(saOrderExt, cuMemberBonusRange, sourceMemberSettleExt, targetMemberSettleExt);
            if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusRange.getIncomeStatus()) {
                cuMemberBonus.setRepurRangeIncome(ComputeUtil.computeAdd(cuMemberBonus.getRepurRangeIncome(), cuMemberBonusRange.getPretaxIncome()));
                setPurchaseTotal(bdBonusDeduct, cuMemberBonus, cuMemberBonusRange);
            }
            cuMemberBonusRangeList.add(cuMemberBonusRange);
            targetMemberSettleExt.setUsableQuantity(targetMemberSettleExt.getUsableQuantity() - saOrderItemsExt.getWaresQuantity());
            AcPickExt acPickExt = new AcPickExt();
            acPickExt.setPkId(targetMemberSettleExt.getApPkId());
            acPickExt.setPkMember(targetMemberSettleExt.getPkMember());
            acPickExt.setOrderCode(saOrderExt.getOrderCode());
            acPickExt.setPickType(EPickType.COUPON.getValue());
            acPickExt.setUsedQuantity(saOrderItemsExt.getWaresQuantity());
            acPickExt.setPkBaseConfig(0L);
            acPickExt.setPkDataId(targetMemberSettleExt.getPkDataId());
            return acPickExt;
        }
        return null;
    }

    /**
     * 根据极差获取会员
     */
    private CuMemberSettleExt getCuMemberTree(Map<Long, CuMemberSettleExt> cuMemberTreeMap, Map<String, RangeDTO> rangeDtoMap, Map<String, RangeDTO> rangePkDtoMap,
                                              Map<String, BdAwards> awardsMap, BonusConfigDTO bonusConfigDTO, CuMemberSettleExt parentMemberTree,
                                              SaOrderRangeDto saOrderRangeDto, Map<Long, CuAwardsControl> cuAwardsControlMap, Map<Long, CuMemberAssess> cuMemberAssessMap, SaOrderExt saOrderExt) {
        if (parentMemberTree == null) {
            return null;
        }
        RangeDTO rangeDto = getRangeDto(rangeDtoMap, awardsMap, parentMemberTree, cuAwardsControlMap);
        RangeDTO orderRangeDto = rangePkDtoMap.get(saOrderRangeDto.getPkCountry().toString() + saOrderRangeDto.getPkRange());
        if (rangeDto == null || orderRangeDto == null) {
            return null;
        }
        // 需要复购考核，复购考核未通过的没有奖金
        CuMemberAssess cuMemberAssess = cuMemberAssessMap.get(parentMemberTree.getPkMember());
        boolean assessFlag = EYesNo.NO.getIntValue() == parentMemberTree.getPurchaseStatus() && cuMemberAssess == null;
        // 海粉没有复购极差
        if (Objects.equals(MagicNumberConstants.CHINA_HA_FUN, parentMemberTree.getPkGrade()) || ECategory.NORMAL.getValue() != parentMemberTree.getCategory() || EAccountStatus.STOP_INCOME.getValue() == parentMemberTree.getAccountStatus() ||
                validatePkBonusItems(bonusConfigDTO, parentMemberTree.getPkSettleCountry(), EBonusItems.REPURCHASE_RANGE_INCOME.getValue()) ||
                rangeDto.getRangeValue() < orderRangeDto.getRangeValue() || assessFlag) {
            parentMemberTree = cuMemberTreeMap.get(parentMemberTree.getPkParent());
            if (parentMemberTree == null) {
                return null;
            }
            parentMemberTree = getCuMemberTree(cuMemberTreeMap, rangeDtoMap, rangePkDtoMap, awardsMap, bonusConfigDTO,
                    parentMemberTree, saOrderRangeDto, cuAwardsControlMap, cuMemberAssessMap, saOrderExt);
        }
        return parentMemberTree;
    }

    /**
     * 根据极差获取会员
     */
    private CuMemberSettleExt getRangeCuMemberTree(Map<Long, CuMemberSettleExt> cuMemberTreeMap, Map<String, RangeDTO> rangeDtoMap, Map<String, BdAwards> awardsMap,
                                                   BonusConfigDTO bonusConfigDTO, CuMemberSettleExt sourceMemberTree, CuMemberSettleExt parentMemberTree,
                                                   Map<Long, CuAwardsControl> cuAwardsControlMap, Map<Long, CuMemberAssess> cuMemberAssessMap) {
        RangeDTO parentRangeDto = getRangeDto(rangeDtoMap, awardsMap, parentMemberTree, cuAwardsControlMap);
        RangeDTO sourceRangeDto = getRangeDto(rangeDtoMap, awardsMap, sourceMemberTree, cuAwardsControlMap);
        if (parentRangeDto == null || sourceRangeDto == null || parentMemberTree == null) {
            return null;
        }
        // 需要复购考核，复购考核未通过的没有奖金
        CuMemberAssess cuMemberAssess = cuMemberAssessMap.get(parentMemberTree.getPkMember());
        boolean assessFlag = EYesNo.NO.getIntValue() == parentMemberTree.getPurchaseStatus() && cuMemberAssess == null;
        if (ECategory.NORMAL.getValue() != parentMemberTree.getCategory() || EAccountStatus.STOP_INCOME.getValue() == parentMemberTree.getAccountStatus() ||
                validatePkBonusItems(bonusConfigDTO, parentMemberTree.getPkSettleCountry(), EBonusItems.REPURCHASE_RANGE_INCOME.getValue()) ||
                parentRangeDto.getRangeValue() < sourceRangeDto.getRangeValue() || assessFlag) {
            parentMemberTree = cuMemberTreeMap.get(parentMemberTree.getPkParent());
            if (parentMemberTree == null) {
                return null;
            }
            parentMemberTree = getRangeCuMemberTree(cuMemberTreeMap, rangeDtoMap, awardsMap, bonusConfigDTO, sourceMemberTree, parentMemberTree, cuAwardsControlMap, cuMemberAssessMap);
        }
        return parentMemberTree;
    }

    private CuMemberSettleExt getSpecialRangeCuMemberTree(Map<Long, CuMemberSettleExt> cuMemberTreeMap,
                                                          BonusConfigDTO bonusConfigDTO, CuMemberSettleExt sourceMemberTree, CuMemberSettleExt parentMemberTree,
                                                          Map<Long, CuMemberAssess> cuMemberAssessMap, int pkGrade) {
        // 需要复购考核，复购考核未通过的没有奖金
//        CuMemberAssess cuMemberAssess = cuMemberAssessMap.get(parentMemberTree.getPkMember());
//        boolean assessFlag = EYesNo.NO.getIntValue() == parentMemberTree.getPurchaseStatus() && cuMemberAssess == null;
        if (ECategory.NORMAL.getValue() != parentMemberTree.getCategory() || EAccountStatus.STOP_INCOME.getValue() == parentMemberTree.getAccountStatus() ||
                validatePkBonusItems(bonusConfigDTO, parentMemberTree.getPkSettleCountry(), EBonusItems.REPURCHASE_RANGE_INCOME.getValue()) ||
                parentMemberTree.getPkGrade() <= pkGrade || parentMemberTree.getPkGrade() <= sourceMemberTree.getPkGrade()) {
//            if(parentMemberTree.getPkAwards() > 1 && ECategory.NORMAL.getValue() == parentMemberTree.getCategory()) {
//                return parentMemberTree;
//            }
            parentMemberTree = cuMemberTreeMap.get(parentMemberTree.getPkParent());
            if (parentMemberTree == null) {
                return null;
            }
            parentMemberTree = getSpecialRangeCuMemberTree(cuMemberTreeMap, bonusConfigDTO, sourceMemberTree, parentMemberTree, cuMemberAssessMap, pkGrade);
        }
        return parentMemberTree;
    }

    /**
     * @param period                  期间
     * @param cuMemberBonusMap        奖金汇总
     * @param cuMemberBonusExpandList 拓展奖奖金明细
     * @param saOrderExt              订单
     * @param touchNum                碰次
     * @param i                       当前碰次
     * @param sourceMemberSettleExt   提供奖金的人
     * @param targetMemberSettleExt   获得奖金的人
     * @param bdGrade                 等级对应的参数
     * @param countryExpandRatio      4-n时计算国家对应的参数
     * @Description: 计算量碰
     * @return: void
     * @Author: sui q
     * @Date: 2022/11/16 15:51
     */
    private void calculateRepurchaseExpandBonusByAchieve(Integer period, Map<Long, CuMemberBonus> weekMemberBonusMap, Map<Long, CuMemberBonus> cuMemberBonusMap,
                                                         BonusConfigDTO bonusConfigDTO, Map<String, String> systemConfigMap, List<CuMemberBonusExpand> cuMemberBonusExpandList, SaOrderExt saOrderExt, int touchNum, int i,
                                                         CuMemberSettleExt sourceMemberSettleExt, CuMemberSettleExt targetMemberSettleExt, BdGrade bdGrade, BigDecimal countryExpandRatio, Integer pkBonusItems,
                                                         Map<Long, CuMemberAssess> cuMemberAssessMap) {
        // 验证封顶方式 1=日 2=周，若是周的话需要获取 周奖金
        BigDecimal weekRepExpandIncome = BigDecimal.ZERO;
        if (systemConfigMap.containsKey(targetMemberSettleExt.getPkSettleCountry() + ESystemConfig.EXPAND_CAP_METHOD.getKey())) {
            String expandCapMethod = systemConfigMap.get(targetMemberSettleExt.getPkSettleCountry() + ESystemConfig.EXPAND_CAP_METHOD.getKey());
            if (Objects.equals(expandCapMethod, EExpandCapMethod.WEEK_SETTLE.toString()) &&
                    weekMemberBonusMap.containsKey(targetMemberSettleExt.getPkMember())) {
                weekRepExpandIncome = weekMemberBonusMap.get(targetMemberSettleExt.getPkMember()).getRepurExpandIncome();
            }
        }
        // 国内使用首购比例首购封顶
        BigDecimal gradeExpandRatio = bdGrade.getRepurchaseRatio();
        if (targetMemberSettleExt.getPkSettleCountry().equals(CountryConstants.CHINA_COUNTRY)) {
            gradeExpandRatio = bdGrade.getExpandRatio();
            // 验证受益人等级
            CuMember cuMember = cuMemberService.getMember(targetMemberSettleExt.getMemberCode());
            if (cuMember != null && DateUtils.compareDate(DateUtils.parseStringToDate("2024-09-11"), cuMember.getPayTime()) > 0) {
                if (targetMemberSettleExt.getGradeValue() == EGrade.VIP.getValue()) {
                    if (ComputeUtil.compareValue(countryExpandRatio, BigDecimal.valueOf(11)) && (i <= 3 || touchNum <= 10)) {
                        countryExpandRatio = BigDecimal.valueOf(13);
                    }
                } else if (targetMemberSettleExt.getGradeValue() == EGrade.MAKER.getValue()) {
                    if (ComputeUtil.compareValue(countryExpandRatio, BigDecimal.valueOf(10)) && (i <= 3 || touchNum <= 10)) {
                        countryExpandRatio = BigDecimal.valueOf(11);
                    }
                }
            }
        }
        // 获取等级封顶值
        CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, targetMemberSettleExt);
        BigDecimal calAchieve;
        if (targetMemberSettleExt.getPkSettleCountry().equals(CountryConstants.CHINA_COUNTRY)) {
            calAchieve = ComputeUtil.obtainMinAchieve(targetMemberSettleExt.getABalance(), targetMemberSettleExt.getBBalance());
        } else {
            calAchieve = ComputeUtil.obtainMinAchieve(targetMemberSettleExt.getRepABalance(), targetMemberSettleExt.getRepBBalance());
        }
        CuMemberBonusExpand cuMemberBonusExpand = getCuMemberBonusExpand(saOrderExt, touchNum, i, targetMemberSettleExt, countryExpandRatio, cuMemberBonus, calAchieve, pkBonusItems, BigDecimal.ZERO);
        if (targetMemberSettleExt.getPkSettleCountry().equals(CountryConstants.CHINA_COUNTRY)) {
            targetMemberSettleExt.setABalance(ComputeUtil.computeSubtract(targetMemberSettleExt.getABalance(), calAchieve));
            targetMemberSettleExt.setBBalance(ComputeUtil.computeSubtract(targetMemberSettleExt.getBBalance(), calAchieve));
        } else {
            // 扣减a、b区结余
            targetMemberSettleExt.setRepABalance(ComputeUtil.computeSubtractAbs(targetMemberSettleExt.getRepABalance(), calAchieve));
            targetMemberSettleExt.setRepBBalance(ComputeUtil.computeSubtractAbs(targetMemberSettleExt.getRepBBalance(), calAchieve));
        }
        // 计算收益 先计算出会员最大的收益
        BigDecimal maxIncome = ComputeUtil.computeBonusMultiply(cuMemberBonusExpand.getCalAchieve(), gradeExpandRatio);
        BigDecimal actualIncome = ComputeUtil.computeBonusMultiply(cuMemberBonusExpand.getCalAchieve(), countryExpandRatio);
        // 获取等级对应的封顶值
        BigDecimal expandIncome = ComputeUtil.computeAdd(ComputeUtil.computeAdd(cuMemberBonus.getRepurExpandIncome(), actualIncome), weekRepExpandIncome);
        if (targetMemberSettleExt.getPkSettleCountry().equals(CountryConstants.CHINA_COUNTRY)) {
            // 获取等级对应的封顶值
            expandIncome = ComputeUtil.computeAdd(ComputeUtil.computeAdd(cuMemberBonus.getExpandIncome(), expandIncome), weekRepExpandIncome);
            // 判断收益 是否超过封顶值，超过的话变为封顶值
            // 判断是否烧伤，判断原奖金是否>=封顶值,奖金到了就是烧伤
            // 验证会员状态 封停状态跟冻结都正常计算奖金，封停的账号发放的时候奖金发放为0，冻结的账号正常发放，不能登录
            packageCuMemberBonusExpand(cuMemberBonusExpand, cuMemberBonus, bdGrade.getExpandCapping(), maxIncome, actualIncome);
            if (ComputeUtil.compareValue(ComputeUtil.computeAdd(cuMemberBonus.getExpandIncome(), cuMemberBonus.getRepurExpandIncome()), bdGrade.getExpandCapping())) {
                if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusExpand.getIncomeStatus()) {
                    cuMemberBonusExpand.setIncomeStatus(EBonusIncomeStatus.FIRE_BURN.getValue());
                }
                expandIncome = ComputeUtil.computeSubtract(bdGrade.getExpandCapping(), ComputeUtil.computeAdd(cuMemberBonus.getExpandIncome(), cuMemberBonus.getRepurExpandIncome()));
            } else if (ComputeUtil.compareGreaterThan(expandIncome, bdGrade.getExpandCapping())) {
                BigDecimal realIncome = ComputeUtil.computeSubtract(ComputeUtil.computeSubtract(bdGrade.getExpandCapping(), weekRepExpandIncome), ComputeUtil.computeAdd(cuMemberBonus.getExpandIncome(), cuMemberBonus.getRepurExpandIncome()));
                //存在一半烧伤一半正常的值，拆2条
                // 拓展明细表 进行修改
                packageCuMemberBonusExpand(cuMemberBonusExpand, cuMemberBonus, bdGrade.getExpandCapping(), realIncome, realIncome);
                CuMemberBonusExpand fireCuMemberBonusExpand = BeanUtil.copyProperties(cuMemberBonusExpand, CuMemberBonusExpand.class);
                BigDecimal fireIncome = ComputeUtil.computeSubtract(maxIncome, realIncome);
                if (EBonusIncomeStatus.NORMAL.getValue() == fireCuMemberBonusExpand.getIncomeStatus()) {
                    fireCuMemberBonusExpand.setIncomeStatus(EBonusIncomeStatus.FIRE_BURN.getValue());
                }
                packageCuMemberBonusExpand(fireCuMemberBonusExpand, cuMemberBonus, bdGrade.getExpandCapping(), fireIncome, fireIncome);
                // 收益占比
                setBonusDetailDeduct(bonusConfigDTO, fireCuMemberBonusExpand, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
                expandRemark(saOrderExt, fireCuMemberBonusExpand, sourceMemberSettleExt, targetMemberSettleExt);
                cuMemberBonusExpandList.add(fireCuMemberBonusExpand);
                // 拓展收益取封顶最大值
                expandIncome = ComputeUtil.computeSubtract(bdGrade.getExpandCapping(), weekRepExpandIncome);
            }
        } else {
            // 判断收益 是否超过封顶值，超过的话变为封顶值
            // 判断是否烧伤，判断原奖金是否>=封顶值,奖金到了就是烧伤
            packageCuMemberBonusRepurchaseExpand(cuMemberBonusExpand, cuMemberBonus, bdGrade.getRepurchaseCapping(), maxIncome, actualIncome);
            if (ComputeUtil.compareValue(ComputeUtil.computeAdd(cuMemberBonus.getRepurExpandIncome(), weekRepExpandIncome), bdGrade.getRepurchaseCapping())) {
                if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusExpand.getIncomeStatus()) {
                    cuMemberBonusExpand.setIncomeStatus(EBonusIncomeStatus.FIRE_BURN.getValue());
                }
                expandIncome = ComputeUtil.computeSubtract(bdGrade.getRepurchaseCapping(), weekRepExpandIncome);
            } else if (ComputeUtil.compareGreaterThan(expandIncome, bdGrade.getRepurchaseCapping())) {
                BigDecimal realIncome = ComputeUtil.computeSubtract(ComputeUtil.computeSubtract(bdGrade.getRepurchaseCapping(), weekRepExpandIncome), cuMemberBonus.getRepurExpandIncome());
                //存在一半烧伤一半正常的值，拆2条
                // 拓展明细表 进行修改
                packageCuMemberBonusRepurchaseExpand(cuMemberBonusExpand, cuMemberBonus, bdGrade.getRepurchaseCapping(), realIncome, realIncome);
                CuMemberBonusExpand fireCuMemberBonusExpand = BeanUtil.copyProperties(cuMemberBonusExpand, CuMemberBonusExpand.class);
                BigDecimal fireIncome = ComputeUtil.computeSubtract(maxIncome, realIncome);
                if (EBonusIncomeStatus.NORMAL.getValue() == fireCuMemberBonusExpand.getIncomeStatus()) {
                    fireCuMemberBonusExpand.setIncomeStatus(EBonusIncomeStatus.FIRE_BURN.getValue());
                }
                packageCuMemberBonusRepurchaseExpand(fireCuMemberBonusExpand, cuMemberBonus, bdGrade.getRepurchaseCapping(), fireIncome, fireIncome);
                // 收益占比
                setBonusDetailDeduct(bonusConfigDTO, fireCuMemberBonusExpand, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
                repurchaseExpandRemark(saOrderExt, fireCuMemberBonusExpand, sourceMemberSettleExt, targetMemberSettleExt);
                cuMemberBonusExpandList.add(fireCuMemberBonusExpand);
                // 拓展收益取封顶最大值
                expandIncome = ComputeUtil.computeSubtract(bdGrade.getRepurchaseCapping(), weekRepExpandIncome);
            }
        }
        BdBonusDeduct bdBonusDeduct = setBonusDetailDeduct(bonusConfigDTO, cuMemberBonusExpand, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
        // 正常状态的辅导奖需要判断复购考核，复购考核不通过的状态变为烧伤
        if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusExpand.getIncomeStatus()
                && EYesNo.NO.getIntValue() == targetMemberSettleExt.getPurchaseStatus()) {
            // 需要复购考核的
            CuMemberAssess cuMemberAssess = cuMemberAssessMap.get(targetMemberSettleExt.getPkMember());
            if (cuMemberAssess == null) {
                cuMemberBonusExpand.setIncomeStatus(EBonusIncomeStatus.FIRE_BURN.getValue());
            }
        }
        if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusExpand.getIncomeStatus()) {
            if (targetMemberSettleExt.getPkSettleCountry().equals(CountryConstants.CHINA_COUNTRY)) {
                cuMemberBonus.setRepurExpandIncome(ComputeUtil.computeSubtract(expandIncome, cuMemberBonus.getExpandIncome()));
                setPurchaseTotal(bdBonusDeduct, cuMemberBonus, cuMemberBonusExpand);
            } else {
                cuMemberBonus.setRepurExpandIncome(ComputeUtil.computeSubtract(expandIncome, weekRepExpandIncome));
                setPurchaseTotal(bdBonusDeduct, cuMemberBonus, cuMemberBonusExpand);
            }
        }
        repurchaseExpandRemark(saOrderExt, cuMemberBonusExpand, sourceMemberSettleExt, targetMemberSettleExt);
        cuMemberBonusExpandList.add(cuMemberBonusExpand);
    }


    private CuMemberBonusRange getCuMemberBonusRange(BonusConfigDTO bonusConfigDTO, SaOrderExt saOrderExt, CuMemberSettleExt cuMemberSettleExt, CuMemberBonus cuMemberBonus) {
        CuMemberBonusRange cuMemberBonusRange = CuMemberBonusRange.builder()
                .pkOrder(saOrderExt.getPkId())
                .pkBonus(cuMemberBonus.getPkId())
                .pkBonusItems(getPkBonusItems(bonusConfigDTO, cuMemberSettleExt.getPkSettleCountry(), EBonusItems.REPURCHASE_RANGE_INCOME.getValue()))
                // 正常还是烧伤
                .incomeStatus(cuMemberSettleExt.getIncomeStatus())
                .calType(ECalType.VALUE.getValue())
                .calAchieve(saOrderExt.getOrderAmount())
                .calValue(BigDecimal.ZERO)
                .incomeRatio(BigDecimal.ZERO)
                .incomeDialRatio(BigDecimal.ZERO)
                .orderDialRatio(BigDecimal.ZERO)
                .build();
        cuMemberBonusRange.setPkCountry(cuMemberSettleExt.getPkSettleCountry());
        cuMemberBonusRange.setPkCreator(MagicNumberConstants.PK_ADMIN);
        return cuMemberBonusRange;
    }
}
