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

import com.hzs.common.core.constant.BonusFieldConstants;
import com.hzs.common.core.enums.EBonusIncomeStatus;
import com.hzs.common.core.enums.EBonusItems;
import com.hzs.common.core.enums.ELevelValue;
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.bonus.risk.CuMemberRiskControl;
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.member.store.dto.StoreDto;
import com.hzs.system.config.dto.BonusConfigDTO;
import org.springframework.stereotype.Component;

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

/**
 * @description: 计算店铺奖金
 * @author: sui q
 * @time: 2023/5/8 15:40
 * @classname: BonusStoreHandle
 * @package_name: com.hzs.member.bonus.service.impl
 * version 1.0.0
 */
@Component
public class BonusSettleStoreHandle extends BonusSettleHandle{

    /**
     * @param period            期间
     * @param bonusConfigDTO    参数
     * @param cuMemberBonusMap  所有计算出来的奖金汇总
     * @Description: 计算店铺收益
     * @return: void
     * @Author: sui q
     * @Date: 2022/11/10 14:17
     */
    List<CuMemberBonusDetail> calculateStoreBonus(Map<String, StoreDto> areaStoreMap, Map<Integer, StoreDto> cuntryStoreMap,
                                                  List<SaOrderExt> storeSaOrderExtList, String settleTableName, String settleTableNameDetail,
                                                  BonusConfigDTO bonusConfigDTO, Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap, Map<Long, CuMemberRiskControl> riskControlMap) {
        List<CuMemberBonusDetail> cuMemberBonusDetailList = new ArrayList<>();
        // 查询店铺奖金配置表
        Set<Long> serviceMemberSet = new HashSet<>();
        storeSaOrderExtList.forEach(saOrderExt -> {
            serviceMemberSet.add(saOrderExt.getPkMember());
            serviceMemberSet.add(saOrderExt.getStoreMember());
        });
        areaStoreMap.forEach((key,value)-> serviceMemberSet.add(value.getPkMember()));
        cuntryStoreMap.forEach((key, value)-> serviceMemberSet.add(value.getPkMember()));
        List<CuMemberSettleExt> cuMemberSettleExtList = cuMemberTreeService.batchQueryMemberSettleByMemberPk(settleTableName, settleTableNameDetail, serviceMemberSet);
        Map<Long, CuMemberSettleExt> cuMemberSettleExtMap = new HashMap<>();
        getMemberSettle(cuMemberSettleExtMap, cuMemberSettleExtList);
        for (SaOrderExt saOrderExt : storeSaOrderExtList) {
            CuMemberSettleExt sourceMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
            CuMemberSettleExt targetMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getStoreMember());
            CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, targetMemberSettleExt);
            CuMemberBonusDetail cuMemberBonusDetail = getCuMemberBonusDetail(saOrderExt, targetMemberSettleExt, cuMemberBonus);
            // 计算比例
            cuMemberBonusDetail.setCalAchieve(saOrderExt.getOrderAchieve());
            cuMemberBonusDetail.setCalValue(saOrderExt.getRatio());
            cuMemberBonusDetail.setPkBonusItems(getPkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.STORE_INCOME.getValue()));
            cuMemberBonusDetail.setPretaxIncome(ComputeUtil.computeBonusMultiply(saOrderExt.getOrderAchieve(), saOrderExt.getRatio()));
            // K值设置
            if (riskControlMap.containsKey(cuMemberBonus.getPkMember())) {
                cuMemberBonusDetail.setPretaxIncome(ComputeUtil.computeBonusMultiply(cuMemberBonusDetail.getPretaxIncome(), riskControlMap.get(cuMemberBonus.getPkMember()).getStoreRevenue()));
                cuMemberBonusDetail.setIncomeStatus(EBonusIncomeStatus.RISK_CONTROL.getValue());
            }
            storeRemark(cuMemberBonusDetail, saOrderExt, sourceMemberSettleExt, targetMemberSettleExt);
            if (riskControlMap.containsKey(cuMemberBonus.getPkMember())) {
                cuMemberBonusDetail.setRemark(cuMemberBonusDetail.getRemark() + ",K值比例:" + riskControlMap.get(cuMemberBonus.getPkMember()).getStoreRevenue());
            }
            cuMemberBonusDetailList.add(cuMemberBonusDetail);
            BdBonusDeduct bdBonusDeduct = setBonusDetailDeduct(bonusConfigDTO, cuMemberBonusDetail, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
            if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusDetail.getIncomeStatus() ||
                    EBonusIncomeStatus.RISK_CONTROL.getValue() == cuMemberBonusDetail.getIncomeStatus()) {
                cuMemberBonus.setStoreIncome(ComputeUtil.computeAdd(cuMemberBonus.getStoreIncome(), cuMemberBonusDetail.getPretaxIncome()));
                setFirstPurchaseTotal(bdBonusDeduct, cuMemberBonus, cuMemberBonusDetail);
//                setBonusDeduct(bdBonusDeduct, cuMemberBonus, cuMemberBonusDetail);
//                BigDecimal realIncome = ReflectUtils.invokeGetter(cuMemberBonusDetail, BonusFieldConstants.REAL_INCOME);
//                cuMemberBonus.setRealIncomeTotal(ComputeUtil.computeAdd(cuMemberBonus.getRealIncomeTotal(), realIncome));
            }
            // 计算极差奖， 区域代理找国代
           if(ELevelValue.COPPER_DRILL.getValue() == saOrderExt.getLevelValue() || ELevelValue.SILVER_DRILL.getValue() == saOrderExt.getLevelValue()) {
                // 服务中心 找2级 先找区域， 然后再找国代
                BigDecimal ratio = saOrderExt.getRatio();
               String beforeLevelName= saOrderExt.getStoreLevelName();
               if (ELevelValue.COPPER_DRILL.getValue() == saOrderExt.getLevelValue()){
                    String key = saOrderExt.getPkCountry() + "_" + saOrderExt.getStoreProvince();
                    if (areaStoreMap.containsKey(key)) {
                        StoreDto storeDto = areaStoreMap.get(key);
                        CuMemberSettleExt rangeTargetMemberSettleExt = cuMemberSettleExtMap.get(storeDto.getPkMember());
                        packageStoreRangeIncome(bonusConfigDTO, period, cuMemberBonusMap, cuMemberBonusDetailList, saOrderExt, sourceMemberSettleExt, rangeTargetMemberSettleExt,
                                beforeLevelName, storeDto.getStoreLevelName(), ComputeUtil.computeSubtract(storeDto.getRatio(), ratio));
                        ratio = storeDto.getRatio();
                        beforeLevelName = storeDto.getStoreLevelName();
                    }
                }
               if(cuntryStoreMap.containsKey(saOrderExt.getPkCountry())){
                   StoreDto storeDto = cuntryStoreMap.get(saOrderExt.getPkCountry());
                   CuMemberSettleExt rangeTargetMemberSettleExt = cuMemberSettleExtMap.get(storeDto.getPkMember());
                   packageStoreRangeIncome(bonusConfigDTO, period, cuMemberBonusMap, cuMemberBonusDetailList, saOrderExt, sourceMemberSettleExt, rangeTargetMemberSettleExt,
                           beforeLevelName, storeDto.getStoreLevelName(), ComputeUtil.computeSubtract(storeDto.getRatio(), ratio));
               }
            }
        }
        return cuMemberBonusDetailList;

    }

    private void packageStoreRangeIncome(BonusConfigDTO bonusConfigDTO, Integer period, Map<Long, CuMemberBonus> cuMemberBonusMap, List<CuMemberBonusDetail> cuMemberBonusDetailList,
                                         SaOrderExt saOrderExt, CuMemberSettleExt sourceMemberSettleExt, CuMemberSettleExt targetMemberSettleExt,
                                         String beforeLevelName, String levelName, BigDecimal ratio) {
        if(ComputeUtil.compareValue(ratio)){
            CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, targetMemberSettleExt);
            CuMemberBonusDetail cuMemberBonusDetail = getCuMemberBonusDetail(saOrderExt, targetMemberSettleExt, cuMemberBonus);
            // 计算比例
            cuMemberBonusDetail.setCalAchieve(saOrderExt.getOrderAchieve());
            cuMemberBonusDetail.setCalValue(ratio);
            cuMemberBonusDetail.setPkBonusItems(getPkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.STORE_RANGE_INCOME.getValue()));
            cuMemberBonusDetail.setPretaxIncome(ComputeUtil.computeBonusMultiply(saOrderExt.getOrderAchieve(), ratio));
            storeRangeRemark(cuMemberBonusDetail, saOrderExt, sourceMemberSettleExt, targetMemberSettleExt, beforeLevelName, levelName);
            cuMemberBonusDetailList.add(cuMemberBonusDetail);
            BdBonusDeduct bdBonusDeduct = setBonusDetailDeduct(bonusConfigDTO, cuMemberBonusDetail, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
            if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusDetail.getIncomeStatus()) {
                cuMemberBonus.setStoreRangeIncome(ComputeUtil.computeAdd(cuMemberBonus.getStoreRangeIncome(), cuMemberBonusDetail.getPretaxIncome()));
                setFirstPurchaseTotal(bdBonusDeduct, cuMemberBonus, cuMemberBonusDetail);
//                setBonusDeduct(bdBonusDeduct, cuMemberBonus, cuMemberBonusDetail);
//                BigDecimal realIncome = ReflectUtils.invokeGetter(cuMemberBonusDetail, BonusFieldConstants.REAL_INCOME);
//                cuMemberBonus.setRealIncomeTotal(ComputeUtil.computeAdd(cuMemberBonus.getRealIncomeTotal(), realIncome));
            }
        }
    }
}
