package com.inspect.common.security.utils;

import com.inspect.base.core.utils.SpringUtils;
import com.inspect.base.core.utils.StringUtils;
import com.inspect.base.redis.service.RedisService;
import com.inspect.system.base.domain.SysDictData;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DictUtils {
    public static void setDictCache(String key, List<SysDictData> dictDatas) {
        SpringUtils.getBean(RedisService.class).setCacheObject(getCacheKey(key), dictDatas);
    }

    public static List<SysDictData> getDictCache(String key) {
        Object cacheObj = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key));
        return StringUtils.isNotNull(cacheObj) ? (List) StringUtils.cast(cacheObj) : null;
    }

    public static Map<String, SysDictData> getDictMap(String key) {
        Map<String, SysDictData> map = new HashMap<>();
        List<SysDictData> dictCache = getDictCache(key);
        if (dictCache == null) {
            return map;
        } else {
            dictCache.forEach((item) -> {
                SysDictData var10000 = map.put(item.getDictValue(), item);
            });
            return map;
        }
    }

    public static void removeDictCache(String key) {
        ((RedisService) SpringUtils.getBean(RedisService.class)).deleteObject(getCacheKey(key));
    }

    public static void clearDictCache() {
        Collection<String> keys = SpringUtils.getBean(RedisService.class).keys("sys_dict:*");
        SpringUtils.getBean(RedisService.class).deleteObject(keys);
    }

    public static String getCacheKey(String configKey) {
        return "sys_dict:" + configKey;
    }
}
