|
@@ -1,21 +1,20 @@
|
|
|
package beilv.common.utils;
|
|
package beilv.common.utils;
|
|
|
|
|
|
|
|
|
|
+import beilv.common.constant.Constants;
|
|
|
|
|
+import beilv.common.core.domain.entity.SysDictData;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
|
|
-import beilv.common.constant.Constants;
|
|
|
|
|
-import beilv.common.core.domain.entity.SysDictData;
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 字典工具类
|
|
* 字典工具类
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @author ruoyi
|
|
* @author ruoyi
|
|
|
*/
|
|
*/
|
|
|
@Component
|
|
@Component
|
|
|
-public class DictUtils
|
|
|
|
|
-{
|
|
|
|
|
|
|
+public class DictUtils {
|
|
|
/**
|
|
/**
|
|
|
* 分隔符
|
|
* 分隔符
|
|
|
*/
|
|
*/
|
|
@@ -23,36 +22,33 @@ public class DictUtils
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 设置字典缓存
|
|
* 设置字典缓存
|
|
|
- *
|
|
|
|
|
- * @param key 参数键
|
|
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param key 参数键
|
|
|
* @param dictDatas 字典数据列表
|
|
* @param dictDatas 字典数据列表
|
|
|
*/
|
|
*/
|
|
|
- public static void setDictCache(String key, List<SysDictData> dictDatas)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static void setDictCache(String key, List<SysDictData> dictDatas) {
|
|
|
CacheUtils.put(getCacheName(), getCacheKey(key), dictDatas);
|
|
CacheUtils.put(getCacheName(), getCacheKey(key), dictDatas);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 获取字典缓存
|
|
* 获取字典缓存
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param key 参数键
|
|
* @param key 参数键
|
|
|
* @return dictDatas 字典数据列表
|
|
* @return dictDatas 字典数据列表
|
|
|
*/
|
|
*/
|
|
|
- public static List<SysDictData> getDictCache(String key)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static List<SysDictData> getDictCache(String key) {
|
|
|
Object cacheObj = CacheUtils.get(getCacheName(), getCacheKey(key));
|
|
Object cacheObj = CacheUtils.get(getCacheName(), getCacheKey(key));
|
|
|
- if (StringUtils.isNotNull(cacheObj))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (StringUtils.isNotNull(cacheObj)) {
|
|
|
return StringUtils.cast(cacheObj);
|
|
return StringUtils.cast(cacheObj);
|
|
|
}
|
|
}
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static Map<String, String> getDictCacheToMap(String key){
|
|
|
|
|
|
|
+ public static Map<String, String> getDictCacheToMap(String key) {
|
|
|
List<SysDictData> dictCache = getDictCache(key);
|
|
List<SysDictData> dictCache = getDictCache(key);
|
|
|
Map<String, String> result = new HashMap<>();
|
|
Map<String, String> result = new HashMap<>();
|
|
|
|
|
|
|
|
- dictCache.forEach(sysdictData->{
|
|
|
|
|
|
|
+ dictCache.forEach(sysdictData -> {
|
|
|
result.put(sysdictData.getDictValue(), sysdictData.getDictLabel());
|
|
result.put(sysdictData.getDictValue(), sysdictData.getDictLabel());
|
|
|
});
|
|
});
|
|
|
return result;
|
|
return result;
|
|
@@ -60,15 +56,13 @@ public class DictUtils
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 根据字典类型和字典值获取字典标签
|
|
* 根据字典类型和字典值获取字典标签
|
|
|
- *
|
|
|
|
|
- * @param dictType 字典类型
|
|
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param dictType 字典类型
|
|
|
* @param dictValue 字典值
|
|
* @param dictValue 字典值
|
|
|
* @return 字典标签
|
|
* @return 字典标签
|
|
|
*/
|
|
*/
|
|
|
- public static String getDictLabel(String dictType, String dictValue)
|
|
|
|
|
- {
|
|
|
|
|
- if (StringUtils.isEmpty(dictValue))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static String getDictLabel(String dictType, String dictValue) {
|
|
|
|
|
+ if (StringUtils.isEmpty(dictValue)) {
|
|
|
return StringUtils.EMPTY;
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
}
|
|
|
return getDictLabel(dictType, dictValue, SEPARATOR);
|
|
return getDictLabel(dictType, dictValue, SEPARATOR);
|
|
@@ -76,15 +70,13 @@ public class DictUtils
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 根据字典类型和字典标签获取字典值
|
|
* 根据字典类型和字典标签获取字典值
|
|
|
- *
|
|
|
|
|
- * @param dictType 字典类型
|
|
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param dictType 字典类型
|
|
|
* @param dictLabel 字典标签
|
|
* @param dictLabel 字典标签
|
|
|
* @return 字典值
|
|
* @return 字典值
|
|
|
*/
|
|
*/
|
|
|
- public static String getDictValue(String dictType, String dictLabel)
|
|
|
|
|
- {
|
|
|
|
|
- if (StringUtils.isEmpty(dictLabel))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static String getDictValue(String dictType, String dictLabel) {
|
|
|
|
|
+ if (StringUtils.isEmpty(dictLabel)) {
|
|
|
return StringUtils.EMPTY;
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
}
|
|
|
return getDictValue(dictType, dictLabel, SEPARATOR);
|
|
return getDictValue(dictType, dictLabel, SEPARATOR);
|
|
@@ -92,40 +84,30 @@ public class DictUtils
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 根据字典类型和字典值获取字典标签
|
|
* 根据字典类型和字典值获取字典标签
|
|
|
- *
|
|
|
|
|
- * @param dictType 字典类型
|
|
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param dictType 字典类型
|
|
|
* @param dictValue 字典值
|
|
* @param dictValue 字典值
|
|
|
* @param separator 分隔符
|
|
* @param separator 分隔符
|
|
|
* @return 字典标签
|
|
* @return 字典标签
|
|
|
*/
|
|
*/
|
|
|
- public static String getDictLabel(String dictType, String dictValue, String separator)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static String getDictLabel(String dictType, String dictValue, String separator) {
|
|
|
StringBuilder propertyString = new StringBuilder();
|
|
StringBuilder propertyString = new StringBuilder();
|
|
|
List<SysDictData> datas = getDictCache(dictType);
|
|
List<SysDictData> datas = getDictCache(dictType);
|
|
|
- if (StringUtils.isNull(datas))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (StringUtils.isNull(datas)) {
|
|
|
return StringUtils.EMPTY;
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
}
|
|
|
- if (StringUtils.containsAny(dictValue, separator))
|
|
|
|
|
- {
|
|
|
|
|
- for (SysDictData dict : datas)
|
|
|
|
|
- {
|
|
|
|
|
- for (String value : dictValue.split(separator))
|
|
|
|
|
- {
|
|
|
|
|
- if (value.equals(dict.getDictValue()))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (StringUtils.containsAny(dictValue, separator)) {
|
|
|
|
|
+ for (SysDictData dict : datas) {
|
|
|
|
|
+ for (String value : dictValue.split(separator)) {
|
|
|
|
|
+ if (value.equals(dict.getDictValue())) {
|
|
|
propertyString.append(dict.getDictLabel()).append(separator);
|
|
propertyString.append(dict.getDictLabel()).append(separator);
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- for (SysDictData dict : datas)
|
|
|
|
|
- {
|
|
|
|
|
- if (dictValue.equals(dict.getDictValue()))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } else {
|
|
|
|
|
+ for (SysDictData dict : datas) {
|
|
|
|
|
+ if (dictValue.equals(dict.getDictValue())) {
|
|
|
return dict.getDictLabel();
|
|
return dict.getDictLabel();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -135,40 +117,30 @@ public class DictUtils
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 根据字典类型和字典标签获取字典值
|
|
* 根据字典类型和字典标签获取字典值
|
|
|
- *
|
|
|
|
|
- * @param dictType 字典类型
|
|
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param dictType 字典类型
|
|
|
* @param dictLabel 字典标签
|
|
* @param dictLabel 字典标签
|
|
|
* @param separator 分隔符
|
|
* @param separator 分隔符
|
|
|
* @return 字典值
|
|
* @return 字典值
|
|
|
*/
|
|
*/
|
|
|
- public static String getDictValue(String dictType, String dictLabel, String separator)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static String getDictValue(String dictType, String dictLabel, String separator) {
|
|
|
StringBuilder propertyString = new StringBuilder();
|
|
StringBuilder propertyString = new StringBuilder();
|
|
|
List<SysDictData> datas = getDictCache(dictType);
|
|
List<SysDictData> datas = getDictCache(dictType);
|
|
|
- if (StringUtils.isNull(datas))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (StringUtils.isNull(datas)) {
|
|
|
return StringUtils.EMPTY;
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
}
|
|
|
- if (StringUtils.containsAny(dictLabel, separator))
|
|
|
|
|
- {
|
|
|
|
|
- for (SysDictData dict : datas)
|
|
|
|
|
- {
|
|
|
|
|
- for (String label : dictLabel.split(separator))
|
|
|
|
|
- {
|
|
|
|
|
- if (label.equals(dict.getDictLabel()))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (StringUtils.containsAny(dictLabel, separator)) {
|
|
|
|
|
+ for (SysDictData dict : datas) {
|
|
|
|
|
+ for (String label : dictLabel.split(separator)) {
|
|
|
|
|
+ if (label.equals(dict.getDictLabel())) {
|
|
|
propertyString.append(dict.getDictValue()).append(separator);
|
|
propertyString.append(dict.getDictValue()).append(separator);
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- for (SysDictData dict : datas)
|
|
|
|
|
- {
|
|
|
|
|
- if (dictLabel.equals(dict.getDictLabel()))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } else {
|
|
|
|
|
+ for (SysDictData dict : datas) {
|
|
|
|
|
+ if (dictLabel.equals(dict.getDictLabel())) {
|
|
|
return dict.getDictValue();
|
|
return dict.getDictValue();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -182,16 +154,13 @@ public class DictUtils
|
|
|
* @param dictType 字典类型
|
|
* @param dictType 字典类型
|
|
|
* @return 字典值
|
|
* @return 字典值
|
|
|
*/
|
|
*/
|
|
|
- public static String getDictValues(String dictType)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static String getDictValues(String dictType) {
|
|
|
StringBuilder propertyString = new StringBuilder();
|
|
StringBuilder propertyString = new StringBuilder();
|
|
|
List<SysDictData> datas = getDictCache(dictType);
|
|
List<SysDictData> datas = getDictCache(dictType);
|
|
|
- if (StringUtils.isNull(datas))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (StringUtils.isNull(datas)) {
|
|
|
return StringUtils.EMPTY;
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
}
|
|
|
- for (SysDictData dict : datas)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ for (SysDictData dict : datas) {
|
|
|
propertyString.append(dict.getDictValue()).append(SEPARATOR);
|
|
propertyString.append(dict.getDictValue()).append(SEPARATOR);
|
|
|
}
|
|
}
|
|
|
return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
|
|
return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
|
|
@@ -203,16 +172,13 @@ public class DictUtils
|
|
|
* @param dictType 字典类型
|
|
* @param dictType 字典类型
|
|
|
* @return 字典值
|
|
* @return 字典值
|
|
|
*/
|
|
*/
|
|
|
- public static String getDictLabels(String dictType)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static String getDictLabels(String dictType) {
|
|
|
StringBuilder propertyString = new StringBuilder();
|
|
StringBuilder propertyString = new StringBuilder();
|
|
|
List<SysDictData> datas = getDictCache(dictType);
|
|
List<SysDictData> datas = getDictCache(dictType);
|
|
|
- if (StringUtils.isNull(datas))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (StringUtils.isNull(datas)) {
|
|
|
return StringUtils.EMPTY;
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
}
|
|
|
- for (SysDictData dict : datas)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ for (SysDictData dict : datas) {
|
|
|
propertyString.append(dict.getDictLabel()).append(SEPARATOR);
|
|
propertyString.append(dict.getDictLabel()).append(SEPARATOR);
|
|
|
}
|
|
}
|
|
|
return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
|
|
return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
|
|
@@ -220,40 +186,36 @@ public class DictUtils
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 删除指定字典缓存
|
|
* 删除指定字典缓存
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param key 字典键
|
|
* @param key 字典键
|
|
|
*/
|
|
*/
|
|
|
- public static void removeDictCache(String key)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static void removeDictCache(String key) {
|
|
|
CacheUtils.remove(getCacheName(), getCacheKey(key));
|
|
CacheUtils.remove(getCacheName(), getCacheKey(key));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 清空字典缓存
|
|
* 清空字典缓存
|
|
|
*/
|
|
*/
|
|
|
- public static void clearDictCache()
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static void clearDictCache() {
|
|
|
CacheUtils.removeAll(getCacheName());
|
|
CacheUtils.removeAll(getCacheName());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 获取cache name
|
|
* 获取cache name
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @return 缓存名
|
|
* @return 缓存名
|
|
|
*/
|
|
*/
|
|
|
- public static String getCacheName()
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static String getCacheName() {
|
|
|
return Constants.SYS_DICT_CACHE;
|
|
return Constants.SYS_DICT_CACHE;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 设置cache key
|
|
* 设置cache key
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param configKey 参数键
|
|
* @param configKey 参数键
|
|
|
* @return 缓存键key
|
|
* @return 缓存键key
|
|
|
*/
|
|
*/
|
|
|
- public static String getCacheKey(String configKey)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static String getCacheKey(String configKey) {
|
|
|
return Constants.SYS_DICT_KEY + configKey;
|
|
return Constants.SYS_DICT_KEY + configKey;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|