From af0e0a110e7187bf008655f7510199a0c0b25ec4 Mon Sep 17 00:00:00 2001 From: Nymph2333 <498092988@qq.com> Date: 星期一, 10 四月 2023 14:27:40 +0800 Subject: [PATCH] newInstance() 已弃用,使用clazz.getDeclaredConstructor().newInstance() This method propagates any exception thrown by the nullary constructor, including a checked exception. Use of this method effectively bypasses the compile-time exception checking that would otherwise be performed by the compiler. The Constructor.newInstance method avoids this problem by wrapping any exception thrown by the constructor in a (checked) InvocationTargetException. The call clazz.newInstance() can be replaced by clazz.getDeclaredConstructor().newInstance() The latter sequence of calls is inferred to be able to throw the additional exception types InvocationTargetException and NoSuchMethodException. Both of these exception types are subclasses of ReflectiveOperationException. --- ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java | 126 ++++++++++++++++++++++++++++++++--------- 1 files changed, 98 insertions(+), 28 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java index cdc6f6f..39ad84a 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java @@ -2,7 +2,8 @@ import java.util.Collection; import java.util.List; -import com.ruoyi.common.constant.Constants; +import com.alibaba.fastjson2.JSONArray; +import com.ruoyi.common.constant.CacheConstants; import com.ruoyi.common.core.domain.entity.SysDictData; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.spring.SpringUtils; @@ -14,6 +15,11 @@ */ public class DictUtils { + /** + * 分隔符 + */ + public static final String SEPARATOR = ","; + /** * 设置字典缓存 * @@ -33,11 +39,10 @@ */ public static List<SysDictData> getDictCache(String key) { - Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key)); - if (StringUtils.isNotNull(cacheObj)) + JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key)); + if (StringUtils.isNotNull(arrayCache)) { - List<SysDictData> DictDatas = StringUtils.cast(cacheObj); - return DictDatas; + return arrayCache.toList(SysDictData.class); } return null; } @@ -51,21 +56,7 @@ */ public static String getDictLabel(String dictType, String dictValue) { - if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotEmpty(dictValue)) - { - List<SysDictData> datas = getDictCache(dictType); - if (StringUtils.isNotEmpty(datas)) - { - for (SysDictData dict : datas) - { - if (dictValue.equals(dict.getDictValue())) - { - return dict.getDictLabel(); - } - } - } - } - return dictValue; + return getDictLabel(dictType, dictValue, SEPARATOR); } /** @@ -77,21 +68,100 @@ */ public static String getDictValue(String dictType, String dictLabel) { - if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotEmpty(dictLabel)) + return getDictValue(dictType, dictLabel, SEPARATOR); + } + + /** + * 根据字典类型和字典值获取字典标签 + * + * @param dictType 字典类型 + * @param dictValue 字典值 + * @param separator 分隔符 + * @return 字典标签 + */ + public static String getDictLabel(String dictType, String dictValue, String separator) + { + StringBuilder propertyString = new StringBuilder(); + List<SysDictData> datas = getDictCache(dictType); + + if (StringUtils.isNotNull(datas)) { - List<SysDictData> datas = getDictCache(dictType); - if (StringUtils.isNotEmpty(datas)) + if (StringUtils.containsAny(separator, dictValue)) { for (SysDictData dict : datas) { - if (dictLabel.equals(dict.getDictLabel())) + for (String value : dictValue.split(separator)) { - return dict.getDictValue(); + if (value.equals(dict.getDictValue())) + { + propertyString.append(dict.getDictLabel()).append(separator); + break; + } + } + } + } + else + { + for (SysDictData dict : datas) + { + if (dictValue.equals(dict.getDictValue())) + { + return dict.getDictLabel(); } } } } - return dictLabel; + return StringUtils.stripEnd(propertyString.toString(), separator); + } + + /** + * 根据字典类型和字典标签获取字典值 + * + * @param dictType 字典类型 + * @param dictLabel 字典标签 + * @param separator 分隔符 + * @return 字典值 + */ + public static String getDictValue(String dictType, String dictLabel, String separator) + { + StringBuilder propertyString = new StringBuilder(); + List<SysDictData> datas = getDictCache(dictType); + + if (StringUtils.containsAny(separator, dictLabel) && StringUtils.isNotEmpty(datas)) + { + for (SysDictData dict : datas) + { + for (String label : dictLabel.split(separator)) + { + if (label.equals(dict.getDictLabel())) + { + propertyString.append(dict.getDictValue()).append(separator); + break; + } + } + } + } + else + { + for (SysDictData dict : datas) + { + if (dictLabel.equals(dict.getDictLabel())) + { + return dict.getDictValue(); + } + } + } + return StringUtils.stripEnd(propertyString.toString(), separator); + } + + /** + * 删除指定字典缓存 + * + * @param key 字典键 + */ + public static void removeDictCache(String key) + { + SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(key)); } /** @@ -99,7 +169,7 @@ */ public static void clearDictCache() { - Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(Constants.SYS_DICT_KEY + "*"); + Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(CacheConstants.SYS_DICT_KEY + "*"); SpringUtils.getBean(RedisCache.class).deleteObject(keys); } @@ -111,6 +181,6 @@ */ public static String getCacheKey(String configKey) { - return Constants.SYS_DICT_KEY + configKey; + return CacheConstants.SYS_DICT_KEY + configKey; } } -- Gitblit v1.9.2