1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
@Override public ResponseResult updateRole(Long id, UpdateRoleDto updateRoleDto) { SysRole oldRole = sysRoleService.getById(id); if (oldRole == null) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "该角色不存在!"); }
LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(SysRole::getRoleCode, updateRoleDto.getRoleCode()) .ne(SysRole::getId, id); SysRole existRole = sysRoleService.getOne(wrapper); if (existRole != null) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "角色编码已存在!"); } boolean isProtectedRole = Objects.equals(oldRole.getRoleCode(), SystemConstants.ROLE_IS_ADMIN) || Objects.equals(oldRole.getId(), 1L); if (isProtectedRole) { if (!Objects.equals(oldRole.getRoleCode(), updateRoleDto.getRoleCode())) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "系统内置角色不允许修改角色编码!"); } if (!Objects.equals(oldRole.getName(), updateRoleDto.getName())) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "系统内置角色不允许修改角色名称!"); } if (updateRoleDto.getStatus() != null && !Objects.equals(updateRoleDto.getStatus(), SystemConstants.STATUS_NORMAL)) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "系统内置角色不允许禁用!"); } if (updateRoleDto.getDataScope() != null && !Objects.equals(oldRole.getDataScope(), updateRoleDto.getDataScope())) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "系统内置角色不允许修改数据权限!"); } }
if (updateRoleDto.getDataScope() != null && updateRoleDto.getDataScope().equals(SystemConstants.CUSTOM_DEPT_DATA_SCOPE) && (updateRoleDto.getDeptIds() == null || updateRoleDto.getDeptIds().isEmpty())) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "自定义数据权限时必须选择部门!"); }
SysRole sysRole = BeanCopyUtils.copyBean(updateRoleDto, SysRole.class); sysRole.setId(id); boolean update = sysRoleService.updateById(sysRole); if (!update) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "编辑角色失败!"); }
LambdaQueryWrapper<SysRoleDept> deptWrapper = new LambdaQueryWrapper<>(); deptWrapper.eq(SysRoleDept::getRoleId, id); sysRoleDeptService.remove(deptWrapper);
if (updateRoleDto.getDataScope() != null && updateRoleDto.getDataScope().equals(SystemConstants.CUSTOM_DEPT_DATA_SCOPE) && updateRoleDto.getDeptIds() != null && !updateRoleDto.getDeptIds().isEmpty()) { List<SysRoleDept> roleDeptList = new ArrayList<>(); for (Long deptId : updateRoleDto.getDeptIds()) { SysRoleDept roleDept = new SysRoleDept(); roleDept.setRoleId(id); roleDept.setDeptId(deptId); roleDeptList.add(roleDept); }
boolean saveDept = sysRoleDeptService.saveBatch(roleDeptList); if (!saveDept) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "编辑角色部门关联失败!"); } }
return ResponseResult.okResult(); }
|