DictUtils.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. public static final String SEPARATOR = ",";
  17. /**
  18. * 设置字典缓存
  19. *
  20. * @param key 参数键
  21. * @param dictDatas 字典数据列表
  22. */
  23. public static void setDictCache(String key, List<SysDictData> dictDatas) {
  24. RedisUtils.setCacheObject(getCacheKey(key), dictDatas);
  25. }
  26. /**
  27. * 获取字典缓存
  28. *
  29. * @param key 参数键
  30. * @return dictDatas 字典数据列表
  31. */
  32. public static List<SysDictData> getDictCache(String key) {
  33. List<SysDictData> dictDatas = RedisUtils.getCacheObject(getCacheKey(key));
  34. if (StringUtils.isNotNull(dictDatas)) {
  35. return dictDatas;
  36. }
  37. return null;
  38. }
  39. /**
  40. * 根据字典类型和字典值获取字典标签
  41. *
  42. * @param dictType 字典类型
  43. * @param dictValue 字典值
  44. * @return 字典标签
  45. */
  46. public static String getDictLabel(String dictType, String dictValue) {
  47. return getDictLabel(dictType, dictValue, SEPARATOR);
  48. }
  49. /**
  50. * 根据字典类型和字典标签获取字典值
  51. *
  52. * @param dictType 字典类型
  53. * @param dictLabel 字典标签
  54. * @return 字典值
  55. */
  56. public static String getDictValue(String dictType, String dictLabel) {
  57. return getDictValue(dictType, dictLabel, SEPARATOR);
  58. }
  59. /**
  60. * 根据字典类型和字典值获取字典标签
  61. *
  62. * @param dictType 字典类型
  63. * @param dictValue 字典值
  64. * @param separator 分隔符
  65. * @return 字典标签
  66. */
  67. public static String getDictLabel(String dictType, String dictValue, String separator) {
  68. StringBuilder propertyString = new StringBuilder();
  69. List<SysDictData> datas = getDictCache(dictType);
  70. if (StringUtils.containsAny(dictValue, separator) && CollUtil.isNotEmpty(datas)) {
  71. for (SysDictData dict : datas) {
  72. for (String value : dictValue.split(separator)) {
  73. if (value.equals(dict.getDictValue())) {
  74. propertyString.append(dict.getDictLabel() + separator);
  75. break;
  76. }
  77. }
  78. }
  79. } else {
  80. for (SysDictData dict : datas) {
  81. if (dictValue.equals(dict.getDictValue())) {
  82. return dict.getDictLabel();
  83. }
  84. }
  85. }
  86. return StringUtils.stripEnd(propertyString.toString(), separator);
  87. }
  88. /**
  89. * 根据字典类型和字典标签获取字典值
  90. *
  91. * @param dictType 字典类型
  92. * @param dictLabel 字典标签
  93. * @param separator 分隔符
  94. * @return 字典值
  95. */
  96. public static String getDictValue(String dictType, String dictLabel, String separator) {
  97. StringBuilder propertyString = new StringBuilder();
  98. List<SysDictData> datas = getDictCache(dictType);
  99. if (StringUtils.containsAny(dictLabel, separator) && CollUtil.isNotEmpty(datas)) {
  100. for (SysDictData dict : datas) {
  101. for (String label : dictLabel.split(separator)) {
  102. if (label.equals(dict.getDictLabel())) {
  103. propertyString.append(dict.getDictValue() + separator);
  104. break;
  105. }
  106. }
  107. }
  108. } else {
  109. for (SysDictData dict : datas) {
  110. if (dictLabel.equals(dict.getDictLabel())) {
  111. return dict.getDictValue();
  112. }
  113. }
  114. }
  115. return StringUtils.stripEnd(propertyString.toString(), separator);
  116. }
  117. /**
  118. * 删除指定字典缓存
  119. *
  120. * @param key 字典键
  121. */
  122. public static void removeDictCache(String key) {
  123. RedisUtils.deleteObject(getCacheKey(key));
  124. }
  125. /**
  126. * 清空字典缓存
  127. */
  128. public static void clearDictCache() {
  129. Collection<String> keys = RedisUtils.keys(Constants.SYS_DICT_KEY + "*");
  130. RedisUtils.deleteObject(keys);
  131. }
  132. /**
  133. * 设置cache key
  134. *
  135. * @param configKey 参数键
  136. * @return 缓存键key
  137. */
  138. public static String getCacheKey(String configKey) {
  139. return Constants.SYS_DICT_KEY + configKey;
  140. }
  141. }