update 抽取公共模块 ruoyi-common-dict 实现字典多服务调用

2.X
疯狂的狮子li 3 years ago
parent f35dbc9241
commit bccfe2c0b2

@ -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);
}

@ -11,6 +11,7 @@
<modules> <modules>
<module>ruoyi-common-bom</module> <module>ruoyi-common-bom</module>
<module>ruoyi-common-log</module> <module>ruoyi-common-log</module>
<module>ruoyi-common-dict</module>
<module>ruoyi-common-core</module> <module>ruoyi-common-core</module>
<module>ruoyi-common-redis</module> <module>ruoyi-common-redis</module>
<module>ruoyi-common-swagger</module> <module>ruoyi-common-swagger</module>

@ -57,6 +57,13 @@
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<!-- 字典 -->
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-dict</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 缓存服务 --> <!-- 缓存服务 -->
<dependency> <dependency>
<groupId>com.ruoyi</groupId> <groupId>com.ruoyi</groupId>

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common</artifactId>
<version>0.3.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ruoyi-common-dict</artifactId>
<description>
ruoyi-common-dict 字典
</description>
<dependencies>
<!-- RuoYi Common Security -->
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-redis</artifactId>
</dependency>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-api-system</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

@ -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);
}

@ -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);
}
}

@ -1,68 +1,67 @@
package com.ruoyi.common.security.utils; package com.ruoyi.common.dict.utils;
import com.ruoyi.common.core.constant.Constants; import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.redis.utils.RedisUtils; import com.ruoyi.common.redis.utils.RedisUtils;
import com.ruoyi.system.api.domain.SysDictData; import com.ruoyi.system.api.domain.SysDictData;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
/** /**
* *
* *
* @author ruoyi * @author ruoyi
*/ */
public class DictUtils { public class DictUtils {
/** /**
* *
* *
* @param key * @param key
* @param dictDatas * @param dictDatas
*/ */
public static void setDictCache(String key, List<SysDictData> dictDatas) { public static void setDictCache(String key, List<SysDictData> dictDatas) {
RedisUtils.setCacheObject(getCacheKey(key), dictDatas); RedisUtils.setCacheObject(getCacheKey(key), dictDatas);
} }
/** /**
* *
* *
* @param key * @param key
* @return dictDatas * @return dictDatas
*/ */
public static List<SysDictData> getDictCache(String key) { public static List<SysDictData> getDictCache(String key) {
Object cacheObj = RedisUtils.getCacheObject(getCacheKey(key)); List<SysDictData> dictDatas = RedisUtils.getCacheObject(getCacheKey(key));
if (StringUtils.isNotNull(cacheObj)) { if (StringUtils.isNotNull(dictDatas)) {
List<SysDictData> dictDatas = StringUtils.cast(cacheObj); return dictDatas;
return dictDatas; }
} return null;
return null; }
}
/**
/** *
* *
* * @param key
* @param key */
*/ public static void removeDictCache(String key) {
public static void removeDictCache(String key) { RedisUtils.deleteObject(getCacheKey(key));
RedisUtils.deleteObject(getCacheKey(key)); }
}
/**
/** *
* */
*/ public static void clearDictCache() {
public static void clearDictCache() { Collection<String> keys = RedisUtils.keys(Constants.SYS_DICT_KEY + "*");
Collection<String> keys = RedisUtils.keys(Constants.SYS_DICT_KEY + "*"); RedisUtils.deleteObject(keys);
RedisUtils.deleteObject(keys); }
}
/**
/** * cache key
* cache key *
* * @param configKey
* @param configKey * @return key
* @return key */
*/ public static String getCacheKey(String configKey) {
public static String getCacheKey(String configKey) { return Constants.SYS_DICT_KEY + configKey;
return Constants.SYS_DICT_KEY + configKey; }
} }
}

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ruoyi.common.dict.service.impl.DictServiceImpl

@ -71,6 +71,11 @@
<groupId>com.ruoyi</groupId> <groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-log</artifactId> <artifactId>ruoyi-common-log</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-dict</artifactId>
</dependency>
<!-- RuoYi Common Swagger --> <!-- RuoYi Common Swagger -->
<dependency> <dependency>

@ -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<SysDictData> 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<SysDictData> 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);
}
}

@ -27,6 +27,14 @@ public interface ISysDictDataService {
*/ */
String selectDictLabel(String dictType, String dictValue); String selectDictLabel(String dictType, String dictValue);
/**
*
*
* @param dictType
* @return
*/
List<SysDictData> selectDictDataByType(String dictType);
/** /**
* ID * ID
* *

@ -1,6 +1,8 @@
package com.ruoyi.system.service.impl; 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.api.domain.SysDictData;
import com.ruoyi.system.mapper.SysDictDataMapper; import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.ISysDictDataService; import com.ruoyi.system.service.ISysDictDataService;
@ -44,6 +46,26 @@ public class SysDictDataServiceImpl implements ISysDictDataService {
return dictDataMapper.selectDictLabel(dictType, dictValue); return dictDataMapper.selectDictLabel(dictType, dictValue);
} }
/**
*
*
* @param dictType
* @return
*/
@Override
public List<SysDictData> selectDictDataByType(String dictType) {
List<SysDictData> 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 * ID
* *

@ -3,7 +3,7 @@ package com.ruoyi.system.service.impl;
import com.ruoyi.common.core.constant.UserConstants; import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils; 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.SysDictData;
import com.ruoyi.system.api.domain.SysDictType; import com.ruoyi.system.api.domain.SysDictType;
import com.ruoyi.system.mapper.SysDictDataMapper; import com.ruoyi.system.mapper.SysDictDataMapper;

Loading…
Cancel
Save