From d9bbcb3fbf858b15ea7bd6d3574d807490e0ea57 Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Wed, 17 Jan 2024 22:39:58 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=908.1.0=E3=80=91=E3=80=90user-role?= =?UTF-8?q?=E3=80=91=E6=9B=B4=E6=96=B0=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=95=8C=E9=9D=A2=EF=BC=8C=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E8=A7=92=E8=89=B2=E8=BF=94=E5=9B=9E=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E8=A7=92=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kernel/sys/api/SysUserRoleServiceApi.java | 8 +++++ .../service/impl/SysUserRoleServiceImpl.java | 33 +++++++++++++++---- .../user/service/impl/SysUserServiceImpl.java | 4 +-- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/SysUserRoleServiceApi.java b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/SysUserRoleServiceApi.java index bb950bee5..5171bab47 100644 --- a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/SysUserRoleServiceApi.java +++ b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/SysUserRoleServiceApi.java @@ -19,6 +19,14 @@ public interface SysUserRoleServiceApi { */ List getUserRoleIdList(Long userId); + /** + * 获取用户系统级别的角色,用在用户管理界面,分配角色时使用 + * + * @author fengshuonan + * @since 2024/1/17 22:35 + */ + Set getUserSystemRoleIdList(Long userId); + /** * 获取用户的当前所在机构的所有角色id列表 * diff --git a/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/service/impl/SysUserRoleServiceImpl.java b/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/service/impl/SysUserRoleServiceImpl.java index 187b5be42..0ba7cf8c5 100644 --- a/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/service/impl/SysUserRoleServiceImpl.java +++ b/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/service/impl/SysUserRoleServiceImpl.java @@ -25,10 +25,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; import static cn.stylefeng.roses.kernel.sys.modular.user.constants.UserConstants.UPDATE_USER_ROLE_EVENT; @@ -147,6 +144,28 @@ public class SysUserRoleServiceImpl extends ServiceImpl getUserSystemRoleIdList(Long userId) { + if (userId == null) { + return new LinkedHashSet<>(); + } + + // 先从缓存查找用户的角色 + List cachedRoleList = userRoleCache.get(userId.toString()); + if (ObjectUtil.isNotEmpty(cachedRoleList)) { + return cachedRoleList.stream().filter(i -> RoleTypeEnum.SYSTEM_ROLE.getCode().equals(i.getRoleType())).map(SysUserRole::getRoleId).collect(Collectors.toSet()); + } + + List sysUserRoleList = this.dbGetUserTotalRoleList(userId); + + // 查询结果缓存起来 + if (ObjectUtil.isNotEmpty(sysUserRoleList)) { + userRoleCache.put(userId.toString(), sysUserRoleList, SysConstants.DEFAULT_SYS_CACHE_TIMEOUT_SECONDS); + } + + return sysUserRoleList.stream().filter(i -> RoleTypeEnum.SYSTEM_ROLE.getCode().equals(i.getRoleType())).map(SysUserRole::getRoleId).collect(Collectors.toSet()); + } + @Override public List getUserRoleIdListCurrentCompany(Long userId, Long orgId) { @@ -208,10 +227,12 @@ public class SysUserRoleServiceImpl extends ServiceImpl systemRoleIdList = roleResults.stream().filter(i -> i.getRoleType().equals(RoleTypeEnum.SYSTEM_ROLE.getCode())).map(SysRoleDTO::getRoleId).collect(Collectors.toSet()); + Set systemRoleIdList = roleResults.stream().filter(i -> i.getRoleType().equals(RoleTypeEnum.SYSTEM_ROLE.getCode())).map(SysRoleDTO::getRoleId) + .collect(Collectors.toSet()); // 3. 所有当前公司角色id集合 - Set currentCompanyRoleIdList = roleResults.stream().filter(i -> i.getRoleType().equals(RoleTypeEnum.COMPANY_ROLE.getCode())).map(SysRoleDTO::getRoleId).collect(Collectors.toSet()); + Set currentCompanyRoleIdList = roleResults.stream().filter(i -> i.getRoleType().equals(RoleTypeEnum.COMPANY_ROLE.getCode())).map(SysRoleDTO::getRoleId) + .collect(Collectors.toSet()); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); diff --git a/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/service/impl/SysUserServiceImpl.java b/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/service/impl/SysUserServiceImpl.java index 02697ee12..84a1dd4c2 100644 --- a/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/service/impl/SysUserServiceImpl.java +++ b/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/service/impl/SysUserServiceImpl.java @@ -218,8 +218,8 @@ public class SysUserServiceImpl extends ServiceImpl impl sysUser.setUserOrgDTOList(userOrgList); // 获取用户的角色信息 - List userRoleIdList = sysUserRoleService.getUserRoleIdList(sysUser.getUserId()); - sysUser.setRoleIdList(userRoleIdList); + Set userRoleIdList = sysUserRoleService.getUserSystemRoleIdList(sysUser.getUserId()); + sysUser.setRoleIdList(new ArrayList<>(userRoleIdList)); // 获取用户证书信息 List userCertificateList = sysUserCertificateService.getUserCertificateList(sysUser.getUserId());