package com.hzs.system.component.load;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.constant.Constants;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.ESystemConfig;
import com.hzs.common.core.enums.ETransportType;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.domain.system.base.BdCountry;
import com.hzs.common.domain.system.base.BdCurrency;
import com.hzs.common.domain.system.base.BdSystemConfig;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.domain.system.ext.AreaCurrencyExt;
import com.hzs.system.base.service.IBdCountryService;
import com.hzs.system.base.service.IBdCurrencyService;
import com.hzs.system.base.service.IBdSystemConfigService;
import com.hzs.system.base.service.IBdTransactionService;
import com.hzs.system.config.dto.RangeDTO;
import com.hzs.system.config.mapper.BdAccountMapper;
import com.hzs.system.config.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: sui q
 * @Time: 2022/9/6 17:50
 * @Classname: RedisComponent
 * @PackageName: com.hzs.system.component.load
 */
@Component
@Slf4j
public class RedisComponent {

    private RedisService redisService;
    private IBdSystemConfigService systemConfigService;
    private IBdCountryService countryService;
    private IBdCurrencyService currencyService;
    private IBdAreaCurrencyService bdAreaCurrencyService;
    private BdAccountMapper bdAccountMapper;
    private IBdRangeService bdRangeService;
    private IBdTransactionService bdTransactionService;

    @Autowired
    public void setBdRangeService(IBdRangeService bdRangeService) {
        this.bdRangeService = bdRangeService;
    }

    @Autowired
    public void setBdAreaCurrencyService(IBdAreaCurrencyService bdAreaCurrencyService) {
        this.bdAreaCurrencyService = bdAreaCurrencyService;
    }

    @Autowired
    public void setBdAccountMapper(BdAccountMapper bdAccountMapper) {
        this.bdAccountMapper = bdAccountMapper;
    }

    @Autowired
    public void setCurrencyService(IBdCurrencyService currencyService) {
        this.currencyService = currencyService;
    }

    @Autowired
    public void setCountryService(IBdCountryService countryService) {
        this.countryService = countryService;
    }

    @Autowired
    public void setSystemConfigService(IBdSystemConfigService systemConfigService) {
        this.systemConfigService = systemConfigService;
    }

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    @Autowired
    public void setBdTransactionService(IBdTransactionService bdTransactionService) {
        this.bdTransactionService = bdTransactionService;
    }

    public void initConfig() {
        // 国家
        List<BdCountry> bdCountryList = initBdCountry();
        bdCountryList.forEach(bdCountry -> {
            redisService.deleteObjectByKey(CacheConstants.BD_BONUS_ITEMS + bdCountry.getPkId());
            redisService.deleteObjectByKey(CacheConstants.SYSTEM_CONFIG + bdCountry.getPkId());
            redisService.deleteObjectByKey(CacheConstants.BD_AWARDS + bdCountry.getPkId());
        });
        // 汇率
        initCurrency();
        // 系统参数
        initSystemConfig();
        // 专区币种配置
        initCurrencyConfig();
        // 极差配置
        initBdRange();
        // 初始化翻译
        initBdTransaction();
    }

    /**
     * 初始化系统参数 bd_system_config
     */
    public void initSystemConfig() {
        List<BdSystemConfig> bdSystemConfigList = systemConfigService.list();
        Map<Integer, List<Integer>> transportTypeMap = new HashMap<>();
        bdSystemConfigList.forEach(systemConfig -> {
            if (systemConfig.getKey().equals(ESystemConfig.IS_LAND_TRANS.getKey())) {
                setTransportTypeMap(transportTypeMap, systemConfig, ETransportType.LAND.getValue());
            } else if (systemConfig.getKey().equals(ESystemConfig.IS_AIR_TRANS.getKey())) {
                setTransportTypeMap(transportTypeMap, systemConfig, ETransportType.AIR.getValue());
            } else if (systemConfig.getKey().equals(ESystemConfig.IS_OCEAN_TRANS.getKey())) {
                setTransportTypeMap(transportTypeMap, systemConfig, ETransportType.OCEAN.getValue());
            } else {
                redisService.setCacheMapValue(CacheConstants.SYSTEM_CONFIG + systemConfig.getPkCountry(),
                        systemConfig.getKey(), systemConfig.getValue());
                redisService.expire(CacheConstants.SYSTEM_CONFIG + systemConfig.getPkCountry(), Constants.TIMEOUT_DAY, TimeUnit.DAYS);
            }
        });
        transportTypeMap.forEach((pkCountry, transportValueList) -> {
            String key = CacheConstants.TRANSPORT_TYPE + pkCountry;
            redisService.deleteObjectByKey(key);
            redisService.setCacheList(key, transportValueList);
            redisService.expire(key, Constants.TIMEOUT_DAY, TimeUnit.DAYS);
        });
    }

