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

import com.hzs.common.core.constant.BonusFieldConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.reflect.ReflectUtils;
import com.hzs.common.domain.bonus.BdBonusDeduct;
import com.hzs.common.domain.member.base.CuAwardsControl;
import com.hzs.common.domain.member.bonus.CuMemberBonus;
import com.hzs.common.domain.member.bonus.CuMemberBonusDetail;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.sale.ext.SaOrderItemsExt;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.system.config.dto.BonusConfigDTO;
import com.hzs.system.config.dto.BonusHiPinkDTO;
import com.hzs.system.config.dto.RangeDTO;
import org.springframework.stereotype.Component;

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

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

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

}
