Merge pull request #10 from hisequel/fix-role-add-duplicated-users

fix:修复角色分配用户时会有重复用户ID的情况。
This commit is contained in:
阿沐 2025-03-28 09:58:24 +08:00 committed by GitHub
commit 457a7dd958
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 39 additions and 3 deletions

View File

@ -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<String> userSave(@PathVariable("roleId") Long roleId, @RequestBody List<Long> userIdList) {
sysUserRoleService.saveUserList(roleId, userIdList);
if(userIdList.isEmpty()) {
return Result.error("UserId is empty!");
}
//查询数据库该角色对应的用户列表
List<Long> existsUserIdList = sysUserRoleService.getExistsUserIdList(roleId);
//取出需要新增的用户列表
List<Long> 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();
}

View File

@ -23,4 +23,12 @@ public interface SysUserRoleDao extends BaseDao<SysUserRoleEntity> {
* @return 返回角色ID列表
*/
List<Long> getRoleIdList(@Param("userId") Long userId);
/**
* 根据角色ID查询对应的用户ID列表
* @param roleId 角色ID
*
* @return 返回用户ID列表
*/
List<Long> getExistsUserIdList(@Param("roleId") Long roleId);
}

View File

@ -51,4 +51,10 @@ public interface SysUserRoleService extends BaseService<SysUserRoleEntity> {
* @param userId 用户ID
*/
List<Long> getRoleIdList(Long userId);
/**
* 根据角色ID查询对应的用户ID列表
* @param roleId 角色ID
*/
List<Long> getExistsUserIdList(Long roleId);
}

View File

@ -91,4 +91,9 @@ public class SysUserRoleServiceImpl extends BaseServiceImpl<SysUserRoleDao, SysU
public List<Long> getRoleIdList(Long userId) {
return baseMapper.getRoleIdList(userId);
}
@Override
public List<Long> getExistsUserIdList(Long roleId) {
return baseMapper.getExistsUserIdList(roleId);
}
}

View File

@ -7,4 +7,7 @@
select role_id from sys_user_role where user_id = #{userId} and deleted = 0
</select>
<select id="getExistsUserIdList" resultType="long">
select user_id from sys_user_role where role_id = #{roleId} and deleted = 0
</select>
</mapper>