package com.ruoyi.demo.init;

import com.ruoyi.demo.constant.RedisInitContant;
import com.ruoyi.demo.entity.AddrCategory;
import com.ruoyi.demo.enums.EquipmentOnStateEnum;
import com.ruoyi.demo.enums.EquipmentStateEnum;
import com.ruoyi.demo.mapper.AddrCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.PostConstruct;
import java.util.HashMap;

@Configuration
public class BenyunCoreInit {
    @Autowired
    AddrCategoryMapper addrCategoryDao;

    @Autowired
    RedisTemplate redisTemplate;

    @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已完成");
    }

    @Bean("equipmentStateMap")
    public HashMap<Integer,String> getEquipmentStateMap(){
        HashMap<Integer, String> equipmentStateMap = new HashMap<>();
        for (EquipmentStateEnum value : EquipmentStateEnum.values()) {
            equipmentStateMap.put(value.getState(),value.getName());
        }
        return equipmentStateMap;
    }

    @Bean("equipmentOnStateMap")
    public HashMap<Integer,String> getEquipmentOnStateMap(){
        HashMap<Integer, String> equipmentOnStateMap = new HashMap<>();
        for (EquipmentOnStateEnum value : EquipmentOnStateEnum.values()) {
            equipmentOnStateMap.put(value.getState(),value.getName());
        }
        return equipmentOnStateMap;
    }

}
