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

import com.hzs.common.core.constant.BonusFieldConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.reflect.ReflectUtils;
import com.hzs.common.domain.bonus.BdBonusDeduct;
import com.hzs.common.domain.member.base.CuAwardsControl;
import com.hzs.common.domain.member.bonus.CuMemberBonus;
import com.hzs.common.domain.member.bonus.CuMemberBonusDetail;
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.system.config.dto.BonusConfigDTO;
import com.hzs.system.config.dto.BonusHiPinkDTO;
import com.hzs.system.config.dto.RangeDTO;
import org.springframework.stereotype.Component;

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

/**
 * @Description: 计算嗨粉订单
 * @Author: sui q
 * @Time: 2023/1/4 13:38
 * @Classname: BonusSettleHiFunHandle
 * @PackageName: com.hzs.member.bonus.service.impl
 */
@Component
public class BonusSettleHiFunHandle extends BonusSettleHandle {

    /**
     * @param period           期间
     * @param bonusConfigDTO   参数
     * @param hiFunSaOrderList 复购订单
     * @param cuMemberBonusMap 所有计算出来的奖金汇总
     * @Description: 计算嗨粉奖金
     * @return: void
     * @Author: sui q
     * @Date: 2022/11/10 14:17
     */
    List<CuMemberBonusDetail> calculateHiFunPushBonus(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, String settleTableName, Map<String, RangeDTO> rangeDtoMap, Map<String, BdAwards> awardsMap,
                                                      BonusConfigDTO bonusConfigDTO, List<SaOrderExt> hiFunSaOrderList, Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap, Map<Long, CuAwardsControl> cuAwardsControlMap) {
        // 根据订单明细开始计算奖金，计算嗨粉推荐奖
        List<CuMemberBonusDetail> cuMemberBonusDetailList = new ArrayList<>();
        // cuMemberSettleExtMap 需要含自己
        getMemberParentBySaOrder(cuMemberSettleExtMap, settleTableName, hiFunSaOrderList);
        // 拆分为2种，一种是嗨粉，一种是1000pv的，奖金不一样
        List<SaOrderExt> oldHaiFunOrderList = new ArrayList<>();
//        List<SaOrderExt> newHaiFunOrderList = new ArrayList<>();
        for (SaOrderExt saOrderExt : hiFunSaOrderList) {
            if (EOrderTypeExtend.NORMAL.getValue() == saOrderExt.getOrderTypeExtend()) {
                oldHaiFunOrderList.add(saOrderExt);
            }
        }
        // 原来413嗨粉推荐海粉极差收益
        oldHaiFunOrderList.forEach(saOrderExt -> {
            List<CuMemberBonusDetail> bonusDetailList = calculateHiFunPushBonusOne(cuMemberSettleExtMap, rangeDtoMap, awardsMap, bonusConfigDTO, period, cuMemberBonusMap, saOrderExt, cuAwardsControlMap);
            if (bonusDetailList.size() > 0) {
                cuMemberBonusDetailList.addAll(bonusDetailList);
            }
        });
//        // 新5680 推荐海粉极差收益
//        newHaiFunOrderList.forEach(saOrderExt -> {
//            // 海粉极差
//            List<CuMemberBonusDetail> bonusDetailList = calculateNewHiFunPushBonusOne(cuMemberSettleExtMap, rangeDtoMap, awardsMap, bonusConfigDTO, period, cuMemberBonusMap, saOrderExt, cuAwardsControlMap);
//            if (bonusDetailList.size() > 0) {
//                cuMemberBonusDetailList.addAll(bonusDetailList);
//            }
//            if(saOrderExt.getPkMember().equals(saOrderExt.getPkCreator())){
//                return;
//            }
//            // 给注册人复购券
//            // 获得奖金
//            CuMemberSettleExt sourceMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
//            CuMemberSettleExt targetMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkCreator());
//            if (targetMemberSettleExt == null || validatePkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.REPURCHASE_COUPONS.getValue())) {
//                return;
//            }
//            CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, targetMemberSettleExt);
//            CuMemberBonusDetail cuMemberBonusDetail = getCuMemberBonusDetail(saOrderExt, targetMemberSettleExt, cuMemberBonus);
//            // 计算类型
//            cuMemberBonusDetail.setCalType(ECalType.VALUE.getValue());
//            // 计算值
//            cuMemberBonusDetail.setCalValue(BigDecimal.ZERO);
//            cuMemberBonusDetail.setPkBonusItems(getPkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.REPURCHASE_COUPONS.getValue()));
//            cuMemberBonusDetail.setPretaxIncome(BigDecimal.valueOf(15.46875));
//            BdBonusDeduct bdBonusDeduct = setBonusDetailDeduct(bonusConfigDTO, cuMemberBonusDetail, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
//            if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusDetail.getIncomeStatus()) {
//                cuMemberBonus.setRepurCoupon(ComputeUtil.computeAdd(cuMemberBonus.getRepurCoupon(), cuMemberBonusDetail.getPretaxIncome()));
//                setRepurchaseCouponTotal(bdBonusDeduct, cuMemberBonus, cuMemberBonusDetail);
//            }
//            repurchaseCouponsRemark(saOrderExt, cuMemberBonusDetail, sourceMemberSettleExt, targetMemberSettleExt);
//            cuMemberBonusDetailList.add(cuMemberBonusDetail);
//        });
        return cuMemberBonusDetailList;
    }

