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

import com.hzs.bonus.achieve.param.MemberAchieveParam;
import com.hzs.bonus.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.bonus.achieve.service.ICuMemberTreeService;
import com.hzs.bonus.detail.service.ICuMemberAssessService;
import com.hzs.common.core.constant.BonusFieldConstants;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.constant.TableNameConstants;
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.core.utils.reflect.ReflectUtils;
import com.hzs.common.domain.bonus.BdBonusDeduct;
import com.hzs.common.domain.member.achieve.CuMemberTree;
import com.hzs.common.domain.member.achieve.ext.CuMemberRangeExt;
import com.hzs.common.domain.member.base.CuAwardsControl;
import com.hzs.common.domain.member.bonus.*;
import com.hzs.common.domain.member.detail.CuMemberAssess;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.domain.system.config.BdGrade;
import com.hzs.common.domain.system.config.ext.BdBonusItemsExt;
import com.hzs.sale.order.ISaOrderServiceApi;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
import com.hzs.system.config.dto.BonusConfigDTO;
import com.hzs.system.config.dto.BonusExpandDTO;
import com.hzs.system.config.dto.RangeDTO;
import com.hzs.system.sys.dto.BdCurrencyDTO;
import lombok.extern.slf4j.Slf4j;
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.*;

@Slf4j
@Component
public abstract class BonusSettleHandle {

    @Autowired
    protected ICuMemberTreeService iCuMemberTreeService;
    @Autowired
    protected ICuMemberSettlePeriodService iCuMemberSettlePeriodService;
    @Autowired
    protected ICuMemberAssessService iCuMemberAssessService;

    @DubboReference
    ISaOrderServiceApi iSaOrderServiceApi;
    @DubboReference
    ICurrencyServiceApi iCurrencyServiceApi;

    /**
     * 获取等级对应的拓展奖配置
     *
     * @param bonusConfigDTO  奖金参数汇总
     * @param memberSettleExt 会员
     */
    protected BdGrade getGradeExpand(BonusConfigDTO bonusConfigDTO, CuMemberSettleExt memberSettleExt) {
        return bonusConfigDTO.getGradeMap().get(memberSettleExt.getPkSettleCountry().toString() + "_" + memberSettleExt.getPkGrade());
    }

    /**
     * 获取奖衔对应的辅导奖配置
     *
     * @param bonusConfigDTO  奖金参数汇总
     * @param memberSettleExt 会员
     */
    protected BdAwards getAwardsCouch(BonusConfigDTO bonusConfigDTO, CuMemberSettleExt memberSettleExt, Map<Long, CuAwardsControl> cuAwardsControlMap) {
        Integer pkAwards = getPkRealAwards(memberSettleExt.getPkMember(), memberSettleExt.getPkAwards(), cuAwardsControlMap);
        return bonusConfigDTO.getAwardsMap().get(memberSettleExt.getPkSettleCountry().toString() + ERangeType.AWARDS.getValue() + pkAwards);
    }

    private Integer getPkRealAwards(Long pkMember, Integer pkAwards, Map<Long, CuAwardsControl> cuAwardsControlMap) {
        if (cuAwardsControlMap.containsKey(pkMember)) {
            if (cuAwardsControlMap.get(pkMember).getMentoringAward() != null) {
                pkAwards = cuAwardsControlMap.get(pkMember).getMentoringAward();
            }
        }
        return pkAwards;
    }

    /**
     * 获取扩展比例（拓展配置比例 >= 等级拓展比例，取等级拓展比例）
     * -- 获得国家对应的4-n参数比例
     *
     * @param bonusConfigDTO  奖金配置汇总
     * @param memberSettleExt 会员
     * @param endTouch        4-n的次数
     * @param bdGrade         等级
     * @param addTouch        配置追加的碰次
     */
    protected BigDecimal getCountryExpand(BonusConfigDTO bonusConfigDTO, CuMemberSettleExt memberSettleExt, Integer endTouch, BdGrade bdGrade,
                                          BigDecimal addTouch, BigDecimal editExpandRatio) {
        BonusExpandDTO bonusExpand = bonusConfigDTO.getBonusExpandMap().get(memberSettleExt.getPkSettleCountry());
        // 总碰次 = 实际计算出来的碰次 + 配置追加的碰次
        BigDecimal totalTouch = ComputeUtil.computeAdd(BigDecimal.valueOf(endTouch), addTouch);
        // 配置的4-N总比例（一般26%）
        BigDecimal expandRatio = bonusExpand.getExpandRatio();
        if (ComputeUtil.compareValue(editExpandRatio)) {
            expandRatio = editExpandRatio;
        }
        // 4-N拓展比例 % 总碰次，得出一碰的比例
        BigDecimal countryExpandRatio = ComputeUtil.computeDivide(expandRatio, totalTouch);
        if (ComputeUtil.compareValue(countryExpandRatio, bdGrade.getExpandRatio())) {
            // 一碰的比例大于等级的拓展比例，使用等于的拓展比例
            return bdGrade.getExpandRatio();
        }
        return countryExpandRatio;
    }

    /**
     * 获得国家对应的4-n参数比例
     *
     * @param bonusConfigDTO  奖金配置汇总
     * @param memberSettleExt 会员
     * @param endTouch        4-n的次数
     * @param bdGrade         等级
     */
    protected BigDecimal getCountryRepurchaseExpand(BonusConfigDTO bonusConfigDTO, CuMemberSettleExt memberSettleExt, Integer endTouch, BdGrade bdGrade, BigDecimal addTouch, BigDecimal editExpandRatio) {
        BonusExpandDTO bonusExpand = bonusConfigDTO.getBonusExpandMap().get(memberSettleExt.getPkSettleCountry());
        // 4-N 拓展比例（一般为26%）
        BigDecimal expandRatio = bonusExpand.getExpandRatio();
//        log.info("expandRatio 配置N使用的比例 : {}", expandRatio);
        if (ComputeUtil.compareValue(editExpandRatio)) {
            expandRatio = editExpandRatio;
        }
//        log.info("expandRatio 2 : {}", expandRatio);
        BigDecimal countryExpandRatio = ComputeUtil.computeDivide(expandRatio, BigDecimal.valueOf(endTouch));
        if (ComputeUtil.compareValue(countryExpandRatio, bdGrade.getRepurchaseRatio())) {
//            log.info("expandRatio 3 : {}", bdGrade.getRepurchaseRatio());
            return bdGrade.getRepurchaseRatio();
        }
//        log.info("expandRatio N均过比较 : {}", countryExpandRatio);
        return countryExpandRatio;
    }

    /**
     * 获得奖项主键，验证该国是否有该奖项
     *
     * @param bonusConfigDTO 参数
     * @param pkCountry      国家
     * @param bonusItems     值
     * @return int
     */
    protected Boolean validatePkBonusItems(BonusConfigDTO bonusConfigDTO, Integer pkCountry, Integer bonusItems) {
        BdBonusItemsExt bdBonusItemsExt = bonusConfigDTO.getBonusItemsExtMap().get(pkCountry.toString() + bonusItems);
        return bdBonusItemsExt == null || bdBonusItemsExt.getPkId() == null;
    }

    /**
     * 获得奖项主键
     *
     * @param bonusConfigDTO 参数
     * @param pkCountry      国家
     * @param bonusItems     值
     * @return int
     */
    protected Integer getPkBonusItems(BonusConfigDTO bonusConfigDTO, Integer pkCountry, Integer bonusItems) {
        return bonusConfigDTO.getBonusItemsExtMap().get(pkCountry.toString() + bonusItems).getPkId();
    }

    /**
     * 获得有量奖的会员
     *
     * @param cuMemberBonusMap        奖金汇总表
     * @param cuMemberBonusExpandList 量奖明细
     */
    protected Set<Long> getMemberExpandBonus(Map<Long, CuMemberBonus> cuMemberBonusMap, List<CuMemberBonusExpand> cuMemberBonusExpandList) {
        // 这一步感觉有点多余。。在奖金入库之前，奖金汇总map的key和奖金汇总ID都是用会员ID做临时处理的
        Map<Long, CuMemberBonus> pkMemberSettleMap = new HashMap<>(ComputeUtil.mapInitCapacity(cuMemberBonusMap.size()));
        cuMemberBonusMap.forEach((key, cuMemberBonus) -> {
            // 判断 cuMemberSettleExtMap中这个人是否是免考，免考的不查询考核是否通过，过滤数据
            pkMemberSettleMap.put(cuMemberBonus.getPkId(), cuMemberBonus);
        });

        Set<Long> expandMemberSet = new HashSet<>();
        cuMemberBonusExpandList.forEach(cuMemberBonusExpand -> {
            // 只查询有量奖的人的考核记录
            if (pkMemberSettleMap.containsKey(cuMemberBonusExpand.getPkBonus())) {
                expandMemberSet.add(pkMemberSettleMap.get(cuMemberBonusExpand.getPkBonus()).getPkMember());
            }
        });
        return expandMemberSet;
    }

    /**
     * 获得有量奖的会员
     *
     * @param cuMemberSettleExtMap 奖金汇总表
     */
    protected Map<Long, CuMemberSettleExt> getMemberExpandBonus(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap) {
        Map<Long, CuMemberSettleExt> pkMemberSettleMap = new HashMap<>(ComputeUtil.mapInitCapacity(cuMemberSettleExtMap.size()));
        cuMemberSettleExtMap.forEach((key, cuMemberSettleExt) ->
                pkMemberSettleMap.put(cuMemberSettleExt.getPkMember(), cuMemberSettleExt));
        return pkMemberSettleMap;
    }

