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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import com.hzs.bonus.achieve.mapper.CuMemberRetailRangeMapper;
import com.hzs.bonus.achieve.service.ICuMemberRetailDetailService;
import com.hzs.bonus.achieve.service.impl.CuMemberBonusSettle;
import com.hzs.bonus.bonus.dto.BonusStageData;
import com.hzs.bonus.bonus.mapper.CuMemberBonusStageMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.bonus.service.ICuMemberBonusStageService;
import com.hzs.bonus.constant.BonusConstants;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.domain.member.achieve.CuMemberRetailDetail;
import com.hzs.common.domain.member.achieve.ext.CuMemberRetailRangeExt;
import com.hzs.common.domain.member.bonus.CuMemberBonus;
import com.hzs.common.domain.member.bonus.CuMemberBonusStage;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

/**
 * 会员奖金-阶段收益明细表 服务实现类
 */
@Service
public class CuMemberBonusStageServiceImpl extends ServiceImpl<CuMemberBonusStageMapper, CuMemberBonusStage> implements ICuMemberBonusStageService {

    @Autowired
    private CuMemberBonusSettle cuMemberBonusSettle;
    @Autowired
    private ICuMemberRetailDetailService iCuMemberRetailDetailService;

    @Resource
    private CuMemberRetailRangeMapper cuMemberRetailRangeMapper;

    @Override
    public void deleteCuMemberBonusStage(Integer period) {
        baseMapper.deleteCuMemberBonusStage(period);
    }

    @Override
    public void batchInsertCuMemberBonusStage(List<CuMemberBonusStage> cuMemberBonusStageList, Set<Long> memberIdSet) {
        if (CollectionUtil.isEmpty(cuMemberBonusStageList)) {
            return;
        }
        List<CuMemberBonusStage> addMemberBonusStageList = new ArrayList<>();
        for (CuMemberBonusStage cuMemberBonusStage : cuMemberBonusStageList) {
            if (memberIdSet.contains(cuMemberBonusStage.getPkBonus())) {
                addMemberBonusStageList.add(cuMemberBonusStage);
            }
        }
        if (addMemberBonusStageList.size() == 0) {
            return;
        }
        List<List<?>> lists = cuMemberBonusSettle.handleCutList(addMemberBonusStageList);
        lists.forEach(list ->
                baseMapper.batchInsertCuMemberBonusStage(list));
    }

    /**
     * 获取阶段收益信息
     */
    @Override
    public CuMemberBonusStage getCuMemberBonusStage(SaOrderExt saOrderExt, CuMemberRetailRangeExt cuMemberRetailRange, CuMemberBonus cuMemberBonus, Integer pkBonusItems) {
        CuMemberBonusStage cuMemberBonusStage = CuMemberBonusStage.builder()
                .pkBonus(cuMemberBonus.getPkId())
                .pkOrder(saOrderExt.getPkId())
                .pkBonusItems(pkBonusItems)
                .incomeStatus(cuMemberRetailRange.getIncomeStatus())
                .calType(ECalType.VALUE.getValue())
                .calAchieve(BigDecimal.ZERO)
                .calValue(BigDecimal.ZERO)
                .pretaxIncome(BigDecimal.ZERO)
                .incomeTax(BigDecimal.ZERO)
                .realIncome(BigDecimal.ZERO)
                .incomeRatio(BigDecimal.ZERO)
                .incomeDialRatio(BigDecimal.ZERO)
                .orderDialRatio(BigDecimal.ZERO)
                .build();
        cuMemberBonusStage.setPkCountry(cuMemberRetailRange.getPkSettleCountry());
        cuMemberBonusStage.setPkCreator(MagicNumberConstants.PK_ADMIN);
        return cuMemberBonusStage;
    }

