From b0af4a1c049baecf3f7e4991b49ca26bb7ec56b6 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Thu, 4 Jun 2020 17:36:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=97=E5=85=B8=E7=AE=A1=E7=90=86=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=BC=93=E5=AD=98=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/core/constant/Constants.java | 5 ++ .../system/controller/SysDictDataController.java | 8 +- .../system/controller/SysDictTypeController.java | 12 +++ .../ruoyi/system/service/ISysDictDataService.java | 17 ----- .../ruoyi/system/service/ISysDictTypeService.java | 23 +++--- .../service/impl/SysDictDataServiceImpl.java | 48 +++++------- .../service/impl/SysDictTypeServiceImpl.java | 88 ++++++++++++++++++---- .../java/com/ruoyi/system/utils/DictUtils.java | 65 ++++++++++++++++ ruoyi-ui/src/api/system/dict/data.js | 2 +- ruoyi-ui/src/api/system/dict/type.js | 8 ++ ruoyi-ui/src/views/system/config/index.vue | 1 - ruoyi-ui/src/views/system/dict/index.vue | 21 +++++- 12 files changed, 223 insertions(+), 75 deletions(-) create mode 100644 ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/utils/DictUtils.java diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/Constants.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/Constants.java index 1d58d1d..8ae378a 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/Constants.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/Constants.java @@ -87,6 +87,11 @@ public class Constants public static final String SYS_CONFIG_KEY = "sys_config:"; /** + * 字典管理 cache key + */ + public static final String SYS_DICT_KEY = "sys_dict:"; + + /** * 资源映射路径 前缀 */ public static final String RESOURCE_PREFIX = "/profile"; diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictDataController.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictDataController.java index 4550dbf..5a908f6 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictDataController.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictDataController.java @@ -23,6 +23,7 @@ import com.ruoyi.common.log.enums.BusinessType; import com.ruoyi.common.security.utils.SecurityUtils; import com.ruoyi.system.domain.SysDictData; import com.ruoyi.system.service.ISysDictDataService; +import com.ruoyi.system.service.ISysDictTypeService; /** * 数据字典信息 @@ -35,6 +36,9 @@ public class SysDictDataController extends BaseController { @Autowired private ISysDictDataService dictDataService; + + @Autowired + private ISysDictTypeService dictTypeService; @PreAuthorize("@ss.hasPermi('system:dict:list')") @GetMapping("/list") @@ -68,10 +72,10 @@ public class SysDictDataController extends BaseController /** * 根据字典类型查询字典数据信息 */ - @GetMapping(value = "/dictType/{dictType}") + @GetMapping(value = "/type/{dictType}") public AjaxResult dictType(@PathVariable String dictType) { - return AjaxResult.success(dictDataService.selectDictDataByType(dictType)); + return AjaxResult.success(dictTypeService.selectDictDataByType(dictType)); } /** diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictTypeController.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictTypeController.java index 3ee1773..41a7eed 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictTypeController.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysDictTypeController.java @@ -110,6 +110,18 @@ public class SysDictTypeController extends BaseController } /** + * 清空缓存 + */ + @PreAuthorize("@ss.hasPermi('system:dict:remove')") + @Log(title = "字典类型", businessType = BusinessType.CLEAN) + @DeleteMapping("/clearCache") + public AjaxResult clearCache() + { + dictTypeService.clearCache(); + return AjaxResult.success(); + } + + /** * 获取字典选择框列表 */ @GetMapping("/optionselect") 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 617fab5..21fa861 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 @@ -1,7 +1,6 @@ package com.ruoyi.system.service; import java.util.List; - import com.ruoyi.system.domain.SysDictData; /** @@ -20,14 +19,6 @@ public interface ISysDictDataService public List selectDictDataList(SysDictData dictData); /** - * 根据字典类型查询字典数据 - * - * @param dictType 字典类型 - * @return 字典数据集合信息 - */ - public List selectDictDataByType(String dictType); - - /** * 根据字典类型和字典键值查询字典数据信息 * * @param dictType 字典类型 @@ -45,14 +36,6 @@ public interface ISysDictDataService public SysDictData selectDictDataById(Long dictCode); /** - * 通过字典ID删除字典数据信息 - * - * @param dictCode 字典数据ID - * @return 结果 - */ - public int deleteDictDataById(Long dictCode); - - /** * 批量删除字典数据信息 * * @param dictCodes 需要删除的字典数据ID diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictTypeService.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictTypeService.java index 90a78b3..1ac88ce 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictTypeService.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDictTypeService.java @@ -1,7 +1,7 @@ package com.ruoyi.system.service; import java.util.List; - +import com.ruoyi.system.domain.SysDictData; import com.ruoyi.system.domain.SysDictType; /** @@ -27,6 +27,14 @@ public interface ISysDictTypeService public List selectDictTypeAll(); /** + * 根据字典类型查询字典数据 + * + * @param dictType 字典类型 + * @return 字典数据集合信息 + */ + public List selectDictDataByType(String dictType); + + /** * 根据字典类型ID查询信息 * * @param dictId 字典类型ID @@ -43,14 +51,6 @@ public interface ISysDictTypeService public SysDictType selectDictTypeByType(String dictType); /** - * 通过字典ID删除字典信息 - * - * @param dictId 字典ID - * @return 结果 - */ - public int deleteDictTypeById(Long dictId); - - /** * 批量删除字典信息 * * @param dictIds 需要删除的字典ID @@ -59,6 +59,11 @@ public interface ISysDictTypeService public int deleteDictTypeByIds(Long[] dictIds); /** + * 清空缓存数据 + */ + public void clearCache(); + + /** * 新增保存字典类型信息 * * @param dictType 字典类型信息 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 069a638..204bc97 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,13 +1,12 @@ package com.ruoyi.system.service.impl; import java.util.List; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; - import com.ruoyi.system.domain.SysDictData; import com.ruoyi.system.mapper.SysDictDataMapper; import com.ruoyi.system.service.ISysDictDataService; +import com.ruoyi.system.utils.DictUtils; /** * 字典 业务层处理 @@ -33,18 +32,6 @@ public class SysDictDataServiceImpl implements ISysDictDataService } /** - * 根据字典类型查询字典数据 - * - * @param dictType 字典类型 - * @return 字典数据集合信息 - */ - @Override - public List selectDictDataByType(String dictType) - { - return dictDataMapper.selectDictDataByType(dictType); - } - - /** * 根据字典类型和字典键值查询字典数据信息 * * @param dictType 字典类型 @@ -70,18 +57,6 @@ public class SysDictDataServiceImpl implements ISysDictDataService } /** - * 通过字典ID删除字典数据信息 - * - * @param dictCode 字典数据ID - * @return 结果 - */ - @Override - public int deleteDictDataById(Long dictCode) - { - return dictDataMapper.deleteDictDataById(dictCode); - } - - /** * 批量删除字典数据信息 * * @param dictCodes 需要删除的字典数据ID @@ -89,7 +64,12 @@ public class SysDictDataServiceImpl implements ISysDictDataService */ public int deleteDictDataByIds(Long[] dictCodes) { - return dictDataMapper.deleteDictDataByIds(dictCodes); + int row = dictDataMapper.deleteDictDataByIds(dictCodes); + if (row > 0) + { + DictUtils.clearDictCache(); + } + return row; } /** @@ -101,7 +81,12 @@ public class SysDictDataServiceImpl implements ISysDictDataService @Override public int insertDictData(SysDictData dictData) { - return dictDataMapper.insertDictData(dictData); + int row = dictDataMapper.insertDictData(dictData); + if (row > 0) + { + DictUtils.clearDictCache(); + } + return row; } /** @@ -113,6 +98,11 @@ public class SysDictDataServiceImpl implements ISysDictDataService @Override public int updateDictData(SysDictData dictData) { - return dictDataMapper.updateDictData(dictData); + int row = dictDataMapper.updateDictData(dictData); + if (row > 0) + { + DictUtils.clearDictCache(); + } + return row; } } 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 7684566..4f939d3 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 @@ -1,17 +1,19 @@ package com.ruoyi.system.service.impl; import java.util.List; - +import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; - import com.ruoyi.common.core.constant.UserConstants; +import com.ruoyi.common.core.exception.CustomException; import com.ruoyi.common.core.utils.StringUtils; +import com.ruoyi.system.domain.SysDictData; import com.ruoyi.system.domain.SysDictType; import com.ruoyi.system.mapper.SysDictDataMapper; import com.ruoyi.system.mapper.SysDictTypeMapper; import com.ruoyi.system.service.ISysDictTypeService; +import com.ruoyi.system.utils.DictUtils; /** * 字典 业务层处理 @@ -28,6 +30,20 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService private SysDictDataMapper dictDataMapper; /** + * 项目启动时,初始化字典到缓存 + */ + @PostConstruct + public void init() + { + List dictTypeList = dictTypeMapper.selectDictTypeAll(); + for (SysDictType dictType : dictTypeList) + { + List dictDatas = dictDataMapper.selectDictDataByType(dictType.getDictType()); + DictUtils.setDictCache(dictType.getDictType(), dictDatas); + } + } + + /** * 根据条件分页查询字典类型 * * @param dictType 字典类型信息 @@ -51,6 +67,29 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService } /** + * 根据字典类型查询字典数据 + * + * @param dictType 字典类型 + * @return 字典数据集合信息 + */ + @Override + public List selectDictDataByType(String dictType) + { + List dictDatas = DictUtils.getDictCache(dictType); + if (StringUtils.isNotNull(dictDatas)) + { + return dictDatas; + } + dictDatas = dictDataMapper.selectDictDataByType(dictType); + if (StringUtils.isNotNull(dictDatas)) + { + DictUtils.setDictCache(dictType, dictDatas); + return dictDatas; + } + return null; + } + + /** * 根据字典类型ID查询信息 * * @param dictId 字典类型ID @@ -74,26 +113,35 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService } /** - * 通过字典ID删除字典信息 + * 批量删除字典类型信息 * - * @param dictId 字典ID + * @param dictIds 需要删除的字典ID * @return 结果 */ - @Override - public int deleteDictTypeById(Long dictId) + public int deleteDictTypeByIds(Long[] dictIds) { - return dictTypeMapper.deleteDictTypeById(dictId); + for (Long dictId : dictIds) + { + SysDictType dictType = selectDictTypeById(dictId); + if (dictDataMapper.countDictDataByType(dictType.getDictType()) > 0) + { + throw new CustomException(String.format("%1$s已分配,不能删除", dictType.getDictName())); + } + } + int count = dictTypeMapper.deleteDictTypeByIds(dictIds); + if (count > 0) + { + DictUtils.clearDictCache(); + } + return count; } /** - * 批量删除字典类型信息 - * - * @param dictIds 需要删除的字典ID - * @return 结果 + * 清空缓存数据 */ - public int deleteDictTypeByIds(Long[] dictIds) + public void clearCache() { - return dictTypeMapper.deleteDictTypeByIds(dictIds); + DictUtils.clearDictCache(); } /** @@ -105,7 +153,12 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService @Override public int insertDictType(SysDictType dictType) { - return dictTypeMapper.insertDictType(dictType); + int row = dictTypeMapper.insertDictType(dictType); + if (row > 0) + { + DictUtils.clearDictCache(); + } + return row; } /** @@ -120,7 +173,12 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService { SysDictType oldDict = dictTypeMapper.selectDictTypeById(dictType.getDictId()); dictDataMapper.updateDictDataType(oldDict.getDictType(), dictType.getDictType()); - return dictTypeMapper.updateDictType(dictType); + int row = dictTypeMapper.updateDictType(dictType); + if (row > 0) + { + DictUtils.clearDictCache(); + } + return row; } /** diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/utils/DictUtils.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/utils/DictUtils.java new file mode 100644 index 0000000..4629f30 --- /dev/null +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/utils/DictUtils.java @@ -0,0 +1,65 @@ +package com.ruoyi.system.utils; + +import java.util.Collection; +import java.util.List; +import com.ruoyi.common.core.constant.Constants; +import com.ruoyi.common.core.utils.SpringUtils; +import com.ruoyi.common.core.utils.StringUtils; +import com.ruoyi.common.redis.service.RedisService; +import com.ruoyi.system.domain.SysDictData; + +/** + * 字典工具类 + * + * @author ruoyi + */ +public class DictUtils +{ + /** + * 设置字典缓存 + * + * @param key 参数键 + * @param dictDatas 字典数据列表 + */ + public static void setDictCache(String key, List dictDatas) + { + SpringUtils.getBean(RedisService.class).setCacheObject(getCacheKey(key), dictDatas); + } + + /** + * 获取字典缓存 + * + * @param key 参数键 + * @return dictDatas 字典数据列表 + */ + public static List getDictCache(String key) + { + Object cacheObj = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key)); + if (StringUtils.isNotNull(cacheObj)) + { + List DictDatas = StringUtils.cast(cacheObj); + return DictDatas; + } + return null; + } + + /** + * 清空字典缓存 + */ + public static void clearDictCache() + { + Collection keys = SpringUtils.getBean(RedisService.class).keys(Constants.SYS_DICT_KEY + "*"); + SpringUtils.getBean(RedisService.class).deleteObject(keys); + } + + /** + * 设置cache key + * + * @param configKey 参数键 + * @return 缓存键key + */ + public static String getCacheKey(String configKey) + { + return Constants.SYS_DICT_KEY + configKey; + } +} diff --git a/ruoyi-ui/src/api/system/dict/data.js b/ruoyi-ui/src/api/system/dict/data.js index ca10f2f..2a6e481 100644 --- a/ruoyi-ui/src/api/system/dict/data.js +++ b/ruoyi-ui/src/api/system/dict/data.js @@ -20,7 +20,7 @@ export function getData(dictCode) { // 根据字典类型查询字典数据信息 export function getDicts(dictType) { return request({ - url: '/system/dict/data/dictType/' + dictType, + url: '/system/dict/data/type/' + dictType, method: 'get' }) } diff --git a/ruoyi-ui/src/api/system/dict/type.js b/ruoyi-ui/src/api/system/dict/type.js index aeb4a1d..83b66f4 100644 --- a/ruoyi-ui/src/api/system/dict/type.js +++ b/ruoyi-ui/src/api/system/dict/type.js @@ -43,6 +43,14 @@ export function delType(dictId) { }) } +// 清理参数缓存 +export function clearCache() { + return request({ + url: '/system/dict/type/clearCache', + method: 'delete' + }) +} + // 获取字典选择框列表 export function optionselect() { return request({ diff --git a/ruoyi-ui/src/views/system/config/index.vue b/ruoyi-ui/src/views/system/config/index.vue index 657e85b..a9c49b8 100644 --- a/ruoyi-ui/src/views/system/config/index.vue +++ b/ruoyi-ui/src/views/system/config/index.vue @@ -341,7 +341,6 @@ export default { }, /** 清理缓存按钮操作 */ handleClearCache() { - const queryParams = this.queryParams; clearCache().then(response => { if (response.code === 200) { this.msgSuccess("清理成功"); diff --git a/ruoyi-ui/src/views/system/dict/index.vue b/ruoyi-ui/src/views/system/dict/index.vue index 3406aef..c6fc6b2 100644 --- a/ruoyi-ui/src/views/system/dict/index.vue +++ b/ruoyi-ui/src/views/system/dict/index.vue @@ -94,6 +94,15 @@ v-hasPermi="['system:dict:export']" >导出 + + 清理缓存 + @@ -173,7 +182,7 @@