DictUtils.java 6.5 KB

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