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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.domain.bonus.BdBonusDeduct;
import com.hzs.common.domain.member.bonus.CuMemberBonus;
import com.hzs.common.domain.member.bonus.CuMemberBonusExpand;
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.system.config.BdGrade;
import com.hzs.system.config.dto.BonusConfigDTO;
import com.hzs.system.config.dto.BonusExpandDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class BonusSettleNewExpandHandle extends BonusSettleHandle {

    /**
     * 计算新拓展收益
     */
    List<CuMemberBonusExpand> calculateNewExpandBonus(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, String settleTableName,
                                                      BonusConfigDTO bonusConfigDTO, List<SaOrderExt> expandOrderList, Integer period,
                                                      Map<Long, CuMemberBonus> cuMemberBonusMap, Map<Long, CuBonusExpandExt> cuBonusExpandExtMap) {
        List<CuMemberBonusExpand> cuMemberBonusExpandList = new ArrayList<>();
        // 获取所有订单的上级，根据订单往上一步步计算
        // key：订单ID，value：订单向上找安置人列表（会查询出来延缓发放奖金数据）
        Map<Long, List<CuMemberSettleExt>> memberSettleMap = iCuMemberTreeService.batchQueryCuMemberSettlePlaceParent(settleTableName, period, expandOrderList);

        // 订单安置上找所有会员列表
        List<CuMemberSettleExt> memberSettleExtList = new ArrayList<>();
        for (List<CuMemberSettleExt> cuMemberSettleExt : memberSettleMap.values()) {
            memberSettleExtList.addAll(cuMemberSettleExt);
        }
        // 订单安置上所有会员List转成map
        getMemberSettle(cuMemberSettleExtMap, memberSettleExtList);

        for (SaOrderExt saOrderExt : expandOrderList) {
            // 所有需要计算的父节点，只计算新增（当前订单会员向上找的所有安置人）
            List<CuMemberSettleExt> cuMemberSettleExtList = memberSettleMap.get(saOrderExt.getPkId());
            // 这三个参数是单独配置4-N的参数
            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> expandList = calculateNewExpandBonusOne(cuMemberSettleExtMap, bonusConfigDTO, period,
                    cuMemberBonusMap, saOrderExt, cuMemberSettleExtList, addTouch, expandRatio, expandNumber);
            if (expandList.size() > 0) {
                cuMemberBonusExpandList.addAll(expandList);
            }
        }

        return cuMemberBonusExpandList;
    }


    /**
     * 计算量奖（拓展收益）
     */
    private List<CuMemberBonusExpand> calculateNewExpandBonusOne(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, BonusConfigDTO bonusConfigDTO,
                                                                 Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap,
                                                                 SaOrderExt saOrderExt, List<CuMemberSettleExt> cuMemberSettleExtList,
                                                                 BigDecimal addTouch, BigDecimal expandRatio, Integer expandNumber) {
        List<CuMemberBonusExpand> cuMemberBonusExpandList = new ArrayList<>();
        if (CollectionUtil.isEmpty(cuMemberSettleExtList)) {
            return cuMemberBonusExpandList;
        }
        getMemberSettle(cuMemberSettleExtMap, cuMemberSettleExtList);

        CuMemberSettleExt settleExt = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
        if (null == settleExt) {
            return cuMemberBonusExpandList;
        }

        // 先计算新增，根据新增计算碰次
        // 先进行初始化所有的安置人，存储需要修改的值,cuMemberSettleExtMap最终修改的
        Map<Integer, BonusExpandDTO> bdBonusExpandMap = bonusConfigDTO.getBonusExpandMap();
        // 先准备值，准备好值后，开始计算，计算碰次
        int touchNum = calculateTouchNum(cuMemberSettleExtMap, saOrderExt, cuMemberSettleExtList, bonusConfigDTO);
        // 有碰次，计算拓展奖
        if (touchNum > 0) {
            // 判断是否>3 是否需要启动4-N，4-N的参数按照伞上各安置人结算国家进行获取
            // 先计算前3碰
            int startTouch = touchNum;
            // 后面碰次
            int endTouch = 0;
            // 拓展次数（一般配置的为3，前3碰保持原来的处理逻辑，3次以后判断是否启动4-N，进行分摊收益）
            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 (null == targetMemberSettleExt
                            || 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 (ComputeUtil.compareValue(targetMemberSettleExt.getAExpandPv()) && ComputeUtil.compareValue(targetMemberSettleExt.getBExpandPv())) {
                        // 根据会员等级获取对应的配置
                        BdGrade bdGrade = getGradeExpand(bonusConfigDTO, targetMemberSettleExt);
                        BigDecimal countryExpandRatio = bdGrade.getRepurchaseRatio();
                        // 计算量碰
                        calculateExpandBonusByAchieve(period, cuMemberBonusMap, bonusConfigDTO, cuMemberBonusExpandList, saOrderExt,
                                touchNum, i, sourceMemberSettleExt, targetMemberSettleExt, bdGrade, countryExpandRatio, BigDecimal.ZERO);
                        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 (ComputeUtil.compareValue(targetMemberSettleExt.getAExpandPv()) && ComputeUtil.compareValue(targetMemberSettleExt.getBExpandPv())) {
                            // 根据会员等级获取对应的配置
                            BdGrade bdGrade = getGradeExpand(bonusConfigDTO, targetMemberSettleExt);
                            // 获取国家4-n的配置
                            BigDecimal countryExpandRatio = getCountryRepurchaseExpand(bonusConfigDTO, targetMemberSettleExt, endTouch, bdGrade, addTouch, expandRatio);
                            // 计算量碰
                            calculateExpandBonusByAchieve(period, cuMemberBonusMap, bonusConfigDTO, cuMemberBonusExpandList, saOrderExt,
                                    touchNum, i + startTouch, sourceMemberSettleExt, targetMemberSettleExt, bdGrade, countryExpandRatio, addTouch);
                            break;
                        }
                    }
                }
            }
        }
        return cuMemberBonusExpandList;
    }

    /**
     * 计算碰次
     */
    private int calculateTouchNum(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, SaOrderExt saOrderExt,
                                  List<CuMemberSettleExt> cuMemberSettleExtList, BonusConfigDTO bonusConfigDTO) {
        int touchNum = 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 (EPlaceDept.LEFT_DEPT.getValue() == cuMemberSettleExt.getPlaceDept()) {
                // 左区
                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()));

                // 需要区分结余的 首购 + 复购 组成
                if (EOrderType.REGISTER_ORDER.getValue() == saOrderExt.getOrderType()
                        || EOrderType.UPGRADE_ORDER.getValue() == saOrderExt.getOrderType()
                        || EOrderType.FICTITIOUS_ORDER.getValue() == saOrderExt.getOrderType()) {
                    // 注册、升级、虚拟（空单） 算在首购里面
                    targetMemberSettleExt.setAFirBalance(ComputeUtil.computeAdd(targetMemberSettleExt.getAFirBalance(), saOrderExt.getUploadAchieve()));
                } else {
                    // 其它的算复购
                    targetMemberSettleExt.setARepBalance(ComputeUtil.computeAdd(targetMemberSettleExt.getARepBalance(), saOrderExt.getUploadAchieve()));
                }
            } 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 (EOrderType.REGISTER_ORDER.getValue() == saOrderExt.getOrderType()
                        || EOrderType.UPGRADE_ORDER.getValue() == saOrderExt.getOrderType()
                        || EOrderType.FICTITIOUS_ORDER.getValue() == saOrderExt.getOrderType()) {
                    // 注册、升级、虚拟（空单） 算在首购里面
                    targetMemberSettleExt.setBFirBalance(ComputeUtil.computeAdd(targetMemberSettleExt.getBFirBalance(), saOrderExt.getUploadAchieve()));
                } else {
                    // 其它的算复购
                    targetMemberSettleExt.setBRepBalance(ComputeUtil.computeAdd(targetMemberSettleExt.getBRepBalance(), saOrderExt.getUploadAchieve()));
                }
            }

            // 2025.05.14 对碰大小值走配置
            BigDecimal small = new BigDecimal("1440");
            BigDecimal big = new BigDecimal("2880");
            BonusExpandDTO bonusExpandDTO = bonusConfigDTO.getBonusExpandMap().get(saOrderExt.getPkCountry());
            if (null != bonusExpandDTO) {
                small = bonusExpandDTO.getExpandSmall();
                big = bonusExpandDTO.getExpandBig();
            }

            targetMemberSettleExt.setAExpandPv(BigDecimal.ZERO);
            targetMemberSettleExt.setBExpandPv(BigDecimal.ZERO);

            if (targetMemberSettleExt.getABalance().compareTo(targetMemberSettleExt.getBBalance()) <= 0) {
                // 左区 <= 右区
                if (targetMemberSettleExt.getABalance().compareTo(small) >= 0 && targetMemberSettleExt.getBBalance().compareTo(big) >= 0) {
                    // 左区大于1440，右区大于2880
                    touchNum++;

                    // 左区除1440，得到乘数
                    int smallNum = targetMemberSettleExt.getABalance().divide(small, 0, RoundingMode.FLOOR).intValue();
                    // 右区除2880，得到乘数
                    int bigNum = targetMemberSettleExt.getBBalance().divide(big, 0, RoundingMode.FLOOR).intValue();
                    // 得到最终乘数
                    BigDecimal num = new BigDecimal(Math.min(smallNum, bigNum));
                    targetMemberSettleExt.setAExpandPv(small.multiply(num));
                    targetMemberSettleExt.setBExpandPv(big.multiply(num));
                    // 本次拓展收益左右区需要扣减的结余数据
                    targetMemberSettleExt.setRABalance(ComputeUtil.computeSubtractAbs(targetMemberSettleExt.getRABalance(), small.multiply(num)));
                    targetMemberSettleExt.setRBBalance(ComputeUtil.computeSubtractAbs(targetMemberSettleExt.getRBBalance(), big.multiply(num)));
                }
            } else {
                // 左区 > 右区
                if (targetMemberSettleExt.getABalance().compareTo(big) >= 0 && targetMemberSettleExt.getBBalance().compareTo(small) >= 0) {
                    // 左区大于2880，右区大于1440
                    touchNum++;

                    // 左区除2880，得到乘数
                    int smallNum = targetMemberSettleExt.getABalance().divide(big, 0, RoundingMode.FLOOR).intValue();
                    // 右区除1440，得到乘数
                    int bigNum = targetMemberSettleExt.getBBalance().divide(small, 0, RoundingMode.FLOOR).intValue();
                    // 得到最终乘数
                    BigDecimal num = new BigDecimal(Math.min(smallNum, bigNum));
                    targetMemberSettleExt.setAExpandPv(big.multiply(num));
                    targetMemberSettleExt.setBExpandPv(small.multiply(num));
                    // 本次拓展收益左右区需要扣减的结余数据
                    targetMemberSettleExt.setRABalance(ComputeUtil.computeSubtractAbs(targetMemberSettleExt.getRABalance(), big.multiply(num)));
                    targetMemberSettleExt.setRBBalance(ComputeUtil.computeSubtractAbs(targetMemberSettleExt.getRBBalance(), small.multiply(num)));
                }
            }
        }
        return touchNum;
    }


    /**
     * 计算量碰
     */
    private void calculateExpandBonusByAchieve(Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap, BonusConfigDTO bonusConfigDTO,
                                               List<CuMemberBonusExpand> cuMemberBonusExpandList, SaOrderExt saOrderExt, int touchNum, int i,
                                               CuMemberSettleExt sourceMemberSettleExt, CuMemberSettleExt targetMemberSettleExt,
                                               BdGrade bdGrade, BigDecimal countryExpandRatio, BigDecimal addTouch) {
        // 获取拓展收益奖项ID
        Integer pkBonusItems = getPkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.EXPANSION_INCOME.getValue());

        // 等级对应拓展比例
        BigDecimal gradeExpandRatio = bdGrade.getExpandRatio();

        // 会员奖金汇总
        CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, targetMemberSettleExt);
        // 左区量碰业绩
        BigDecimal calAAchieve = targetMemberSettleExt.getAExpandPv();
        // 右区量碰业绩
        BigDecimal calBAchieve = targetMemberSettleExt.getBExpandPv();
        // 需要计算量碰的业绩
        BigDecimal calAchieve = calAAchieve.compareTo(calBAchieve) <= 0 ? calAAchieve : calBAchieve;

        // 拓展收益明细
        CuMemberBonusExpand cuMemberBonusExpand = getCuMemberBonusExpand(saOrderExt, touchNum, i, targetMemberSettleExt, countryExpandRatio, cuMemberBonus, calAchieve, pkBonusItems, addTouch, bdGrade);
        // 扣减a、b区结余
        targetMemberSettleExt.setABalance(ComputeUtil.computeSubtract(targetMemberSettleExt.getABalance(), calAAchieve));
        targetMemberSettleExt.setBBalance(ComputeUtil.computeSubtract(targetMemberSettleExt.getBBalance(), calBAchieve));

        // 扣减a、b区首购、复购结余
        // a区首购 + a区复购
        BigDecimal aSumBal = targetMemberSettleExt.getAFirBalance().add(targetMemberSettleExt.getARepBalance());
        // a区首购扣减结余 = 量碰计算业绩 乘 （a区首购结余 除 a区结余）
        BigDecimal aSubBal = calAAchieve.multiply(targetMemberSettleExt.getAFirBalance().divide(aSumBal, 6, RoundingMode.FLOOR));
        targetMemberSettleExt.setAFirBalance(targetMemberSettleExt.getAFirBalance().subtract(aSubBal));
        targetMemberSettleExt.setARepBalance(targetMemberSettleExt.getARepBalance().subtract(calAAchieve.subtract(aSubBal)));
        if (aSumBal.compareTo(calAAchieve) == 0) {
            // 需要扣减结余等于a区结余
            targetMemberSettleExt.setAFirBalance(BigDecimal.ZERO);
            targetMemberSettleExt.setARepBalance(BigDecimal.ZERO);
        }

        // b区首购 + b区复购
        BigDecimal bSumBal = targetMemberSettleExt.getBFirBalance().add(targetMemberSettleExt.getBRepBalance());
        // a区首购扣减结余 = 量碰计算业绩 乘 （a区首购结余 除 a区结余）
        BigDecimal bSubBal = calBAchieve.multiply(targetMemberSettleExt.getBFirBalance().divide(bSumBal, 6, RoundingMode.FLOOR));
        targetMemberSettleExt.setBFirBalance(targetMemberSettleExt.getBFirBalance().subtract(bSubBal));
        targetMemberSettleExt.setBRepBalance(targetMemberSettleExt.getBRepBalance().subtract(calBAchieve.subtract(bSubBal)));
        if (bSumBal.compareTo(calBAchieve) == 0) {
            // 需要扣减结余等于a区结余
            targetMemberSettleExt.setBFirBalance(BigDecimal.ZERO);
            targetMemberSettleExt.setBRepBalance(BigDecimal.ZERO);
        }

        if (calAAchieve.compareTo(calBAchieve) <= 0) {
            // 左区 小于等于 右区，使用左区业绩
            cuMemberBonusExpand.setCalFirAchieve(aSubBal);
            cuMemberBonusExpand.setCalRepAchieve(calAAchieve.subtract(aSubBal));
        } else {
            // 使用右区业绩
            cuMemberBonusExpand.setCalFirAchieve(bSubBal);
            cuMemberBonusExpand.setCalRepAchieve(calBAchieve.subtract(bSubBal));
        }

        targetMemberSettleExt.setAExpandPv(BigDecimal.ZERO);
        targetMemberSettleExt.setBExpandPv(BigDecimal.ZERO);

        // 计算收益 先计算出会员最大的收益（按等级拓展比例进行计算出最大值）
        // maxIncome = 业绩 * 等级扩展比例 -- 等级配置的最大比例
        BigDecimal maxIncome = ComputeUtil.computeBonusMultiply(cuMemberBonusExpand.getCalAchieve(), gradeExpandRatio);
        // actualIncome = 业绩 * N过比例 -- 前3碰最大13%，4-N 根据实际计算得出
        BigDecimal actualIncome = ComputeUtil.computeBonusMultiply(cuMemberBonusExpand.getCalAchieve(), countryExpandRatio);

        // 拓展收益（首购 + 复购 + 当前）
        BigDecimal expandIncome = ComputeUtil.computeAdd(ComputeUtil.computeAdd(cuMemberBonus.getExpandIncome(), actualIncome), cuMemberBonus.getRepurExpandIncome());

        // 判断收益 是否超过封顶值，超过的话变为封顶值
        // 判断是否烧伤，判断原奖金是否>=封顶值,奖金到了就是烧伤
        // 验证会员状态 封停状态跟冻结都正常计算奖金，封停的账号发放的时候奖金发放为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()) {
                // V3 V4 没有烧伤，改为延缓发放
                if (EGrade.VIP.getValue() == bdGrade.getGradeValue()
                        || EGrade.S_VIP.getValue() == bdGrade.getGradeValue()) {
                    cuMemberBonusExpand.setIncomeStatus(EBonusIncomeStatus.DELAY_GRANT.getValue());
                } else {
                    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(bdGrade.getExpandCapping(), 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()) {
                // V3 V4 没有烧伤，改为延缓发放
                if (EGrade.VIP.getValue() == bdGrade.getGradeValue()
                        || EGrade.S_VIP.getValue() == bdGrade.getGradeValue()) {
                    fireCuMemberBonusExpand.setIncomeStatus(EBonusIncomeStatus.DELAY_GRANT.getValue());
                } else {
                    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 = bdGrade.getExpandCapping();
        }

        // 收益占比
        BdBonusDeduct bdBonusDeduct = setBonusDetailDeduct(bonusConfigDTO, cuMemberBonusExpand, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
        expandRemark(saOrderExt, cuMemberBonusExpand, sourceMemberSettleExt, targetMemberSettleExt);

        if (ComputeUtil.compareValue(addTouch)) {
            cuMemberBonusExpand.setRemark(cuMemberBonusExpand.getRemark() + "增加碰次：" + addTouch + "。");
        }

        if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusExpand.getIncomeStatus()) {
            // 判断会员状态，会员状态为封停的话状态修改为停收
            cuMemberBonus.setExpandIncome(ComputeUtil.computeSubtract(expandIncome, cuMemberBonus.getRepurExpandIncome()));
            setFirstPurchaseTotal(bdBonusDeduct, cuMemberBonus, cuMemberBonusExpand);
        }
        cuMemberBonusExpandList.add(cuMemberBonusExpand);
    }

}
