sfd
2025-05-26 2a64b537e8e3bce9ce030585a3da17d48379c0ad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.aps.common.security.utils;
 
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
 
import com.alibaba.fastjson2.JSONArray;
import com.aps.common.core.constant.CacheConstants;
import com.aps.common.core.utils.SpringUtils;
import com.aps.common.core.utils.StringUtils;
import com.aps.common.redis.service.RedisService;
import com.aps.system.api.domain.SysDictData;
import org.springframework.util.CollectionUtils;
 
/**
 * 字典工具类
 *
 * @author ruoyi
 */
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 字典键
     */
    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;
    }
 
 
    public static interface CacheValue{
        String get(String label);
    }
 
    /**
     * 根据字典类型和字典标签获取字典值
     *
     * @param dictType
     * @return
     */
    public static CacheValue getCacheValue(String dictType) {
 
        List<SysDictData> cacheData = StringUtils.isEmpty(dictType) ? Collections.emptyList() : getDictCache(dictType);
        return label -> {
            if (CollectionUtils.isEmpty(cacheData)){
                return null;
            }
            if(StringUtils.isEmpty(label)){
                return null;
            }
            return cacheData.stream().filter(item -> Objects.equals(item.getDictLabel(), label))
                    .findFirst()
                    .map(SysDictData::getDictValue)
                    .orElse(null);
        };
    }
}