package com.ruoyi.demo.init;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ruoyi.demo.config.RedisConfiguration;
import com.ruoyi.demo.constant.RedisInitContant;
import com.ruoyi.demo.entity.AddrCategory;
import com.ruoyi.demo.entity.ManageType;
import com.ruoyi.demo.entity.TypeBy;
import com.ruoyi.demo.mapper.AddrCategoryDao;
import com.ruoyi.demo.mapper.TypeByDao;
import com.ruoyi.demo.service.ManageTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;

@Configuration
public class BenyunCoreInit {
    @Autowired
    AddrCategoryDao addrCategoryDao;

    @Autowired
    ManageTypeService manageTypeService;

    @Autowired
    TypeByDao byDao;

    @Autowired
    RedisTemplate redisTemplate;

    @Bean("executor")
    public ExecutorService getExecutor() {
        return new ThreadPoolExecutor(0, 8,
            60L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());
    }
    /**
     * HashMap<地区Code,城市等级>（redis版本）
     */
    @PostConstruct
    public void initCityTierMap(){
        if(redisTemplate.hasKey(RedisInitContant.INIT_CITY_TIER_MAP))
            return;
        System.out.println("正在加载CityTierMap");
        QueryWrapper<AddrCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("city_tier");
        for (AddrCategory addrCategory : addrCategoryDao.selectList(queryWrapper)) {
            if (addrCategory.getCityTier() != null && !addrCategory.getCityTier().equals("")) {
                redisTemplate.boundHashOps(RedisInitContant.INIT_CITY_TIER_MAP).put(addrCategory.getAddrCode().substring(0, 4), addrCategory.getCityTier());
            }
        }
        initCityTierListMap();
        System.out.println("CityTierMap已完成");
    }


    /**
     * 一线城市的addrCode（redis）
     */
    public void initCityTierListMap(){
        if(redisTemplate.hasKey(RedisInitContant.INIT_CITY_TIER_LIST_MAP))
            return;
        System.out.println("正在加载CityTierListMap");
        List<String> one = new ArrayList<>();
        List<String> two = new ArrayList<>();
        List<String> three = new ArrayList<>();
        List<String> four = new ArrayList<>();
        List<String> five = new ArrayList<>();

        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(RedisInitContant.INIT_CITY_TIER_MAP);
        Set<String> keys = boundHashOperations.keys();
        for (String s : keys) {
            String s1 = (String) boundHashOperations.get(s);
            switch (s1) {
                case "一线":
                    one.add(s);
                    break;
                case "二线":
                    two.add(s);
                    break;
                case "三线":
                    three.add(s);
                    break;
                case "四线":
                    four.add(s);
                    break;
                case "五线":
                    five.add(s);
                    break;
            }
        }
        redisTemplate.boundHashOps(RedisInitContant.INIT_CITY_TIER_LIST_MAP).put("一线",one);
        redisTemplate.boundHashOps(RedisInitContant.INIT_CITY_TIER_LIST_MAP).put("二线",two);
        redisTemplate.boundHashOps(RedisInitContant.INIT_CITY_TIER_LIST_MAP).put("三线",three);
        redisTemplate.boundHashOps(RedisInitContant.INIT_CITY_TIER_LIST_MAP).put("四线",four);
        redisTemplate.boundHashOps(RedisInitContant.INIT_CITY_TIER_LIST_MAP).put("五线",five);
        System.out.println("CityTierListMap已完成");
    }

    @PostConstruct
    public void initManageType() {
        if(redisTemplate.hasKey(RedisInitContant.INIT_MANAGE_TYPE))
            return;
        System.out.println("正在加载ManageType");
        for (ManageType datum : manageTypeService.getData()) {
            if (!datum.getManageTypeCode().equals(""))
                redisTemplate.boundHashOps(RedisInitContant.INIT_MANAGE_TYPE).put(datum.getManageTypeCode(), datum);
        }
        System.out.println("ManageType已完成");
    }


    @PostConstruct
    public void initAddrCodeMap() {
        if(redisTemplate.hasKey(RedisInitContant.INIT_ADDR_CODE_MAP))
            return;
        System.out.println("正在加载AddrCodeMap");
        for (AddrCategory addrCode : addrCategoryDao.selectList(null)) {
            redisTemplate.boundHashOps(RedisInitContant.INIT_ADDR_CODE_MAP).put(addrCode.getAddrCode(), addrCode.getAddrInfo());
        }
        System.out.println("AddrCodeMap已完成");
    }


    @PostConstruct
    public void initTypeByMap() {
        if(redisTemplate.hasKey(RedisInitContant.INIT_TYPE_BY_MAP))
            return;
        System.out.println("正在加载TypeByMap");
        for (TypeBy datum : byDao.selectList(null)) {
            redisTemplate.boundHashOps(RedisInitContant.INIT_TYPE_BY_MAP).put(datum.getTypeCodeBy(), datum.getFullInfo());
        }
        System.out.println("TypeByMap已完成");
    }
}
