DictUtils.java 4.9 KB

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