DictUtils.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 java.util.Collection;
  6. import java.util.List;
  7. /**
  8. * 字典工具类
  9. *
  10. * @author ruoyi
  11. */
  12. public class DictUtils
  13. {
  14. /**
  15. * 分隔符
  16. */
  17. public static final String SEPARATOR = ",";
  18. /**
  19. * 设置字典缓存
  20. *
  21. * @param key 参数键
  22. * @param dictDatas 字典数据列表
  23. */
  24. public static void setDictCache(String key, List<SysDictData> dictDatas)
  25. {
  26. RedisUtils.setCacheObject(getCacheKey(key), dictDatas);
  27. }
  28. /**
  29. * 获取字典缓存
  30. *
  31. * @param key 参数键
  32. * @return dictDatas 字典数据列表
  33. */
  34. public static List<SysDictData> getDictCache(String key)
  35. {
  36. Object cacheObj = RedisUtils.getCacheObject(getCacheKey(key));
  37. if (StringUtils.isNotNull(cacheObj))
  38. {
  39. List<SysDictData> dictDatas = (List<SysDictData>)cacheObj;
  40. return dictDatas;
  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(dictValue, separator) && CollUtil.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() + 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(dictLabel, separator) && CollUtil.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() + 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. RedisUtils.deleteObject(getCacheKey(key));
  150. }
  151. /**
  152. * 清空字典缓存
  153. */
  154. public static void clearDictCache()
  155. {
  156. Collection<String> keys = RedisUtils.keys(Constants.SYS_DICT_KEY + "*");
  157. RedisUtils.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. }