From a82ed1e9ddf3763108e37864c400250e8bc75ef6 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: Mon, 11 Nov 2024 13:30:58 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=9D=83=E9=99=90=E6=9F=A5=E8=AF=A2=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/core/constant/CacheNames.java | 10 ++++++ .../common/core/utils/ObjectUtils.java | 32 +++++++++++-------- .../service/impl/SysDataScopeServiceImpl.java | 4 +++ .../service/impl/SysDeptServiceImpl.java | 12 +++++-- .../service/impl/SysRoleServiceImpl.java | 4 +++ 5 files changed, 46 insertions(+), 16 deletions(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/constant/CacheNames.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/constant/CacheNames.java index 28ba17739..bf8efc550 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/constant/CacheNames.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/constant/CacheNames.java @@ -60,6 +60,16 @@ public interface CacheNames { */ String SYS_OSS = "sys_oss#30d"; + /** + * 角色自定义权限 + */ + String SYS_ROLE_CUSTOM = "sys_role_custom#30d"; + + /** + * 部门及以下权限 + */ + String SYS_DEPT_AND_CHILD = "sys_dept_and_child#30d"; + /** * OSS配置 */ diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/ObjectUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/ObjectUtils.java index e0de69a98..c86b55b71 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/ObjectUtils.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/ObjectUtils.java @@ -15,28 +15,32 @@ import java.util.function.Function; public class ObjectUtils extends ObjectUtil { /** - * 如果对象不为空,则获取对象中的某个字段 - *

- * 例: - * - *

public class User { - *

private String name; - *

// 省略 getter/setter - *

} - * - * - *

User user = userService.queryById(userId); - *

String name = ObjectUtils.notNullGetter(user,User::getName); - * + * 如果对象不为空,则获取对象中的某个字段 ObjectUtils.notNullGetter(user, User::getName); + * * @param obj 对象 * @param func 获取方法 * @return 对象字段 */ - public static E notNullGetter(T obj, Function func) { + public static E notNullGetter(T obj, Function func) { if (isNotNull(obj) && isNotNull(func)) { return func.apply(obj); } return null; } + /** + * 如果对象不为空,则获取对象中的某个字段 ObjectUtils.notNullGetter(user, User::getName, ""); + * + * @param obj 对象 + * @param func 获取方法 + * @param defaultValue 默认值 + * @return 对象字段 + */ + public static E notNullGetter(T obj, Function func, E defaultValue) { + if (isNotNull(obj) && isNotNull(func)) { + return func.apply(obj); + } + return defaultValue; + } + } diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDataScopeServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDataScopeServiceImpl.java index 0c07f7f67..470646f35 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDataScopeServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDataScopeServiceImpl.java @@ -5,12 +5,14 @@ import cn.hutool.core.convert.Convert; import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import lombok.RequiredArgsConstructor; +import org.dromara.common.core.constant.CacheNames; import org.dromara.common.core.utils.StreamUtils; import org.dromara.system.domain.SysDept; import org.dromara.system.domain.SysRoleDept; import org.dromara.system.mapper.SysDeptMapper; import org.dromara.system.mapper.SysRoleDeptMapper; import org.dromara.system.service.ISysDataScopeService; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.List; @@ -36,6 +38,7 @@ public class SysDataScopeServiceImpl implements ISysDataScopeService { * @param roleId 角色Id * @return 部门Id组 */ + @Cacheable(cacheNames = CacheNames.SYS_ROLE_CUSTOM, key = "#roleId") @Override public String getRoleCustom(Long roleId) { if (ObjectUtil.isNull(roleId)) { @@ -57,6 +60,7 @@ public class SysDataScopeServiceImpl implements ISysDataScopeService { * @param deptId 部门Id * @return 部门Id组 */ + @Cacheable(cacheNames = CacheNames.SYS_DEPT_AND_CHILD, key = "#deptId") @Override public String getDeptAndChild(Long deptId) { if (ObjectUtil.isNull(deptId)) { diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDeptServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDeptServiceImpl.java index 44ff90e9c..3c7bf5e36 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDeptServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysDeptServiceImpl.java @@ -27,6 +27,7 @@ import org.dromara.system.mapper.SysUserMapper; import org.dromara.system.service.ISysDeptService; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; +import org.springframework.cache.annotation.Caching; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -250,6 +251,7 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService { * @param bo 部门信息 * @return 结果 */ + @CacheEvict(cacheNames = CacheNames.SYS_DEPT_AND_CHILD, allEntries = true) @Override public int insertDept(SysDeptBo bo) { SysDept info = baseMapper.selectById(bo.getParentId()); @@ -268,7 +270,10 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService { * @param bo 部门信息 * @return 结果 */ - @CacheEvict(cacheNames = CacheNames.SYS_DEPT, key = "#bo.deptId") + @Caching(evict = { + @CacheEvict(cacheNames = CacheNames.SYS_DEPT, key = "#bo.deptId"), + @CacheEvict(cacheNames = CacheNames.SYS_DEPT_AND_CHILD, allEntries = true) + }) @Override public int updateDept(SysDeptBo bo) { SysDept dept = MapstructUtils.convert(bo, SysDept.class); @@ -341,7 +346,10 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService { * @param deptId 部门ID * @return 结果 */ - @CacheEvict(cacheNames = CacheNames.SYS_DEPT, key = "#deptId") + @Caching(evict = { + @CacheEvict(cacheNames = CacheNames.SYS_DEPT, key = "#deptId"), + @CacheEvict(cacheNames = CacheNames.SYS_DEPT_AND_CHILD, key = "#deptId") + }) @Override public int deleteDeptById(Long deptId) { return baseMapper.deleteById(deptId); diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysRoleServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysRoleServiceImpl.java index 4148b7308..c11b1ac1b 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysRoleServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysRoleServiceImpl.java @@ -12,6 +12,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.RequiredArgsConstructor; +import org.dromara.common.core.constant.CacheNames; import org.dromara.common.core.constant.SystemConstants; import org.dromara.common.core.constant.TenantConstants; import org.dromara.common.core.domain.model.LoginUser; @@ -33,6 +34,7 @@ import org.dromara.system.mapper.SysRoleMapper; import org.dromara.system.mapper.SysRoleMenuMapper; import org.dromara.system.mapper.SysUserRoleMapper; import org.dromara.system.service.ISysRoleService; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -328,6 +330,7 @@ public class SysRoleServiceImpl implements ISysRoleService { * @param bo 角色信息 * @return 结果 */ + @CacheEvict(cacheNames = CacheNames.SYS_ROLE_CUSTOM, key = "#bo.roleId") @Override @Transactional(rollbackFor = Exception.class) public int authDataScope(SysRoleBo bo) { @@ -404,6 +407,7 @@ public class SysRoleServiceImpl implements ISysRoleService { * @param roleIds 需要删除的角色ID * @return 结果 */ + @CacheEvict(cacheNames = CacheNames.SYS_ROLE_CUSTOM, allEntries = true) @Override @Transactional(rollbackFor = Exception.class) public int deleteRoleByIds(Long[] roleIds) {