懒羊羊
2024-01-31 e57a8990ae56f657a59c435a0613c5f7a8728003
提交 | 用户 | 时间
e57a89 1 package com.jcdm.common.utils;
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.jcdm.common.constant.CacheConstants;
9 import com.jcdm.common.core.domain.entity.SysDictData;
10 import com.jcdm.common.core.redis.RedisCache;
11 import com.jcdm.common.utils.spring.SpringUtils;
12
13 /**
14  * 字典工具类
15  * 
16  * @author jc
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 = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
45 //        if (StringUtils.isNotNull(arrayCache))
46 //        {
47 //            return arrayCache.toList(SysDictData.class);
48 //        }
49 //        return null;
50 //    }
51     /**
52      * 获取字典缓存
53      *
54      * @param key 参数键
55      * @return dictDatas 字典数据列表
56      */
57     public static List<SysDictData> getDictCache(String key)
58     {
59         JSONArray arrayCache = JSONArray.parseArray(JSON.toJSONString(SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key))));
60 //        JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
61         if (StringUtils.isNotNull(arrayCache))
62         {
63             return arrayCache.toList(SysDictData.class);
64         }
65         return null;
66     }
67
68     /**
69      * 根据字典类型和字典值获取字典标签
70      * 
71      * @param dictType 字典类型
72      * @param dictValue 字典值
73      * @return 字典标签
74      */
75     public static String getDictLabel(String dictType, String dictValue)
76     {
77         return getDictLabel(dictType, dictValue, SEPARATOR);
78     }
79
80     /**
81      * 根据字典类型和字典标签获取字典值
82      * 
83      * @param dictType 字典类型
84      * @param dictLabel 字典标签
85      * @return 字典值
86      */
87     public static String getDictValue(String dictType, String dictLabel)
88     {
89         return getDictValue(dictType, dictLabel, SEPARATOR);
90     }
91
92     /**
93      * 根据字典类型和字典值获取字典标签
94      * 
95      * @param dictType 字典类型
96      * @param dictValue 字典值
97      * @param separator 分隔符
98      * @return 字典标签
99      */
100     public static String getDictLabel(String dictType, String dictValue, String separator)
101     {
102         StringBuilder propertyString = new StringBuilder();
103         List<SysDictData> datas = getDictCache(dictType);
104
105         if (StringUtils.isNotNull(datas))
106         {
107             if (StringUtils.containsAny(separator, dictValue))
108             {
109                 for (SysDictData dict : datas)
110                 {
111                     for (String value : dictValue.split(separator))
112                     {
113                         if (value.equals(dict.getDictValue()))
114                         {
115                             propertyString.append(dict.getDictLabel()).append(separator);
116                             break;
117                         }
118                     }
119                 }
120             }
121             else
122             {
123                 for (SysDictData dict : datas)
124                 {
125                     if (dictValue.equals(dict.getDictValue()))
126                     {
127                         return dict.getDictLabel();
128                     }
129                 }
130             }
131         }
132         return StringUtils.stripEnd(propertyString.toString(), separator);
133     }
134
135     /**
136      * 根据字典类型和字典标签获取字典值
137      * 
138      * @param dictType 字典类型
139      * @param dictLabel 字典标签
140      * @param separator 分隔符
141      * @return 字典值
142      */
143     public static String getDictValue(String dictType, String dictLabel, String separator)
144     {
145         StringBuilder propertyString = new StringBuilder();
146         List<SysDictData> datas = getDictCache(dictType);
147
148         if (StringUtils.containsAny(separator, dictLabel) && StringUtils.isNotEmpty(datas))
149         {
150             for (SysDictData dict : datas)
151             {
152                 for (String label : dictLabel.split(separator))
153                 {
154                     if (label.equals(dict.getDictLabel()))
155                     {
156                         propertyString.append(dict.getDictValue()).append(separator);
157                         break;
158                     }
159                 }
160             }
161         }
162         else
163         {
164             for (SysDictData dict : datas)
165             {
166                 if (dictLabel.equals(dict.getDictLabel()))
167                 {
168                     return dict.getDictValue();
169                 }
170             }
171         }
172         return StringUtils.stripEnd(propertyString.toString(), separator);
173     }
174
175     /**
176      * 删除指定字典缓存
177      * 
178      * @param key 字典键
179      */
180     public static void removeDictCache(String key)
181     {
182         SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(key));
183     }
184
185     /**
186      * 清空字典缓存
187      */
188     public static void clearDictCache()
189     {
190         Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(CacheConstants.SYS_DICT_KEY + "*");
191         SpringUtils.getBean(RedisCache.class).deleteObject(keys);
192     }
193
194     /**
195      * 设置cache key
196      * 
197      * @param configKey 参数键
198      * @return 缓存键key
199      */
200     public static String getCacheKey(String configKey)
201     {
202         return CacheConstants.SYS_DICT_KEY + configKey;
203     }
204 }