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 index d01ec261..1cab6d01 100644 --- 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 @@ -1,5 +1,9 @@ package com.ruoyi.system.api; +import com.ruoyi.system.api.domain.SysDictData; + +import java.util.List; + /** * 字典服务 * @@ -8,22 +12,10 @@ package com.ruoyi.system.api; 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 字典值 + * @param dictType 字典类型 + * @return 字典数据集合信息 */ - String getDictValue(String dictType, String dictLabel, String separator); + List selectDictDataByType(String dictType); } diff --git a/ruoyi-common/ruoyi-common-dict/pom.xml b/ruoyi-common/ruoyi-common-dict/pom.xml index 685f6c6f..0c70bff3 100644 --- a/ruoyi-common/ruoyi-common-dict/pom.xml +++ b/ruoyi-common/ruoyi-common-dict/pom.xml @@ -33,6 +33,10 @@ dubbo-spring-boot-starter provided + + cn.dev33 + sa-token-core + 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 index d0fe1803..a29af23a 100644 --- 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 @@ -1,10 +1,21 @@ package com.ruoyi.common.dict.service.impl; +import cn.dev33.satoken.context.SaHolder; +import cn.hutool.core.collection.CollStreamUtil; +import cn.hutool.core.util.ObjectUtil; +import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.service.DictService; +import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.system.api.RemoteDictService; +import com.ruoyi.system.api.domain.SysDictData; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.stereotype.Service; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + /** * 字典服务服务 * @@ -24,9 +35,24 @@ public class DictServiceImpl implements DictService { * @param separator 分隔符 * @return 字典标签 */ + @SuppressWarnings("unchecked cast") @Override public String getDictLabel(String dictType, String dictValue, String separator) { - return remoteDictService.getDictLabel(dictType, dictValue, separator); + // 优先从本地缓存获取 + List datas = (List) SaHolder.getStorage().get(CacheConstants.SYS_DICT_KEY + dictType); + if (ObjectUtil.isNull(datas)) { + datas = remoteDictService.selectDictDataByType(dictType); + SaHolder.getStorage().set(CacheConstants.SYS_DICT_KEY + dictType, datas); + } + + Map map = CollStreamUtil.toMap(datas, SysDictData::getDictValue, SysDictData::getDictLabel); + if (StringUtils.containsAny(dictValue, separator)) { + return Arrays.stream(dictValue.split(separator)) + .map(v -> map.getOrDefault(v, StringUtils.EMPTY)) + .collect(Collectors.joining(separator)); + } else { + return map.getOrDefault(dictValue, StringUtils.EMPTY); + } } /** @@ -37,9 +63,24 @@ public class DictServiceImpl implements DictService { * @param separator 分隔符 * @return 字典值 */ + @SuppressWarnings("unchecked cast") @Override public String getDictValue(String dictType, String dictLabel, String separator) { - return remoteDictService.getDictValue(dictType, dictLabel, separator); + // 优先从本地缓存获取 + List datas = (List) SaHolder.getStorage().get(CacheConstants.SYS_DICT_KEY + dictType); + if (ObjectUtil.isNull(datas)) { + datas = remoteDictService.selectDictDataByType(dictType); + SaHolder.getStorage().set(CacheConstants.SYS_DICT_KEY + dictType, datas); + } + + Map map = CollStreamUtil.toMap(datas, SysDictData::getDictLabel, SysDictData::getDictValue); + if (StringUtils.containsAny(dictLabel, separator)) { + return Arrays.stream(dictLabel.split(separator)) + .map(l -> map.getOrDefault(l, StringUtils.EMPTY)) + .collect(Collectors.joining(separator)); + } else { + return map.getOrDefault(dictLabel, StringUtils.EMPTY); + } } } 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 index 82e8c513..64e38399 100644 --- 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 @@ -1,8 +1,5 @@ package com.ruoyi.system.dubbo; -import cn.hutool.core.collection.CollUtil; -import com.ruoyi.common.core.utils.SpringUtils; -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.ISysDictTypeService; @@ -26,50 +23,8 @@ public class RemoteDictServiceImpl implements RemoteDictService { @Override - public String getDictLabel(String dictType, String dictValue, String separator) { - StringBuilder propertyString = new StringBuilder(); - List datas = sysDictTypeService.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); + public List selectDictDataByType(String dictType) { + return sysDictTypeService.selectDictDataByType(dictType); } - @Override - public String getDictValue(String dictType, String dictLabel, String separator) { - StringBuilder propertyString = new StringBuilder(); - List datas = sysDictTypeService.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); - } }