From 234f74f8f009a9f164639666d8f3e654f1845b7c 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: Sat, 4 Feb 2023 11:47:11 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=96=B0=E5=A2=9E=20=E7=BF=BB=E8=AF=91?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=20=E9=83=A8=E9=97=A8=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E7=BF=BB=E8=AF=91=20update=20=E4=BC=98=E5=8C=96=20=E9=83=A8?= =?UTF-8?q?=E9=97=A8=E4=B8=9A=E5=8A=A1=E5=A2=9E=E5=8A=A0=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/core/constant/CacheNames.java | 5 +++ .../common/core/service/DeptService.java | 18 ++++++++ .../translation/constant/TransConstant.java | 11 +++-- .../core/TranslationInterface.java | 2 +- .../core/impl/DeptNameTranslationImpl.java | 30 +++++++++++++ .../service/impl/SysDeptServiceImpl.java | 43 ++++++++++++++++--- 6 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/service/DeptService.java create mode 100644 ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/DeptNameTranslationImpl.java diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/CacheNames.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/CacheNames.java index df3a10a1..b0d65737 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/CacheNames.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/CacheNames.java @@ -35,6 +35,11 @@ public interface CacheNames { */ String SYS_USER_NAME = "sys_user_name#30d"; + /** + * 部门 + */ + String SYS_DEPT = "sys_dept#30d"; + /** * OSS内容 */ diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/service/DeptService.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/service/DeptService.java new file mode 100644 index 00000000..c27f4618 --- /dev/null +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/service/DeptService.java @@ -0,0 +1,18 @@ +package com.ruoyi.common.core.service; + +/** + * 通用 部门服务 + * + * @author Lion Li + */ +public interface DeptService { + + /** + * 通过部门ID查询部门名称 + * + * @param deptIds 部门ID串逗号分隔 + * @return 部门名称串逗号分隔 + */ + String selectDeptNameByIds(String deptIds); + +} diff --git a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/constant/TransConstant.java b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/constant/TransConstant.java index 372f3a2a..1cfec58e 100644 --- a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/constant/TransConstant.java +++ b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/constant/TransConstant.java @@ -10,16 +10,21 @@ public interface TransConstant { /** * 用户id转账号 */ - String USER_ID_TO_NAME = "userIdToName"; + String USER_ID_TO_NAME = "user_id_to_name"; + + /** + * 部门id转名称 + */ + String DEPT_ID_TO_NAME = "dept_id_to_name"; /** * 字典type转label */ - String DICT_TYPE_TO_LABEL = "dictTypeToLabel"; + String DICT_TYPE_TO_LABEL = "dict_type_to_label"; /** * ossId转url */ - String OSS_ID_TO_URL = "ossIdToUrl"; + String OSS_ID_TO_URL = "oss_id_to_url"; } diff --git a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/TranslationInterface.java b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/TranslationInterface.java index df4c3a2c..f325c3e3 100644 --- a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/TranslationInterface.java +++ b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/TranslationInterface.java @@ -10,7 +10,7 @@ public interface TranslationInterface { /** * 翻译 * - * @param key 需要被翻译的键 + * @param key 需要被翻译的键(不为空) * @return 返回键对应的值 */ String translation(Object key, String other); diff --git a/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/DeptNameTranslationImpl.java b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/DeptNameTranslationImpl.java new file mode 100644 index 00000000..17b3314b --- /dev/null +++ b/ruoyi-common/ruoyi-common-translation/src/main/java/com/ruoyi/common/translation/core/impl/DeptNameTranslationImpl.java @@ -0,0 +1,30 @@ +package com.ruoyi.common.translation.core.impl; + +import com.ruoyi.common.core.service.DeptService; +import com.ruoyi.common.translation.annotation.TranslationType; +import com.ruoyi.common.translation.constant.TransConstant; +import com.ruoyi.common.translation.core.TranslationInterface; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Component; + +/** + * 部门翻译实现 + * + * @author Lion Li + */ +@Component +@AllArgsConstructor +@TranslationType(type = TransConstant.DEPT_ID_TO_NAME) +public class DeptNameTranslationImpl implements TranslationInterface { + + private final DeptService deptService; + + public String translation(Object key, String other) { + if (key instanceof String ids) { + return deptService.selectDeptNameByIds(ids); + } else if (key instanceof Long id) { + return deptService.selectDeptNameByIds(id.toString()); + } + return null; + } +} diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java index 8206bdc2..453d98f7 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java @@ -8,15 +8,19 @@ import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.ruoyi.common.core.constant.CacheNames; import com.ruoyi.common.core.constant.UserConstants; -import com.ruoyi.system.domain.SysDept; -import com.ruoyi.system.domain.SysRole; -import com.ruoyi.system.domain.SysUser; import com.ruoyi.common.core.exception.ServiceException; -import com.ruoyi.common.mybatis.helper.DataBaseHelper; -import com.ruoyi.common.satoken.utils.LoginHelper; +import com.ruoyi.common.core.service.DeptService; +import com.ruoyi.common.core.utils.SpringUtils; import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.TreeBuildUtils; +import com.ruoyi.common.mybatis.helper.DataBaseHelper; +import com.ruoyi.common.redis.utils.CacheUtils; +import com.ruoyi.common.satoken.utils.LoginHelper; +import com.ruoyi.system.domain.SysDept; +import com.ruoyi.system.domain.SysRole; +import com.ruoyi.system.domain.SysUser; import com.ruoyi.system.domain.bo.SysDeptBo; import com.ruoyi.system.domain.vo.SysDeptVo; import com.ruoyi.system.mapper.SysDeptMapper; @@ -24,6 +28,8 @@ import com.ruoyi.system.mapper.SysRoleMapper; import com.ruoyi.system.mapper.SysUserMapper; import com.ruoyi.system.service.ISysDeptService; import lombok.RequiredArgsConstructor; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -37,7 +43,7 @@ import java.util.List; */ @RequiredArgsConstructor @Service -public class SysDeptServiceImpl implements ISysDeptService { +public class SysDeptServiceImpl implements ISysDeptService, DeptService { private final SysDeptMapper baseMapper; private final SysRoleMapper roleMapper; @@ -117,6 +123,7 @@ public class SysDeptServiceImpl implements ISysDeptService { * @param deptId 部门ID * @return 部门信息 */ + @Cacheable(cacheNames = CacheNames.SYS_DEPT, key = "#deptId") @Override public SysDeptVo selectDeptById(Long deptId) { SysDeptVo dept = baseMapper.selectVoById(deptId); @@ -126,6 +133,24 @@ public class SysDeptServiceImpl implements ISysDeptService { return dept; } + /** + * 通过部门ID查询部门名称 + * + * @param deptIds 部门ID串逗号分隔 + * @return 部门名称串逗号分隔 + */ + @Override + public String selectDeptNameByIds(String deptIds) { + List list = new ArrayList<>(); + for (Long id : Arrays.stream(deptIds.split(",")).map(Long::parseLong).toList()) { + SysDeptVo vo = SpringUtils.getAopProxy(this).selectDeptById(id); + if (ObjectUtil.isNotNull(vo)) { + list.add(vo.getDeptName()); + } + } + return String.join(",", list); + } + /** * 根据ID查询所有子部门数(正常状态) * @@ -222,6 +247,7 @@ public class SysDeptServiceImpl implements ISysDeptService { * @param bo 部门信息 * @return 结果 */ + @CacheEvict(cacheNames = CacheNames.SYS_DEPT, key = "#bo.deptId") @Override public int updateDept(SysDeptBo bo) { SysDept dept = BeanUtil.toBean(bo, SysDept.class); @@ -273,7 +299,9 @@ public class SysDeptServiceImpl implements ISysDeptService { list.add(dept); } if (CollUtil.isNotEmpty(list)) { - baseMapper.updateBatchById(list); + if (baseMapper.updateBatchById(list)) { + list.forEach(dept -> CacheUtils.evict(CacheNames.SYS_DEPT, dept.getDeptId())); + } } } @@ -283,6 +311,7 @@ public class SysDeptServiceImpl implements ISysDeptService { * @param deptId 部门ID * @return 结果 */ + @CacheEvict(cacheNames = CacheNames.SYS_DEPT, key = "#deptId") @Override public int deleteDeptById(Long deptId) { return baseMapper.deleteById(deptId);