    /**
     * 记录需要修改的会员结算表数据, 安置的
     *
     * @param cuMemberSettleExtMap 会员结算
     * @param cuMemberSettleExt    查询出来的结算数据
     */
    protected void getMemberSettle(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, CuMemberSettleExt cuMemberSettleExt) {
        if (cuMemberSettleExtMap.containsKey(cuMemberSettleExt.getPkMember())) {
            CuMemberSettleExt memberSettleExt = cuMemberSettleExtMap.get(cuMemberSettleExt.getPkMember());
            if (memberSettleExt.getPkParent() == null) {
                memberSettleExt.setPkParent(cuMemberSettleExt.getPkParent());
            }
            if (memberSettleExt.getPkPlaceParent() == null) {
                memberSettleExt.setPkPlaceParent(cuMemberSettleExt.getPkPlaceParent());
            }
            if (memberSettleExt.getPlaceDept() == null) {
                memberSettleExt.setPlaceDept(cuMemberSettleExt.getPlaceDept());
            }
            if (memberSettleExt.getPkGrade() == null) {
                memberSettleExt.setPkGrade(cuMemberSettleExt.getPkGrade());
            }
            if (memberSettleExt.getGradeValue() == null) {
                memberSettleExt.setGradeValue(cuMemberSettleExt.getGradeValue());
            }
            if (memberSettleExt.getPkAwards() == null) {
                memberSettleExt.setPkAwards(cuMemberSettleExt.getPkAwards());
            }
            if (memberSettleExt.getRegisterAuthority() == null) {
                memberSettleExt.setRegisterAuthority(cuMemberSettleExt.getRegisterAuthority());
            }
            if (memberSettleExt.getCustomerType() == null) {
                memberSettleExt.setCustomerType(cuMemberSettleExt.getCustomerType());
            }
            if (memberSettleExt.getPkVertex() == null) {
                memberSettleExt.setPkVertex(cuMemberSettleExt.getPkVertex());
            }
            if (memberSettleExt.getUnGrantIncome() == null || memberSettleExt.getUnGrantIncome().compareTo(BigDecimal.ZERO) == 0) {
                memberSettleExt.setUnGrantIncome(null != cuMemberSettleExt.getUnGrantIncome() ? cuMemberSettleExt.getUnGrantIncome() : BigDecimal.ZERO);
            }
        } else {
            cuMemberSettleExt.setANewAmount(BigDecimal.ZERO);
            cuMemberSettleExt.setANewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setBNewAmount(BigDecimal.ZERO);
            cuMemberSettleExt.setBNewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setRepANewAmount(BigDecimal.ZERO);
            cuMemberSettleExt.setRepBNewAmount(BigDecimal.ZERO);
            cuMemberSettleExt.setRepANewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setRepBNewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setRegisterNewAmount(BigDecimal.ZERO);
            cuMemberSettleExt.setRegisterNewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setUpgradeNewAmount(BigDecimal.ZERO);
            cuMemberSettleExt.setUpgradeNewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setRepurchaseNewAmount(BigDecimal.ZERO);
            cuMemberSettleExt.setRepurchaseNewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setHiFunNewAmount(BigDecimal.ZERO);
            cuMemberSettleExt.setHiFunNewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setMallNewAmount(BigDecimal.ZERO);
            cuMemberSettleExt.setMallNewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setRegisterBloNewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setUpgradeBloNewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setRepurchaseBloNewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setHiFunBloNewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setMallBloNewPv(BigDecimal.ZERO);
            cuMemberSettleExt.setAFirBalance(BigDecimal.ZERO);
            cuMemberSettleExt.setARepBalance(BigDecimal.ZERO);
            cuMemberSettleExt.setBFirBalance(BigDecimal.ZERO);
            cuMemberSettleExt.setBRepBalance(BigDecimal.ZERO);
            if (cuMemberSettleExt.getAFirBalance().compareTo(BigDecimal.ZERO) == 0
                    && cuMemberSettleExt.getARepBalance().compareTo(BigDecimal.ZERO) == 0) {
                cuMemberSettleExt.setAFirBalance(cuMemberSettleExt.getABalance());
            }
            if (cuMemberSettleExt.getBFirBalance().compareTo(BigDecimal.ZERO) == 0
                    && cuMemberSettleExt.getBRepBalance().compareTo(BigDecimal.ZERO) == 0) {
                cuMemberSettleExt.setBFirBalance(cuMemberSettleExt.getBBalance());
            }
            cuMemberSettleExtMap.put(cuMemberSettleExt.getPkMember(), cuMemberSettleExt);
        }
    }

    /**
     * 记录需要修改的会员结算表数据, 安置的
     *
     * @param cuMemberSettleExtMap  会员结算
     * @param cuMemberSettleExtList 查询出来的结算数据
     */
    protected void getMemberSettle(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, List<CuMemberSettleExt> cuMemberSettleExtList) {
        cuMemberSettleExtList.forEach(cuMemberSettleExt -> {
            if (cuMemberSettleExtMap.containsKey(cuMemberSettleExt.getPkMember())) {
                CuMemberSettleExt memberSettleExt = cuMemberSettleExtMap.get(cuMemberSettleExt.getPkMember());
                if (memberSettleExt.getPkParent() == null) {
                    memberSettleExt.setPkParent(cuMemberSettleExt.getPkParent());
                }
                if (memberSettleExt.getPkPlaceParent() == null) {
                    memberSettleExt.setPkPlaceParent(cuMemberSettleExt.getPkPlaceParent());
                }
                if (memberSettleExt.getPlaceDept() == null) {
                    memberSettleExt.setPlaceDept(cuMemberSettleExt.getPlaceDept());
                }
                if (memberSettleExt.getPkGrade() == null) {
                    memberSettleExt.setPkGrade(cuMemberSettleExt.getPkGrade());
                }
                if (memberSettleExt.getGradeValue() == null) {
                    memberSettleExt.setGradeValue(cuMemberSettleExt.getGradeValue());
                }
                if (memberSettleExt.getPkAwards() == null) {
                    memberSettleExt.setPkAwards(cuMemberSettleExt.getPkAwards());
                }
                if (memberSettleExt.getRegisterAuthority() == null) {
                    memberSettleExt.setRegisterAuthority(cuMemberSettleExt.getRegisterAuthority());
                }
                if (memberSettleExt.getCustomerType() == null) {
                    memberSettleExt.setCustomerType(cuMemberSettleExt.getCustomerType());
                }
                if (memberSettleExt.getPkVertex() == null) {
                    memberSettleExt.setPkVertex(cuMemberSettleExt.getPkVertex());
                }
                if (memberSettleExt.getUnGrantIncome() == null || memberSettleExt.getUnGrantIncome().compareTo(BigDecimal.ZERO) == 0) {
                    memberSettleExt.setUnGrantIncome(null != cuMemberSettleExt.getUnGrantIncome() ? cuMemberSettleExt.getUnGrantIncome() : BigDecimal.ZERO);
                }
            } else {
                cuMemberSettleExt.setANewAmount(BigDecimal.ZERO);
                cuMemberSettleExt.setANewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setBNewAmount(BigDecimal.ZERO);
                cuMemberSettleExt.setBNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setRepANewAmount(BigDecimal.ZERO);
                cuMemberSettleExt.setRepBNewAmount(BigDecimal.ZERO);
                cuMemberSettleExt.setRepANewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setRepBNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setRegisterNewAmount(BigDecimal.ZERO);
                cuMemberSettleExt.setRegisterNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setUpgradeNewAmount(BigDecimal.ZERO);
                cuMemberSettleExt.setUpgradeNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setRepurchaseNewAmount(BigDecimal.ZERO);
                cuMemberSettleExt.setRepurchaseNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setHiFunNewAmount(BigDecimal.ZERO);
                cuMemberSettleExt.setHiFunNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setMallNewAmount(BigDecimal.ZERO);
                cuMemberSettleExt.setMallNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setRegisterBloNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setUpgradeBloNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setRepurchaseBloNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setHiFunBloNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setMallBloNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setAFirBalance(BigDecimal.ZERO);
                cuMemberSettleExt.setARepBalance(BigDecimal.ZERO);
                cuMemberSettleExt.setBFirBalance(BigDecimal.ZERO);
                cuMemberSettleExt.setBRepBalance(BigDecimal.ZERO);
                if (cuMemberSettleExt.getAFirBalance().compareTo(BigDecimal.ZERO) == 0
                        && cuMemberSettleExt.getARepBalance().compareTo(BigDecimal.ZERO) == 0) {
                    cuMemberSettleExt.setAFirBalance(cuMemberSettleExt.getABalance());
                }
                if (cuMemberSettleExt.getBFirBalance().compareTo(BigDecimal.ZERO) == 0
                        && cuMemberSettleExt.getBRepBalance().compareTo(BigDecimal.ZERO) == 0) {
                    cuMemberSettleExt.setBFirBalance(cuMemberSettleExt.getBBalance());
                }
                cuMemberSettleExtMap.put(cuMemberSettleExt.getPkMember(), cuMemberSettleExt);
            }
        });
    }