    /**
     * 封装运输方式
     *
     * @param transportTypeMap 运输方式map
     * @param systemConfig     系统配置参数
     */
    private void setTransportTypeMap(Map<Integer, List<Integer>> transportTypeMap, BdSystemConfig systemConfig, Integer transportValue) {
        if (systemConfig.getValue().equals(Integer.toString(EYesNo.YES.getIntValue()))) {
            if (transportTypeMap.containsKey(systemConfig.getPkCountry())) {
                transportTypeMap.get(systemConfig.getPkCountry()).add(transportValue);
            } else {
                List<Integer> transportList = new ArrayList<>();
                transportList.add(transportValue);
                transportTypeMap.put(systemConfig.getPkCountry(), transportList);
            }
        }
    }

    /**
     * 初始化国家
     */
    private List<BdCountry> initBdCountry() {
        QueryWrapper<BdCountry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SystemFieldConstants.ENABLE_STATE, EYesNo.YES.getIntValue());
        List<BdCountry> bdCountryList = countryService.list(queryWrapper);
        bdCountryList.forEach(country -> {
            redisService.setCacheObject(CacheConstants.BD_COUNTRY + country.getPkId(), country, Constants.TIMEOUT_DAY, TimeUnit.DAYS);
        });
        return bdCountryList;
    }

    /**
     * 初始化汇率
     */
    private void initCurrency() {
        QueryWrapper<BdCurrency> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SystemFieldConstants.ENABLE_STATE, EYesNo.YES.getIntValue());
        List<BdCurrency> currencyList = currencyService.list(queryWrapper);
        currencyList.forEach(currency -> {
            redisService.setCacheObject(CacheConstants.BD_CURRENCY + currency.getPkCountry(), currency, Constants.TIMEOUT_DAY, TimeUnit.DAYS);
        });
    }

    private void initCurrencyConfig() {
        Map<String, List<AreaCurrencyExt>> currencyMap = new HashMap<>();
        List<AreaCurrencyExt> areaCurrencyExtList = bdAccountMapper.queryAreaCurrencyByCondition(null, null);
        areaCurrencyExtList.forEach(areaCurrencyExt -> {
            String key = areaCurrencyExt.getPkCountry().toString() + areaCurrencyExt.getSpecialArea();
            if (currencyMap.containsKey(key)) {
                currencyMap.get(key).add(areaCurrencyExt);
            } else {
                List<AreaCurrencyExt> areaCurrencyList = new ArrayList<>();
                areaCurrencyList.add(areaCurrencyExt);
                currencyMap.put(key, areaCurrencyList);
            }
        });
        if (currencyMap.size() > 0) {
            currencyMap.forEach((oldKey, areaCurrencyList) -> {
                String key = CacheConstants.BD_AREA_CURRENCY + oldKey;
                if (redisService.hasKey(key)) {
                    redisService.deleteObject(key);
                }
                if (areaCurrencyList.size() > 0) {
                    redisService.setCacheList(key, areaCurrencyList);
                    redisService.expire(key, Constants.LANG_TIMEOUT, TimeUnit.DAYS);
                }
            });
        }

    }

    /**
     * @Description: 初始化极差类型
     * @return: void
     * @Author: sui q
     * @Date: 2023/2/4 16:13
     */
    private void initBdRange() {
        Map<String, RangeDTO> rangeDtoMap = bdRangeService.queryRangeDto();
        if (rangeDtoMap.size() > 0) {
            rangeDtoMap.forEach((key, value) -> {
                RangeDTO rangeDTO = redisService.getCacheObject(key);
                if (rangeDTO != null) {
                    redisService.deleteObject(key);
                }
                redisService.setCacheObject(key, rangeDTO, Constants.TIMEOUT_DAY, TimeUnit.DAYS);
            });
        }
    }

    /**
     * 初始化翻译
     */
    private void initBdTransaction() {
        bdTransactionService.flushResources(null, true, false);
    }

}
