package com.microservices.common.security.utils;

import com.alibaba.fastjson2.JSONArray;
import com.microservices.common.core.constant.CacheConstants;
import com.microservices.common.core.utils.SpringUtils;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.redis.service.RedisService;
import com.microservices.system.api.domain.SysDictData;

import java.util.Collection;
import java.util.List;

/**
 * 字典工具类
 *
 * @author microservices
 */
public class DictUtils
{
    /**
     * 设置字典缓存
     * 
     * @param key 参数键
     * @param dictDatas 字典数据列表
     */
    public static void setDictCache(String key, List<SysDictData> dictDatas)
    {
        SpringUtils.getBean(RedisService.class).setCacheObject(getCacheKey(key), dictDatas);
    }

    /**
     * 获取字典缓存
     * 
     * @param key 参数键
     * @return dictDatas 字典数据列表
     */
    public static List<SysDictData> getDictCache(String key)
    {
        JSONArray arrayCache = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key));
        if (StringUtils.isNotNull(arrayCache))
        {
            return arrayCache.toList(SysDictData.class);
        }
        return null;
    }

    /**
     * 获取具体字典值
     * @param key
     * @param dictValue
     * @return
     */
    public static SysDictData getDictDataCacheByValue(String key, String dictValue){
        List<SysDictData> dictDataList = getDictCache(key);
        if (dictDataList == null) {
            return null;
        }
        return dictDataList.stream()
                .filter(x -> x.getDictValue().equals(dictValue))
                .findFirst().orElse(null);
    }

    /**
     * 根据字典值获取字典标签
     *
     * @param key       字典Key
     * @param dictValue 字典值
     * @return 字典标签
     */
    public static String getDictDataLabelByValue(String key, String dictValue) {
        List<SysDictData> dictDataList = getDictCache(key);
        if (dictDataList == null) {
            return null;
        }
        return dictDataList.stream()
                .filter(x -> x.getDictValue().equals(dictValue))
                .map(SysDictData::getDictLabel)
                .findFirst().orElse(null);
    }

    /**
     * 获取具体字典值
     * @param key
     * @param dictLabel
     * @return
     */
    public static SysDictData getDictDataCacheByLabel(String key, String dictLabel){
        List<SysDictData> dictDataList = getDictCache(key);
        if(dictDataList==null){
            return null;
        }
        return dictDataList.stream()
                .filter(x -> x.getDictLabel().equals(dictLabel))
                .findFirst().orElse(null);
    }

    /**
     * 删除指定字典缓存
     * 
     * @param key 字典键
     */
    public static void removeDictCache(String key)
    {
        SpringUtils.getBean(RedisService.class).deleteObject(getCacheKey(key));
    }

    /**
     * 清空字典缓存
     */
    public static void clearDictCache()
    {
        Collection<String> keys = SpringUtils.getBean(RedisService.class).keys(CacheConstants.SYS_DICT_KEY + "*");
        SpringUtils.getBean(RedisService.class).deleteObject(keys);
    }

    /**
     * 设置cache key
     * 
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey)
    {
        return CacheConstants.SYS_DICT_KEY + configKey;
    }
}
