From bccfe2c0b20b221fe53cc2e230a73d1824fbf5c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Tue, 11 Jan 2022 11:49:05 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=8A=BD=E5=8F=96=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=20ruoyi-common-dict=20=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=AD=97=E5=85=B8=E5=A4=9A=E6=9C=8D=E5=8A=A1=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/system/api/RemoteDictService.java | 29 ++++ ruoyi-common/pom.xml | 1 + ruoyi-common/ruoyi-common-bom/pom.xml | 7 + ruoyi-common/ruoyi-common-dict/pom.xml | 38 +++++ .../common/dict/service/DictService.java | 57 ++++++++ .../dict/service/impl/DictServiceImpl.java | 45 ++++++ .../ruoyi/common/dict}/utils/DictUtils.java | 135 +++++++++--------- .../main/resources/META-INF/spring.factories | 2 + ruoyi-modules/ruoyi-system/pom.xml | 5 + .../system/dubbo/RemoteDictServiceImpl.java | 75 ++++++++++ .../system/service/ISysDictDataService.java | 8 ++ .../service/impl/SysDictDataServiceImpl.java | 24 +++- .../service/impl/SysDictTypeServiceImpl.java | 2 +- 13 files changed, 358 insertions(+), 70 deletions(-) create mode 100644 ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/RemoteDictService.java create mode 100644 ruoyi-common/ruoyi-common-dict/pom.xml create mode 100644 ruoyi-common/ruoyi-common-dict/src/main/java/com/ruoyi/common/dict/service/DictService.java create mode 100644 ruoyi-common/ruoyi-common-dict/src/main/java/com/ruoyi/common/dict/service/impl/DictServiceImpl.java rename ruoyi-common/{ruoyi-common-security/src/main/java/com/ruoyi/common/security => ruoyi-common-dict/src/main/java/com/ruoyi/common/dict}/utils/DictUtils.java (83%) create mode 100644 ruoyi-common/ruoyi-common-dict/src/main/resources/META-INF/spring.factories create mode 100644 ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/dubbo/RemoteDictServiceImpl.java diff --git a/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/RemoteDictService.java b/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/RemoteDictService.java new file mode 100644 index 00000000..d01ec261 --- /dev/null +++ b/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/RemoteDictService.java @@ -0,0 +1,29 @@ +package com.ruoyi.system.api; + +/** + * 字典服务 + * + * @author Lion Li + */ +public interface RemoteDictService { + + /** + * 根据字典类型和字典值获取字典标签 + * + * @param dictType 字典类型 + * @param dictValue 字典值 + * @param separator 分隔符 + * @return 字典标签 + */ + String getDictLabel(String dictType, String dictValue, String separator); + + /** + * 根据字典类型和字典标签获取字典值 + * + * @param dictType 字典类型 + * @param dictLabel 字典标签 + * @param separator 分隔符 + * @return 字典值 + */ + String getDictValue(String dictType, String dictLabel, String separator); +} diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 8bd357f5..ad292fd9 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -11,6 +11,7 @@ ruoyi-common-bom ruoyi-common-log + ruoyi-common-dict ruoyi-common-core ruoyi-common-redis ruoyi-common-swagger diff --git a/ruoyi-common/ruoyi-common-bom/pom.xml b/ruoyi-common/ruoyi-common-bom/pom.xml index 1fa2fb82..53836a77 100644 --- a/ruoyi-common/ruoyi-common-bom/pom.xml +++ b/ruoyi-common/ruoyi-common-bom/pom.xml @@ -57,6 +57,13 @@ ${project.version} + + + com.ruoyi + ruoyi-common-dict + ${project.version} + + com.ruoyi diff --git a/ruoyi-common/ruoyi-common-dict/pom.xml b/ruoyi-common/ruoyi-common-dict/pom.xml new file mode 100644 index 00000000..f6f3ce47 --- /dev/null +++ b/ruoyi-common/ruoyi-common-dict/pom.xml @@ -0,0 +1,38 @@ + + + + com.ruoyi + ruoyi-common + 0.3.0 + + 4.0.0 + + ruoyi-common-dict + + + ruoyi-common-dict 字典 + + + + + + + com.ruoyi + ruoyi-common-redis + + + + com.ruoyi + ruoyi-api-system + + + + com.alibaba.cloud + spring-cloud-starter-dubbo + provided + + + + \ No newline at end of file diff --git a/ruoyi-common/ruoyi-common-dict/src/main/java/com/ruoyi/common/dict/service/DictService.java b/ruoyi-common/ruoyi-common-dict/src/main/java/com/ruoyi/common/dict/service/DictService.java new file mode 100644 index 00000000..ddcc67eb --- /dev/null +++ b/ruoyi-common/ruoyi-common-dict/src/main/java/com/ruoyi/common/dict/service/DictService.java @@ -0,0 +1,57 @@ +package com.ruoyi.common.dict.service; + +/** + * 字典服务服务 + * + * @author Lion Li + */ +public interface DictService { + + /** + * 分隔符 + */ + String SEPARATOR = ","; + + /** + * 根据字典类型和字典值获取字典标签 + * + * @param dictType 字典类型 + * @param dictValue 字典值 + * @return 字典标签 + */ + default String getDictLabel(String dictType, String dictValue) { + return getDictLabel(dictType, dictValue, SEPARATOR); + } + + /** + * 根据字典类型和字典标签获取字典值 + * + * @param dictType 字典类型 + * @param dictLabel 字典标签 + * @return 字典值 + */ + default String getDictValue(String dictType, String dictLabel) { + return getDictValue(dictType, dictLabel, SEPARATOR); + } + + /** + * 根据字典类型和字典值获取字典标签 + * + * @param dictType 字典类型 + * @param dictValue 字典值 + * @param separator 分隔符 + * @return 字典标签 + */ + String getDictLabel(String dictType, String dictValue, String separator); + + /** + * 根据字典类型和字典标签获取字典值 + * + * @param dictType 字典类型 + * @param dictLabel 字典标签 + * @param separator 分隔符 + * @return 字典值 + */ + String getDictValue(String dictType, String dictLabel, String separator); + +} diff --git a/ruoyi-common/ruoyi-common-dict/src/main/java/com/ruoyi/common/dict/service/impl/DictServiceImpl.java b/ruoyi-common/ruoyi-common-dict/src/main/java/com/ruoyi/common/dict/service/impl/DictServiceImpl.java new file mode 100644 index 00000000..689a2bf3 --- /dev/null +++ b/ruoyi-common/ruoyi-common-dict/src/main/java/com/ruoyi/common/dict/service/impl/DictServiceImpl.java @@ -0,0 +1,45 @@ +package com.ruoyi.common.dict.service.impl; + +import com.ruoyi.common.dict.service.DictService; +import com.ruoyi.system.api.RemoteDictService; +import org.apache.dubbo.config.annotation.DubboReference; +import org.springframework.stereotype.Service; + +/** + * 字典服务服务 + * + * @author Lion Li + */ +@Service +public class DictServiceImpl implements DictService { + + @DubboReference + private RemoteDictService remoteDictService; + + /** + * 根据字典类型和字典值获取字典标签 + * + * @param dictType 字典类型 + * @param dictValue 字典值 + * @param separator 分隔符 + * @return 字典标签 + */ + @Override + public String getDictLabel(String dictType, String dictValue, String separator) { + return remoteDictService.getDictLabel(dictType, dictValue, separator); + } + + /** + * 根据字典类型和字典标签获取字典值 + * + * @param dictType 字典类型 + * @param dictLabel 字典标签 + * @param separator 分隔符 + * @return 字典值 + */ + @Override + public String getDictValue(String dictType, String dictLabel, String separator) { + return remoteDictService.getDictValue(dictType, dictLabel, separator); + } + +} diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/utils/DictUtils.java b/ruoyi-common/ruoyi-common-dict/src/main/java/com/ruoyi/common/dict/utils/DictUtils.java similarity index 83% rename from ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/utils/DictUtils.java rename to ruoyi-common/ruoyi-common-dict/src/main/java/com/ruoyi/common/dict/utils/DictUtils.java index 00bbc2c2..5bf68bd7 100644 --- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/utils/DictUtils.java +++ b/ruoyi-common/ruoyi-common-dict/src/main/java/com/ruoyi/common/dict/utils/DictUtils.java @@ -1,68 +1,67 @@ -package com.ruoyi.common.security.utils; - -import com.ruoyi.common.core.constant.Constants; -import com.ruoyi.common.core.utils.StringUtils; -import com.ruoyi.common.redis.utils.RedisUtils; -import com.ruoyi.system.api.domain.SysDictData; - -import java.util.Collection; -import java.util.List; - -/** - * 字典工具类 - * - * @author ruoyi - */ -public class DictUtils { - /** - * 设置字典缓存 - * - * @param key 参数键 - * @param dictDatas 字典数据列表 - */ - public static void setDictCache(String key, List dictDatas) { - RedisUtils.setCacheObject(getCacheKey(key), dictDatas); - } - - /** - * 获取字典缓存 - * - * @param key 参数键 - * @return dictDatas 字典数据列表 - */ - public static List getDictCache(String key) { - Object cacheObj = RedisUtils.getCacheObject(getCacheKey(key)); - if (StringUtils.isNotNull(cacheObj)) { - List dictDatas = StringUtils.cast(cacheObj); - return dictDatas; - } - return null; - } - - /** - * 删除指定字典缓存 - * - * @param key 字典键 - */ - public static void removeDictCache(String key) { - RedisUtils.deleteObject(getCacheKey(key)); - } - - /** - * 清空字典缓存 - */ - public static void clearDictCache() { - Collection keys = RedisUtils.keys(Constants.SYS_DICT_KEY + "*"); - RedisUtils.deleteObject(keys); - } - - /** - * 设置cache key - * - * @param configKey 参数键 - * @return 缓存键key - */ - public static String getCacheKey(String configKey) { - return Constants.SYS_DICT_KEY + configKey; - } -} +package com.ruoyi.common.dict.utils; + +import com.ruoyi.common.core.constant.Constants; +import com.ruoyi.common.core.utils.StringUtils; +import com.ruoyi.common.redis.utils.RedisUtils; +import com.ruoyi.system.api.domain.SysDictData; + +import java.util.Collection; +import java.util.List; + +/** + * 字典工具类 + * + * @author ruoyi + */ +public class DictUtils { + /** + * 设置字典缓存 + * + * @param key 参数键 + * @param dictDatas 字典数据列表 + */ + public static void setDictCache(String key, List dictDatas) { + RedisUtils.setCacheObject(getCacheKey(key), dictDatas); + } + + /** + * 获取字典缓存 + * + * @param key 参数键 + * @return dictDatas 字典数据列表 + */ + public static List getDictCache(String key) { + List dictDatas = RedisUtils.getCacheObject(getCacheKey(key)); + if (StringUtils.isNotNull(dictDatas)) { + return dictDatas; + } + return null; + } + + /** + * 删除指定字典缓存 + * + * @param key 字典键 + */ + public static void removeDictCache(String key) { + RedisUtils.deleteObject(getCacheKey(key)); + } + + /** + * 清空字典缓存 + */ + public static void clearDictCache() { + Collection keys = RedisUtils.keys(Constants.SYS_DICT_KEY + "*"); + RedisUtils.deleteObject(keys); + } + + /** + * 设置cache key + * + * @param configKey 参数键 + * @return 缓存键key + */ + public static String getCacheKey(String configKey) { + return Constants.SYS_DICT_KEY + configKey; + } +} diff --git a/ruoyi-common/ruoyi-common-dict/src/main/resources/META-INF/spring.factories b/ruoyi-common/ruoyi-common-dict/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..3d9fd7d4 --- /dev/null +++ b/ruoyi-common/ruoyi-common-dict/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.ruoyi.common.dict.service.impl.DictServiceImpl \ No newline at end of file diff --git a/ruoyi-modules/ruoyi-system/pom.xml b/ruoyi-modules/ruoyi-system/pom.xml index def4af68..0b00a0b4 100644 --- a/ruoyi-modules/ruoyi-system/pom.xml +++ b/ruoyi-modules/ruoyi-system/pom.xml @@ -71,6 +71,11 @@ com.ruoyi ruoyi-common-log + + + com.ruoyi + ruoyi-common-dict + diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/dubbo/RemoteDictServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/dubbo/RemoteDictServiceImpl.java new file mode 100644 index 00000000..683fd699 --- /dev/null +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/dubbo/RemoteDictServiceImpl.java @@ -0,0 +1,75 @@ +package com.ruoyi.system.dubbo; + +import cn.hutool.core.collection.CollUtil; +import com.ruoyi.common.core.utils.StringUtils; +import com.ruoyi.system.api.RemoteDictService; +import com.ruoyi.system.api.domain.SysDictData; +import com.ruoyi.system.service.ISysDictDataService; +import lombok.RequiredArgsConstructor; +import org.apache.dubbo.config.annotation.DubboService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * 操作日志记录 + * + * @author Lion Li + */ +@RequiredArgsConstructor(onConstructor_ = @Autowired) +@Service +@DubboService +public class RemoteDictServiceImpl implements RemoteDictService { + + private final ISysDictDataService sysDictDataService; + + + @Override + public String getDictLabel(String dictType, String dictValue, String separator) { + StringBuilder propertyString = new StringBuilder(); + List datas = sysDictDataService.selectDictDataByType(dictType); + + if (StringUtils.containsAny(dictValue, separator) && CollUtil.isNotEmpty(datas)) { + for (SysDictData dict : datas) { + for (String value : dictValue.split(separator)) { + if (value.equals(dict.getDictValue())) { + propertyString.append(dict.getDictLabel() + separator); + break; + } + } + } + } else { + for (SysDictData dict : datas) { + if (dictValue.equals(dict.getDictValue())) { + return dict.getDictLabel(); + } + } + } + return StringUtils.stripEnd(propertyString.toString(), separator); + } + + @Override + public String getDictValue(String dictType, String dictLabel, String separator) { + StringBuilder propertyString = new StringBuilder(); + List datas = sysDictDataService.selectDictDataByType(dictType); + + if (StringUtils.containsAny(dictLabel, separator) && CollUtil.isNotEmpty(datas)) { + for (SysDictData dict : datas) { + for (String label : dictLabel.split(separator)) { + if (label.equals(dict.getDictLabel())) { + propertyString.append(dict.getDictValue() + separator); + break; + } + } + } + } else { + for (SysDictData dict : datas) { + if (dictLabel.equals(dict.getDictLabel())) { + return dict.getDictValue(); + } + } + } + return StringUtils.stripEnd(propertyString.toString(), separator); + } +} diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictDataService.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictDataService.java index 6bffbfc9..42bf6399 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictDataService.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictDataService.java @@ -27,6 +27,14 @@ public interface ISysDictDataService { */ String selectDictLabel(String dictType, String dictValue); + /** + * 根据字典类型查询字典数据 + * + * @param dictType 字典类型 + * @return 字典数据集合信息 + */ + List selectDictDataByType(String dictType); + /** * 根据字典数据ID查询信息 * diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictDataServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictDataServiceImpl.java index bb8946ab..476db364 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictDataServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictDataServiceImpl.java @@ -1,6 +1,8 @@ package com.ruoyi.system.service.impl; -import com.ruoyi.common.security.utils.DictUtils; +import cn.hutool.core.collection.CollUtil; +import com.ruoyi.common.core.utils.StringUtils; +import com.ruoyi.common.dict.utils.DictUtils; import com.ruoyi.system.api.domain.SysDictData; import com.ruoyi.system.mapper.SysDictDataMapper; import com.ruoyi.system.service.ISysDictDataService; @@ -44,6 +46,26 @@ public class SysDictDataServiceImpl implements ISysDictDataService { return dictDataMapper.selectDictLabel(dictType, dictValue); } + /** + * 根据字典类型查询字典数据 + * + * @param dictType 字典类型 + * @return 字典数据集合信息 + */ + @Override + public List selectDictDataByType(String dictType) { + List dictDatas = DictUtils.getDictCache(dictType); + if (StringUtils.isNotEmpty(dictDatas)) { + return dictDatas; + } + dictDatas = dictDataMapper.selectDictDataByType(dictType); + if (CollUtil.isNotEmpty(dictDatas)) { + DictUtils.setDictCache(dictType, dictDatas); + return dictDatas; + } + return null; + } + /** * 根据字典数据ID查询信息 * diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java index 7eb31180..f4e22f85 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java @@ -3,7 +3,7 @@ package com.ruoyi.system.service.impl; import com.ruoyi.common.core.constant.UserConstants; import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.utils.StringUtils; -import com.ruoyi.common.security.utils.DictUtils; +import com.ruoyi.common.dict.utils.DictUtils; import com.ruoyi.system.api.domain.SysDictData; import com.ruoyi.system.api.domain.SysDictType; import com.ruoyi.system.mapper.SysDictDataMapper;