Merge branch 'dev-role' into dev

This commit is contained in:
cuijiawang 2025-09-23 11:50:06 +08:00
commit f4ce15986c
5 changed files with 62 additions and 37 deletions

View File

@ -48,7 +48,7 @@ public class SysRoleController {
* 根据角色编号获取详细信息
*/
@GetMapping(value = "/{roleId}")
public R<RoleVO> getInfo(@PathVariable @NotNull Long roleId) {
public R<RoleVO> getInfo(@PathVariable("roleId") @NotNull Long roleId) {
RoleVO roleInfo = sysRoleService.getRoleInfo(roleId);
return R.ok(roleInfo);
}
@ -65,7 +65,7 @@ public class SysRoleController {
/**
* 移除角色
*/
@PostMapping(value = "/{roleId}")
@PostMapping(value = "/del/{roleId}")
public R<Void> remove(@PathVariable("roleId") List<Long> roleIds) {
sysRoleService.deleteRole(roleIds);
return R.ok();
@ -74,7 +74,7 @@ public class SysRoleController {
/**
* 修改保存角色
*/
@PostMapping
@PostMapping("/update")
public R<Void> edit(@Validated @RequestBody UpdateRoleDTO updateDTO) {
sysRoleService.updateRole(updateDTO);
return R.ok();
@ -94,7 +94,7 @@ public class SysRoleController {
/**
* 角色状态修改
*/
@PostMapping("/{roleId}/status")
@PostMapping("/status/{roleId}")
public R<Void> changeStatus(@PathVariable("roleId") Long roleId,
@RequestBody UpdateStatusDTO updateDTO) {
updateDTO.setRoleId(roleId);

View File

@ -1,10 +1,10 @@
package com.agileboot.system.role.mapper;
import com.agileboot.common.mybatis.mapper.BaseMapperDelete;
import com.agileboot.system.role.entity.SysRole;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
public interface SysRoleMapper extends BaseMapper<SysRole> {
public interface SysRoleMapper extends BaseMapperDelete<SysRole> {
/**
* 判断菜单是否被角色分配

View File

@ -1,8 +1,8 @@
package com.agileboot.system.role.mapper;
import com.agileboot.common.mybatis.mapper.BaseMapperDelete;
import com.agileboot.system.role.entity.SysRoleMenu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface SysRoleMenuMapper extends BaseMapper<SysRoleMenu> {
public interface SysRoleMenuMapper extends BaseMapperDelete<SysRoleMenu> {
}

View File

@ -23,7 +23,7 @@ public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRo
@Override
public boolean saveMenus(Long roleId, List<Long> menuIds) {
List<SysRoleMenu> list = new ArrayList<>();
if (menuIds != null) {
if (menuIds != null && !menuIds.isEmpty()) {
for (Long menuId : menuIds) {
SysRoleMenu rm = new SysRoleMenu();
rm.setRoleId(roleId);
@ -40,7 +40,6 @@ public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRo
if (roleIds == null || roleIds.isEmpty()) {
return;
}
LambdaQueryWrapper<SysRoleMenu> queryWrapper = Wrappers.lambdaQuery(SysRoleMenu.class).in(SysRoleMenu::getRoleId, roleIds);
super.remove(queryWrapper);
this.baseMapper.deleteAbsoluteByIds(roleIds);
}
}

View File

@ -18,20 +18,20 @@ import com.agileboot.system.user.dto.UserInfo;
import com.agileboot.system.user.entity.SysUser;
import com.agileboot.system.user.service.ISysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
@Service
@ -65,12 +65,9 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
public void addRole(AddRoleDTO addDTO) {
SysRole entity = new SysRole();
BeanUtil.copyProperties(addDTO, entity);
if (this.isRoleNameDuplicated(null, addDTO.getRoleName())) {
throw new BizException(ErrorCode.Business.ROLE_NAME_IS_NOT_UNIQUE, addDTO.getRoleName());
}
if (this.isRoleKeyDuplicated(null, addDTO.getRoleKey())) {
throw new BizException(ErrorCode.Business.ROLE_KEY_IS_NOT_UNIQUE, addDTO.getRoleKey());
}
this.checkRoleNameUnique(null, addDTO.getRoleName());
this.checkRoleKeyUnique(null, addDTO.getRoleKey());
super.save(entity);
roleMenuService.saveMenus(entity.getRoleId(), addDTO.getMenuIds());
}
@ -94,30 +91,55 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
// 清空之前的角色菜单关联
roleMenuService.cleanOldMenus(collect);
super.removeBatchByIds(collect);
super.baseMapper.deleteAbsoluteByIds(collect);
}
// @Override
// @Transactional(rollbackFor = Exception.class)
// public void updateRole(UpdateRoleDTO updateDTO) {
// LambdaQueryWrapper<SysRole> roleLqw = new LambdaQueryWrapper<>();
// roleLqw.eq(SysRole::getRoleId, updateDTO.getRoleId());
// roleLqw.eq(SysRole::getStatus, StatusEnum.ENABLE.getValue());
// SysRole sysRole = getOne(roleLqw);
// Preconditions.checkArgument(Objects.nonNull(sysRole), "角色不存在");
// LambdaQueryWrapper<SysRoleMenu> roleMenuLqw = new LambdaQueryWrapper<>();
// roleMenuLqw.eq(SysRoleMenu::getRoleId, updateDTO.getRoleId());
// //角色对应的菜单
// LambdaUpdateWrapper<SysRoleMenu> roleMenuLuw = new LambdaUpdateWrapper<>();
// roleMenuLuw.eq(SysRoleMenu::getRoleId, updateDTO.getRoleId());
// roleMenuService.cleanOldMenus(List.of(updateDTO.getRoleId()));//清空之前的角色菜单关联
// List<Long> menuIds = updateDTO.getMenuIds();//新的菜单列表
// if (!CollectionUtils.isEmpty(menuIds)) {
// roleMenuService.saveMenus(updateDTO.getRoleId(), menuIds);
// }
// BeanUtils.copyProperties(updateDTO, sysRole, "roleId");
// updateById(sysRole);
//
// }
@Override
@Transactional(rollbackFor = Exception.class)
public void updateRole(UpdateRoleDTO updateDTO) {
SysRole byId = super.getById(updateDTO.getRoleId());
if (byId == null) {
throw new BizException(ErrorCode.Business.COMMON_OBJECT_NOT_FOUND, updateDTO.getRoleId(), "角色");
}
List<SysRoleMenu> roleMenus = roleMenuService.listById(updateDTO.getRoleId());
List<Long> menuIds = roleMenus.stream().map(SysRoleMenu::getMenuId).toList();
// List<SysRoleMenu> roleMenus = roleMenuService.listById(updateDTO.getRoleId());
List<Long> menuIds = updateDTO.getMenuIds();
// List<Long> menuIds = roleMenus.stream().map(SysRoleMenu::getMenuId).toList();
// List<Long> deptIds = StrUtil.split(byId.getDeptIdSet(), ",").stream().map(Convert::toLong).toList();
SysRole entity = new SysRole();
BeanUtil.copyProperties(byId, entity);
if (this.isRoleKeyDuplicated(entity.getRoleId(), entity.getRoleKey())) {
throw new BizException(ErrorCode.Business.ROLE_KEY_IS_NOT_UNIQUE, entity.getRoleKey());
BeanUtils.copyProperties(updateDTO, byId, "roleId");
this.checkRoleKeyUnique(byId.getRoleId(), byId.getRoleKey());
this.checkRoleNameUnique(byId.getRoleId(), byId.getRoleName());
// 清空之前的角色菜单关联
roleMenuService.cleanOldMenus(List.of(byId.getRoleId()));
boolean b = roleMenuService.saveMenus(byId.getRoleId(), menuIds);
if (!b) {
throw new BizException("修改角色失败");
}
if (this.isRoleNameDuplicated(entity.getRoleId(), entity.getRoleName())) {
throw new BizException(ErrorCode.Business.ROLE_NAME_IS_NOT_UNIQUE, entity.getRoleName());
}
roleMenuService.cleanOldMenus(List.of(entity.getRoleId()));
roleMenuService.saveMenus(entity.getRoleId(), menuIds);
super.updateById(byId);
}
@Override
@ -183,18 +205,22 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
//------------------------------
private boolean isRoleNameDuplicated(Long roleId, String roleName) {
private void checkRoleNameUnique(Long roleId, String roleName) {
LambdaQueryWrapper<SysRole> queryWrapper = Wrappers.lambdaQuery(SysRole.class)
.ne(roleId != null, SysRole::getRoleId, roleId)
.eq(SysRole::getRoleName, roleName);
return this.baseMapper.exists(queryWrapper);
if (super.exists(queryWrapper)) {
throw new BizException(ErrorCode.Business.ROLE_NAME_IS_NOT_UNIQUE, roleName);
}
}
private boolean isRoleKeyDuplicated(Long roleId, String roleKey) {
private void checkRoleKeyUnique(Long roleId, String roleKey) {
LambdaQueryWrapper<SysRole> queryWrapper = Wrappers.lambdaQuery(SysRole.class)
.ne(roleId != null, SysRole::getRoleId, roleId)
.eq(SysRole::getRoleKey, roleKey);
return this.baseMapper.exists(queryWrapper);
if (super.exists(queryWrapper)) {
throw new BizException(ErrorCode.Business.ROLE_KEY_IS_NOT_UNIQUE, roleKey);
}
}
private String generateDeptIdSet(List<Long> deptIds) {