DictUtils.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.ruoyi.common.utils;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.lang.Validator;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.ruoyi.common.constant.Constants;
  6. import com.ruoyi.common.core.domain.entity.SysDictData;
  7. import com.ruoyi.common.core.redis.RedisCache;
  8. import com.ruoyi.common.utils.spring.SpringUtils;
  9. import java.util.Collection;
  10. import java.util.List;
  11. /**
  12. * 字典工具类
  13. *
  14. * @author ruoyi
  15. */
  16. public class DictUtils
  17. {
  18. /**
  19. * 分隔符
  20. */
  21. public static final String SEPARATOR = ",";
  22. /**
  23. * 设置字典缓存
  24. *
  25. * @param key 参数键
  26. * @param dictDatas 字典数据列表
  27. */
  28. public static void setDictCache(String key, List<SysDictData> dictDatas)
  29. {
  30. SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), dictDatas);
  31. }
  32. /**
  33. * 获取字典缓存
  34. *
  35. * @param key 参数键
  36. * @return dictDatas 字典数据列表
  37. */
  38. public static List<SysDictData> getDictCache(String key)
  39. {
  40. Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
  41. if (Validator.isNotNull(cacheObj))
  42. {
  43. List<SysDictData> dictDatas = (List<SysDictData>)cacheObj;
  44. return dictDatas;
  45. }
  46. return null;
  47. }
  48. /**
  49. * 根据字典类型和字典值获取字典标签
  50. *
  51. * @param dictType 字典类型
  52. * @param dictValue 字典值
  53. * @return 字典标签
  54. */
  55. public static String getDictLabel(String dictType, String dictValue)
  56. {
  57. return getDictLabel(dictType, dictValue, SEPARATOR);
  58. }
  59. /**
  60. * 根据字典类型和字典标签获取字典值
  61. *
  62. * @param dictType 字典类型
  63. * @param dictLabel 字典标签
  64. * @return 字典值
  65. */
  66. public static String getDictValue(String dictType, String dictLabel)
  67. {
  68. return getDictValue(dictType, dictLabel, SEPARATOR);
  69. }
  70. /**
  71. * 根据字典类型和字典值获取字典标签
  72. *
  73. * @param dictType 字典类型
  74. * @param dictValue 字典值
  75. * @param separator 分隔符
  76. * @return 字典标签
  77. */
  78. public static String getDictLabel(String dictType, String dictValue, String separator)
  79. {
  80. StringBuilder propertyString = new StringBuilder();
  81. List<SysDictData> datas = getDictCache(dictType);
  82. if (StrUtil.containsAny(dictValue, separator) && CollUtil.isNotEmpty(datas))
  83. {
  84. for (SysDictData dict : datas)
  85. {
  86. for (String value : dictValue.split(separator))
  87. {
  88. if (value.equals(dict.getDictValue()))
  89. {
  90. propertyString.append(dict.getDictLabel() + separator);
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. else
  97. {
  98. for (SysDictData dict : datas)
  99. {
  100. if (dictValue.equals(dict.getDictValue()))
  101. {
  102. return dict.getDictLabel();
  103. }
  104. }
  105. }
  106. return StrUtil.strip(propertyString.toString(), null, separator);
  107. }
  108. /**
  109. * 根据字典类型和字典标签获取字典值
  110. *
  111. * @param dictType 字典类型
  112. * @param dictLabel 字典标签
  113. * @param separator 分隔符
  114. * @return 字典值
  115. */
  116. public static String getDictValue(String dictType, String dictLabel, String separator)
  117. {
  118. StringBuilder propertyString = new StringBuilder();
  119. List<SysDictData> datas = getDictCache(dictType);
  120. if (StrUtil.containsAny(dictLabel, separator) && CollUtil.isNotEmpty(datas))
  121. {
  122. for (SysDictData dict : datas)
  123. {
  124. for (String label : dictLabel.split(separator))
  125. {
  126. if (label.equals(dict.getDictLabel()))
  127. {
  128. propertyString.append(dict.getDictValue() + separator);
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. else
  135. {
  136. for (SysDictData dict : datas)
  137. {
  138. if (dictLabel.equals(dict.getDictLabel()))
  139. {
  140. return dict.getDictValue();
  141. }
  142. }
  143. }
  144. return StrUtil.strip(propertyString.toString(), null, separator);
  145. }
  146. /**
  147. * 删除指定字典缓存
  148. *
  149. * @param key 字典键
  150. */
  151. public static void removeDictCache(String key)
  152. {
  153. SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(key));
  154. }
  155. /**
  156. * 清空字典缓存
  157. */
  158. public static void clearDictCache()
  159. {
  160. Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(Constants.SYS_DICT_KEY + "*");
  161. SpringUtils.getBean(RedisCache.class).deleteObject(keys);
  162. }
  163. /**
  164. * 设置cache key
  165. *
  166. * @param configKey 参数键
  167. * @return 缓存键key
  168. */
  169. public static String getCacheKey(String configKey)
  170. {
  171. return Constants.SYS_DICT_KEY + configKey;
  172. }
  173. }