| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- 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.List;
- import java.util.Map;
- /**
- * 字典工具类
- *
- * @author ruoyi
- */
- @Component
- public class DictUtils {
- /**
- * 分隔符
- */
- public static final String SEPARATOR = ",";
- /**
- * 设置字典缓存
- *
- * @param key 参数键
- * @param dictDatas 字典数据列表
- */
- public static void setDictCache(String key, List<SysDictData> dictDatas) {
- CacheUtils.put(getCacheName(), getCacheKey(key), dictDatas);
- }
- /**
- * 获取字典缓存
- *
- * @param key 参数键
- * @return dictDatas 字典数据列表
- */
- public static List<SysDictData> getDictCache(String key) {
- Object cacheObj = CacheUtils.get(getCacheName(), getCacheKey(key));
- if (StringUtils.isNotNull(cacheObj)) {
- return StringUtils.cast(cacheObj);
- }
- return null;
- }
- public static Map<String, String> getDictCacheToMap(String key) {
- List<SysDictData> dictCache = getDictCache(key);
- Map<String, String> result = new HashMap<>();
- dictCache.forEach(sysdictData -> {
- result.put(sysdictData.getDictValue(), sysdictData.getDictLabel());
- });
- return result;
- }
- /**
- * 根据字典类型和字典值获取字典标签
- *
- * @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<SysDictData> datas = getDictCache(dictType);
- if (StringUtils.isNull(datas)) {
- return StringUtils.EMPTY;
- }
- 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);
- break;
- }
- }
- }
- } else {
- for (SysDictData dict : datas) {
- if (dictValue.equals(dict.getDictValue())) {
- return dict.getDictLabel();
- }
- }
- }
- 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.isNull(datas)) {
- return StringUtils.EMPTY;
- }
- 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);
- break;
- }
- }
- }
- } else {
- for (SysDictData 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<SysDictData> datas = getDictCache(dictType);
- if (StringUtils.isNull(datas)) {
- return StringUtils.EMPTY;
- }
- for (SysDictData 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<SysDictData> datas = getDictCache(dictType);
- if (StringUtils.isNull(datas)) {
- return StringUtils.EMPTY;
- }
- for (SysDictData dict : datas) {
- propertyString.append(dict.getDictLabel()).append(SEPARATOR);
- }
- return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
- }
- /**
- * 删除指定字典缓存
- *
- * @param key 字典键
- */
- public static void removeDictCache(String key) {
- CacheUtils.remove(getCacheName(), getCacheKey(key));
- }
- /**
- * 清空字典缓存
- */
- public static void clearDictCache() {
- CacheUtils.removeAll(getCacheName());
- }
- /**
- * 获取cache name
- *
- * @return 缓存名
- */
- public static String getCacheName() {
- return Constants.SYS_DICT_CACHE;
- }
- /**
- * 设置cache key
- *
- * @param configKey 参数键
- * @return 缓存键key
- */
- public static String getCacheKey(String configKey) {
- return Constants.SYS_DICT_KEY + configKey;
- }
- }
|