From 47801aff8e712de8135ac80ced4515997b26f2b2 Mon Sep 17 00:00:00 2001 From: Zero Date: Tue, 11 Mar 2025 19:44:33 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E5=88=86=E9=85=8D=E7=94=A8=E6=88=B7=E6=97=B6=E4=BC=9A?= =?UTF-8?q?=E6=9C=89=E9=87=8D=E5=A4=8D=E7=94=A8=E6=88=B7ID=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../maku/system/controller/SysRoleController.java | 20 +++++++++++++++++--- .../java/net/maku/system/dao/SysUserRoleDao.java | 8 ++++++++ .../net/maku/system/service/SysUserRoleService.java | 6 ++++++ .../system/service/impl/SysUserRoleServiceImpl.java | 5 +++++ .../src/main/resources/mapper/SysUserRoleDao.xml | 3 +++ 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/maku-boot-system/src/main/java/net/maku/system/controller/SysRoleController.java b/maku-boot-system/src/main/java/net/maku/system/controller/SysRoleController.java index 82fe0f5..644a227 100644 --- a/maku-boot-system/src/main/java/net/maku/system/controller/SysRoleController.java +++ b/maku-boot-system/src/main/java/net/maku/system/controller/SysRoleController.java @@ -22,8 +22,8 @@ import net.maku.system.vo.SysUserVO; import org.springdoc.core.annotations.ParameterObject; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; - import java.util.List; +import java.util.stream.Collectors; /** * 角色管理 @@ -152,10 +152,24 @@ public class SysRoleController { @PostMapping("user/{roleId}") @Operation(summary = "分配角色给用户列表") - @OperateLog(type = OperateTypeEnum.DELETE) + @OperateLog(type = OperateTypeEnum.UPDATE) @PreAuthorize("hasAuthority('sys:role:update')") public Result userSave(@PathVariable("roleId") Long roleId, @RequestBody List userIdList) { - sysUserRoleService.saveUserList(roleId, userIdList); + if(userIdList.isEmpty()) { + return Result.error("UserId is empty!"); + } + //查询数据库该角色对应的用户列表 + List existsUserIdList = sysUserRoleService.getExistsUserIdList(roleId); + + //取出需要新增的用户列表 + List addUserIdList = userIdList.stream() + .filter(userId -> !existsUserIdList.contains(userId)) + .collect(Collectors.toList()); + + if(addUserIdList.isEmpty()) { + return Result.error("UserId existed!"); + } + sysUserRoleService.saveUserList(roleId, addUserIdList); return Result.ok(); } diff --git a/maku-boot-system/src/main/java/net/maku/system/dao/SysUserRoleDao.java b/maku-boot-system/src/main/java/net/maku/system/dao/SysUserRoleDao.java index dad3115..7eacb71 100644 --- a/maku-boot-system/src/main/java/net/maku/system/dao/SysUserRoleDao.java +++ b/maku-boot-system/src/main/java/net/maku/system/dao/SysUserRoleDao.java @@ -23,4 +23,12 @@ public interface SysUserRoleDao extends BaseDao { * @return 返回角色ID列表 */ List getRoleIdList(@Param("userId") Long userId); + + /** + * 根据角色ID查询对应的用户ID列表 + * @param roleId 角色ID + * + * @return 返回用户ID列表 + */ + List getExistsUserIdList(@Param("roleId") Long roleId); } \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/service/SysUserRoleService.java b/maku-boot-system/src/main/java/net/maku/system/service/SysUserRoleService.java index 44837f7..58683a9 100644 --- a/maku-boot-system/src/main/java/net/maku/system/service/SysUserRoleService.java +++ b/maku-boot-system/src/main/java/net/maku/system/service/SysUserRoleService.java @@ -51,4 +51,10 @@ public interface SysUserRoleService extends BaseService { * @param userId 用户ID */ List getRoleIdList(Long userId); + + /** + * 根据角色ID查询对应的用户ID列表 + * @param roleId 角色ID + */ + List getExistsUserIdList(Long roleId); } \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/service/impl/SysUserRoleServiceImpl.java b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysUserRoleServiceImpl.java index 026615e..9f28d9b 100644 --- a/maku-boot-system/src/main/java/net/maku/system/service/impl/SysUserRoleServiceImpl.java +++ b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysUserRoleServiceImpl.java @@ -91,4 +91,9 @@ public class SysUserRoleServiceImpl extends BaseServiceImpl getRoleIdList(Long userId) { return baseMapper.getRoleIdList(userId); } + + @Override + public List getExistsUserIdList(Long roleId) { + return baseMapper.getExistsUserIdList(roleId); + } } \ No newline at end of file diff --git a/maku-boot-system/src/main/resources/mapper/SysUserRoleDao.xml b/maku-boot-system/src/main/resources/mapper/SysUserRoleDao.xml index b39cf6d..f7aeb22 100644 --- a/maku-boot-system/src/main/resources/mapper/SysUserRoleDao.xml +++ b/maku-boot-system/src/main/resources/mapper/SysUserRoleDao.xml @@ -7,4 +7,7 @@ select role_id from sys_user_role where user_id = #{userId} and deleted = 0 + \ No newline at end of file