DictUtils.java 5.1 KB

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