package com.hzs.system.config.provider;

import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.ERangeType;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.domain.bonus.BdBonusDeduct;
import com.hzs.common.domain.bonus.BdBonusService;
import com.hzs.common.domain.system.base.BdCountry;
import com.hzs.common.domain.system.base.BdCurrency;
import com.hzs.common.domain.system.config.*;
import com.hzs.common.domain.system.config.ext.BdBonusDirectLoopExt;
import com.hzs.common.domain.system.config.ext.BdBonusItemsExt;
import com.hzs.system.base.service.IBdCountryService;
import com.hzs.system.base.service.IBdCurrencyService;
import com.hzs.system.bonus.service.IBdBonusDeductService;
import com.hzs.system.bonus.service.IBdBonusServiceService;
import com.hzs.system.config.IBonusItemsServiceApi;
import com.hzs.system.config.dto.*;
import com.hzs.system.config.service.*;
import com.hzs.system.sys.dto.BdCurrencyDTO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 奖项配置
 **/
@DubboService
public class BonusItemsServiceProvider implements IBonusItemsServiceApi {

    @Autowired
    private IBdBonusItemsService bonusItemsService;

    @Autowired
    private IBdBonusDirectLoopService bdBonusDirectLoopService;

    @Autowired
    private IBdBonusHiPinkService bdBonusHiPinkService;

    @Autowired
    private IBdBonusDirectService bdBonusDirectService;

    @Autowired
    private IBdBonusDifferenceService bdBonusDifferenceService;

    @Autowired
    private IBdBonusExpandService bdBonusExpandService;

    @Autowired
    private IBdGradeService gradeService;

    @Autowired
    private IBdAwardsService awardsService;

    @Autowired
    private IBdBonusDeductService bdBonusDeductService;

    @Autowired
    private IBdBonusServiceService bdBonusServiceService;

    @Autowired
    private IBdCurrencyService bdCurrencyService;

    @Autowired
    private IBdCountryService bdCountryService;

    @Autowired
    private IBdRepoDirectPushService bdRepoDirectPushService;

    @Autowired
    private IBdBonusCloudService bdBonusCloudService;

    @Override
    public R<List<BdBonusItemsExt>> queryBonusItems(Integer pkCountry) {
        return R.ok(bonusItemsService.queryBonusItems(pkCountry));
    }

    @Override
    public R<BdBonusItems> queryBonusItems(Integer pkCountry, Integer pkBonusItems) {
        return R.ok(bonusItemsService.queryBonusItems(pkCountry, pkBonusItems));
    }

    @Override
    public R<BonusConfigDTO> queryBonusConfigDTO() {
        BonusConfigDTO bonusConfigDTO = new BonusConfigDTO();
        // 汇率
        bonusConfigDTO.setCurrencyDtoMap(getCurrencyDtoMap());
        // 收益类型
        List<BdCountry> allCountryList = bdCountryService.getAllCountry();
        Set<Integer> countrySet = new HashSet<>();
        allCountryList.forEach(bdCountry ->
                countrySet.add(bdCountry.getPkId()));
        bonusConfigDTO.setBonusItemsExtMap(getBdBonusItemsExtMap(countrySet));
        // 根据奖金项验证参数，没有配置参数的，需要提醒
        // 查询循环配置参数
        bonusConfigDTO.setBonusDirectLoopDtoMap(getBonusLoopConfig());
        // 查询直推奖励配置
        bonusConfigDTO.setBonusDirectDtoMap(getBonusDirectConfig());
        // 嗨粉直推奖励配置
        bonusConfigDTO.setBonusHiPinkDtoMap(getBonusHiPinkMap());
        // 一次性补差
        bonusConfigDTO.setBonusDifferenceDtoMap(getBonusDifferenceConfig());
        // 奖金4-N配置
        bonusConfigDTO.setBonusExpandMap(getBonusExpandConfig());
        // 复购直推配置
        bonusConfigDTO.setBonusRepurchasePushMap(getRepurchasePushMap());
        // 拓展奖跟辅导奖需要查询所有国家的配置
        // 拓展奖配置
        bonusConfigDTO.setGradeMap(getBonusExpandGradeConfig());
        // 见点奖配置
        bonusConfigDTO.setSeePointGradeMap(getSeeBonusGradeConfig());
        // 辅导奖配置
        Map<String, BdAwards> awardsValueMap = new HashMap<>();
        bonusConfigDTO.setAwardsMap(getBonusCoachConfig(awardsValueMap));
        bonusConfigDTO.setAwardsValueMap(awardsValueMap);
        // 奖金扣项
        bonusConfigDTO.setBonusDeductsMap(getBonusDeductConfig());
        // 报单服务费
        bonusConfigDTO.setBonusServiceMap(getBonusServiceConfig());
        // 云代奖金
        bonusConfigDTO.setBonusCloudMap(getBonusCloudConfig());
        return R.ok(bonusConfigDTO);
    }

