DictUtils.java 5.0 KB

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