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