package com.hzs.system.config.provider;

import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.domain.bonus.BdBonusDeduct;
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.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.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 iBdBonusItemsService;
    @Autowired
    private IBdBonusDirectService iBdBonusDirectService;
    @Autowired
    private IBdAwardsService iBdAwardsService;
    @Autowired
    private IBdGradeService iBdGradeService;
    @Autowired
    private IBdBonusDeductService iBdBonusDeductService;
    @Autowired
    private IBdCurrencyService iBdCurrencyService;
    @Autowired
    private IBdCountryService iBdCountryService;

    @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<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.setBonusDirectDtoMap(getBonusDirectConfig());
        // 奖金扣项
        bonusConfigDTO.setBonusDeductsMap(getBonusDeductConfig());
        // 奖衔列表
        bonusConfigDTO.setAwardsList(iBdAwardsService.queryAwards());
        // 等级列表
        bonusConfigDTO.setGradeList(iBdGradeService.queryBdGrade());
        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<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, 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;
    }

}
