2025-09-22 17:06:47 +08:00
|
|
|
|
package com.ruoyi.cms.util;
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONArray;
|
|
|
|
|
|
import com.ruoyi.cms.domain.BussinessDictData;
|
|
|
|
|
|
import com.ruoyi.common.constant.CacheConstants;
|
|
|
|
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
2026-05-20 19:45:06 +08:00
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
2025-09-22 17:06:47 +08:00
|
|
|
|
|
|
|
|
|
|
import java.util.Collection;
|
2026-05-20 19:45:06 +08:00
|
|
|
|
import java.util.HashMap;
|
2025-09-22 17:06:47 +08:00
|
|
|
|
import java.util.List;
|
2026-05-20 19:45:06 +08:00
|
|
|
|
import java.util.Map;
|
2025-09-22 17:06:47 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 字典工具类
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author ruoyi
|
|
|
|
|
|
*/
|
|
|
|
|
|
public class DictUtils
|
|
|
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 分隔符
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static final String SEPARATOR = ",";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置字典缓存
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param key 参数键
|
|
|
|
|
|
* @param dictDatas 字典数据列表
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static void setDictCache(String key, List<BussinessDictData> dictDatas)
|
|
|
|
|
|
{
|
|
|
|
|
|
SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), dictDatas);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取字典缓存
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param key 参数键
|
|
|
|
|
|
* @return dictDatas 字典数据列表
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static List<BussinessDictData> getDictCache(String key)
|
|
|
|
|
|
{
|
|
|
|
|
|
JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
|
|
|
|
|
|
if (StringUtils.isNotNull(arrayCache))
|
|
|
|
|
|
{
|
|
|
|
|
|
return arrayCache.toList(BussinessDictData.class);
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 根据字典类型和字典值获取字典标签
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param dictType 字典类型
|
|
|
|
|
|
* @param dictValue 字典值
|
|
|
|
|
|
* @return 字典标签
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getDictLabel(String dictType, String dictValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (StringUtils.isEmpty(dictValue))
|
|
|
|
|
|
{
|
|
|
|
|
|
return StringUtils.EMPTY;
|
|
|
|
|
|
}
|
|
|
|
|
|
return getDictLabel(dictType, dictValue, SEPARATOR);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 根据字典类型和字典标签获取字典值
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param dictType 字典类型
|
|
|
|
|
|
* @param dictLabel 字典标签
|
|
|
|
|
|
* @return 字典值
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getDictValue(String dictType, String dictLabel)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (StringUtils.isEmpty(dictLabel))
|
|
|
|
|
|
{
|
|
|
|
|
|
return StringUtils.EMPTY;
|
|
|
|
|
|
}
|
|
|
|
|
|
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<BussinessDictData> datas = getDictCache(dictType);
|
|
|
|
|
|
if (StringUtils.isNull(datas))
|
|
|
|
|
|
{
|
|
|
|
|
|
return StringUtils.EMPTY;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (StringUtils.containsAny(separator, dictValue))
|
|
|
|
|
|
{
|
|
|
|
|
|
for (BussinessDictData dict : datas)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (String value : dictValue.split(separator))
|
|
|
|
|
|
{
|
2026-02-03 12:39:27 +08:00
|
|
|
|
if (value.trim().equals(dict.getDictValue().trim()))
|
2025-09-22 17:06:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
propertyString.append(dict.getDictLabel()).append(separator);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
for (BussinessDictData dict : datas)
|
|
|
|
|
|
{
|
2026-02-03 12:39:27 +08:00
|
|
|
|
if (dictValue.trim().equals(dict.getDictValue().trim()))
|
2025-09-22 17:06:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
return dict.getDictLabel();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return StringUtils.stripEnd(propertyString.toString(), separator);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-20 19:45:06 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 根据字典类型和字典值获取字典标签
|
|
|
|
|
|
* @param dictType 字典类型
|
|
|
|
|
|
* @param dictValue 字典值(支持单个值 或 逗号/指定分隔符分隔的多个值)
|
|
|
|
|
|
* @param separator 分隔符
|
|
|
|
|
|
* @return 字典标签(多个用传入的分隔符隔开)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getDictLabelNames(String dictType, String dictValue, String separator) {
|
|
|
|
|
|
if (StringUtils.isBlank(dictType) || StringUtils.isBlank(dictValue)) {
|
|
|
|
|
|
return StringUtils.EMPTY;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<BussinessDictData> datas = getDictCache(dictType);
|
|
|
|
|
|
if (CollectionUtils.isEmpty(datas)) {
|
|
|
|
|
|
return StringUtils.EMPTY;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, String> dictMap = new HashMap<>();
|
|
|
|
|
|
for (BussinessDictData dict : datas) {
|
|
|
|
|
|
dictMap.put(dict.getDictValue().trim(), dict.getDictLabel());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder labelSb = new StringBuilder();
|
|
|
|
|
|
String[] valueArr = dictValue.split(separator);
|
|
|
|
|
|
|
|
|
|
|
|
for (String val : valueArr) {
|
|
|
|
|
|
String value = val.trim();
|
|
|
|
|
|
if (dictMap.containsKey(value)) {
|
|
|
|
|
|
labelSb.append(dictMap.get(value)).append(separator);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return StringUtils.stripEnd(labelSb.toString(), separator);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-22 17:06:47 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 根据字典类型和字典标签获取字典值
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param dictType 字典类型
|
|
|
|
|
|
* @param dictLabel 字典标签
|
|
|
|
|
|
* @param separator 分隔符
|
|
|
|
|
|
* @return 字典值
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getDictValue(String dictType, String dictLabel, String separator)
|
|
|
|
|
|
{
|
|
|
|
|
|
StringBuilder propertyString = new StringBuilder();
|
|
|
|
|
|
List<BussinessDictData> datas = getDictCache(dictType);
|
|
|
|
|
|
if (StringUtils.isNull(datas))
|
|
|
|
|
|
{
|
|
|
|
|
|
return StringUtils.EMPTY;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (StringUtils.containsAny(separator, dictLabel))
|
|
|
|
|
|
{
|
|
|
|
|
|
for (BussinessDictData dict : datas)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (String label : dictLabel.split(separator))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (label.equals(dict.getDictLabel()))
|
|
|
|
|
|
{
|
|
|
|
|
|
propertyString.append(dict.getDictValue()).append(separator);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
for (BussinessDictData dict : datas)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dictLabel.equals(dict.getDictLabel()))
|
|
|
|
|
|
{
|
|
|
|
|
|
return dict.getDictValue();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return StringUtils.stripEnd(propertyString.toString(), separator);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 根据字典类型获取字典所有值
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param dictType 字典类型
|
|
|
|
|
|
* @return 字典值
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getDictValues(String dictType)
|
|
|
|
|
|
{
|
|
|
|
|
|
StringBuilder propertyString = new StringBuilder();
|
|
|
|
|
|
List<BussinessDictData> datas = getDictCache(dictType);
|
|
|
|
|
|
if (StringUtils.isNull(datas))
|
|
|
|
|
|
{
|
|
|
|
|
|
return StringUtils.EMPTY;
|
|
|
|
|
|
}
|
|
|
|
|
|
for (BussinessDictData dict : datas)
|
|
|
|
|
|
{
|
|
|
|
|
|
propertyString.append(dict.getDictValue()).append(SEPARATOR);
|
|
|
|
|
|
}
|
|
|
|
|
|
return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 根据字典类型获取字典所有标签
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param dictType 字典类型
|
|
|
|
|
|
* @return 字典值
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getDictLabels(String dictType)
|
|
|
|
|
|
{
|
|
|
|
|
|
StringBuilder propertyString = new StringBuilder();
|
|
|
|
|
|
List<BussinessDictData> datas = getDictCache(dictType);
|
|
|
|
|
|
if (StringUtils.isNull(datas))
|
|
|
|
|
|
{
|
|
|
|
|
|
return StringUtils.EMPTY;
|
|
|
|
|
|
}
|
|
|
|
|
|
for (BussinessDictData dict : datas)
|
|
|
|
|
|
{
|
|
|
|
|
|
propertyString.append(dict.getDictLabel()).append(SEPARATOR);
|
|
|
|
|
|
}
|
|
|
|
|
|
return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 删除指定字典缓存
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param key 字典键
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static void removeDictCache(String key)
|
|
|
|
|
|
{
|
|
|
|
|
|
SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 清空字典缓存
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static void clearDictCache()
|
|
|
|
|
|
{
|
|
|
|
|
|
Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(CacheConstants.BUSSINESS_DICT_KEY + "*");
|
|
|
|
|
|
SpringUtils.getBean(RedisCache.class).deleteObject(keys);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置cache key
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param configKey 参数键
|
|
|
|
|
|
* @return 缓存键key
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getCacheKey(String configKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CacheConstants.BUSSINESS_DICT_KEY + configKey;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|