    /**
     * @param settleTableName  结算表
     * @param period           期间
     * @param bonusConfigDTO   参数
     * @param haiFunOrderList  订单列表
     * @param cuMemberBonusMap 所有计算出来的奖金汇总
     * @Description: 计算报单服务费
     * @return: List<CuMemberBonusCoach>
     * @Author: sui q
     * @Date: 2022/11/17 14:14
     */
    List<CuMemberBonusDetail> calculateHaiFunBonus(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, String settleTableName, BonusConfigDTO bonusConfigDTO,
                                                   List<SaOrderExt> haiFunOrderList, Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap) {
        List<CuMemberBonusDetail> cuMemberBonusDetailList = new ArrayList<>();
        // 先过滤数据，验证报单人是否在网体列表中，不在需要从数据库中查询
        Set<Long> haiFunMemberSet = new HashSet<>();
        haiFunOrderList.forEach(saOrderExt -> {
            CuMemberSettleExt cuMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
            if (cuMemberSettleExt == null || cuMemberSettleExtMap.get(cuMemberSettleExt.getPkParent()) == null) {
                haiFunMemberSet.add(saOrderExt.getPkMember());
            }
        });
        if (haiFunMemberSet.size() > 0) {
            List<CuMemberSettleExt> cuMemberSettleExtList = iCuMemberTreeService.batchQueryMemberSettleParentByMemberPk(settleTableName, haiFunMemberSet);
            // 初始化父类网体的值
            getMemberSettle(cuMemberSettleExtMap, cuMemberSettleExtList);
        }
        // 计算海粉推荐
        for (SaOrderExt saOrderExt : haiFunOrderList) {
            List<CuMemberBonusDetail> cuMemberBonusDetails = calculateHaiFunBonusOne(cuMemberSettleExtMap, bonusConfigDTO, period, cuMemberBonusMap, saOrderExt);
            if (cuMemberBonusDetails.size() > 0) {
                cuMemberBonusDetailList.addAll(cuMemberBonusDetails);
            }
        }
        // 查询会员
        return cuMemberBonusDetailList;
    }


