DictUtils.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.isNotNull(datas))
  88. {
  89. if (StringUtils.containsAny(separator, dictValue))
  90. {
  91. for (SysDictData dict : datas)
  92. {
  93. for (String value : dictValue.split(separator))
  94. {
  95. if (value.equals(dict.getDictValue()))
  96. {
  97. propertyString.append(dict.getDictLabel()).append(separator);
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. else
  104. {
  105. for (SysDictData dict : datas)
  106. {
  107. if (dictValue.equals(dict.getDictValue()))
  108. {
  109. return dict.getDictLabel();
  110. }
  111. }
  112. }
  113. }
  114. return StringUtils.stripEnd(propertyString.toString(), separator);
  115. }
  116. /**
  117. * 根据字典类型和字典标签获取字典值
  118. *
  119. * @param dictType 字典类型
  120. * @param dictLabel 字典标签
  121. * @param separator 分隔符
  122. * @return 字典值
  123. */
  124. public static String getDictValue(String dictType, String dictLabel, String separator)
  125. {
  126. StringBuilder propertyString = new StringBuilder();
  127. List<SysDictData> datas = getDictCache(dictType);
  128. if (StringUtils.containsAny(separator, dictLabel) && StringUtils.isNotEmpty(datas))
  129. {
  130. for (SysDictData dict : datas)
  131. {
  132. for (String label : dictLabel.split(separator))
  133. {
  134. if (label.equals(dict.getDictLabel()))
  135. {
  136. propertyString.append(dict.getDictValue()).append(separator);
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. else
  143. {
  144. for (SysDictData dict : datas)
  145. {
  146. if (dictLabel.equals(dict.getDictLabel()))
  147. {
  148. return dict.getDictValue();
  149. }
  150. }
  151. }
  152. return StringUtils.stripEnd(propertyString.toString(), separator);
  153. }
  154. /**
  155. * 删除指定字典缓存
  156. *
  157. * @param key 字典键
  158. */
  159. public static void removeDictCache(String key)
  160. {
  161. SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(key));
  162. }
  163. /**
  164. * 清空字典缓存
  165. */
  166. public static void clearDictCache()
  167. {
  168. Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(CacheConstants.SYS_DICT_KEY + "*");
  169. SpringUtils.getBean(RedisCache.class).deleteObject(keys);
  170. }
  171. /**
  172. * 设置cache key
  173. *
  174. * @param configKey 参数键
  175. * @return 缓存键key
  176. */
  177. public static String getCacheKey(String configKey)
  178. {
  179. return CacheConstants.SYS_DICT_KEY + configKey;
  180. }
  181. }