package com.hzs.system.config.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EAreaType;
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.IBdAwardsRegionConfigService;
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.math.BigDecimal;
import java.util.*;

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

    @Autowired
    private IBdBonusItemsService iBdBonusItemsService;
    @Autowired
    private IBdBonusDirectLoopService iBdBonusDirectLoopService;
    @Autowired
    private IBdBonusHiPinkService iBdBonusHiPinkService;
    @Autowired
    private IBdBonusDirectService iBdBonusDirectService;
    @Autowired
    private IBdBonusDifferenceService iBdBonusDifferenceService;
    @Autowired
    private IBdBonusExpandService iBdBonusExpandService;
    @Autowired
    private IBdBonusDeductService iBdBonusDeductService;
    @Autowired
    private IBdBonusServiceService iBdBonusServiceService;
    @Autowired
    private IBdGradeService iBdGradeService;
    @Autowired
    private IBdAwardsService iBdAwardsService;
    @Autowired
    private IBdCurrencyService iBdCurrencyService;
    @Autowired
    private IBdCountryService iBdCountryService;
    @Autowired
    private IBdRepoDirectPushService iBdRepoDirectPushService;
    @Autowired
    private IBdAwardsRegionConfigService iBdAwardsRegionConfigService;

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

    @Override
    public R<List<BdBonusItems>> queryExistsBonusItems(List<Integer> bonusValueList) {
        return R.ok(iBdBonusItemsService.queryBonusItemsByValue(bonusValueList));
    }

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

    @Override
    public R<BdBonusItems> queryBonusItemsByBonusValue(Integer pkCountry, Integer bonusValue) {
        return R.ok(iBdBonusItemsService.queryBonusItemsByBonusValue(pkCountry, bonusValue));
    }

    @Override
    public R<BonusConfigDTO> queryBonusConfigDTO() {
        BonusConfigDTO bonusConfigDTO = new BonusConfigDTO();
        // 汇率
        bonusConfigDTO.setCurrencyDtoMap(getCurrencyDtoMap());
        // 收益类型
        List<BdCountry> allCountryList = iBdCountryService.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());
        // 辅导奖配置
        Map<String, BdAwards> awardsValueMap = new HashMap<>();
        bonusConfigDTO.setAwardsMap(getBonusCoachConfig(awardsValueMap));
        bonusConfigDTO.setAwardsValueMap(awardsValueMap);
        // 奖金扣项
        bonusConfigDTO.setBonusDeductsMap(getBonusDeductConfig());
        // 报单服务费
        bonusConfigDTO.setBonusServiceMap(getBonusServiceConfig());
        // 区域收益配置
        bonusConfigDTO.setRegionAreaConfigMap(getRegionAreaConfig());
        return R.ok(bonusConfigDTO);
    }

    private Map<String, BdCurrencyDTO> getCurrencyDtoMap() {
        List<BdCurrency> bdCurrencyList = iBdCurrencyService.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 = iBdBonusItemsService.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 = iBdBonusHiPinkService.queryBonusHiPink();
        Map<Integer, List<BonusHiPinkDTO>> bonusHiPinkMap = new LinkedHashMap<>(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 = iBdRepoDirectPushService.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 = iBdBonusDirectLoopService.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 = iBdBonusDirectService.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 = iBdBonusDifferenceService.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 = iBdBonusExpandService.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 = iBdGradeService.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<String, BdAwards> getBonusCoachConfig(Map<String, BdAwards> awardsValueMap) {
        List<BdAwards> bdAwardsList = iBdAwardsService.queryAwards();
        Map<String, BdAwards> bdAwardsMap = new LinkedHashMap<>(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 = iBdBonusDeductService.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 = iBdBonusServiceService.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<Integer, BigDecimal> getRegionAreaConfig() {
        Map<Integer, BigDecimal> resultMap = new HashMap<>();

        List<BdAwardsRegionConfig> regionConfigList = iBdAwardsRegionConfigService.list();
        if (CollectionUtil.isNotEmpty(regionConfigList)) {
            for (BdAwardsRegionConfig bdAwardsRegionConfig : regionConfigList) {
                resultMap.put(bdAwardsRegionConfig.getAreaType(), bdAwardsRegionConfig.getRate());
            }
        } else {
            // 省默认15%，市默认为10%，区默认为5%
            resultMap.put(EAreaType.PROVINCE.getValue(), new BigDecimal("0.15"));
            resultMap.put(EAreaType.CITY.getValue(), new BigDecimal("0.10"));
            resultMap.put(EAreaType.COUNTY.getValue(), new BigDecimal("0.05"));
        }

        return resultMap;
    }

}