    /**
     * @Description: 计算海粉推荐奖
     * @return: CuMemberBonusDetail
     * @Author: sui q
     * @Date: 2023/2/8 17:22
     */
    List<CuMemberBonusDetail> calculateHaiFunBonusOne(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, BonusConfigDTO bonusConfigDTO, Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap, SaOrderExt saOrderExt) {
        List<CuMemberBonusDetail> cuMemberBonusDetailList = new ArrayList<>();
        CuMemberSettleExt sourceMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
        if (sourceMemberSettleExt == null) {
            return cuMemberBonusDetailList;
        }
        CuMemberSettleExt targetMemberSettleExt = cuMemberSettleExtMap.get(sourceMemberSettleExt.getPkParent());
        if (targetMemberSettleExt == null || ECategory.NORMAL.getValue() != targetMemberSettleExt.getCategory() ||
                EAccountStatus.STOP_INCOME.getValue() == targetMemberSettleExt.getAccountStatus()) {
            return cuMemberBonusDetailList;
        }
        // 验证是否有奖金
//        if (validatePkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.HAI_DOU_INCOME.getValue())) {
//            return cuMemberBonusDetailList;
//        }
        for (SaOrderItemsExt saOrderItemsExt : saOrderExt.getOrderItemsExtList()) {
            CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, targetMemberSettleExt);
            // 海粉直推奖金明细
            CuMemberBonusDetail cuMemberBonusDetail = getCuMemberBonusDetail(saOrderExt, targetMemberSettleExt, cuMemberBonus);
            // 计算比例
            cuMemberBonusDetail.setCalValue(BigDecimal.ZERO);
//            cuMemberBonusDetail.setPkBonusItems(getPkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.HAI_DOU_INCOME.getValue()));
            cuMemberBonusDetail.setPretaxIncome(saOrderItemsExt.getShareAchieve());
            // 收益占比
            BdBonusDeduct bdBonusDeduct = setBonusDetailDeduct(bonusConfigDTO, cuMemberBonusDetail, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
            haiFunRemark(saOrderExt, cuMemberBonusDetail, sourceMemberSettleExt, targetMemberSettleExt);
            if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusDetail.getIncomeStatus()) {
                cuMemberBonus.setHaiFunIncome(ComputeUtil.computeAdd(cuMemberBonus.getHaiFunIncome(), cuMemberBonusDetail.getPretaxIncome()));
                setBonusDeduct(bdBonusDeduct, cuMemberBonus, cuMemberBonusDetail);
                BigDecimal realIncome = ReflectUtils.invokeGetter(cuMemberBonusDetail, BonusFieldConstants.REAL_INCOME);
                cuMemberBonus.setRealIncomeTotal(ComputeUtil.computeAdd(cuMemberBonus.getRealIncomeTotal(), realIncome));
            }
            cuMemberBonusDetailList.add(cuMemberBonusDetail);
        }

