DictUtils.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * @param key 参数键
  19. * @param dictDatas 字典数据列表
  20. */
  21. public static void setDictCache(String key, List<SysDictData> dictDatas)
  22. {
  23. SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), dictDatas);
  24. }
  25. /**
  26. * 获取字典缓存
  27. *
  28. * @param key 参数键
  29. * @return dictDatas 字典数据列表
  30. */
  31. public static List<SysDictData> getDictCache(String key)
  32. {
  33. Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
  34. if (StringUtils.isNotNull(cacheObj))
  35. {
  36. List<SysDictData> DictDatas = StringUtils.cast(cacheObj);
  37. return DictDatas;
  38. }
  39. return null;
  40. }
  41. /**
  42. * 根据字典类型和字典值获取字典标签
  43. *
  44. * @param dictType 字典类型
  45. * @param dictValue 字典值
  46. * @return 字典标签
  47. */
  48. public static String getDictLabel(String dictType, String dictValue)
  49. {
  50. if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotEmpty(dictValue))
  51. {
  52. List<SysDictData> datas = getDictCache(dictType);
  53. if (StringUtils.isNotEmpty(datas))
  54. {
  55. for (SysDictData dict : datas)
  56. {
  57. if (dictValue.equals(dict.getDictValue()))
  58. {
  59. return dict.getDictLabel();
  60. }
  61. }
  62. }
  63. }
  64. return dictValue;
  65. }
  66. /**
  67. * 根据字典类型和字典标签获取字典值
  68. *
  69. * @param dictType 字典类型
  70. * @param dictLabel 字典标签
  71. * @return 字典值
  72. */
  73. public static String getDictValue(String dictType, String dictLabel)
  74. {
  75. if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotEmpty(dictLabel))
  76. {
  77. List<SysDictData> datas = getDictCache(dictType);
  78. if (StringUtils.isNotEmpty(datas))
  79. {
  80. for (SysDictData dict : datas)
  81. {
  82. if (dictLabel.equals(dict.getDictLabel()))
  83. {
  84. return dict.getDictValue();
  85. }
  86. }
  87. }
  88. }
  89. return dictLabel;
  90. }
  91. /**
  92. * 清空字典缓存
  93. */
  94. public static void clearDictCache()
  95. {
  96. Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(Constants.SYS_DICT_KEY + "*");
  97. SpringUtils.getBean(RedisCache.class).deleteObject(keys);
  98. }
  99. /**
  100. * 设置cache key
  101. *
  102. * @param configKey 参数键
  103. * @return 缓存键key
  104. */
  105. public static String getCacheKey(String configKey)
  106. {
  107. return Constants.SYS_DICT_KEY + configKey;
  108. }
  109. }