DictUtils.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package com.ruoyi.common.utils;
  2. import java.util.Collection;
  3. import java.util.List;
  4. import com.alibaba.fastjson2.JSONArray;
  5. import com.ruoyi.common.constant.CacheConstants;
  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. /**
  10. * 字典工具类
  11. *
  12. * @author ruoyi
  13. */
  14. public class DictUtils
  15. {
  16. /**
  17. * 分隔符
  18. */
  19. public static final String SEPARATOR = ",";
  20. /**
  21. * 设置字典缓存
  22. *
  23. * @param key 参数键
  24. * @param dictDatas 字典数据列表
  25. */
  26. public static void setDictCache(String key, List<SysDictData> dictDatas)
  27. {
  28. SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), dictDatas);
  29. }
  30. /**
  31. * 获取字典缓存
  32. *
  33. * @param key 参数键
  34. * @return dictDatas 字典数据列表
  35. */
  36. public static List<SysDictData> getDictCache(String key)
  37. {
  38. JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
  39. if (StringUtils.isNotNull(arrayCache))
  40. {
  41. return arrayCache.toList(SysDictData.class);
  42. }
  43. return null;
  44. }
  45. /**
  46. * 根据字典类型和字典值获取字典标签
  47. *
  48. * @param dictType 字典类型
  49. * @param dictValue 字典值
  50. * @return 字典标签
  51. */
  52. public static String getDictLabel(String dictType, String dictValue)
  53. {
  54. if (StringUtils.isEmpty(dictValue))
  55. {
  56. return StringUtils.EMPTY;
  57. }
  58. return getDictLabel(dictType, dictValue, SEPARATOR);
  59. }
  60. /**
  61. * 根据字典类型和字典标签获取字典值
  62. *
  63. * @param dictType 字典类型
  64. * @param dictLabel 字典标签
  65. * @return 字典值
  66. */
  67. public static String getDictValue(String dictType, String dictLabel)
  68. {
  69. if (StringUtils.isEmpty(dictLabel))
  70. {
  71. return StringUtils.EMPTY;
  72. }
  73. return getDictValue(dictType, dictLabel, SEPARATOR);
  74. }
  75. /**
  76. * 根据字典类型和字典值获取字典标签
  77. *
  78. * @param dictType 字典类型
  79. * @param dictValue 字典值
  80. * @param separator 分隔符
  81. * @return 字典标签
  82. */
  83. public static String getDictLabel(String dictType, String dictValue, String separator)
  84. {
  85. StringBuilder propertyString = new StringBuilder();
  86. List<SysDictData> datas = getDictCache(dictType);
  87. if (StringUtils.isNull(datas))
  88. {
  89. return StringUtils.EMPTY;
  90. }
  91. if (StringUtils.containsAny(separator, dictValue))
  92. {
  93. for (SysDictData dict : datas)
  94. {
  95. for (String value : dictValue.split(separator))
  96. {
  97. if (value.equals(dict.getDictValue()))
  98. {
  99. propertyString.append(dict.getDictLabel()).append(separator);
  100. break;
  101. }
  102. }
  103. }
  104. }
  105. else
  106. {
  107. for (SysDictData dict : datas)
  108. {
  109. if (dictValue.equals(dict.getDictValue()))
  110. {
  111. return dict.getDictLabel();
  112. }
  113. }
  114. }
  115. return StringUtils.stripEnd(propertyString.toString(), separator);
  116. }
  117. /**
  118. * 根据字典类型和字典标签获取字典值
  119. *
  120. * @param dictType 字典类型
  121. * @param dictLabel 字典标签
  122. * @param separator 分隔符
  123. * @return 字典值
  124. */
  125. public static String getDictValue(String dictType, String dictLabel, String separator)
  126. {
  127. StringBuilder propertyString = new StringBuilder();
  128. List<SysDictData> datas = getDictCache(dictType);
  129. if (StringUtils.isNull(datas))
  130. {
  131. return StringUtils.EMPTY;
  132. }
  133. if (StringUtils.containsAny(separator, dictLabel))
  134. {
  135. for (SysDictData dict : datas)
  136. {
  137. for (String label : dictLabel.split(separator))
  138. {
  139. if (label.equals(dict.getDictLabel()))
  140. {
  141. propertyString.append(dict.getDictValue()).append(separator);
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. else
  148. {
  149. for (SysDictData dict : datas)
  150. {
  151. if (dictLabel.equals(dict.getDictLabel()))
  152. {
  153. return dict.getDictValue();
  154. }
  155. }
  156. }
  157. return StringUtils.stripEnd(propertyString.toString(), separator);
  158. }
  159. /**
  160. * 根据字典类型获取字典所有值
  161. *
  162. * @param dictType 字典类型
  163. * @return 字典值
  164. */
  165. public static String getDictValues(String dictType)
  166. {
  167. StringBuilder propertyString = new StringBuilder();
  168. List<SysDictData> datas = getDictCache(dictType);
  169. if (StringUtils.isNull(datas))
  170. {
  171. return StringUtils.EMPTY;
  172. }
  173. for (SysDictData dict : datas)
  174. {
  175. propertyString.append(dict.getDictValue()).append(SEPARATOR);
  176. }
  177. return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
  178. }
  179. /**
  180. * 根据字典类型获取字典所有标签
  181. *
  182. * @param dictType 字典类型
  183. * @return 字典值
  184. */
  185. public static String getDictLabels(String dictType)
  186. {
  187. StringBuilder propertyString = new StringBuilder();
  188. List<SysDictData> datas = getDictCache(dictType);
  189. if (StringUtils.isNull(datas))
  190. {
  191. return StringUtils.EMPTY;
  192. }
  193. for (SysDictData dict : datas)
  194. {
  195. propertyString.append(dict.getDictLabel()).append(SEPARATOR);
  196. }
  197. return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
  198. }
  199. /**
  200. * 删除指定字典缓存
  201. *
  202. * @param key 字典键
  203. */
  204. public static void removeDictCache(String key)
  205. {
  206. SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(key));
  207. }
  208. /**
  209. * 清空字典缓存
  210. */
  211. public static void clearDictCache()
  212. {
  213. Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(CacheConstants.SYS_DICT_KEY + "*");
  214. SpringUtils.getBean(RedisCache.class).deleteObject(keys);
  215. }
  216. /**
  217. * 设置cache key
  218. *
  219. * @param configKey 参数键
  220. * @return 缓存键key
  221. */
  222. public static String getCacheKey(String configKey)
  223. {
  224. return CacheConstants.SYS_DICT_KEY + configKey;
  225. }
  226. }