mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 02:29:19 +08:00
feat: 优化系统菜单和权限管理页
This commit is contained in:
+1
-1
@@ -71,7 +71,7 @@ public class MenuController {
|
||||
}
|
||||
|
||||
@PostMapping("/batch-sort")
|
||||
@RequirePermission(value = "sys:menu:update", description = "批量菜单排序", allowSuper = true)
|
||||
@RequirePermission(value = "sys:menu:update", description = "批量菜单排序与移动", allowSuper = true)
|
||||
public ApiResponse<Void> batchSortMenu(@Valid @RequestBody BatchSortMenuDTO batchSortMenuDTO) {
|
||||
menuService.batchSortMenu(batchSortMenuDTO);
|
||||
return ApiResponse.success("菜单排序成功");
|
||||
|
||||
+10
-16
@@ -1,5 +1,7 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.menu.model.dto;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
@@ -8,36 +10,28 @@ import lombok.NoArgsConstructor;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 批量菜单排序DTO
|
||||
* 批量菜单排序/移动(可同时改 parentId 与同级 order)
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class BatchSortMenuDTO {
|
||||
|
||||
/**
|
||||
* 菜单排序项列表
|
||||
*/
|
||||
@NotNull(message = "菜单排序项列表不能为空")
|
||||
@NotEmpty(message = "菜单排序项列表不能为空")
|
||||
@Valid
|
||||
private List<MenuSortItem> sortItems;
|
||||
|
||||
/**
|
||||
* 菜单排序项
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class MenuSortItem {
|
||||
|
||||
/**
|
||||
* 菜单ID
|
||||
*/
|
||||
|
||||
@NotNull(message = "菜单ID不能为空")
|
||||
private Long menuId;
|
||||
|
||||
/**
|
||||
* 排序值
|
||||
*/
|
||||
|
||||
/** 新父级;null 表示挂到顶级 */
|
||||
private Long parentId;
|
||||
|
||||
@NotNull(message = "排序值不能为空")
|
||||
private Integer order;
|
||||
}
|
||||
|
||||
+28
-9
@@ -217,26 +217,45 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuPO> implements
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量菜单排序
|
||||
* @param batchSortMenuDTO 批量排序DTO
|
||||
* 批量菜单排序/移动
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void batchSortMenu(BatchSortMenuDTO batchSortMenuDTO) {
|
||||
log.info("批量菜单排序,排序项数量:{}", batchSortMenuDTO.getSortItems().size());
|
||||
|
||||
// 批量更新菜单排序
|
||||
for (BatchSortMenuDTO.MenuSortItem sortItem : batchSortMenuDTO.getSortItems()) {
|
||||
List<BatchSortMenuDTO.MenuSortItem> items = batchSortMenuDTO.getSortItems();
|
||||
log.info("批量菜单排序/移动,数量:{}", items.size());
|
||||
|
||||
for (BatchSortMenuDTO.MenuSortItem sortItem : items) {
|
||||
MenuPO menuPO = this.getById(sortItem.getMenuId());
|
||||
if (menuPO == null) {
|
||||
throw new BusinessException("菜单不存在,ID:" + sortItem.getMenuId());
|
||||
}
|
||||
|
||||
assertValidMenuParent(sortItem.getMenuId(), sortItem.getParentId());
|
||||
menuPO.setParentId(sortItem.getParentId());
|
||||
menuPO.setOrder(sortItem.getOrder());
|
||||
menuPO.setUpdateBy(currentUserContext.getCurrentUserId());
|
||||
this.updateById(menuPO);
|
||||
}
|
||||
|
||||
log.info("批量菜单排序完成");
|
||||
|
||||
log.info("批量菜单排序/移动完成");
|
||||
}
|
||||
|
||||
/** 父级合法:存在、且不能是自己或子孙 */
|
||||
private void assertValidMenuParent(Long menuId, Long parentId) {
|
||||
if (parentId == null) {
|
||||
return;
|
||||
}
|
||||
if (parentId.equals(menuId)) {
|
||||
throw new BusinessException("不能将菜单挂到自身下");
|
||||
}
|
||||
MenuPO parent = this.getById(parentId);
|
||||
if (parent == null) {
|
||||
throw new BusinessException("父菜单不存在");
|
||||
}
|
||||
List<Long> descendants = baseMapper.selectChildMenuIds(menuId);
|
||||
if (descendants != null && descendants.contains(parentId)) {
|
||||
throw new BusinessException("不能将菜单挂到其子孙节点下");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+1
-1
@@ -108,7 +108,7 @@ public class PermissionController {
|
||||
}
|
||||
|
||||
@PutMapping("/sort")
|
||||
@RequirePermission(value = "sys:permission:update", description = "批量更新权限排序", allowSuper = true)
|
||||
@RequirePermission(value = "sys:permission:manage", description = "批量更新权限排序与父级", allowSuper = true)
|
||||
public ApiResponse<Void> batchSortPermission(
|
||||
@RequestBody @Valid BatchSortPermissionDTO dto) {
|
||||
permissionService.batchSortPermission(dto);
|
||||
|
||||
+8
-2
@@ -1,5 +1,7 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.permission.model.dto;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
@@ -8,14 +10,15 @@ import lombok.NoArgsConstructor;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 批量权限排序DTO
|
||||
* 批量权限排序/移动(可同时改 parentId 与同级 sortOrder)
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class BatchSortPermissionDTO {
|
||||
|
||||
@NotNull(message = "排序项列表不能为空")
|
||||
@NotEmpty(message = "排序项列表不能为空")
|
||||
@Valid
|
||||
private List<SortItem> sortItems;
|
||||
|
||||
@Data
|
||||
@@ -26,6 +29,9 @@ public class BatchSortPermissionDTO {
|
||||
@NotNull(message = "权限ID不能为空")
|
||||
private Long permissionId;
|
||||
|
||||
/** 新父级;null 表示挂到顶级 */
|
||||
private Long parentId;
|
||||
|
||||
@NotNull(message = "排序值不能为空")
|
||||
private Integer sortOrder;
|
||||
}
|
||||
|
||||
+72
-25
@@ -1,15 +1,17 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.permission.service.impl;
|
||||
|
||||
import top.xyquan.ecomdmbbackend.common.utils.BeanCopyUtils;
|
||||
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.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.common.utils.BeanCopyUtils;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.company.permission.service.CompanyPermissionService;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.position.permission.service.PositionPermissionService;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.permission.mapper.PermissionMapper;
|
||||
@@ -48,14 +50,8 @@ public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permiss
|
||||
|
||||
@Override
|
||||
public PermissionVO createPermission(CreatePermissionDTO dto) {
|
||||
LambdaQueryWrapper<PermissionPO> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(PermissionPO::getCode, dto.getCode());
|
||||
if (dto.getParentId() != null) {
|
||||
queryWrapper.eq(PermissionPO::getParentId, dto.getParentId());
|
||||
}
|
||||
if (permissionMapper.selectCount(queryWrapper) > 0) {
|
||||
throw new BusinessException("该父节点下已存在相同权限码");
|
||||
}
|
||||
assertValidParent(null, dto.getParentId());
|
||||
assertCodeUniqueUnderParent(dto.getCode(), dto.getParentId(), null);
|
||||
|
||||
PermissionPO po = new PermissionPO();
|
||||
BeanCopyUtils.copyProperties(dto, po);
|
||||
@@ -72,22 +68,22 @@ public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permiss
|
||||
PermissionPO existing = permissionMapper.selectById(id);
|
||||
if (existing == null) throw new BusinessException("权限不存在");
|
||||
|
||||
LambdaQueryWrapper<PermissionPO> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(PermissionPO::getCode, dto.getCode())
|
||||
.ne(PermissionPO::getId, id);
|
||||
if (dto.getParentId() != null) {
|
||||
queryWrapper.eq(PermissionPO::getParentId, dto.getParentId());
|
||||
}
|
||||
if (permissionMapper.selectCount(queryWrapper) > 0) {
|
||||
throw new BusinessException("该父节点下已存在相同权限码");
|
||||
}
|
||||
assertValidParent(id, dto.getParentId());
|
||||
assertCodeUniqueUnderParent(dto.getCode(), dto.getParentId(), id);
|
||||
|
||||
BeanCopyUtils.copyProperties(dto, existing);
|
||||
existing.setUpdateBy(currentUserContext.getCurrentUserId());
|
||||
permissionMapper.updateById(existing);
|
||||
return convertToVO(existing);
|
||||
LambdaUpdateWrapper<PermissionPO> uw = new LambdaUpdateWrapper<>();
|
||||
uw.eq(PermissionPO::getId, id)
|
||||
.set(PermissionPO::getParentId, dto.getParentId())
|
||||
.set(PermissionPO::getCode, dto.getCode())
|
||||
.set(PermissionPO::getName, dto.getName())
|
||||
.set(PermissionPO::getType, dto.getType())
|
||||
.set(PermissionPO::getStatus, dto.getStatus() != null ? dto.getStatus() : existing.getStatus())
|
||||
.set(PermissionPO::getRemark, dto.getRemark())
|
||||
.set(PermissionPO::getSortOrder, dto.getSortOrder() != null ? dto.getSortOrder() : existing.getSortOrder())
|
||||
.set(PermissionPO::getUpdateBy, currentUserContext.getCurrentUserId());
|
||||
permissionMapper.update(null, uw);
|
||||
return convertToVO(permissionMapper.selectById(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deletePermission(Long id) {
|
||||
PermissionPO permission = permissionMapper.selectById(id);
|
||||
@@ -248,9 +244,60 @@ public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permiss
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void batchSortPermission(BatchSortPermissionDTO dto) {
|
||||
log.info("批量权限排序,数量:{}", dto.getSortItems().size());
|
||||
permissionMapper.batchUpdateSortOrder(dto.getSortItems());
|
||||
List<BatchSortPermissionDTO.SortItem> items = dto.getSortItems();
|
||||
log.info("批量权限排序/移动,数量:{}", items.size());
|
||||
for (BatchSortPermissionDTO.SortItem item : items) {
|
||||
PermissionPO po = permissionMapper.selectById(item.getPermissionId());
|
||||
if (po == null) {
|
||||
throw new BusinessException("权限不存在,ID:" + item.getPermissionId());
|
||||
}
|
||||
assertValidParent(item.getPermissionId(), item.getParentId());
|
||||
assertCodeUniqueUnderParent(po.getCode(), item.getParentId(), item.getPermissionId());
|
||||
}
|
||||
permissionMapper.batchUpdateSortOrder(items);
|
||||
}
|
||||
|
||||
/** 父级合法:存在、且不能是自己或子孙 */
|
||||
private void assertValidParent(Long permissionId, Long parentId) {
|
||||
if (parentId == null) {
|
||||
return;
|
||||
}
|
||||
if (permissionId != null && parentId.equals(permissionId)) {
|
||||
throw new BusinessException("不能将权限挂到自身下");
|
||||
}
|
||||
PermissionPO parent = permissionMapper.selectById(parentId);
|
||||
if (parent == null) {
|
||||
throw new BusinessException("父级权限不存在");
|
||||
}
|
||||
if (permissionId == null) {
|
||||
return;
|
||||
}
|
||||
List<Long> descendants = permissionMapper.selectChildPermissionIds(permissionId);
|
||||
if (descendants != null && descendants.contains(parentId)) {
|
||||
throw new BusinessException("不能将权限挂到其子孙节点下");
|
||||
}
|
||||
}
|
||||
|
||||
/** 同一父级下权限码唯一 */
|
||||
private void assertCodeUniqueUnderParent(String code, Long parentId, Long excludeId) {
|
||||
if (code == null || code.isBlank()) {
|
||||
return;
|
||||
}
|
||||
LambdaQueryWrapper<PermissionPO> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(PermissionPO::getCode, code);
|
||||
if (excludeId != null) {
|
||||
queryWrapper.ne(PermissionPO::getId, excludeId);
|
||||
}
|
||||
if (parentId != null) {
|
||||
queryWrapper.eq(PermissionPO::getParentId, parentId);
|
||||
} else {
|
||||
queryWrapper.isNull(PermissionPO::getParentId);
|
||||
}
|
||||
if (permissionMapper.selectCount(queryWrapper) > 0) {
|
||||
throw new BusinessException("该父节点下已存在相同权限码");
|
||||
}
|
||||
}
|
||||
|
||||
private PermissionVO convertToVO(PermissionPO po) {
|
||||
|
||||
Reference in New Issue
Block a user