    @Override
    public String fillChildNode(CuMemberRetailDetail cuMemberRetailDetail, CuMemberRetailDetail newRetailDetail) {
        for (String suffix : BonusConstants.FIELD_LIST) {
            Field pointMemberField = BonusConstants.FIELD_MAP.get(BonusConstants.POINT_MEMBER_FIELD_NAME + suffix);
            Field pointField = BonusConstants.FIELD_MAP.get(BonusConstants.POINT_FIELD_NAME + suffix);
            Object pointFieldValue = ReflectUtil.getFieldValue(cuMemberRetailDetail, pointField);
            if (ObjectUtil.isEmpty(pointFieldValue)) {
                ReflectUtil.setFieldValue(cuMemberRetailDetail, pointMemberField, newRetailDetail.getPkMember());
                ReflectUtil.setFieldValue(cuMemberRetailDetail, pointField, newRetailDetail.getChildNode());
                ReflectUtil.setFieldValue(cuMemberRetailDetail, BonusConstants.UPDATE_FLAG, 0);
                return suffix;
            }
        }
        return null;
    }

    /**
     * 向上填充子点位
     */
    private void fillUpTargetRetailDetail(CuMemberRetailDetail targetRetailDetail, CuMemberRetailDetail newParentRetailDetail,
                                          CuMemberRetailDetail newRetailDetail, String node, Date settleDate, EBonusStage eBonusStage,
                                          List<BonusStageData> bonusStageDataList) {
        if (null == targetRetailDetail) {
            return;
        }
        for (String suffix : BonusConstants.FIELD_LIST) {
            Field pointMemberField = BonusConstants.FIELD_MAP.get(BonusConstants.POINT_MEMBER_FIELD_NAME + suffix);
            Field pointField = BonusConstants.FIELD_MAP.get(BonusConstants.POINT_FIELD_NAME + suffix);
            if (ReflectUtil.getFieldValue(targetRetailDetail, pointMemberField).equals(newParentRetailDetail.getPkMember())
                    && ReflectUtil.getFieldValue(targetRetailDetail, pointField).equals(newParentRetailDetail.getChildNode())) {
                // 中间点位在目标点位的位置，根据位置获取新点位填充的位置
                switch (suffix) {
                    case BonusConstants.POINT_11:
                        if (BonusConstants.POINT_11.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_21,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_12.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_22,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_21.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_31,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_22.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_32,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_23.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_33,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_24.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_34,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        }
                        break;
                    case BonusConstants.POINT_12:
                        if (BonusConstants.POINT_11.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_23,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_12.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_24,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_21.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_35,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_22.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_36,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_23.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_37,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_24.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_38,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        }
                        break;
                    case BonusConstants.POINT_21:
                        if (BonusConstants.POINT_11.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_31,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_12.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_32,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        }
                        break;
                    case BonusConstants.POINT_22:
                        if (BonusConstants.POINT_11.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_33,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_12.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_34,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        }
                        break;
                    case BonusConstants.POINT_23:
                        if (BonusConstants.POINT_11.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_35,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_12.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_36,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        }
                        break;
                    case BonusConstants.POINT_24:
                        if (BonusConstants.POINT_11.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_37,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        } else if (BonusConstants.POINT_12.equals(node)) {
                            this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_38,
                                    newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                        }
                        break;
                }
                break;
            }
        }
    }