        return cuMemberBonusDetailList;
    }

    /**
     * @Description: 计算嗨粉奖金，嗨粉推荐，嗨粉极差
     * @return: List<CuMemberBonusDetail>
     * @Author: sui q
     * @Date: 2023/2/9 10:25
     */
    protected List<CuMemberBonusDetail> calculateHiFunPushBonusOne(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, Map<String, RangeDTO> rangeDtoMap, Map<String, BdAwards> awardsMap, BonusConfigDTO bonusConfigDTO, Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap, SaOrderExt saOrderExt, Map<Long, CuAwardsControl> cuAwardsControlMap) {
        List<CuMemberBonusDetail> cuMemberBonusDetailList = new ArrayList<>();
        CuMemberSettleExt sourceMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
        if (sourceMemberSettleExt == null) {
            return cuMemberBonusDetailList;
        }
        BigDecimal waresNum = ComputeUtil.computeDivide(saOrderExt.getOrderAmount(), BigDecimal.valueOf(413));
        sourceMemberSettleExt.setHiFunAmount(ComputeUtil.computeAdd(sourceMemberSettleExt.getHiFunAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
        sourceMemberSettleExt.setHiFunPv(ComputeUtil.computeAdd(sourceMemberSettleExt.getHiFunPv(), saOrderExt.getOrderAchieve()));
        sourceMemberSettleExt.setHiFunNewAmount(ComputeUtil.computeAdd(sourceMemberSettleExt.getHiFunNewAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
        sourceMemberSettleExt.setHiFunNewPv(ComputeUtil.computeAdd(sourceMemberSettleExt.getHiFunNewPv(), saOrderExt.getOrderAchieve()));
        Map<Integer, List<BonusHiPinkDTO>> bonusHiPinkDtoMap = bonusConfigDTO.getBonusHiPinkDtoMap();
        CuMemberSettleExt targetMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkReference());
        // 找到最大的奖金
        BigDecimal maxBonusValue = BigDecimal.ZERO;
        List<BonusHiPinkDTO> bonusHiPinkDTOList = bonusHiPinkDtoMap.get(saOrderExt.getPkCountry());
        for (BonusHiPinkDTO bonusHiPinkDTO : bonusHiPinkDTOList) {
            if (ComputeUtil.compareGreaterThan(bonusHiPinkDTO.getBonusValue(), maxBonusValue)) {
                maxBonusValue = bonusHiPinkDTO.getBonusValue();
            }
        }
        BigDecimal income = BigDecimal.ZERO;
        boolean flag = false;
        int i = 0;
        while (targetMemberSettleExt != null && targetMemberSettleExt.getPkParent() != 0) {
            // 嗨粉直推奖励的配置，从第一级开始获取
            BonusHiPinkDTO startBonusHiPinkDto = bonusHiPinkDTOList.get(i);
            targetMemberSettleExt = getCuMemberTree(cuMemberSettleExtMap, rangeDtoMap, awardsMap, targetMemberSettleExt, startBonusHiPinkDto, cuAwardsControlMap);
            if (targetMemberSettleExt == null) {
                break;
            }
            String key = getMemberKey(awardsMap, targetMemberSettleExt);
            if (ECategory.NORMAL.getValue() != targetMemberSettleExt.getCategory() || EAccountStatus.STOP_INCOME.getValue() == targetMemberSettleExt.getAccountStatus() ||
                    validatePkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.HI_PINK.getValue())) {
                targetMemberSettleExt = cuMemberSettleExtMap.get(targetMemberSettleExt.getPkParent());
                continue;
            }
            for (int x = i; x < bonusHiPinkDTOList.size(); x++) {
                BonusHiPinkDTO bonusHiPinkDTO = bonusHiPinkDTOList.get(x);
                String hiKey = bonusHiPinkDTO.getRangeType().toString() + bonusHiPinkDTO.getPkLevel();
                if (key.equals(hiKey)) {
                    CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, targetMemberSettleExt);
                    BigDecimal bonusIncome = ComputeUtil.computeSubtract(bonusHiPinkDTO.getBonusValue(), income);
                    bonusIncome = ComputeUtil.computeMultiply(bonusIncome, waresNum);
                    if (ComputeUtil.compareValue(BigDecimal.ZERO, bonusIncome)) {
//                        flag = true;
                        break;
                    }
                    // 直推奖金明细
                    CuMemberBonusDetail cuMemberBonusDetail = getCuMemberBonusDetail(saOrderExt, targetMemberSettleExt, cuMemberBonus);
                    cuMemberBonusDetail.setPkBonusItems(getPkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.HI_PINK.getValue()));
                    // 计算比例
                    cuMemberBonusDetail.setCalValue(bonusHiPinkDTO.getBonusValue());
                    cuMemberBonusDetail.setPretaxIncome(bonusIncome);
                    // 收益占比
                    BdBonusDeduct bdBonusDeduct = setBonusDetailDeduct(bonusConfigDTO, cuMemberBonusDetail, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
                    hiFunRemark(saOrderExt, cuMemberBonusDetail, sourceMemberSettleExt, targetMemberSettleExt);
                    if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusDetail.getIncomeStatus()) {
                        cuMemberBonus.setHiFunIncome(ComputeUtil.computeAdd(cuMemberBonus.getHiFunIncome(), cuMemberBonusDetail.getPretaxIncome()));
                        setBonusDeduct(bdBonusDeduct, cuMemberBonus, cuMemberBonusDetail);
                        cuMemberBonus.setRealIncomeTotal(ComputeUtil.computeAdd(cuMemberBonus.getRealIncomeTotal(), cuMemberBonusDetail.getRealIncome()));
                    }
                    income = bonusHiPinkDTO.getBonusValue();
                    cuMemberBonusDetailList.add(cuMemberBonusDetail);
                    if (ComputeUtil.compareEqual(maxBonusValue, bonusHiPinkDTO.getBonusValue())) {
                        flag = true;
                    }
                    if (x < bonusHiPinkDTOList.size() - 1) {
                        i = x + 1;
                    } else {
                        flag = true;
                    }
                    break;
                }
            }
            targetMemberSettleExt = cuMemberSettleExtMap.get(targetMemberSettleExt.getPkParent());
            if (flag) {
                break;
            }
        }
        return cuMemberBonusDetailList;
    }

    /**
     * @Description: 计算嗨粉奖金，嗨粉推荐，嗨粉极差
     * @return: List<CuMemberBonusDetail>
     * @Author: sui q
     * @Date: 2023/2/9 10:25
     */
    protected List<CuMemberBonusDetail> calculateNewHiFunPushBonusOne(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, Map<String, RangeDTO> rangeDtoMap, Map<String, BdAwards> awardsMap, BonusConfigDTO bonusConfigDTO, Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap, SaOrderExt saOrderExt, Map<Long, CuAwardsControl> cuAwardsControlMap) {
        List<CuMemberBonusDetail> cuMemberBonusDetailList = new ArrayList<>();
        CuMemberSettleExt sourceMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
        if (sourceMemberSettleExt == null) {
            return cuMemberBonusDetailList;
        }
        sourceMemberSettleExt.setHiFunAmount(ComputeUtil.computeAdd(sourceMemberSettleExt.getHiFunAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
        sourceMemberSettleExt.setHiFunPv(ComputeUtil.computeAdd(sourceMemberSettleExt.getHiFunPv(), saOrderExt.getOrderAchieve()));
        sourceMemberSettleExt.setHiFunNewAmount(ComputeUtil.computeAdd(sourceMemberSettleExt.getHiFunNewAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
        sourceMemberSettleExt.setHiFunNewPv(ComputeUtil.computeAdd(sourceMemberSettleExt.getHiFunNewPv(), saOrderExt.getOrderAchieve()));
//        Map<Integer, List<BonusHiPinkDTO>> bonusHiPinkDtoMap = bonusConfigDTO.getBonusHiPinkDtoMap();
        CuMemberSettleExt targetMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkReference());
        // 找到最大的奖金
//        BigDecimal maxBonusValue = BigDecimal.ZERO;
        BigDecimal maxBonusValue = BigDecimal.valueOf(1200);
        BigDecimal grantBonusValue = BigDecimal.ZERO;
//        List<BonusHiPinkDTO> bonusHiPinkDTOList = bonusHiPinkDtoMap.get(saOrderExt.getPkCountry());
//        for (BonusHiPinkDTO bonusHiPinkDTO : bonusHiPinkDTOList) {
//            if (ComputeUtil.compareGreaterThan(bonusHiPinkDTO.getBonusValue(), maxBonusValue)) {
//                maxBonusValue = bonusHiPinkDTO.getBonusValue();
//            }
//        }
//        BigDecimal income = BigDecimal.ZERO;
        Integer gradeValue = EGrade.YOU_KE.getValue();
        while (targetMemberSettleExt != null && targetMemberSettleExt.getPkParent() != 0) {
            // 嗨粉直推奖励的配置，从第一级开始获取
            targetMemberSettleExt = getCuMemberTree(cuMemberSettleExtMap, targetMemberSettleExt, gradeValue);
            if (targetMemberSettleExt == null) {
                break;
            }
            if (ECategory.NORMAL.getValue() != targetMemberSettleExt.getCategory() || EAccountStatus.STOP_INCOME.getValue() == targetMemberSettleExt.getAccountStatus() ||
                    validatePkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.HI_PINK.getValue())) {
                targetMemberSettleExt = cuMemberSettleExtMap.get(targetMemberSettleExt.getPkParent());
                continue;
            }
            BigDecimal memberMaxBonusValue = BigDecimal.ZERO;
            if (targetMemberSettleExt.getGradeValue() == EGrade.YOU_KE.getValue() || targetMemberSettleExt.getGradeValue() == EGrade.MAKER.getValue()) {
                memberMaxBonusValue = BigDecimal.valueOf(800);
            } else if (targetMemberSettleExt.getGradeValue() == EGrade.VIP.getValue()) {
                memberMaxBonusValue = BigDecimal.valueOf(1000);
            } else if (targetMemberSettleExt.getGradeValue() == EGrade.S_VIP.getValue()) {
                memberMaxBonusValue = BigDecimal.valueOf(1200);
            }

            CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, targetMemberSettleExt);
            BigDecimal bonusIncome = ComputeUtil.computeSubtract(memberMaxBonusValue, grantBonusValue);
            if (ComputeUtil.compareValue(BigDecimal.ZERO, bonusIncome)) {
                targetMemberSettleExt = cuMemberSettleExtMap.get(targetMemberSettleExt.getPkParent());
                continue;
            }
            // 直推奖金明细
            CuMemberBonusDetail cuMemberBonusDetail = getCuMemberBonusDetail(saOrderExt, targetMemberSettleExt, cuMemberBonus);
            cuMemberBonusDetail.setPkBonusItems(getPkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.HI_PINK.getValue()));
            // 计算比例
            cuMemberBonusDetail.setCalValue(BigDecimal.ZERO);
            cuMemberBonusDetail.setPretaxIncome(bonusIncome);
            // 收益占比
            BdBonusDeduct bdBonusDeduct = setBonusDetailDeduct(bonusConfigDTO, cuMemberBonusDetail, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
            hiFunRemark(saOrderExt, cuMemberBonusDetail, sourceMemberSettleExt, targetMemberSettleExt);
            if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusDetail.getIncomeStatus()) {
                cuMemberBonus.setHiFunIncome(ComputeUtil.computeAdd(cuMemberBonus.getHiFunIncome(), cuMemberBonusDetail.getPretaxIncome()));
                setBonusDeduct(bdBonusDeduct, cuMemberBonus, cuMemberBonusDetail);
                cuMemberBonus.setRealIncomeTotal(ComputeUtil.computeAdd(cuMemberBonus.getRealIncomeTotal(), cuMemberBonusDetail.getRealIncome()));
            }
            grantBonusValue = ComputeUtil.computeAdd(grantBonusValue, bonusIncome);
            cuMemberBonusDetailList.add(cuMemberBonusDetail);
            if (ComputeUtil.compareEqual(maxBonusValue, grantBonusValue)) {
                break;
            } else {
                targetMemberSettleExt = cuMemberSettleExtMap.get(targetMemberSettleExt.getPkParent());
            }
        }
        return cuMemberBonusDetailList;
    }

    /**
     * 根据极差获取会员
     */
    private CuMemberSettleExt getCuMemberTree(Map<Long, CuMemberSettleExt> cuMemberTreeMap, Map<String, RangeDTO> rangeDtoMap,
                                              Map<String, BdAwards> awardsMap, CuMemberSettleExt parentMemberTree, BonusHiPinkDTO bonusHiPinkDTO, Map<Long, CuAwardsControl> cuAwardsControlMap) {
        if (parentMemberTree == null) {
            return null;
        }
        // 获取目标会员的极差配置信息 等级还是奖衔
        RangeDTO rangeDto = getRangeDto(rangeDtoMap, awardsMap, parentMemberTree, cuAwardsControlMap);
        // 嗨粉直推奖励的配置，获取目标奖励的配置
        RangeDTO targetRangeDto = rangeDtoMap.get(bonusHiPinkDTO.getPkCountry().toString() + bonusHiPinkDTO.getRangeType() + bonusHiPinkDTO.getPkLevel());
        if (rangeDto == null || targetRangeDto == null) {
            return null;
        }
        // 判断目标会员极差奖励，目标会员的极差等级不足 直推奖励的配置，取上一级，一直取到 目标会员的 极差等级>= 直推推荐奖励配置的
        if (rangeDto.getRangeValue() < targetRangeDto.getRangeValue()) {
            parentMemberTree = cuMemberTreeMap.get(parentMemberTree.getPkParent());
            parentMemberTree = getCuMemberTree(cuMemberTreeMap, rangeDtoMap, awardsMap, parentMemberTree, bonusHiPinkDTO, cuAwardsControlMap);
        }
        return parentMemberTree;
    }

    /**
     * 根据极差获取会员
     */
    private CuMemberSettleExt getCuMemberTree(Map<Long, CuMemberSettleExt> cuMemberTreeMap, CuMemberSettleExt parentMemberTree, Integer startGradeValue) {
        if (parentMemberTree == null) {
            return null;
        }
        // 判断目标会员极差奖励，目标会员的极差等级不足 直推奖励的配置，取上一级，一直取到 目标会员的 极差等级>= 直推推荐奖励配置的
        if (parentMemberTree.getGradeValue() < startGradeValue) {
            parentMemberTree = cuMemberTreeMap.get(parentMemberTree.getPkParent());
            parentMemberTree = getCuMemberTree(cuMemberTreeMap, parentMemberTree, startGradeValue);
        }
        return parentMemberTree;
    }

}