    private Map<String, BdCurrencyDTO> getCurrencyDtoMap() {
        List<BdCurrency> bdCurrencyList = bdCurrencyService.queryAllBdCurrency();
        Map<String, BdCurrencyDTO> currencyDtoMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdCurrencyList.size()));
        bdCurrencyList.forEach(bdCurrency ->
                currencyDtoMap.put(bdCurrency.getPkCountry().toString() + bdCurrency.getPkId(),
                        BeanUtil.copyProperties(bdCurrency, BdCurrencyDTO.class)));
        return currencyDtoMap;
    }

    private Map<String, BdBonusItemsExt> getBdBonusItemsExtMap(Set<Integer> countrySet) {
        Map<String, BdBonusItemsExt> bonusItemsMap = new HashMap<>(ComputeUtil.mapInitCapacity(countrySet.size() * 8));
        countrySet.forEach(pkCountry -> {
            List<BdBonusItemsExt> bdBonusItemsExtList = bonusItemsService.queryCalBonusItems(pkCountry);
            for (BdBonusItemsExt bdBonusItemsExt : bdBonusItemsExtList) {
                bonusItemsMap.put(pkCountry.toString() + bdBonusItemsExt.getBonusValue().toString(), bdBonusItemsExt);
            }
        });
        return bonusItemsMap;
    }

    /**
     * 嗨粉直推奖励
     */
    private Map<Integer, List<BonusHiPinkDTO>> getBonusHiPinkMap() {
        List<BdBonusHiPink> bdBonusHiPinks = bdBonusHiPinkService.queryBonusHiPink();
        Map<Integer, List<BonusHiPinkDTO>> bonusHiPinkMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdBonusHiPinks.size()));
        bdBonusHiPinks.forEach(bdBonusHiPink -> {
            BonusHiPinkDTO bonusHiPinkDTO = BeanUtil.copyProperties(bdBonusHiPink, BonusHiPinkDTO.class);
            if (bonusHiPinkMap.containsKey(bonusHiPinkDTO.getPkCountry())) {
                bonusHiPinkMap.get(bonusHiPinkDTO.getPkCountry()).add(bonusHiPinkDTO);
            } else {
                List<BonusHiPinkDTO> bonusHiPinkDTOList = new ArrayList<>();
                bonusHiPinkDTOList.add(bonusHiPinkDTO);
                bonusHiPinkMap.put(bonusHiPinkDTO.getPkCountry(), bonusHiPinkDTOList);
            }
        });
        return bonusHiPinkMap;
    }

    /**
     * 复购直推奖励
     */
    private Map<Integer, Map<Integer, BonusRepurchasePushDTO>> getRepurchasePushMap() {
        List<BdRepoDirectPush> bdRepoDirectPushList = bdRepoDirectPushService.queryRepoDirectPush();
        Map<Integer, Map<Integer, BonusRepurchasePushDTO>> repurchasePushMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdRepoDirectPushList.size()));
        bdRepoDirectPushList.forEach(bdRepoDirectPush -> {
            BonusRepurchasePushDTO bonusRepurchasePushDTO = BeanUtil.copyProperties(bdRepoDirectPush, BonusRepurchasePushDTO.class);
            if (repurchasePushMap.containsKey(bonusRepurchasePushDTO.getPkCountry())) {
                repurchasePushMap.get(bonusRepurchasePushDTO.getPkCountry()).put(bonusRepurchasePushDTO.getAlgebra(), bonusRepurchasePushDTO);
            } else {
                Map<Integer, BonusRepurchasePushDTO> map = new HashMap<>();
                map.put(bonusRepurchasePushDTO.getAlgebra(), bonusRepurchasePushDTO);
                repurchasePushMap.put(bonusRepurchasePushDTO.getPkCountry(), map);
            }
        });
        return repurchasePushMap;
    }

    /**
     * 获得奖金循环配置的参数
     */
    private Map<String, BonusDirectLoopDTO> getBonusLoopConfig() {
        List<BdBonusDirectLoopExt> bonusDirectLoopList = bdBonusDirectLoopService.queryBonusDirectLoop();
        Map<String, BonusDirectLoopDTO> bonusDirectLoopDtoMap = new HashMap<>(4);
        bonusDirectLoopList.forEach(bdBonusDirectLoop -> {
            BonusDirectLoopDTO bonusDirectLoopDTO = BeanUtil.copyProperties(bdBonusDirectLoop, BonusDirectLoopDTO.class);
            bonusDirectLoopDtoMap.put(bonusDirectLoopDTO.getPkCountry().toString() + bonusDirectLoopDTO.getBonusType()
                    + bonusDirectLoopDTO.getPkGrade(), bonusDirectLoopDTO);
        });
        return bonusDirectLoopDtoMap;
    }

    /**
     * 获得奖金 推荐什么等级 直推奖励配置
     */
    private Map<String, BonusDirectDTO> getBonusDirectConfig() {
        List<BdBonusDirect> bdBonusDirectList = bdBonusDirectService.queryBonusDirect();
        Map<String, BonusDirectDTO> directDtoHashMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdBonusDirectList.size()));
        bdBonusDirectList.forEach(bdBonusDirect -> {
            BonusDirectDTO bonusDirectDTO = BeanUtil.copyProperties(bdBonusDirect, BonusDirectDTO.class);
            directDtoHashMap.put(bonusDirectDTO.getPkCountry().toString() + bonusDirectDTO.getRangeType() + bonusDirectDTO.getPkLevel(), bonusDirectDTO);
        });
        return directDtoHashMap;
    }

    /**
     * 获得奖金 推荐什么等级 直推奖励配置
     */
    private Map<Integer, BonusDifferenceDTO> getBonusDifferenceConfig() {
        List<BdBonusDifference> bdBonusDifferenceList = bdBonusDifferenceService.queryBonusDifference();
        Map<Integer, BonusDifferenceDTO> bonusDifferenceDtoMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdBonusDifferenceList.size()));
        bdBonusDifferenceList.forEach(bonusDirectDTO -> {
            BonusDifferenceDTO bonusDifferenceDTO = BeanUtil.copyProperties(bonusDirectDTO, BonusDifferenceDTO.class);
            bonusDifferenceDtoMap.put(bonusDifferenceDTO.getPkCountry(), bonusDifferenceDTO);
        });
        return bonusDifferenceDtoMap;
    }


    /**
     * 拓展奖拓展的次数  国家作为键
     */
    private Map<Integer, BonusExpandDTO> getBonusExpandConfig() {
        List<BdBonusExpand> bdBonusExpandList = bdBonusExpandService.queryBdBonusExpand();
        Map<Integer, BonusExpandDTO> bdBonusExpandMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdBonusExpandList.size()));
        bdBonusExpandList.forEach(bdBonusExpand -> {
            BonusExpandDTO bonusExpandDTO = BeanUtil.copyProperties(bdBonusExpand, BonusExpandDTO.class);
            bdBonusExpandMap.put(bonusExpandDTO.getPkCountry(), bonusExpandDTO);
        });
        return bdBonusExpandMap;
    }

    /**
     * 获取等级对应的拓展奖对应参数
     */
    private Map<String, BdGrade> getBonusExpandGradeConfig() {
        List<BdGrade> bdGradeList = gradeService.queryBdGrade();
        Map<String, BdGrade> bdGradeMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdGradeList.size()));
        bdGradeList.forEach(bdGrade ->
                bdGradeMap.put(bdGrade.getPkCountry().toString() + "_" + bdGrade.getPkId(), bdGrade));
        return bdGradeMap;
    }

    /**
     * 获取等级对应的拓展奖对应参数
     */
    private Map<Integer, List<BdGrade>> getSeeBonusGradeConfig() {
        // 获取国家的见点奖
        List<BdGrade> bdGradeList = gradeService.queryBdGrade();
        Map<Integer, List<BdGrade>> bdGradeMap = new HashMap<>();
        bdGradeList.forEach(bdGrade -> {
            if (bdGradeMap.containsKey(bdGrade.getPkCountry())) {
                bdGradeMap.get(bdGrade.getPkCountry()).add(bdGrade);
            } else {
                List<BdGrade> gradeList = new ArrayList<>();
                gradeList.add(bdGrade);
                bdGradeMap.put(bdGrade.getPkCountry(), gradeList);
            }
        });
        return bdGradeMap;
    }

    /**
     * 获取奖衔对应的辅导奖对应参数
     */
    private Map<String, BdAwards> getBonusCoachConfig(Map<String, BdAwards> awardsValueMap) {
        List<BdAwards> bdAwardsList = awardsService.queryAwards();
        Map<String, BdAwards> bdAwardsMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdAwardsList.size()));
        bdAwardsList.forEach(bdAwards -> {
            bdAwardsMap.put(bdAwards.getPkCountry().toString() + ERangeType.AWARDS.getValue() + bdAwards.getPkId(), bdAwards);
            awardsValueMap.put(bdAwards.getPkCountry().toString() + ERangeType.AWARDS.getValue() + bdAwards.getAwardsValue(), bdAwards);
        });
        return bdAwardsMap;
    }

    /**
     * 获取每个国家奖金项对应的扣项
     */
    private Map<Integer, BdBonusDeduct> getBonusDeductConfig() {
        List<BdBonusDeduct> bdBonusDeductList = bdBonusDeductService.queryBonusDeduct();
        Map<Integer, BdBonusDeduct> bdBonusDeductMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdBonusDeductList.size()));
        bdBonusDeductList.forEach(bdBonusDeduct ->
                bdBonusDeductMap.put(bdBonusDeduct.getPkBonusItems(), bdBonusDeduct));
        return bdBonusDeductMap;
    }

    /**
     * 获取每个国家奖金项对应的扣项
     */
    private Map<String, BdBonusService> getBonusServiceConfig() {
        List<BdBonusService> bdBonusServiceList = bdBonusServiceService.queryBonusService();
        Map<String, BdBonusService> bdBonusDeductMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdBonusServiceList.size()));
        bdBonusServiceList.forEach(bdBonusService ->
                bdBonusDeductMap.put(bdBonusService.getPkCountry().toString() + bdBonusService.getServiceType(), bdBonusService));
        return bdBonusDeductMap;
    }

    /**
     * 获取每个国家奖金项对应的扣项
     */
    private Map<String, BdBonusCloud> getBonusCloudConfig() {
        List<BdBonusCloud> bonusCloudList = bdBonusCloudService.queryBdBonusCloud();
        Map<String, BdBonusCloud> bdBonusCloudHashMap = new HashMap<>(ComputeUtil.mapInitCapacity(bonusCloudList.size()));
        bonusCloudList.forEach(bdBonusCloud ->
                bdBonusCloudHashMap.put(bdBonusCloud.getPkCountry().toString() + bdBonusCloud.getCloudType(), bdBonusCloud));
        return bdBonusCloudHashMap;
    }

}
