DictUtils.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.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. /**
  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. return getDictLabel(dictType, dictValue, SEPARATOR);
  55. }
  56. /**
  57. * 根据字典类型和字典标签获取字典值
  58. *
  59. * @param dictType 字典类型
  60. * @param dictLabel 字典标签
  61. * @return 字典值
  62. */
  63. public static String getDictValue(String dictType, String dictLabel)
  64. {
  65. return getDictValue(dictType, dictLabel, SEPARATOR);
  66. }
  67. /**
  68. * 根据字典类型和字典值获取字典标签
  69. *
  70. * @param dictType 字典类型
  71. * @param dictValue 字典值
  72. * @param separator 分隔符
  73. * @return 字典标签
  74. */
  75. public static String getDictLabel(String dictType, String dictValue, String separator)
  76. {
  77. StringBuilder propertyString = new StringBuilder();
  78. List<SysDictData> datas = getDictCache(dictType);
  79. if (StringUtils.isNotNull(datas))
  80. {
  81. if (StringUtils.containsAny(separator, dictValue))
  82. {
  83. for (SysDictData dict : datas)
  84. {
  85. for (String value : dictValue.split(separator))
  86. {
  87. if (value.equals(dict.getDictValue()))
  88. {
  89. propertyString.append(dict.getDictLabel()).append(separator);
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. else
  96. {
  97. for (SysDictData dict : datas)
  98. {
  99. if (dictValue.equals(dict.getDictValue()))
  100. {
  101. return dict.getDictLabel();
  102. }
  103. }
  104. }
  105. }
  106. return StringUtils.stripEnd(propertyString.toString(), 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 (StringUtils.containsAny(separator, dictLabel) && StringUtils.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()).append(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 StringUtils.stripEnd(propertyString.toString(), 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. }