中国算力平台算力登记系统2.0
YZFG
2024-09-30 43dc2996fd2033991539ed35a0429238829a5417
commit | author | age
43dc29 1 package com.odcc.cpzidc.common.utils;
Y 2
3 import java.util.Collection;
4 import java.util.List;
5 import com.alibaba.fastjson2.JSONArray;
6 import com.odcc.cpzidc.common.constant.CacheConstants;
7 import com.odcc.cpzidc.common.core.domain.entity.SysDictData;
8 import com.odcc.cpzidc.common.core.redis.RedisCache;
9 import com.odcc.cpzidc.common.utils.spring.SpringUtils;
10
11 /**
12  * 字典工具类
13  * 
14  * @author ruoyi
15  */
16 public class DictUtils
17 {
18     /**
19      * 分隔符
20      */
21     public static final String SEPARATOR = ",";
22
23     /**
24      * 设置字典缓存
25      * 
26      * @param key 参数键
27      * @param dictDatas 字典数据列表
28      */
29     public static void setDictCache(String key, List<SysDictData> dictDatas)
30     {
31         SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), dictDatas);
32     }
33
34     /**
35      * 获取字典缓存
36      * 
37      * @param key 参数键
38      * @return dictDatas 字典数据列表
39      */
40     public static List<SysDictData> getDictCache(String key)
41     {
42         JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
43         if (StringUtils.isNotNull(arrayCache))
44         {
45             return arrayCache.toList(SysDictData.class);
46         }
47         return null;
48     }
49
50     /**
51      * 根据字典类型和字典值获取字典标签
52      * 
53      * @param dictType 字典类型
54      * @param dictValue 字典值
55      * @return 字典标签
56      */
57     public static String getDictLabel(String dictType, String dictValue)
58     {
59         if (StringUtils.isEmpty(dictValue))
60         {
61             return StringUtils.EMPTY;
62         }
63         return getDictLabel(dictType, dictValue, SEPARATOR);
64     }
65
66     /**
67      * 根据字典类型和字典标签获取字典值
68      * 
69      * @param dictType 字典类型
70      * @param dictLabel 字典标签
71      * @return 字典值
72      */
73     public static String getDictValue(String dictType, String dictLabel)
74     {
75         if (StringUtils.isEmpty(dictLabel))
76         {
77             return StringUtils.EMPTY;
78         }
79         return getDictValue(dictType, dictLabel, SEPARATOR);
80     }
81
82     /**
83      * 根据字典类型和字典值获取字典标签
84      * 
85      * @param dictType 字典类型
86      * @param dictValue 字典值
87      * @param separator 分隔符
88      * @return 字典标签
89      */
90     public static String getDictLabel(String dictType, String dictValue, String separator)
91     {
92         StringBuilder propertyString = new StringBuilder();
93         List<SysDictData> datas = getDictCache(dictType);
94         if (StringUtils.isNull(datas))
95         {
96             return StringUtils.EMPTY;
97         }
98         if (StringUtils.containsAny(separator, dictValue))
99         {
100             for (SysDictData dict : datas)
101             {
102                 for (String value : dictValue.split(separator))
103                 {
104                     if (value.equals(dict.getDictValue()))
105                     {
106                         propertyString.append(dict.getDictLabel()).append(separator);
107                         break;
108                     }
109                 }
110             }
111         }
112         else
113         {
114             for (SysDictData dict : datas)
115             {
116                 if (dictValue.equals(dict.getDictValue()))
117                 {
118                     return dict.getDictLabel();
119                 }
120             }
121         }
122         return StringUtils.stripEnd(propertyString.toString(), separator);
123     }
124
125     /**
126      * 根据字典类型和字典标签获取字典值
127      * 
128      * @param dictType 字典类型
129      * @param dictLabel 字典标签
130      * @param separator 分隔符
131      * @return 字典值
132      */
133     public static String getDictValue(String dictType, String dictLabel, String separator)
134     {
135         StringBuilder propertyString = new StringBuilder();
136         List<SysDictData> datas = getDictCache(dictType);
137         if (StringUtils.isNull(datas))
138         {
139             return StringUtils.EMPTY;
140         }
141         if (StringUtils.containsAny(separator, dictLabel))
142         {
143             for (SysDictData dict : datas)
144             {
145                 for (String label : dictLabel.split(separator))
146                 {
147                     if (label.equals(dict.getDictLabel()))
148                     {
149                         propertyString.append(dict.getDictValue()).append(separator);
150                         break;
151                     }
152                 }
153             }
154         }
155         else
156         {
157             for (SysDictData dict : datas)
158             {
159                 if (dictLabel.equals(dict.getDictLabel()))
160                 {
161                     return dict.getDictValue();
162                 }
163             }
164         }
165         return StringUtils.stripEnd(propertyString.toString(), separator);
166     }
167
168     /**
169      * 根据字典类型获取字典所有值
170      *
171      * @param dictType 字典类型
172      * @return 字典值
173      */
174     public static String getDictValues(String dictType)
175     {
176         StringBuilder propertyString = new StringBuilder();
177         List<SysDictData> datas = getDictCache(dictType);
178         if (StringUtils.isNull(datas))
179         {
180             return StringUtils.EMPTY;
181         }
182         for (SysDictData dict : datas)
183         {
184             propertyString.append(dict.getDictValue()).append(SEPARATOR);
185         }
186         return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
187     }
188
189     /**
190      * 根据字典类型获取字典所有标签
191      *
192      * @param dictType 字典类型
193      * @return 字典值
194      */
195     public static String getDictLabels(String dictType)
196     {
197         StringBuilder propertyString = new StringBuilder();
198         List<SysDictData> datas = getDictCache(dictType);
199         if (StringUtils.isNull(datas))
200         {
201             return StringUtils.EMPTY;
202         }
203         for (SysDictData dict : datas)
204         {
205             propertyString.append(dict.getDictLabel()).append(SEPARATOR);
206         }
207         return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
208     }
209
210     /**
211      * 删除指定字典缓存
212      * 
213      * @param key 字典键
214      */
215     public static void removeDictCache(String key)
216     {
217         SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(key));
218     }
219
220     /**
221      * 清空字典缓存
222      */
223     public static void clearDictCache()
224     {
225         Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(CacheConstants.SYS_DICT_KEY + "*");
226         SpringUtils.getBean(RedisCache.class).deleteObject(keys);
227     }
228
229     /**
230      * 设置cache key
231      * 
232      * @param configKey 参数键
233      * @return 缓存键key
234      */
235     public static String getCacheKey(String configKey)
236     {
237         return CacheConstants.SYS_DICT_KEY + configKey;
238     }
239 }