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());