    /**
     * @param saOrderExt           订单编号
     * @param cuMemberBonusPush    直推奖
     * @param cuMemberSettleExtMap 存储会员网体的map
     * @Description: 获得 直推收益的备注
     */
    protected void recommendRemark(SaOrderExt saOrderExt, CuMemberBonusPush cuMemberBonusPush, Map<Long, CuMemberSettleExt> cuMemberSettleExtMap) {
        CuMemberSettleExt sourceCuMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
        CuMemberSettleExt targetCuMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkReference());
        String remark;
        if (cuMemberBonusPush.getSecond() == 0) {
            remark = String.format(BonusMsgConstants.RECOMMEND_DIRECT, saOrderExt.getOrderCode(),
                    sourceCuMemberSettleExt.getMemberCode() + sourceCuMemberSettleExt.getMemberName(),
                    targetCuMemberSettleExt.getMemberCode() + targetCuMemberSettleExt.getMemberName(),
                    cuMemberBonusPush.getRealIncome());
        } else {
            remark = String.format(BonusMsgConstants.RECOMMEND, cuMemberBonusPush.getRound() + 1, cuMemberBonusPush.getSecond(), saOrderExt.getOrderCode(),
                    sourceCuMemberSettleExt.getMemberCode() + sourceCuMemberSettleExt.getMemberName(),
                    targetCuMemberSettleExt.getMemberCode() + targetCuMemberSettleExt.getMemberName(),
                    cuMemberBonusPush.getCalAchieve(), cuMemberBonusPush.getCalValue());
        }
        cuMemberBonusPush.setRemark(remark);
    }

    /**
     * @param cuMemberSettleExtMap  会员结算
     * @param cuMemberSettleExtList 查询出来的结算数据
     * @Description: 记录需要修改的会员结算表数据, 安置的
     * @return: void
     * @Author: sui q
     * @Date: 2022/11/15 9:52
     */
    protected void getSecondMemberSettle(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, List<CuMemberSettleExt> cuMemberSettleExtList) {
        cuMemberSettleExtList.forEach(cuMemberSettleExt -> {
            if (cuMemberSettleExtMap.containsKey(cuMemberSettleExt.getPkMember())) {
                CuMemberSettleExt memberSettleExt = cuMemberSettleExtMap.get(cuMemberSettleExt.getPkMember());
                if (memberSettleExt.getPkParent() == null) {
                    memberSettleExt.setPkParent(cuMemberSettleExt.getPkParent());
                }
                if (memberSettleExt.getPkPlaceParent() == null) {
                    memberSettleExt.setPkPlaceParent(cuMemberSettleExt.getPkPlaceParent());
                }
                if (memberSettleExt.getPlaceDept() == null) {
                    memberSettleExt.setPlaceDept(cuMemberSettleExt.getPlaceDept());
                }
                if (memberSettleExt.getPkGrade() == null) {
                    memberSettleExt.setPkGrade(cuMemberSettleExt.getPkGrade());
                }
                if (memberSettleExt.getGradeValue() == null) {
                    memberSettleExt.setGradeValue(cuMemberSettleExt.getGradeValue());
                }
                if (memberSettleExt.getPkAwards() == null) {
                    memberSettleExt.setPkAwards(cuMemberSettleExt.getPkAwards());
                }
                if (memberSettleExt.getRegisterAuthority() == null) {
                    memberSettleExt.setRegisterAuthority(cuMemberSettleExt.getRegisterAuthority());
                }
                if (memberSettleExt.getCustomerType() == null) {
                    memberSettleExt.setCustomerType(cuMemberSettleExt.getCustomerType());
                }
            } else {
                cuMemberSettleExt.setRegisterBloNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setUpgradeBloNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setRepurchaseBloNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setHiFunBloNewPv(BigDecimal.ZERO);
                cuMemberSettleExt.setMallBloNewPv(BigDecimal.ZERO);
                cuMemberSettleExtMap.put(cuMemberSettleExt.getPkMember(), cuMemberSettleExt);
            }
        });
    }

    /**
     * 获得 直推收益的备注
     *
     * @param saOrderExt          订单编号
     * @param cuMemberBonusPush   直推奖
     * @param cuMemberRangeExtMap 存储会员网体的map
     */
    protected void recommendRangeRemark(SaOrderExt saOrderExt, CuMemberBonusPush cuMemberBonusPush, Map<Long, CuMemberRangeExt> cuMemberRangeExtMap, CuMemberRangeExt targetCuMemberRangeExt) {
        CuMemberRangeExt sourceCuMemberRangeExt = cuMemberRangeExtMap.get(saOrderExt.getPkMember());
        String remark = String.format(BonusMsgConstants.RANGE_RECOMMEND, cuMemberBonusPush.getRound(), cuMemberBonusPush.getSecond(), saOrderExt.getOrderCode(),
                sourceCuMemberRangeExt.getMemberCode() + " " + sourceCuMemberRangeExt.getMemberName(),
                targetCuMemberRangeExt.getMemberCode() + " " + targetCuMemberRangeExt.getMemberName(),
                cuMemberBonusPush.getCalAchieve());
        cuMemberBonusPush.setRemark(remark);
    }

    /**
     * 获得 拓展收益的备注
     *
     * @param saOrderExt              订单编号
     * @param cuMemberBonusExpand     拓展奖
     * @param sourceCuMemberSettleExt 提供奖金的人
     * @param targetCuMemberSettleExt 获得奖金的人
     */
    protected void expandRemark(SaOrderExt saOrderExt, CuMemberBonusExpand cuMemberBonusExpand,
                                CuMemberSettleExt sourceCuMemberSettleExt, CuMemberSettleExt targetCuMemberSettleExt) {
        String remark = String.format(BonusMsgConstants.EXPAND, cuMemberBonusExpand.getCurrentTouch(), cuMemberBonusExpand.getExpandTouch(), saOrderExt.getOrderCode(),
                sourceCuMemberSettleExt.getMemberCode() + " " + sourceCuMemberSettleExt.getMemberName(),
                targetCuMemberSettleExt.getMemberCode() + " " + targetCuMemberSettleExt.getMemberName(),
                cuMemberBonusExpand.getCalAchieve(), cuMemberBonusExpand.getCalValue());
        cuMemberBonusExpand.setRemark(remark);
    }

    /**
     * 获得 拓展收益的备注
     *
     * @param saOrderExt              订单编号
     * @param cuMemberBonusPush       拓展奖
     * @param sourceCuMemberSettleExt 提供奖金的人
     * @param targetCuMemberSettleExt 获得奖金的人
     */
    protected void repurchasePushRemark(SaOrderExt saOrderExt, CuMemberBonusPush cuMemberBonusPush,
                                        CuMemberSettleExt sourceCuMemberSettleExt, CuMemberSettleExt targetCuMemberSettleExt, String msg) {
        String remark = String.format(msg, saOrderExt.getOrderCode(),
                sourceCuMemberSettleExt.getMemberCode() + " " + sourceCuMemberSettleExt.getMemberName(),
                targetCuMemberSettleExt.getMemberCode() + " " + targetCuMemberSettleExt.getMemberName(),
                cuMemberBonusPush.getCalAchieve(), cuMemberBonusPush.getCalValue());
        cuMemberBonusPush.setRemark(remark);
    }

    /**
     * 获得 拓展收益的备注
     *
     * @param saOrderExt              订单编号
     * @param cuMemberBonusExpand     拓展奖
     * @param sourceCuMemberSettleExt 提供奖金的人
     * @param targetCuMemberSettleExt 获得奖金的人
     */
    protected void repurchaseExpandRemark(SaOrderExt saOrderExt, CuMemberBonusExpand cuMemberBonusExpand,
                                          CuMemberSettleExt sourceCuMemberSettleExt, CuMemberSettleExt targetCuMemberSettleExt) {
        String remark = String.format(BonusMsgConstants.REPURCHASE_EXPAND, cuMemberBonusExpand.getCurrentTouch(), cuMemberBonusExpand.getExpandTouch(), saOrderExt.getOrderCode(),
                sourceCuMemberSettleExt.getMemberCode() + " " + sourceCuMemberSettleExt.getMemberName(),
                targetCuMemberSettleExt.getMemberCode() + " " + targetCuMemberSettleExt.getMemberName(),
                cuMemberBonusExpand.getCalAchieve(), cuMemberBonusExpand.getCalValue());
        cuMemberBonusExpand.setRemark(remark);
    }

    /**
     * 获得 辅导收益的备注
     *
     * @param saOrderExt              订单编号
     * @param cuMemberBonusCoach      辅导奖
     * @param sourceCuMemberSettleExt 提供奖金的人
     * @param targetCuMemberSettleExt 获得奖金的人
     */
    protected void coachRemark(SaOrderExt saOrderExt, CuMemberBonusCoach cuMemberBonusCoach,
                               CuMemberSettleExt sourceCuMemberSettleExt, CuMemberSettleExt targetCuMemberSettleExt, boolean firstBool) {
        String remark = String.format(firstBool ? BonusMsgConstants.COACH : BonusMsgConstants.REPURCHASE_COACH,
                cuMemberBonusCoach.getCurrentGeneration(), null != saOrderExt ? saOrderExt.getOrderCode() : " ",
                sourceCuMemberSettleExt.getMemberCode() + " " + sourceCuMemberSettleExt.getMemberName(),
                targetCuMemberSettleExt.getMemberCode() + " " + targetCuMemberSettleExt.getMemberName(),
                cuMemberBonusCoach.getCalAchieve(), cuMemberBonusCoach.getCalValue());
        cuMemberBonusCoach.setRemark(remark);
    }

    /**
     * 获得 辅导收益的备注
     *
     * @param saOrderExt           订单编号
     * @param cuMemberBonusCoach   辅导奖
     * @param sourceMemberRangeExt 提供奖金的人
     * @param targetMemberRangeExt 获得奖金的人
     */
    protected void coachRangeRemark(SaOrderExt saOrderExt, CuMemberBonusCoach cuMemberBonusCoach,
                                    CuMemberRangeExt sourceMemberRangeExt, CuMemberRangeExt targetMemberRangeExt) {
        String remark = String.format(BonusMsgConstants.RANGE_COACH, cuMemberBonusCoach.getCurrentGeneration(), cuMemberBonusCoach.getCoachGeneration(),
                saOrderExt.getOrderCode(),
                sourceMemberRangeExt.getMemberCode() + " " + sourceMemberRangeExt.getMemberName(),
                targetMemberRangeExt.getMemberCode() + " " + targetMemberRangeExt.getMemberName(),
                cuMemberBonusCoach.getCalAchieve(), cuMemberBonusCoach.getCalValue());
        cuMemberBonusCoach.setRemark(remark);
    }

    /**
     * 获得 报单服务费收益的备注
     *
     * @param saOrderExt              订单编号
     * @param cuMemberBonusDetail     报单服务费
     * @param sourceCuMemberSettleExt 提供奖金的人
     * @param targetCuMemberSettleExt 获得奖金的人
     */
    protected void serviceRemark(SaOrderExt saOrderExt, CuMemberBonusDetail cuMemberBonusDetail,
                                 CuMemberSettleExt sourceCuMemberSettleExt, CuMemberSettleExt targetCuMemberSettleExt) {
        String remark = String.format(BonusMsgConstants.SERVICE, saOrderExt.getOrderCode(),
                sourceCuMemberSettleExt.getMemberCode() + " " + sourceCuMemberSettleExt.getMemberName(),
                targetCuMemberSettleExt.getMemberCode() + " " + targetCuMemberSettleExt.getMemberName(),
                cuMemberBonusDetail.getCalAchieve(), cuMemberBonusDetail.getCalValue());
        cuMemberBonusDetail.setRemark(remark);
    }

    /**
     * 获得 报单服务费收益的备注
     *
     * @param saOrderExt             订单编号
     * @param cuMemberBonusDetail    报单服务费
     * @param sourceCuMemberRangeExt 提供奖金的人
     * @param targetCuMemberRangeExt 获得奖金的人
     */
    protected void rangeServiceRemark(SaOrderExt saOrderExt, CuMemberBonusDetail cuMemberBonusDetail,
                                      CuMemberRangeExt sourceCuMemberRangeExt, CuMemberRangeExt targetCuMemberRangeExt) {
        String remark = String.format(BonusMsgConstants.RANGE_SERVICE, saOrderExt.getOrderCode(),
                sourceCuMemberRangeExt.getMemberCode() + " " + sourceCuMemberRangeExt.getMemberName(),
                targetCuMemberRangeExt.getMemberCode() + " " + targetCuMemberRangeExt.getMemberName(),
                cuMemberBonusDetail.getCalAchieve(), cuMemberBonusDetail.getCalValue());
        cuMemberBonusDetail.setRemark(remark);
    }

    /**
     * 获得 报单服务费收益的备注
     *
     * @param saOrderExt              订单编号
     * @param cuMemberBonusDetail     报单服务费
     * @param sourceCuMemberSettleExt 提供奖金的人
     * @param targetCuMemberSettleExt 获得奖金的人
     */
    protected void haiFunRemark(SaOrderExt saOrderExt, CuMemberBonusDetail cuMemberBonusDetail,
                                CuMemberSettleExt sourceCuMemberSettleExt, CuMemberSettleExt targetCuMemberSettleExt) {
        String remark = String.format(BonusMsgConstants.HAI_FUN, saOrderExt.getOrderCode(),
                sourceCuMemberSettleExt.getMemberCode() + " " + sourceCuMemberSettleExt.getMemberName(),
                targetCuMemberSettleExt.getMemberCode() + " " + targetCuMemberSettleExt.getMemberName(),
                cuMemberBonusDetail.getPretaxIncome());
        cuMemberBonusDetail.setRemark(remark);
    }

    /**
     * 获得 嗨粉推荐的收益
     *
     * @param saOrderExt              订单编号
     * @param cuMemberBonusDetail     报单服务费
     * @param sourceCuMemberSettleExt 提供奖金的人
     * @param targetCuMemberSettleExt 获得奖金的人
     */
    protected void hiFunRemark(SaOrderExt saOrderExt, CuMemberBonusDetail cuMemberBonusDetail,
                               CuMemberSettleExt sourceCuMemberSettleExt, CuMemberSettleExt targetCuMemberSettleExt) {
        String remark = String.format(BonusMsgConstants.HI_FUN, saOrderExt.getOrderCode(),
                sourceCuMemberSettleExt.getMemberCode() + " " + sourceCuMemberSettleExt.getMemberName(),
                targetCuMemberSettleExt.getMemberCode() + " " + targetCuMemberSettleExt.getMemberName(),
                cuMemberBonusDetail.getRealIncome());
        cuMemberBonusDetail.setRemark(remark);
    }

    /**
     * 获得 分红收益的备注
     *
     * @param cuMemberBonusDetail 分红
     * @param settleDate          结算日期
     */
    protected void shareRemark(CuMemberBonusDetail cuMemberBonusDetail, String settleDate) {
        String remark = String.format(BonusMsgConstants.SHARE, DateUtils.getMonth(settleDate),
                cuMemberBonusDetail.getPretaxIncome());
        cuMemberBonusDetail.setRemark(remark);
    }

    /**
     * 获得 分红收益的备注
     *
     * @param cuMemberBonusDetail 分红
     * @param settleDate          结算日期
     */
    protected void shareCouponRemark(CuMemberBonusDetail cuMemberBonusDetail, String settleDate) {
        String remark = String.format(BonusMsgConstants.SHARE_COUPON, DateUtils.getMonth(settleDate),
                cuMemberBonusDetail.getPretaxIncome());
        cuMemberBonusDetail.setRemark(remark);
    }

    /**
     * 获得 店铺收益的备注
     *
     * @param cuMemberBonusDetail 分红
     * @param settleDate          结算日期
     */
    protected void storeRemark(CuMemberBonusDetail cuMemberBonusDetail, String settleDate) {
        String remark = String.format(BonusMsgConstants.STORE, DateUtils.getMonth(settleDate),
                cuMemberBonusDetail.getPretaxIncome());
        cuMemberBonusDetail.setRemark(remark);
    }

    /**
     * 获得 极差收益的备注
     *
     * @param saOrderExt              订单编号
     * @param cuMemberBonusRange      报单服务费
     * @param sourceCuMemberSettleExt 提供奖金的人
     * @param targetCuMemberSettleExt 获得奖金的人
     */
    protected void rangeRemark(SaOrderExt saOrderExt, CuMemberBonusRange cuMemberBonusRange, CuMemberSettleExt sourceCuMemberSettleExt,
                               CuMemberSettleExt targetCuMemberSettleExt, Map<String, RangeDTO> rangePkDtoMap) {
        RangeDTO beforeOrderRangeDto = rangePkDtoMap.get(targetCuMemberSettleExt.getPkSettleCountry().toString() + cuMemberBonusRange.getPkBeforeRange());
        RangeDTO orderRangeDto = rangePkDtoMap.get(sourceCuMemberSettleExt.getPkSettleCountry().toString() + cuMemberBonusRange.getPkRange());
        String remark = String.format(BonusMsgConstants.SHARE_RANGE, saOrderExt.getOrderCode(),
                sourceCuMemberSettleExt.getMemberCode() + sourceCuMemberSettleExt.getMemberName(),
                targetCuMemberSettleExt.getMemberCode() + targetCuMemberSettleExt.getMemberName(),
                cuMemberBonusRange.getCalAchieve(), beforeOrderRangeDto == null ? "" : beforeOrderRangeDto.getRangeName(), orderRangeDto == null ? "" : orderRangeDto.getRangeName());
        cuMemberBonusRange.setRemark(remark);
    }

    /**
     * 获得 极差收益的备注
     *
     * @param saOrderExt           订单编号
     * @param cuMemberBonusRange   报单服务费
     * @param sourceMemberRangeExt 提供奖金的人
     * @param targetMemberRangeExt 获得奖金的人
     */
    protected void rangeRemark(SaOrderExt saOrderExt, CuMemberBonusRange cuMemberBonusRange, CuMemberRangeExt sourceMemberRangeExt,
                               CuMemberRangeExt targetMemberRangeExt) {
        String remark = String.format(BonusMsgConstants.RANGE, saOrderExt.getOrderCode(),
                sourceMemberRangeExt.getMemberCode() + sourceMemberRangeExt.getMemberName(),
                targetMemberRangeExt.getMemberCode() + targetMemberRangeExt.getMemberName(),
                cuMemberBonusRange.getCalAchieve(), sourceMemberRangeExt.getRangeAwardsValue(), targetMemberRangeExt.getRangeAwardsValue());
        cuMemberBonusRange.setRemark(remark);
    }

    /**
     * 获得 极差收益的备注
     *
     * @param saOrderExt           订单编号
     * @param cuMemberBonusRange   报单服务费
     * @param sourceMemberRangeExt 提供奖金的人
     * @param targetMemberRangeExt 获得奖金的人
     */
    protected void oriRangeRemark(SaOrderExt saOrderExt, CuMemberBonusRange cuMemberBonusRange, CuMemberRangeExt sourceMemberRangeExt,
                                  CuMemberRangeExt targetMemberRangeExt) {
        String remark = String.format(BonusMsgConstants.ORI_RANGE, saOrderExt.getOrderCode(),
                sourceMemberRangeExt.getMemberCode() + sourceMemberRangeExt.getMemberName(),
                targetMemberRangeExt.getMemberCode() + targetMemberRangeExt.getMemberName(),
                cuMemberBonusRange.getCalAchieve(), targetMemberRangeExt.getRangeAwardsValue());
        cuMemberBonusRange.setRemark(remark);
    }

    /**
     * 获得 分享极差收益的备注
     *
     * @param saOrderExt              订单编号
     * @param cuMemberBonusRange      报单服务费
     * @param sourceCuMemberSettleExt 提供奖金的人
     * @param targetCuMemberSettleExt 获得奖金的人
     */
    protected void xueRangeRemark(SaOrderExt saOrderExt, CuMemberBonusRange cuMemberBonusRange, CuMemberSettleExt sourceCuMemberSettleExt,
                                  CuMemberSettleExt targetCuMemberSettleExt) {
        String remark = String.format(BonusMsgConstants.LE_XUE_SHARE_RANGE, saOrderExt.getOrderCode(),
                sourceCuMemberSettleExt.getMemberCode() + sourceCuMemberSettleExt.getMemberName(),
                targetCuMemberSettleExt.getMemberCode() + targetCuMemberSettleExt.getMemberName(),
                ComputeUtil.computeDivide(targetCuMemberSettleExt.getRetailPrice(), BigDecimal.valueOf(1)), targetCuMemberSettleExt.getGradeValue());
        cuMemberBonusRange.setRemark(remark);
    }

    /**
     * @param saOrder                 订单编号
     * @param cuMemberBonusDetail     云代首购
     * @param sourceCuMemberSettleExt 提供奖金的人
     * @param targetCuMemberSettleExt 获得奖金的人
     * @Description: 获得 报单服务费收益的备注
     * @return: String
     * @Author: sui q
     * @Date: 2022/11/17 11:22
     */
    protected void cloudAgentRemark(SaOrder saOrder, CuMemberBonusDetail cuMemberBonusDetail, CuMemberSettleExt sourceCuMemberSettleExt,
                                    CuMemberSettleExt targetCuMemberSettleExt, String msg) {
        String remark = String.format(msg, saOrder.getOrderCode(),
                sourceCuMemberSettleExt.getMemberCode() + sourceCuMemberSettleExt.getMemberName(),
                targetCuMemberSettleExt.getMemberCode() + targetCuMemberSettleExt.getMemberName(),
                cuMemberBonusDetail.getCalAchieve(), cuMemberBonusDetail.getCalValue());
        cuMemberBonusDetail.setRemark(remark);
    }

    /**
     * @param saOrder                 订单编号
     * @param cuMemberBonusDetail     云代首购
     * @param sourceCuMemberSettleExt 提供奖金的人
     * @param targetCuMemberSettleExt 获得奖金的人
     * @Description: 获得 报单服务费收益的备注
     * @return: String
     * @Author: sui q
     * @Date: 2022/11/17 11:22
     */
    protected void repurchaseCouponsRemark(SaOrder saOrder, CuMemberBonusDetail cuMemberBonusDetail, CuMemberSettleExt sourceCuMemberSettleExt,
                                           CuMemberSettleExt targetCuMemberSettleExt) {
        String remark = String.format(BonusMsgConstants.REPURCHASE_COUPON_RANGE, saOrder.getOrderCode(),
                sourceCuMemberSettleExt.getMemberCode() + sourceCuMemberSettleExt.getMemberName(),
                targetCuMemberSettleExt.getMemberCode() + targetCuMemberSettleExt.getMemberName(),
                cuMemberBonusDetail.getCalAchieve(), cuMemberBonusDetail.getCalValue(), cuMemberBonusDetail.getRealIncome());
        cuMemberBonusDetail.setRemark(remark);
    }

    /**
     * 获取会员列表的考核结果
     *
     * @param settleDateStr         结算日期
     * @param cuMemberSettleExtList 会员列表
     */
    protected Map<Long, CuMemberAssess> getLongCuMemberAssessMap(String settleDateStr, List<CuMemberSettleExt> cuMemberSettleExtList) {
        if (cuMemberSettleExtList.size() == 0) {
            return new HashMap<>(0);
        }
        Date settleDate = DateUtils.parseStringToDate(settleDateStr);
        // 获取结算日期的第一天的日期
        Integer assessPeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.currentMonthFirstDateStr(settleDate)).getPkId();
        String assessTableName = TableNameConstants.CU_MEMBER_ASSESS + DateUtils.getYear(settleDateStr);
        return iCuMemberAssessService.queryCuMemberAssessByMember(assessTableName, assessPeriod, EAssessmentType.REPURCHASE_ASSESS.getValue(), cuMemberSettleExtList);
    }

    /**
     * 获取会员奖金汇总表
     *
     * @param period            期间
     * @param cuMemberBonusMap  奖金汇总map
     * @param saOrder           汇率
     * @param cuMemberSettleExt 获取奖金的会员
     * @return CuMemberBonus
     */
    protected CuMemberBonus getCuMemberBonus(Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap, SaOrder saOrder, CuMemberSettleExt cuMemberSettleExt) {
        Integer pkRate;
        if (null == saOrder) {
            CurrencyDTO currencyDTO = iCurrencyServiceApi.getCurrency(cuMemberSettleExt.getPkSettleCountry()).getData();
            pkRate = currencyDTO.getPkId();
        } else {
            pkRate = saOrder.getPkRate();
            if (pkRate == null || !Objects.equals(cuMemberSettleExt.getPkSettleCountry(), saOrder.getPkCountry())) {
                // 结算会员和订单不是一个国家的，需要按结算会员的汇率进行处理
                CurrencyDTO currencyDTO = iCurrencyServiceApi.getCurrency(cuMemberSettleExt.getPkSettleCountry()).getData();
                pkRate = currencyDTO.getPkId();
            }
        }
        return getCuMemberBonus(period, cuMemberBonusMap, cuMemberSettleExt.getPkMember(), cuMemberSettleExt.getIncomeStatus(),
                cuMemberSettleExt.getPkSettleCountry(), pkRate);
    }

    protected CuMemberBonus getCuMemberBonus(Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap,
                                             Long pkMember, Integer incomeStatus, Integer pkCountry, Integer pkRate) {
        if (cuMemberBonusMap.containsKey(pkMember)) {
            return cuMemberBonusMap.get(pkMember);
        } else {
            CuMemberBonus cuMemberBonus = CuMemberBonus.builder()
                    // 暂时赋值一个long，暂时把主子表关联起来，计算后，从数据库获得索引，在修改
                    .pkId(pkMember)
                    .pkMember(pkMember)
                    .period(period)
                    .pkRate(pkRate)
                    .incomeStatus(incomeStatus)
                    .directIncome(BigDecimal.ZERO)
                    .expandIncome(BigDecimal.ZERO)
                    .expandNoCapping(BigDecimal.ZERO)
                    .expandCapping(BigDecimal.ZERO)
                    .coachIncome(BigDecimal.ZERO)
                    .shareIncome(BigDecimal.ZERO)
                    .serviceIncome(BigDecimal.ZERO)
                    .backPoints(BigDecimal.ZERO)
                    .serviceSpend(BigDecimal.ZERO)
                    .purRealSubtotal(BigDecimal.ZERO)
                    .repurPushIncome(BigDecimal.ZERO)
                    .repurRangeIncome(BigDecimal.ZERO)
                    .repurExpandIncome(BigDecimal.ZERO)
                    .repurCoachIncome(BigDecimal.ZERO)
                    .repurRealSubtotal(BigDecimal.ZERO)
                    .cloudDirectIncome(BigDecimal.ZERO)
                    .cloudPurIncome(BigDecimal.ZERO)
                    .cloudRepurIncome(BigDecimal.ZERO)
                    .cloudRealSubtotal(BigDecimal.ZERO)
                    .repurCoupon(BigDecimal.ZERO)
                    .repurCouponShare(BigDecimal.ZERO)
                    .repurCouponSubtotal(BigDecimal.ZERO)
                    .globalPoints(BigDecimal.ZERO)
                    .carAwardPoints(BigDecimal.ZERO)
                    .hiFunIncome(BigDecimal.ZERO)
                    .haiFunIncome(BigDecimal.ZERO)
                    .storeIncome(BigDecimal.ZERO)
                    .makerDirectIncome(BigDecimal.ZERO)
                    .makerShareIncome(BigDecimal.ZERO)
                    .makerRealSubtotal(BigDecimal.ZERO)
                    .realIncomeTotal(BigDecimal.ZERO)
                    .build();
            cuMemberBonus.setPkCreator(MagicNumberConstants.PK_ADMIN);
            cuMemberBonus.setPkCountry(pkCountry);
            cuMemberBonus.setGrantStatus(EGrantStatus.SETTLED.getValue());
            cuMemberBonus.setPkModified(pkMember);
            cuMemberBonusMap.put(pkMember, cuMemberBonus);
            return cuMemberBonus;
        }
    }

    /**
     * 获取会员拓展收益明细
     */
    protected CuMemberBonusExpand getCuMemberBonusExpand(SaOrderExt saOrderExt, int touchNum, int i, CuMemberSettleExt targetMemberSettleExt, BigDecimal countryExpandRatio,
                                                         CuMemberBonus cuMemberBonus, BigDecimal calAchieve, Integer pkBonusItems, BigDecimal addTouch, BdGrade bdGrade) {
        CuMemberBonusExpand cuMemberBonusExpand = CuMemberBonusExpand.builder()
                .pkBonus(cuMemberBonus.getPkId())
                .pkOrder(saOrderExt.getPkId())
                .pkBonusItems(pkBonusItems)
                .calType(ECalType.RATIO.getValue())
                .incomeStatus(targetMemberSettleExt.getIncomeStatus())
                .currentTouch(i)
                .expandTouch(touchNum)
                .addTouch(addTouch)
                .calValue(countryExpandRatio)
                .calAchieve(calAchieve)
                .pretaxIncome(BigDecimal.ZERO)
                .incomeTax(BigDecimal.ZERO)
                .realIncome(BigDecimal.ZERO)
                .incomeRatio(BigDecimal.ZERO)
                .incomeDialRatio(BigDecimal.ZERO)
                .orderDialRatio(BigDecimal.ZERO)
                .pkMember(targetMemberSettleExt.getPkMember())
                .delayExpandCapping(bdGrade.getExpandCapping())
                .unGrantIncome(targetMemberSettleExt.getUnGrantIncome())
                .calFirAchieve(BigDecimal.ZERO)
                .calRepAchieve(BigDecimal.ZERO)
                .build();
        cuMemberBonusExpand.setPkCountry(targetMemberSettleExt.getPkSettleCountry());
        cuMemberBonusExpand.setPkCreator(MagicNumberConstants.PK_ADMIN);
        cuMemberBonusExpand.setPkModified(targetMemberSettleExt.getPkMember());
        if (EAccountStatus.STOP_PROFIT.getValue() == targetMemberSettleExt.getAccountStatus()) {
            cuMemberBonusExpand.setIncomeStatus(EBonusIncomeStatus.STOP_INCOME.getValue());
        }
        if (EExpireStatus.TERMINATED.getValue() == targetMemberSettleExt.getExpireStatus()) {
            cuMemberBonusExpand.setIncomeStatus(EBonusIncomeStatus.FIRE_BURN.getValue());
        }
        if (EGrade.VIP.getValue() == bdGrade.getGradeValue()
                || EGrade.S_VIP.getValue() == bdGrade.getGradeValue()) {
            if (targetMemberSettleExt.getUnGrantIncome().compareTo(BigDecimal.ZERO) > 0) {
                // new TODO 会员是V3、V4，扩展收益延缓发放有值，则状态直接为延缓发放
                cuMemberBonusExpand.setIncomeStatus(EBonusIncomeStatus.DELAY_GRANT.getValue());
            }
        }
        if (EOrderType.REGISTER_ORDER.getValue() == saOrderExt.getOrderType()
                || EOrderType.UPGRADE_ORDER.getValue() == saOrderExt.getOrderType()
                || EOrderType.FICTITIOUS_ORDER.getValue() == saOrderExt.getOrderType()) {
            // 注册、升级、虚拟（空单） 算在首购里面
            cuMemberBonusExpand.setCalFirAchieve(calAchieve);
        } else {
            // 其它的算复购
            cuMemberBonusExpand.setCalRepAchieve(calAchieve);
        }
        return cuMemberBonusExpand;
    }

    /**
     * 获取会员拓展收益延缓明细
     */
    protected CuMemberBonusExpand getCuMemberBonusExpandDelay(CuMemberSettleExt targetMemberSettleExt, CuMemberBonus cuMemberBonus,
                                                              BigDecimal expandRatio, Integer pkBonusItems, BdGrade bdGrade) {
        CuMemberBonusExpand cuMemberBonusExpand = CuMemberBonusExpand.builder()
                .pkBonus(cuMemberBonus.getPkId())
                .pkBonusItems(pkBonusItems)
                .incomeStatus(targetMemberSettleExt.getIncomeStatus())
                .pkOrder(0L)
                .calType(ECalType.RATIO.getValue())
                .calValue(expandRatio)
                .calAchieve(BigDecimal.ZERO)
                .currentTouch(0)
                .expandTouch(0)
                .addTouch(BigDecimal.ZERO)
                .pretaxIncome(BigDecimal.ZERO)
                .incomeRatio(BigDecimal.ZERO)
                .incomeDialRatio(BigDecimal.ZERO)
                .orderDialRatio(BigDecimal.ZERO)
                .incomeTax(BigDecimal.ZERO)
                .realIncome(BigDecimal.ZERO)
                .pkMember(targetMemberSettleExt.getPkMember())
                .delayExpandCapping(bdGrade.getExpandCapping())
                .unGrantIncome(null != targetMemberSettleExt.getUnGrantIncome() ? targetMemberSettleExt.getUnGrantIncome() : BigDecimal.ZERO)
                .calFirAchieve(BigDecimal.ZERO)
                .calRepAchieve(BigDecimal.ZERO)
                .build();
        cuMemberBonusExpand.setPkCountry(targetMemberSettleExt.getPkSettleCountry());
        cuMemberBonusExpand.setPkCreator(MagicNumberConstants.PK_ADMIN);
        cuMemberBonusExpand.setPkModified(targetMemberSettleExt.getPkMember());
        if (EAccountStatus.STOP_PROFIT.getValue() == targetMemberSettleExt.getAccountStatus()) {
            cuMemberBonusExpand.setIncomeStatus(EBonusIncomeStatus.STOP_INCOME.getValue());
        }
        if (EExpireStatus.TERMINATED.getValue() == targetMemberSettleExt.getExpireStatus()) {
            cuMemberBonusExpand.setIncomeStatus(EBonusIncomeStatus.FIRE_BURN.getValue());
        }
        return cuMemberBonusExpand;
    }

    protected void packageCuMemberBonusExpand(CuMemberBonusExpand cuMemberBonusExpand, CuMemberBonus cuMemberBonus,
                                              BigDecimal expandCapping, BigDecimal maxIncome, BigDecimal actualIncome) {
        cuMemberBonusExpand.setExpandNoCapping(maxIncome);
        cuMemberBonusExpand.setExpandCapping(maxIncome);
        // 累计扩展收益 = 累计扩展收益 + 本次扩展收益
        BigDecimal expandIncome = ComputeUtil.computeAdd(cuMemberBonus.getExpandCapping(), maxIncome);
        if (ComputeUtil.compareValue(expandIncome, expandCapping)) {
            // 累计扩展收益 >= 扩展封顶，封顶就是 扩展封顶
            cuMemberBonusExpand.setExpandCapping(ComputeUtil.computeSubtractAbs(cuMemberBonus.getExpandCapping(), maxIncome));
            cuMemberBonus.setExpandCapping(expandCapping);
        } else {
            // 累计扩展收益 < 扩展封顶，封顶就是 累计扩展收益
            cuMemberBonus.setExpandCapping(expandIncome);
        }
        cuMemberBonusExpand.setPretaxIncome(actualIncome);
        cuMemberBonusExpand.setExpandNormalHalf(ComputeUtil.computeMultiplyHalf(cuMemberBonusExpand.getExpandCapping()));
        cuMemberBonusExpand.setExpandNormalReal(ComputeUtil.computeMultiplyHalf(cuMemberBonusExpand.getPretaxIncome()));
    }

    protected void packageCuMemberBonusRepurchaseExpand(CuMemberBonusExpand cuMemberBonusExpand, CuMemberBonus cuMemberBonus,
                                                        BigDecimal expandCapping, BigDecimal income, BigDecimal actualIncome) {
        cuMemberBonusExpand.setExpandNoCapping(income);
        cuMemberBonusExpand.setExpandCapping(income);
        BigDecimal expandIncome = ComputeUtil.computeAdd(cuMemberBonus.getExpandCapping(), income);
        if (ComputeUtil.compareValue(expandIncome, expandCapping)) {
            cuMemberBonusExpand.setExpandCapping(ComputeUtil.computeSubtractAbs(cuMemberBonus.getExpandCapping(), income));
        }
        cuMemberBonusExpand.setPretaxIncome(actualIncome);
        cuMemberBonusExpand.setExpandNormalHalf(ComputeUtil.computeMultiplyHalf(cuMemberBonusExpand.getExpandCapping()));
        cuMemberBonusExpand.setExpandNormalReal(ComputeUtil.computeMultiplyHalf(cuMemberBonusExpand.getPretaxIncome()));
    }

    /**
     * 根据订单获取会员的血缘父级，用于嗨粉推荐、复购极差
     *
     * @param cuMemberSettleExtMap 树关系
     * @param settleTableName      结算表
     * @param saOrderList          订单
     */
    protected List<CuMemberSettleExt> getMemberParentBySaOrder(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, String settleTableName, List<SaOrderExt> saOrderList) {
        Set<Long> memberSet = new HashSet<>();
        saOrderList.forEach(saOrderExt -> memberSet.add(saOrderExt.getPkMember()));
        List<CuMemberSettleExt> cuMemberSettleExtList = iCuMemberTreeService.batchQueryCuMemberSettleParent(settleTableName, memberSet);
        getMemberSettle(cuMemberSettleExtMap, cuMemberSettleExtList);
        return cuMemberSettleExtList;
    }

    /**
     * @param awardsMap        奖衔
     * @param parentMemberTree 会员树历史记录
     * @Description: 获取会员的极差等级，验证会员是否有奖衔
     * @return: String
     * @Author: sui q
     * @Date: 2023/1/4 14:22
     */
    protected String getMemberCountryKey(Map<String, BdAwards> awardsMap, CuMemberTree parentMemberTree, Map<Long, CuAwardsControl> cuAwardsControlMap) {
        Integer pkAwards = getPkRealAwards(parentMemberTree.getPkMember(), parentMemberTree.getPkAwards(), cuAwardsControlMap);
        String key = parentMemberTree.getPkSettleCountry().toString() + ERangeType.AWARDS.getValue() + pkAwards;
        String awardKey = parentMemberTree.getPkSettleCountry() + parentMemberTree.getPkAwards().toString();
        if (awardsMap.get(awardKey) != null && EAwards.MEMBER.getValue() == awardsMap.get(awardKey).getAwardsValue()) {
            key = parentMemberTree.getPkSettleCountry().toString() + ERangeType.GRADE.getValue() + parentMemberTree.getPkGrade();
        }
        return key;
    }

    protected RangeDTO getRangeDto(Map<String, RangeDTO> rangeDtoMap, Map<String, BdAwards> awardsMap, CuMemberTree parentMemberTree, Map<Long, CuAwardsControl> cuAwardsControlMap) {
        String key = getMemberCountryKey(awardsMap, parentMemberTree, cuAwardsControlMap);
        return rangeDtoMap.get(key);
    }

    /**
     * @param awardsMap        奖衔
     * @param parentMemberTree 会员树历史记录
     * @Description: 获取会员的极差等级，验证会员是否有奖衔
     * @return: String
     * @Author: sui q
     * @Date: 2023/1/4 14:22
     */
    protected String getMemberKey(Map<String, BdAwards> awardsMap, CuMemberTree parentMemberTree) {
        String key = Integer.toString(ERangeType.AWARDS.getValue()) + parentMemberTree.getPkAwards();
        String awardKey = parentMemberTree.getPkSettleCountry().toString() + parentMemberTree.getPkAwards().toString();
        if (EAwards.MEMBER.getValue() == awardsMap.get(awardKey).getAwardsValue()) {
            key = Integer.toString(ERangeType.GRADE.getValue()) + parentMemberTree.getPkGrade();
        }
        return key;
    }

    /**
     * @param saOrderExt            订单
     * @param targetMemberSettleExt 奖金网体
     * @param cuMemberBonus         奖金主表
     * @Description: 奖金明细 报单服务费、分红、云代首购、云代复购、海豆收益
     * @return: CuMemberBonusDetail
     * @Author: sui q
     * @Date: 2023/1/5 16:31
     */
    protected CuMemberBonusDetail getCuMemberBonusDetail(SaOrder saOrderExt, CuMemberSettleExt targetMemberSettleExt, CuMemberBonus cuMemberBonus) {
        CuMemberBonusDetail cuMemberBonusDetail = CuMemberBonusDetail.builder()
                .pkBonus(cuMemberBonus.getPkId())
                .pkOrder(saOrderExt.getPkId())
                .calType(ECalType.RATIO.getValue())
                .incomeStatus(targetMemberSettleExt.getIncomeStatus())
                .calAchieve(saOrderExt.getOrderAchieve())
                .pretaxIncome(BigDecimal.ZERO)
                .incomeTax(BigDecimal.ZERO)
                .realIncome(BigDecimal.ZERO)
                .incomeRatio(BigDecimal.ZERO)
                .incomeDialRatio(BigDecimal.ZERO)
                .orderDialRatio(BigDecimal.ZERO)
                .build();
        cuMemberBonusDetail.setPkCountry(targetMemberSettleExt.getPkSettleCountry());
        cuMemberBonusDetail.setPkCreator(MagicNumberConstants.PK_ADMIN);
        if (EExpireStatus.TERMINATED.getValue() == targetMemberSettleExt.getExpireStatus()) {
            cuMemberBonusDetail.setIncomeStatus(EBonusIncomeStatus.FIRE_BURN.getValue());
        }
        return cuMemberBonusDetail;
    }

    /**
     * 设置奖金扣项，计算税前收益，扣税，实际收益, 设置主表
     *
     * @param cuMemberBonus 奖金明细
     * @param bonusDetail   奖金明细表
     */
    protected void setBonusDeduct(BdBonusDeduct bdBonusDeduct, CuMemberBonus cuMemberBonus, Object bonusDetail) {
        BigDecimal pretaxIncome = ReflectUtils.invokeGetter(bonusDetail, BonusFieldConstants.PRETAX_INCOME);
        BigDecimal backRatio = BigDecimal.ZERO;
        BigDecimal platformRatio = BigDecimal.ZERO;
        if (bdBonusDeduct != null) {
            backRatio = bdBonusDeduct.getBackRatio();
            platformRatio = bdBonusDeduct.getPlatformRatio();
        }
        cuMemberBonus.setBackPoints(ComputeUtil.computeAdd(cuMemberBonus.getBackPoints(), ComputeUtil.computeBonusMultiply(pretaxIncome, backRatio)));
        cuMemberBonus.setServiceSpend(ComputeUtil.computeAdd(cuMemberBonus.getServiceSpend(), ComputeUtil.computeBonusMultiply(pretaxIncome, platformRatio)));
    }

    /**
     * 获得需要查询的会员，免考通过的不需要查询考核记录
     *
     * @param cuMemberSettleExtMap  存储目前有的会员数据
     * @param cuMemberSettleExtList 需要查询的会员数据
     */
    protected List<CuMemberSettleExt> getQueryMemberSettleExtList(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, List<CuMemberSettleExt> cuMemberSettleExtList) {
        // 过滤数据，免考的会员不进行查询
        List<CuMemberSettleExt> queryMemberList = new ArrayList<>();
        cuMemberSettleExtList.forEach(cuMemberSettleExt -> {
            CuMemberSettleExt memberSettleExt = cuMemberSettleExtMap.get(cuMemberSettleExt.getPkMember());
            // 免考的不查询
            if (memberSettleExt == null || EYesNo.NO.getIntValue() == memberSettleExt.getPurchaseStatus()) {
                queryMemberList.add(cuMemberSettleExt);
            }
        });
        return queryMemberList;
    }

    /**
     * 首购计算 扣项，小计，总计
     */
    protected void setFirstPurchaseTotal(BdBonusDeduct bdBonusDeduct, CuMemberBonus cuMemberBonus, Object bonusDetail) {
        // 处理扣项
        setBonusDeduct(bdBonusDeduct, cuMemberBonus, bonusDetail);
        BigDecimal realIncome = ReflectUtils.invokeGetter(bonusDetail, BonusFieldConstants.REAL_INCOME);
        // 首购实发小计
        cuMemberBonus.setPurRealSubtotal(ComputeUtil.computeAdd(cuMemberBonus.getPurRealSubtotal(), realIncome));
        // 实发收益总计
        cuMemberBonus.setRealIncomeTotal(ComputeUtil.computeAdd(cuMemberBonus.getRealIncomeTotal(), realIncome));
    }

    /**
     * 复购计算 扣项，小计，总计
     */
    protected void setPurchaseTotal(BdBonusDeduct bdBonusDeduct, CuMemberBonus cuMemberBonus, Object bonusDetail) {
        // 处理扣项
        setBonusDeduct(bdBonusDeduct, cuMemberBonus, bonusDetail);
        BigDecimal realIncome = ReflectUtils.invokeGetter(bonusDetail, BonusFieldConstants.REAL_INCOME);
        // 复购实发小计
        cuMemberBonus.setRepurRealSubtotal(ComputeUtil.computeAdd(cuMemberBonus.getRepurRealSubtotal(), realIncome));
        // 实发收益总计
        cuMemberBonus.setRealIncomeTotal(ComputeUtil.computeAdd(cuMemberBonus.getRealIncomeTotal(), realIncome));
    }

    /**
     * 云代计算 扣项，小计，总计
     */
    protected void setCloudTotal(BdBonusDeduct bdBonusDeduct, CuMemberBonus cuMemberBonus, Object bonusDetail) {
        setBonusDeduct(bdBonusDeduct, cuMemberBonus, bonusDetail);
        BigDecimal realIncome = ReflectUtils.invokeGetter(bonusDetail, BonusFieldConstants.REAL_INCOME);
        cuMemberBonus.setCloudRealSubtotal(ComputeUtil.computeAdd(cuMemberBonus.getCloudRealSubtotal(), realIncome));
        cuMemberBonus.setRealIncomeTotal(ComputeUtil.computeAdd(cuMemberBonus.getRealIncomeTotal(), realIncome));
    }

    /**
     * 复购券 扣项，小计，总计
     */
    protected void setRepurchaseCouponTotal(BdBonusDeduct bdBonusDeduct, CuMemberBonus cuMemberBonus, Object bonusDetail) {
        setBonusDeduct(bdBonusDeduct, cuMemberBonus, bonusDetail);
        BigDecimal realIncome = ReflectUtils.invokeGetter(bonusDetail, BonusFieldConstants.REAL_INCOME);
        cuMemberBonus.setRepurCouponSubtotal(ComputeUtil.computeAdd(cuMemberBonus.getRepurCouponSubtotal(), realIncome));
    }

    /**
     * 设置奖金扣项，计算税前收益，扣税，实际收益, 设置明细表
     *
     * @param cuMemberDetail 奖金明细
     * @param orderAmount    订单金额
     * @param exchangeRate   入汇率
     */
    protected BdBonusDeduct setBonusDetailDeduct(BonusConfigDTO bonusConfigDTO, Object cuMemberDetail, BigDecimal orderAmount, BigDecimal exchangeRate) {
        Integer pkBonusItems = ReflectUtils.invokeGetter(cuMemberDetail, BonusFieldConstants.PK_BONUS_ITEMS);
        BdBonusDeduct bdBonusDeduct = bonusConfigDTO.getBonusDeductsMap().get(pkBonusItems);
        // 税前收益
        BigDecimal pretaxIncome = ReflectUtils.invokeGetter(cuMemberDetail, BonusFieldConstants.PRETAX_INCOME);
        // 扣税收益（两个10%）
        BigDecimal incomeTax = BigDecimal.ZERO;
        if (bdBonusDeduct != null) {
            incomeTax = ComputeUtil.computeBonusMultiply(pretaxIncome, ComputeUtil.computeAdd(bdBonusDeduct.getPlatformRatio(), bdBonusDeduct.getBackRatio()));
        }
        // 实际收益
        BigDecimal realIncome = ComputeUtil.computeSubtract(pretaxIncome, incomeTax);
        // 扣除个税
        ReflectUtils.invokeSetter(cuMemberDetail, BonusFieldConstants.INCOME_TAX, incomeTax);
        // 实际收益
        ReflectUtils.invokeSetter(cuMemberDetail, BonusFieldConstants.REAL_INCOME, realIncome);
        // 收益占比
        ReflectUtils.invokeSetter(cuMemberDetail, BonusFieldConstants.INCOME_RATIO, ComputeUtil.computeDivide(realIncome, ComputeUtil.computeDivide(orderAmount, exchangeRate)));
        return bdBonusDeduct;
    }


    protected void setCuMemberAchieve(SaOrderExt saOrderExt, Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, BonusConfigDTO bonusConfigDTO) {
        CuMemberSettleExt cuMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
        if (EOrderType.REGISTER_ORDER.getValue() == saOrderExt.getOrderType()
                || Objects.equals(EOrderType.SPECIAL_REGISTER_ORDER.getValue(), saOrderExt.getOrderType())) {
            cuMemberSettleExt.setRegisterAmount(ComputeUtil.computeAdd(cuMemberSettleExt.getRegisterAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
            cuMemberSettleExt.setRegisterPv(ComputeUtil.computeAdd(cuMemberSettleExt.getRegisterPv(), saOrderExt.getOrderAchieve()));
            cuMemberSettleExt.setRegisterNewAmount(ComputeUtil.computeAdd(cuMemberSettleExt.getRegisterNewAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
            cuMemberSettleExt.setRegisterNewPv(ComputeUtil.computeAdd(cuMemberSettleExt.getRegisterNewPv(), saOrderExt.getOrderAchieve()));
        } else {
            cuMemberSettleExt.setUpgradeAmount(ComputeUtil.computeAdd(cuMemberSettleExt.getUpgradeAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
            cuMemberSettleExt.setUpgradePv(ComputeUtil.computeAdd(cuMemberSettleExt.getUpgradePv(), saOrderExt.getOrderAchieve()));
            cuMemberSettleExt.setUpgradeNewAmount(ComputeUtil.computeAdd(cuMemberSettleExt.getUpgradeNewAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
            cuMemberSettleExt.setUpgradeNewPv(ComputeUtil.computeAdd(cuMemberSettleExt.getUpgradeNewPv(), saOrderExt.getOrderAchieve()));
        }
    }

    /**
     * @param orderExtList 订单列表
     * @Description: 根据订单分类，分为首购、复购、真实，获取新增业绩
     */
    public void assortSaOrder(Map<Long, MemberAchieveParam> cuMemberAchieveHashMap, List<SaOrderExt> orderExtList,
                              BonusConfigDTO bonusConfigDTO, Boolean isFirst) {
        // 注册 + 升级 首购
        // 除（注册 + 升级）其他 复购
        // 首购 + 复购 = 真实
        for (SaOrderExt saOrderExt : orderExtList) {
            MemberAchieveParam cuMemberAchieve;
            if (cuMemberAchieveHashMap.containsKey(saOrderExt.getPkMember())) {
                cuMemberAchieve = cuMemberAchieveHashMap.get(saOrderExt.getPkMember());
                // 所有的都算真实业绩
            } else {
                cuMemberAchieve = MemberAchieveParam.builder()
                        .pkRate(saOrderExt.getPkRate())
                        .pkMember(saOrderExt.getPkMember())
                        .newAmount(BigDecimal.ZERO)
                        .newPv(BigDecimal.ZERO)
                        .repNewAmount(BigDecimal.ZERO)
                        .repNewPv(BigDecimal.ZERO)
                        .newRealAmount(BigDecimal.ZERO)
                        .newRealPv(BigDecimal.ZERO)
                        .build();
                cuMemberAchieveHashMap.put(saOrderExt.getPkMember(), cuMemberAchieve);
            }
            if (isFirst) {
                // 首购处理
                cuMemberAchieve.setNewAmount(ComputeUtil.computeAdd(cuMemberAchieve.getNewAmount(), saOrderExt.getOrderAmount()));
                cuMemberAchieve.setNewPv(ComputeUtil.computeAdd(cuMemberAchieve.getNewPv(), saOrderExt.getOrderAchieve()));
            } else {
                // 非首购处理
                cuMemberAchieve.setRepNewAmount(ComputeUtil.computeAdd(cuMemberAchieve.getRepNewAmount(), saOrderExt.getOrderAmount()));
                cuMemberAchieve.setRepNewPv(ComputeUtil.computeAdd(cuMemberAchieve.getRepNewPv(), saOrderExt.getOrderAchieve()));
            }
            cuMemberAchieve.setNewRealAmount(ComputeUtil.computeAdd(cuMemberAchieve.getNewRealAmount(), saOrderExt.getOrderAmount()));
            cuMemberAchieve.setNewRealPv(ComputeUtil.computeAdd(cuMemberAchieve.getNewRealPv(), saOrderExt.getOrderAchieve()));
        }
    }

    /**
     * 处理汇率
     *
     * @return
     */
    protected BigDecimal convertLocalCurrency(SaOrder saOrderExt, BonusConfigDTO bonusConfigDTO) {
        Map<String, BdCurrencyDTO> currencyDtoMap = bonusConfigDTO.getCurrencyDtoMap();
        String key = saOrderExt.getPkCountry().toString() + saOrderExt.getPkRate();
        BigDecimal orderAmount = BigDecimal.ZERO;
        if (currencyDtoMap.containsKey(key)) {
            orderAmount = ComputeUtil.computeDivide(saOrderExt.getOrderAmount(), currencyDtoMap.get(key).getOutExchangeRate());
        }
        return orderAmount;
    }

    /**
     * 根据会员主键列表获得会员
     *
     * @param cuMemberSettleExtMap
     * @param settleDate
     * @param settleTableName
     * @param memberSet
     * @return
     */
    protected Map<Long, CuMemberAssess> packageMemberSettleByPkMemberSet(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, String settleDate, String settleTableName, Set<Long> memberSet) {
        if (memberSet.size() > 0) {
            List<CuMemberSettleExt> cuMemberSettleExtList = iCuMemberTreeService.batchQueryMemberSettleByMemberPk(settleTableName, memberSet);
            // 初始化父类网体的值
            getMemberSettle(cuMemberSettleExtMap, cuMemberSettleExtList);
            return getLongCuMemberAssessMap(settleDate, cuMemberSettleExtList);
        }
        return null;
    }

    /**
     * 延缓发放的奖金计算扣项
     *
     * @param bonusConfigDTO
     * @param cuMemberDetail
     * @return
     */
    protected BdBonusDeduct setDeplyBonusDetailDeduct(BonusConfigDTO bonusConfigDTO, CuMemberBonusExpand cuMemberDetail) {
        Integer pkBonusItems = cuMemberDetail.getPkBonusItems();
        BdBonusDeduct bdBonusDeduct = bonusConfigDTO.getBonusDeductsMap().get(pkBonusItems);
        // 税前收益
        BigDecimal pretaxIncome = cuMemberDetail.getPretaxIncome();
        // 扣税收益（两个10%）
        BigDecimal incomeTax = BigDecimal.ZERO;
        if (bdBonusDeduct != null) {
            incomeTax = ComputeUtil.computeBonusMultiply(pretaxIncome, ComputeUtil.computeAdd(bdBonusDeduct.getPlatformRatio(), bdBonusDeduct.getBackRatio()));
        }
        // 实际收益
        BigDecimal realIncome = ComputeUtil.computeSubtract(pretaxIncome, incomeTax);
        // 扣除个税
        cuMemberDetail.setIncomeTax(incomeTax);
        // 实际收益
        cuMemberDetail.setRealIncome(realIncome);
        return bdBonusDeduct;
    }

}