    /**
     * 向下填充子点位
     */
    private void fillDownTargetRetailDetail(CuMemberRetailDetail targetRetailDetail, CuMemberRetailDetail newRetailDetail,
                                            String node, Integer level, Date settleDate, EBonusStage eBonusStage,
                                            List<BonusStageData> bonusStageDataList) {
        if (null == targetRetailDetail) {
            return;
        }
        if (level.equals(1)) {
            if (BonusConstants.POINT_21.equals(node)
                    || BonusConstants.POINT_23.equals(node)
                    || BonusConstants.POINT_31.equals(node)
                    || BonusConstants.POINT_33.equals(node)
                    || BonusConstants.POINT_35.equals(node)
                    || BonusConstants.POINT_37.equals(node)) {
                this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_11,
                        newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
            } else if (BonusConstants.POINT_22.equals(node)
                    || BonusConstants.POINT_24.equals(node)
                    || BonusConstants.POINT_32.equals(node)
                    || BonusConstants.POINT_34.equals(node)
                    || BonusConstants.POINT_36.equals(node)
                    || BonusConstants.POINT_38.equals(node)) {
                this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_12,
                        newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
            }
        } else if (level.equals(2)) {
            if (BonusConstants.POINT_31.equals(node)
                    || BonusConstants.POINT_35.equals(node)) {
                this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_21,
                        newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
            } else if (BonusConstants.POINT_32.equals(node)
                    || BonusConstants.POINT_36.equals(node)) {
                this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_22,
                        newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
            } else if (BonusConstants.POINT_33.equals(node)
                    || BonusConstants.POINT_37.equals(node)) {
                this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_23,
                        newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
            } else if (BonusConstants.POINT_34.equals(node)
                    || BonusConstants.POINT_38.equals(node)) {
                this.fillRetailDetail(targetRetailDetail, BonusConstants.POINT_24,
                        newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
            }
        }
    }

    /**
     * 填充点位
     */
    @Override
    public boolean fillRetailDetail(CuMemberRetailDetail targetRetailDetail, String point,
                                    CuMemberRetailDetail sourceRetailDetail, Date settleDate, EBonusStage eBonusStage,
                                    List<BonusStageData> bonusStageDataList) {
        ReflectUtil.setFieldValue(targetRetailDetail, BonusConstants.POINT_MEMBER_FIELD_NAME + point, sourceRetailDetail.getPkMember());
        ReflectUtil.setFieldValue(targetRetailDetail, BonusConstants.POINT_FIELD_NAME + point, sourceRetailDetail.getChildNode());
        ReflectUtil.setFieldValue(targetRetailDetail, BonusConstants.UPDATE_FLAG, 0);

        switch (point) {
            case BonusConstants.POINT_31:
            case BonusConstants.POINT_32:
            case BonusConstants.POINT_33:
            case BonusConstants.POINT_34:
            case BonusConstants.POINT_35:
            case BonusConstants.POINT_36:
            case BonusConstants.POINT_37:
            case BonusConstants.POINT_38:
                // 如果填充的点位是第三层，理论上有可能已经全部填满，需要判断是否已经完成
                boolean isFinish = true;
                for (String suffix : BonusConstants.FIELD_LIST) {
                    if (null == ReflectUtil.getFieldValue(targetRetailDetail, BonusConstants.POINT_MEMBER_FIELD_NAME + suffix)) {
                        isFinish = false;
                        break;
                    }
                }
                if (isFinish) {
                    targetRetailDetail.setStageStatus(EBonusStageStatus.YES.getValue());
                    targetRetailDetail.setStageDate(settleDate);
                    // TODO 统一处理第二阶段还是 fillRetailDetail 方法进行处理
                    if (EBonusStage.ONE.getValue() == eBonusStage.getValue()) {
                        // 一阶段，全满发放阶段奖
                        // 返回奖金收益处理数据
                        bonusStageDataList.add(BonusStageData.builder()
                                .bonusIncome(BonusConstants.ONE_BONUS)
                                .targetPkMember(targetRetailDetail.getPkMember())
                                .targetChildNode(targetRetailDetail.getChildNode())
                                .stage(eBonusStage.getValue())
                                .stageNum(14)
                                .build());
                    } else if (EBonusStage.TWO.getValue() == eBonusStage.getValue()) {
                        // 二阶段，全满发放阶段奖
                        // 返回奖金收益处理数据
                        bonusStageDataList.add(BonusStageData.builder()
                                .bonusIncome(BonusConstants.TWO_BONUS)
                                .targetPkMember(targetRetailDetail.getPkMember())
                                .targetChildNode(targetRetailDetail.getChildNode())
                                .stage(eBonusStage.getValue())
                                .stageNum(14)
                                .build());
                    }
                    return true;
                }
                break;
        }
        return false;
    }

    @Override
    public void handlePointFirst(Map<Long, CuMemberRetailRangeExt> memberRangeExtMap,
                                 CuMemberRetailDetail newRetailDetail, CuMemberRetailDetail newParentRetailDetail,
                                 String rangeTableName, String rangeDetailTableName, String newNode,
                                 Date settleDate, EBonusStage eBonusStage, List<BonusStageData> bonusStageDataList) {
        // 新点位的上第一层
        newRetailDetail.setPointMemberFirst(newParentRetailDetail.getPkMember());
        newRetailDetail.setPointFirst(newParentRetailDetail.getChildNode());

        List<Long> queryMemberList = new ArrayList<>(2);
        // 获取上第二、三层结算数据
        CuMemberRetailRangeExt memberFirst = memberRangeExtMap.get(newParentRetailDetail.getPointMemberFirst());
        if (null == memberFirst && null != newParentRetailDetail.getPointMemberFirst()) {
            queryMemberList.add(newParentRetailDetail.getPointMemberFirst());
        }
        CuMemberRetailRangeExt memberSecond = memberRangeExtMap.get(newParentRetailDetail.getPointMemberSecond());
        if (null == memberSecond && null != newParentRetailDetail.getPointMemberSecond()) {
            queryMemberList.add(newParentRetailDetail.getPointMemberSecond());
        }
        if (CollectionUtil.isNotEmpty(queryMemberList)) {
            // 查询会员数据
            List<CuMemberRetailRangeExt> retailList = cuMemberRetailRangeMapper.listCuMemberRetailRange(rangeTableName, queryMemberList);
            if (CollectionUtil.isNotEmpty(retailList)) {
                retailList.forEach(tmpRetail -> {
                    memberRangeExtMap.put(tmpRetail.getPkMember(), tmpRetail);
                });
                // 查询血缘会员明细数据（一阶段）
                iCuMemberRetailDetailService.listMemberRetailDetail(rangeDetailTableName, memberRangeExtMap, queryMemberList, EBonusStage.ONE);
            }
        }

        // 新点位的上第二层
        CuMemberRetailRangeExt firstMemberRetail = memberRangeExtMap.get(newParentRetailDetail.getPointMemberFirst());
        if (null != firstMemberRetail) {
            CuMemberRetailDetail memberDetail = firstMemberRetail.getOneDetailMap().get(newParentRetailDetail.getPointFirst());
            if (null != memberDetail) {
                newRetailDetail.setPointMemberSecond(newParentRetailDetail.getPointMemberFirst());
                newRetailDetail.setPointSecond(newParentRetailDetail.getPointFirst());
                this.fillUpTargetRetailDetail(memberDetail, newParentRetailDetail, newRetailDetail, newNode, settleDate, eBonusStage,
                        bonusStageDataList);
            }
        }

        // 新点位的上第三层
        CuMemberRetailRangeExt secondMemberRetail = memberRangeExtMap.get(newParentRetailDetail.getPointMemberSecond());
        if (null != secondMemberRetail) {
            CuMemberRetailDetail memberDetail = secondMemberRetail.getOneDetailMap().get(newParentRetailDetail.getPointSecond());
            if (null != memberDetail) {
                newRetailDetail.setPointMemberThird(newParentRetailDetail.getPointMemberSecond());
                newRetailDetail.setPointThird(newParentRetailDetail.getPointSecond());
                this.fillUpTargetRetailDetail(memberDetail, newParentRetailDetail, newRetailDetail, newNode, settleDate, eBonusStage,
                        bonusStageDataList);
            }
        }
    }

    @Override
    public void handlePointSecond(Map<Long, CuMemberRetailRangeExt> memberRangeExtMap,
                                  CuMemberRetailDetail newRetailDetail, CuMemberRetailDetail newParentRetailDetail,
                                  String rangeTableName, String rangeDetailTableName, String newNode,
                                  Date settleDate, EBonusStage eBonusStage, List<BonusStageData> bonusStageDataList) {
        // 新点位的上第二层
        newRetailDetail.setPointMemberSecond(newParentRetailDetail.getPkMember());
        newRetailDetail.setPointSecond(newParentRetailDetail.getChildNode());

        List<Long> queryMemberList = new ArrayList<>(2);
        // 获取上第三层结算数据
        CuMemberRetailRangeExt memberUpFirst = memberRangeExtMap.get(newParentRetailDetail.getPointMemberFirst());
        if (null == memberUpFirst && null != newParentRetailDetail.getPointMemberFirst()) {
            queryMemberList.add(newParentRetailDetail.getPointMemberFirst());
        }
        // 获取上第一层结算数据
        Long downFirstMember = null;
        Integer downFirstPoint = null;
        switch (newNode) {
            case BonusConstants.POINT_21:
            case BonusConstants.POINT_22:
                downFirstMember = newParentRetailDetail.getPointMember11();
                downFirstPoint = newParentRetailDetail.getPoint11();
                break;
            case BonusConstants.POINT_23:
            case BonusConstants.POINT_24:
                downFirstMember = newParentRetailDetail.getPointMember12();
                downFirstPoint = newParentRetailDetail.getPoint12();
                break;
        }
        CuMemberRetailRangeExt memberDownFirst = memberRangeExtMap.get(downFirstMember);
        if (null == memberDownFirst && null != downFirstMember) {
            queryMemberList.add(downFirstMember);
        }
        if (CollectionUtil.isNotEmpty(queryMemberList)) {
            // 查询会员数据
            List<CuMemberRetailRangeExt> retailList = cuMemberRetailRangeMapper.listCuMemberRetailRange(rangeTableName, queryMemberList);
            if (CollectionUtil.isNotEmpty(retailList)) {
                retailList.forEach(tmpRetail -> {
                    memberRangeExtMap.put(tmpRetail.getPkMember(), tmpRetail);
                });
                // 查询血缘会员明细数据（一阶段）
                iCuMemberRetailDetailService.listMemberRetailDetail(rangeDetailTableName, memberRangeExtMap, queryMemberList, EBonusStage.ONE);
            }
        }

        // 新点位的上第三层
        CuMemberRetailRangeExt thirdMemberRetail = memberRangeExtMap.get(newParentRetailDetail.getPointMemberFirst());
        if (null != thirdMemberRetail) {
            CuMemberRetailDetail memberDetail = thirdMemberRetail.getOneDetailMap().get(newParentRetailDetail.getPointFirst());
            if (null != memberDetail) {
                newRetailDetail.setPointMemberThird(newParentRetailDetail.getPointMemberFirst());
                newRetailDetail.setPointThird(newParentRetailDetail.getPointFirst());
                this.fillUpTargetRetailDetail(memberDetail, newParentRetailDetail, newRetailDetail, newNode, settleDate, eBonusStage,
                        bonusStageDataList);
            }
        }

        // 新点位的上第一层
        CuMemberRetailRangeExt firstMemberRetail = memberRangeExtMap.get(downFirstMember);
        if (null != firstMemberRetail) {
            CuMemberRetailDetail memberDetail = firstMemberRetail.getOneDetailMap().get(downFirstPoint);
            if (null != memberDetail) {
                newRetailDetail.setPointMemberFirst(memberDetail.getPkMember());
                newRetailDetail.setPointFirst(memberDetail.getChildNode());
                this.fillDownTargetRetailDetail(memberDetail, newRetailDetail, newNode, 1, settleDate, eBonusStage,
                        bonusStageDataList);
            }
        }
    }

    @Override
    public void handlePointThird(Map<Long, CuMemberRetailRangeExt> memberRangeExtMap,
                                 CuMemberRetailDetail newRetailDetail, CuMemberRetailDetail newParentRetailDetail,
                                 String rangeTableName, String rangeDetailTableName, String newNode,
                                 Date settleDate, EBonusStage eBonusStage, List<BonusStageData> bonusStageDataList) {
        // 新点位的上第三层
        newRetailDetail.setPointMemberThird(newParentRetailDetail.getPkMember());
        newRetailDetail.setPointThird(newParentRetailDetail.getChildNode());

        List<Long> queryMemberList = new ArrayList<>(2);
        // 获取上第二层结算数据
        Long downSecondMember = null;
        Integer downSecondPoint = null;
        // 获取上第一层结算数据
        Long downFirstMember = null;
        Integer downFirstPoint = null;
        switch (newNode) {
            case BonusConstants.POINT_31:
            case BonusConstants.POINT_32:
            case BonusConstants.POINT_33:
            case BonusConstants.POINT_34:
                downSecondMember = newParentRetailDetail.getPointMember11();
                downSecondPoint = newParentRetailDetail.getPoint11();
                if (BonusConstants.POINT_31.equals(newNode)
                        || BonusConstants.POINT_32.equals(newNode)) {
                    downFirstMember = newParentRetailDetail.getPointMember21();
                    downFirstPoint = newParentRetailDetail.getPoint21();
                } else {
                    downFirstMember = newParentRetailDetail.getPointMember22();
                    downFirstPoint = newParentRetailDetail.getPoint22();
                }
                break;
            case BonusConstants.POINT_35:
            case BonusConstants.POINT_36:
            case BonusConstants.POINT_37:
            case BonusConstants.POINT_38:
                downSecondMember = newParentRetailDetail.getPointMember12();
                downSecondPoint = newParentRetailDetail.getPoint12();
                if (BonusConstants.POINT_35.equals(newNode)
                        || BonusConstants.POINT_36.equals(newNode)) {
                    downFirstMember = newParentRetailDetail.getPointMember23();
                    downFirstPoint = newParentRetailDetail.getPoint23();
                } else {
                    downFirstMember = newParentRetailDetail.getPointMember24();
                    downFirstPoint = newParentRetailDetail.getPoint24();
                }
                break;
        }
        CuMemberRetailRangeExt memberDownSecond = memberRangeExtMap.get(downSecondMember);
        if (null == memberDownSecond && null != downSecondMember) {
            queryMemberList.add(downSecondMember);
        }
        CuMemberRetailRangeExt memberDownFirst = memberRangeExtMap.get(downFirstMember);
        if (null == memberDownFirst && null != downFirstMember) {
            queryMemberList.add(downFirstMember);
        }
        if (CollectionUtil.isNotEmpty(queryMemberList)) {
            // 查询会员数据
            List<CuMemberRetailRangeExt> retailList = cuMemberRetailRangeMapper.listCuMemberRetailRange(rangeTableName, queryMemberList);
            if (CollectionUtil.isNotEmpty(retailList)) {
                retailList.forEach(tmpRetail -> {
                    memberRangeExtMap.put(tmpRetail.getPkMember(), tmpRetail);
                });
                // 查询血缘会员明细数据（一阶段）
                iCuMemberRetailDetailService.listMemberRetailDetail(rangeDetailTableName, memberRangeExtMap, queryMemberList, EBonusStage.ONE);
            }
        }

        // 新点位的上第二层
        CuMemberRetailRangeExt secondMemberRetail = memberRangeExtMap.get(downSecondMember);
        if (null != secondMemberRetail) {
            CuMemberRetailDetail memberDetail = secondMemberRetail.getOneDetailMap().get(downSecondPoint);
            if (null != memberDetail) {
                newRetailDetail.setPointMemberSecond(newParentRetailDetail.getPkMember());
                newRetailDetail.setPointSecond(newParentRetailDetail.getChildNode());
                this.fillDownTargetRetailDetail(memberDetail, newRetailDetail, newNode, 2, settleDate, eBonusStage,
                        bonusStageDataList);
            }
        }

        // 新点位的上第一层
        CuMemberRetailRangeExt firstMemberRetail = memberRangeExtMap.get(downFirstMember);
        if (null != firstMemberRetail) {
            CuMemberRetailDetail memberDetail = firstMemberRetail.getOneDetailMap().get(downFirstPoint);
            if (null != memberDetail) {
                newRetailDetail.setPointMemberFirst(newParentRetailDetail.getPkMember());
                newRetailDetail.setPointFirst(newParentRetailDetail.getChildNode());
                this.fillDownTargetRetailDetail(memberDetail, newRetailDetail, newNode, 1, settleDate, eBonusStage,
                        bonusStageDataList);
            }
        }
    }

    @Override
    public boolean firstLevelhandle(LinkedHashMap<Integer, CuMemberRetailDetail> parentDetailList, CuMemberRetailDetail newRetailDetail,
                                    String rangeTableName, String rangeDetailTableName,
                                    Map<Long, CuMemberRetailRangeExt> memberRangeExtMap, Date settleDate, EBonusStage eBonusStage,
                                    List<BonusStageData> bonusStageDataList) {
        for (Map.Entry<Integer, CuMemberRetailDetail> integerCuMemberRetailDetailEntry : parentDetailList.entrySet()) {
            // 第一层填充
            CuMemberRetailDetail parentRetailDetail = integerCuMemberRetailDetailEntry.getValue();
            // 遍历推荐人点位，使用第一个未完成的点位，进行填充
            if (EBonusStageStatus.NO.getValue() == parentRetailDetail.getStageStatus()) {
                if (null == parentRetailDetail.getPointMember11()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_11,
                            newRetailDetail, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 新点位是推荐人点位的第一层
                    this.handlePointFirst(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_11, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                } else if (null == parentRetailDetail.getPointMember12()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_12,
                            newRetailDetail, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 新点位是推荐人点位的第一层
                    this.handlePointFirst(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_12, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean secondLevelhandle(LinkedHashMap<Integer, CuMemberRetailDetail> parentDetailList, CuMemberRetailDetail newRetailDetail,
                                     String rangeTableName, String rangeDetailTableName,
                                     Map<Long, CuMemberRetailRangeExt> memberRangeExtMap, Date settleDate, EBonusStage eBonusStage,
                                     List<BonusStageData> bonusStageDataList) {
        for (Map.Entry<Integer, CuMemberRetailDetail> integerCuMemberRetailDetailEntry : parentDetailList.entrySet()) {
            // 第二层填充
            CuMemberRetailDetail parentRetailDetail = integerCuMemberRetailDetailEntry.getValue();
            // 遍历推荐人点位，使用第一个未完成的点位，进行填充
            if (EBonusStageStatus.NO.getValue() == parentRetailDetail.getStageStatus()) {
                if (null == parentRetailDetail.getPointMember21()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_21,
                            newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                    // 新点位是推荐人点位的第二层
                    this.handlePointSecond(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_21, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                } else if (null == parentRetailDetail.getPointMember22()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_22,
                            newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                    // 新点位是推荐人点位的第二层
                    this.handlePointSecond(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_22, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                } else if (null == parentRetailDetail.getPointMember23()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_23,
                            newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                    // 新点位是推荐人点位的第二层
                    this.handlePointSecond(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_23, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                } else if (null == parentRetailDetail.getPointMember24()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_24,
                            newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                    // 新点位是推荐人点位的第二层
                    this.handlePointSecond(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_24, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean thirdLevelhandle(LinkedHashMap<Integer, CuMemberRetailDetail> parentDetailList, CuMemberRetailDetail newRetailDetail,
                                    String rangeTableName, String rangeDetailTableName,
                                    Map<Long, CuMemberRetailRangeExt> memberRangeExtMap, Date settleDate, EBonusStage eBonusStage,
                                    List<BonusStageData> bonusStageDataList) {
        for (Map.Entry<Integer, CuMemberRetailDetail> integerCuMemberRetailDetailEntry : parentDetailList.entrySet()) {
            // 第三层填充
            CuMemberRetailDetail parentRetailDetail = integerCuMemberRetailDetailEntry.getValue();
            // 遍历推荐人点位，使用第一个未完成的点位，进行填充
            if (EBonusStageStatus.NO.getValue() == parentRetailDetail.getStageStatus()) {
                if (null == parentRetailDetail.getPointMember31()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_31,
                            newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                    // 新点位是推荐人点位的第三层
                    this.handlePointThird(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_31, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                } else if (null == parentRetailDetail.getPointMember32()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_32,
                            newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                    // 新点位是推荐人点位的第三层
                    this.handlePointThird(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_32, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                } else if (null == parentRetailDetail.getPointMember33()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_33,
                            newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                    // 新点位是推荐人点位的第三层
                    this.handlePointThird(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_33, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                } else if (null == parentRetailDetail.getPointMember34()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_34,
                            newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                    // 新点位是推荐人点位的第三层
                    this.handlePointThird(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_34, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                } else if (null == parentRetailDetail.getPointMember35()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_35,
                            newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                    // 新点位是推荐人点位的第三层
                    this.handlePointThird(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_35, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                } else if (null == parentRetailDetail.getPointMember36()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_36,
                            newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                    // 新点位是推荐人点位的第三层
                    this.handlePointThird(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_36, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                } else if (null == parentRetailDetail.getPointMember37()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_37,
                            newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                    // 新点位是推荐人点位的第三层
                    this.handlePointThird(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_37, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                } else if (null == parentRetailDetail.getPointMember38()) {
                    this.fillRetailDetail(parentRetailDetail, BonusConstants.POINT_38,
                            newRetailDetail, settleDate, eBonusStage, bonusStageDataList);
                    // 新点位是推荐人点位的第三层
                    this.handlePointThird(memberRangeExtMap, newRetailDetail, parentRetailDetail,
                            rangeTableName, rangeDetailTableName, BonusConstants.POINT_38, settleDate, eBonusStage,
                            bonusStageDataList);
                    // 当前新子点位填充完成，进入下一个新子点位
                    return true;
                }
            }
        }
        return false;
    }
}
