添加菜单接口.

This commit is contained in:
lijiahang
2023-07-18 19:08:55 +08:00
parent 7ad2fd54c5
commit 8a5888e81e
18 changed files with 352 additions and 240 deletions

View File

@@ -29,4 +29,6 @@ public class Const implements com.orion.lang.constant.Const {
public static final String INTRANET_IP = "内网IP";
public static final Long ROOT_MENU_ID = 0L;
}

View File

@@ -27,7 +27,11 @@ public interface ErrorMessage {
String USERNAME_PRESENT = "用户名已存在";
String ROLE_ABSENT = "角色存在";
String ROLE_ABSENT = "角色存在";
String PARENT_MENU_ABSENT = "父菜单不存在";
String PARENT_MENU_MUST_PARENT = "上级菜单必须为父菜单";
String DATA_ABSENT = "数据不存在";

View File

@@ -1,6 +1,9 @@
package com.orion.ops.framework.mybatis.core.query;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import java.util.Collection;
/**
* 条件构建类
@@ -34,4 +37,17 @@ public class Conditions {
return new ValidateLambdaWrapper<>(clazz);
}
/**
* id list
*
* @param idMapping idMapping
* @param ids ids
* @param <T> T
* @param <ID> ID
* @return wrapper
*/
public static <T, ID> LambdaQueryWrapper<T> id(SFunction<T, ID> idMapping, Collection<ID> ids) {
return new ValidateLambdaWrapper<T>().in(idMapping, ids);
}
}

View File

@@ -1,10 +1,11 @@
package com.orion.ops.module.infra.controller;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.ops.framework.common.annotation.IgnoreLog;
import com.orion.ops.framework.common.annotation.RestWrapper;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuCreateRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuQueryRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuUpdateRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuUpdateStatusRequest;
import com.orion.ops.module.infra.entity.vo.SystemMenuVO;
import com.orion.ops.module.infra.service.SystemMenuService;
import io.swagger.v3.oas.annotations.Operation;
@@ -40,17 +41,24 @@ public class SystemMenuController {
@PostMapping("/create")
@Operation(summary = "创建菜单")
@PreAuthorize("@ss.hasPermission('infra:system-menu:create')")
public Long createSystemMenu(@Validated @RequestBody SystemMenuCreateRequest request) {
public Long createSystemMenu(@RequestBody SystemMenuCreateRequest request) {
return systemMenuService.createSystemMenu(request);
}
@PutMapping("/update")
@Operation(summary = "通过 id 更新菜单")
@PreAuthorize("@ss.hasPermission('infra:system-menu:update')")
public Integer updateSystemMenu(@Validated @RequestBody SystemMenuUpdateRequest request) {
public Integer updateSystemMenu(@RequestBody SystemMenuUpdateRequest request) {
return systemMenuService.updateSystemMenu(request);
}
@PutMapping("/update-status")
@Operation(summary = "通过 id 级联更新菜单状态")
@PreAuthorize("@ss.hasPermission('infra:system-menu:update')")
public Integer updateSystemMenuStatus(@RequestBody SystemMenuUpdateStatusRequest request) {
return systemMenuService.updateSystemMenuStatus(request);
}
@GetMapping("/get")
@Operation(summary = "通过 id 查询菜单")
@Parameter(name = "id", description = "id", required = true)
@@ -59,36 +67,21 @@ public class SystemMenuController {
return systemMenuService.getSystemMenu(id);
}
@GetMapping("/list")
@Operation(summary = "通过 id 批量查询菜单")
@Parameter(name = "idList", description = "idList", required = true)
@IgnoreLog
@PostMapping("/list")
@Operation(summary = "查询菜单")
@PreAuthorize("@ss.hasPermission('infra:system-menu:query')")
public List<SystemMenuVO> getSystemMenuList(@RequestParam("idList") List<Long> idList) {
return systemMenuService.getSystemMenuList(idList);
}
@PostMapping("/query")
@Operation(summary = "分页查询菜单")
@PreAuthorize("@ss.hasPermission('infra:system-menu:query')")
public DataGrid<SystemMenuVO> getSystemMenuPage(@Validated @RequestBody SystemMenuQueryRequest request) {
return systemMenuService.getSystemMenuPage(request);
public List<SystemMenuVO> getSystemMenuList(@Validated @RequestBody SystemMenuQueryRequest request) {
return systemMenuService.getSystemMenuList(request);
}
@PutMapping("/delete")
@Operation(summary = "通过 id 删除菜单")
@Operation(summary = "通过 id 级联删除菜单")
@Parameter(name = "id", description = "id", required = true)
@PreAuthorize("@ss.hasPermission('infra:system-menu:delete')")
public Integer deleteSystemMenu(@RequestParam("id") Long id) {
return systemMenuService.deleteSystemMenu(id);
}
@PutMapping("/delete-batch")
@Operation(summary = "通过 id 批量删除菜单")
@Parameter(name = "idList", description = "idList", required = true)
@PreAuthorize("@ss.hasPermission('infra:system-menu:delete')")
public Integer batchDeleteSystemMenu(@RequestParam("idList") List<Long> idList) {
return systemMenuService.batchDeleteSystemMenu(idList);
}
}

View File

@@ -2,9 +2,7 @@ package com.orion.ops.module.infra.convert;
import com.orion.ops.module.infra.entity.domain.SystemMenuDO;
import com.orion.ops.module.infra.entity.dto.SystemMenuCacheDTO;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuCreateRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuQueryRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuUpdateRequest;
import com.orion.ops.module.infra.entity.request.menu.*;
import com.orion.ops.module.infra.entity.vo.SystemMenuVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@@ -23,6 +21,12 @@ public interface SystemMenuConvert {
SystemMenuConvert MAPPER = Mappers.getMapper(SystemMenuConvert.class);
SystemMenuCreateValidMenuRequest toMenuValidate(SystemMenuCreateRequest request);
SystemMenuCreateValidFunctionRequest toFunctionValidate(SystemMenuCreateRequest request);
SystemMenuCreateRequest toCreateValidate(SystemMenuUpdateRequest request);
SystemMenuDO to(SystemMenuCreateRequest request);
SystemMenuDO to(SystemMenuUpdateRequest request);
@@ -31,8 +35,12 @@ public interface SystemMenuConvert {
SystemMenuVO to(SystemMenuDO domain);
SystemMenuVO to(SystemMenuCacheDTO cache);
List<SystemMenuVO> to(List<SystemMenuDO> list);
SystemMenuCacheDTO toCache(SystemMenuDO domain);
List<SystemMenuCacheDTO> toCache(List<SystemMenuDO> list);
}

View File

@@ -6,9 +6,6 @@ import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
@@ -25,57 +22,39 @@ import java.io.Serializable;
@Schema(name = "SystemMenuCreateRequest", description = "菜单 创建请求对象")
public class SystemMenuCreateRequest implements Serializable {
@NotNull
@Schema(description = "父id")
private Long parentId;
@Size(max = 32)
@NotBlank
@Schema(description = "菜单名称")
private String name;
@Size(max = 64)
@NotBlank
@Schema(description = "菜单权限")
private String permission;
@NotNull
@Schema(description = "菜单类型 1父菜单 2子菜单 3功能")
private Integer type;
@NotNull
@Schema(description = "排序")
private Integer sort;
@NotNull
@Schema(description = "是否可见 0不可见 1可见")
private Integer visible;
@NotNull
@Schema(description = "菜单状态 0停用 1启用")
private Integer status;
@NotNull
@Schema(description = "菜单缓存 0不缓存 1缓存")
private Integer cache;
@Size(max = 64)
@NotBlank
@Schema(description = "菜单图标")
private String icon;
@Size(max = 128)
@NotBlank
@Schema(description = "路由地址")
private String path;
@Size(max = 64)
@NotBlank
@Schema(description = "组件名称")
private String componentName;
@Size(max = 128)
@NotBlank
@Schema(description = "组件地址")
private String component;

View File

@@ -0,0 +1,50 @@
package com.orion.ops.module.infra.entity.request.menu;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* 菜单 创建功能请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-18 10:18
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemMenuCreateFunctionRequest", description = "菜单 创建功能请求对象")
public class SystemMenuCreateValidFunctionRequest implements Serializable {
@NotNull
@Schema(description = "父id")
private Long parentId;
@Size(max = 32)
@NotBlank
@Schema(description = "菜单名称")
private String name;
@Size(max = 64)
@NotBlank
@Schema(description = "菜单权限")
private String permission;
@NotNull
@Schema(description = "菜单类型 1父菜单 2子菜单 3功能")
private Integer type;
@NotNull
@Schema(description = "菜单状态 0停用 1启用")
private Integer status;
}

View File

@@ -12,7 +12,7 @@ import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* 菜单 创建请求对象
* 菜单 创建菜单请求对象
*
* @author Jiahang Li
* @version 1.0.0
@@ -22,8 +22,8 @@ import java.io.Serializable;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemMenuCreateRequest", description = "菜单 创建请求对象")
public class SystemMenuCreateParentRequest implements Serializable {
@Schema(name = "SystemMenuCreateMenuRequest", description = "菜单 创建菜单请求对象")
public class SystemMenuCreateValidMenuRequest implements Serializable {
@NotNull
@Schema(description = "父id")

View File

@@ -1,8 +1,10 @@
package com.orion.ops.module.infra.entity.request.menu;
import com.orion.ops.framework.common.entity.PageRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.Size;
@@ -17,53 +19,17 @@ import javax.validation.constraints.Size;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Schema(name = "SystemMenuQueryRequest", description = "菜单 查询请求对象")
public class SystemMenuQueryRequest extends PageRequest {
@Schema(description = "id")
private Long id;
@Schema(description = "父id")
private Long parentId;
public class SystemMenuQueryRequest {
@Size(max = 32)
@Schema(description = "菜单名称")
private String name;
@Size(max = 64)
@Schema(description = "菜单权限")
private String permission;
@Schema(description = "菜单类型 1父菜单 2子菜单 3功能")
private Integer type;
@Schema(description = "排序")
private Integer sort;
@Schema(description = "是否可见 0不可见 1可见")
private Integer visible;
@Schema(description = "菜单状态 0停用 1启用")
private Integer status;
@Schema(description = "菜单缓存 0不缓存 1缓存")
private Integer cache;
@Size(max = 64)
@Schema(description = "菜单图标")
private String icon;
@Size(max = 128)
@Schema(description = "路由地址")
private String path;
@Size(max = 64)
@Schema(description = "组件名称")
private String componentName;
@Size(max = 128)
@Schema(description = "组件地址")
private String component;
}

View File

@@ -6,9 +6,6 @@ import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
@@ -25,61 +22,36 @@ import java.io.Serializable;
@Schema(name = "SystemMenuUpdateRequest", description = "菜单 更新请求对象")
public class SystemMenuUpdateRequest implements Serializable {
@NotNull
@Schema(description = "id")
private Long id;
@NotNull
@Schema(description = "父id")
private Long parentId;
@Size(max = 32)
@NotBlank
@Schema(description = "菜单名称")
private String name;
@Size(max = 64)
@NotBlank
@Schema(description = "菜单权限")
private String permission;
@NotNull
@Schema(description = "菜单类型 1父菜单 2子菜单 3功能")
private Integer type;
@NotNull
@Schema(description = "排序")
private Integer sort;
@NotNull
@Schema(description = "是否可见 0不可见 1可见")
private Integer visible;
@NotNull
@Schema(description = "菜单状态 0停用 1启用")
private Integer status;
@NotNull
@Schema(description = "菜单缓存 0不缓存 1缓存")
private Integer cache;
@Size(max = 64)
@NotBlank
@Schema(description = "菜单图标")
private String icon;
@Size(max = 128)
@NotBlank
@Schema(description = "路由地址")
private String path;
@Size(max = 64)
@NotBlank
@Schema(description = "组件名称")
private String componentName;
@Size(max = 128)
@NotBlank
@Schema(description = "组件地址")
private String component;

View File

@@ -0,0 +1,33 @@
package com.orion.ops.module.infra.entity.request.menu;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
/**
* 菜单 更新状态对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-18 10:18
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemMenuUpdateStatusRequest", description = "菜单 更新状态对象")
public class SystemMenuUpdateStatusRequest {
@NotNull
@Schema(description = "id")
private Long id;
@NotNull
@Schema(description = "菜单状态 0停用 1启用")
private Integer status;
}

View File

@@ -7,7 +7,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* 菜单 视图响应对象
@@ -64,16 +64,8 @@ public class SystemMenuVO implements Serializable {
@Schema(description = "组件地址")
private String component;
@Schema(description = "创建时间")
private Date createTime;
@Schema(description = "子节点")
private List<SystemMenuVO> children;
@Schema(description = "修改时间")
private Date updateTime;
@Schema(description = "创建人")
private String creator;
@Schema(description = "修改人")
private String updater;
}

View File

@@ -28,4 +28,16 @@ public enum MenuStatusEnum {
private final Integer status;
public static MenuStatusEnum of(Integer status) {
if (status == null) {
return null;
}
for (MenuStatusEnum value : values()) {
if (value.status.equals(status)) {
return value;
}
}
return null;
}
}

View File

@@ -17,12 +17,12 @@ public enum MenuTypeEnum {
/**
* 父菜单
*/
PARENT(1),
PARENT_MENU(1),
/**
* 子菜单
*/
ITEM(2),
SUB_MENU(2),
/**
* 功能

View File

@@ -16,7 +16,7 @@ import java.util.Map;
public interface PermissionService {
/**
* 获取 菜单缓存
* 获取 角色缓存
*
* @return cache
*/

View File

@@ -1,10 +1,9 @@
package com.orion.ops.module.infra.service;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuCreateParentRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuCreateRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuQueryRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuUpdateRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuUpdateStatusRequest;
import com.orion.ops.module.infra.entity.vo.SystemMenuVO;
import java.util.List;
@@ -26,14 +25,6 @@ public interface SystemMenuService {
*/
Long createSystemMenu(SystemMenuCreateRequest request);
/**
* 创建菜单 父菜单
*
* @param request request
* @return id
*/
Long createParentMenu(SystemMenuCreateParentRequest request);
/**
* 通过 id 更新菜单
*
@@ -51,35 +42,27 @@ public interface SystemMenuService {
SystemMenuVO getSystemMenu(Long id);
/**
* 通过 id 批量查询菜单
*
* @param idList idList
* @return rows
*/
List<SystemMenuVO> getSystemMenuList(List<Long> idList);
/**
* 分页查询菜单
* 查询菜单
*
* @param request request
* @return rows
*/
DataGrid<SystemMenuVO> getSystemMenuPage(SystemMenuQueryRequest request);
List<SystemMenuVO> getSystemMenuList(SystemMenuQueryRequest request);
/**
* 通过 id 删除菜单
* 通过 id 级联更新菜单状态
*
* @param request request
* @return effect
*/
Integer updateSystemMenuStatus(SystemMenuUpdateStatusRequest request);
/**
* 通过 id 级联删除菜单
*
* @param id id
* @return effect
*/
Integer deleteSystemMenu(Long id);
/**
* 通过 id 批量删除菜单
*
* @param idList idList
* @return effect
*/
Integer batchDeleteSystemMenu(List<Long> idList);
}

View File

@@ -35,7 +35,7 @@ import java.util.stream.Collectors;
public class PermissionServiceImpl implements PermissionService {
/**
* 菜单缓存
* 角色缓存
*/
@Getter
private final Map<String, SystemRoleDO> roleCache = new HashMap<>();

View File

@@ -1,25 +1,35 @@
package com.orion.ops.module.infra.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.lang.utils.Strings;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.common.constant.Const;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.mybatis.core.query.Conditions;
import com.orion.ops.module.infra.convert.SystemMenuConvert;
import com.orion.ops.module.infra.dao.SystemMenuDAO;
import com.orion.ops.module.infra.entity.domain.SystemMenuDO;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuCreateParentRequest;
import com.orion.ops.module.infra.entity.dto.SystemMenuCacheDTO;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuCreateRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuQueryRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuUpdateRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemMenuUpdateStatusRequest;
import com.orion.ops.module.infra.entity.vo.SystemMenuVO;
import com.orion.ops.module.infra.enums.MenuStatusEnum;
import com.orion.ops.module.infra.enums.MenuTypeEnum;
import com.orion.ops.module.infra.service.PermissionService;
import com.orion.ops.module.infra.service.SystemMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 菜单 服务实现类
@@ -35,23 +45,22 @@ public class SystemMenuServiceImpl implements SystemMenuService {
@Resource
private SystemMenuDAO systemMenuDAO;
@Override
public Long createSystemMenu(SystemMenuCreateRequest request) {
// 转换
SystemMenuDO record = SystemMenuConvert.MAPPER.to(request);
record.setId(null);
// 查询数据是否冲突
this.checkSystemMenuPresent(record);
// 插入
int effect = systemMenuDAO.insert(record);
log.info("SystemMenuService-createSystemMenu effect: {}, record: {}", effect, JSON.toJSONString(record));
return record.getId();
}
@Resource
private PermissionService permissionService;
@Override
public Long createParentMenu(SystemMenuCreateParentRequest request) {
System.out.println(Valid.valid(request));
return 1L;
public Long createSystemMenu(SystemMenuCreateRequest request) {
// 验证参数
this.validateCreateRequest(request);
// 转换
SystemMenuDO record = SystemMenuConvert.MAPPER.to(request);
// 保存数据
int effect = systemMenuDAO.insert(record);
log.info("SystemMenuService-createSystemMenu effect: {}, record: {}", effect, JSON.toJSONString(record));
// 保存至缓存
List<SystemMenuCacheDTO> menuCache = permissionService.getMenuCache();
menuCache.add(SystemMenuConvert.MAPPER.toCache(record));
return record.getId();
}
@Override
@@ -60,13 +69,24 @@ public class SystemMenuServiceImpl implements SystemMenuService {
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
SystemMenuDO record = systemMenuDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 验证参数
this.validateCreateRequest(SystemMenuConvert.MAPPER.toCreateValidate(request));
// 转换
SystemMenuDO updateRecord = SystemMenuConvert.MAPPER.to(request);
// 查询数据是否冲突
this.checkSystemMenuPresent(updateRecord);
// 更新
int effect = systemMenuDAO.updateById(updateRecord);
log.info("SystemMenuService-updateSystemMenu effect: {}, updateRecord: {}", effect, JSON.toJSONString(updateRecord));
// 重新查询转换为缓存
SystemMenuCacheDTO cache = SystemMenuConvert.MAPPER.toCache(systemMenuDAO.selectById(id));
// 获取原始缓存
permissionService.getMenuCache()
.stream()
.filter(s -> s.getId().equals(id))
.findFirst()
.ifPresent(s -> {
// 覆盖属性 防止引用丢失
BeanUtils.copyProperties(cache, s);
});
return effect;
}
@@ -80,79 +100,161 @@ public class SystemMenuServiceImpl implements SystemMenuService {
}
@Override
public List<SystemMenuVO> getSystemMenuList(List<Long> idList) {
// 查询
List<SystemMenuDO> records = systemMenuDAO.selectBatchIds(idList);
if (records.isEmpty()) {
public List<SystemMenuVO> getSystemMenuList(SystemMenuQueryRequest request) {
String name = request.getName();
Integer type = request.getType();
Integer status = request.getStatus();
// 从缓存中查询
List<SystemMenuVO> menus = permissionService.getMenuCache()
.stream()
.filter(s -> Strings.isBlank(name) || s.getName().contains(name))
.filter(s -> type == null || s.getType().equals(type))
.filter(s -> status == null || s.getStatus().equals(status))
.map(SystemMenuConvert.MAPPER::to)
.collect(Collectors.toList());
if (menus.isEmpty()) {
return Lists.empty();
}
// 转换
return SystemMenuConvert.MAPPER.to(records);
// id 映射数据
Map<Long, SystemMenuVO> idMapping = menus.stream()
.collect(Collectors.toMap(SystemMenuVO::getId, Function.identity()));
// 寻找根节点
List<SystemMenuVO> rootNodes = menus.stream()
.filter(s -> idMapping.get(s.getParentId()) == null)
.collect(Collectors.toList());
// 子级节点数据
Map<Long, List<SystemMenuVO>> childrenNodesGroup = menus.stream()
.collect(Collectors.groupingBy(SystemMenuVO::getParentId));
// 设置子节点
this.setChildrenNodes(rootNodes, childrenNodesGroup);
return rootNodes;
}
private void setChildrenNodes(List<SystemMenuVO> parentNodes, Map<Long, List<SystemMenuVO>> childrenNodesGroup) {
// 为空则跳出
if (Lists.isEmpty(parentNodes)) {
return;
}
// 设置子节点
for (SystemMenuVO parentNode : parentNodes) {
List<SystemMenuVO> childrenNodes = childrenNodesGroup.get(parentNode.getId());
parentNode.setChildren(childrenNodes);
if (Lists.isEmpty(childrenNodes)) {
continue;
}
// 级联设置
this.setChildrenNodes(childrenNodes, childrenNodesGroup);
}
}
@Override
public DataGrid<SystemMenuVO> getSystemMenuPage(SystemMenuQueryRequest request) {
// 构造条件
LambdaQueryWrapper<SystemMenuDO> wrapper = systemMenuDAO.wrapper()
.eq(SystemMenuDO::getId, request.getId())
.eq(SystemMenuDO::getParentId, request.getParentId())
.eq(SystemMenuDO::getName, request.getName())
.eq(SystemMenuDO::getPermission, request.getPermission())
.eq(SystemMenuDO::getType, request.getType())
.eq(SystemMenuDO::getSort, request.getSort())
.eq(SystemMenuDO::getStatus, request.getStatus())
.eq(SystemMenuDO::getCache, request.getCache())
.eq(SystemMenuDO::getIcon, request.getIcon())
.eq(SystemMenuDO::getPath, request.getPath())
.eq(SystemMenuDO::getComponentName, request.getComponentName())
.eq(SystemMenuDO::getComponent, request.getComponent())
.orderByDesc(SystemMenuDO::getId);
public Integer updateSystemMenuStatus(SystemMenuUpdateStatusRequest request) {
Long id = request.getId();
Integer status = Valid.valid(MenuStatusEnum::of, request.getStatus()).getStatus();
// 查询
return systemMenuDAO.of()
.wrapper(wrapper)
.page(request)
.dataGrid(SystemMenuConvert.MAPPER::to);
SystemMenuDO record = systemMenuDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 从缓存中查询
List<SystemMenuCacheDTO> cache = permissionService.getMenuCache();
// 获取要删除的id
List<Long> updateIdList = this.getChildrenIdList(id, cache, record.getType());
// 修改引用缓存状态
cache.stream()
.filter(s -> updateIdList.contains(s.getId()))
.forEach(s -> s.setStatus(status));
// 修改状态
SystemMenuDO update = new SystemMenuDO();
update.setStatus(status);
int effect = systemMenuDAO.update(update, Conditions.id(SystemMenuDO::getId, updateIdList));
log.info("SystemMenuService-updateSystemMenuStatus updateIdList: {}, effect: {}", JSON.toJSONString(updateIdList), effect);
return effect;
}
@Override
public Integer deleteSystemMenu(Long id) {
int effect = systemMenuDAO.deleteById(id);
log.info("SystemMenuService-deleteSystemMenu id: {}, effect: {}", id, effect);
return effect;
}
@Override
public Integer batchDeleteSystemMenu(List<Long> idList) {
int effect = systemMenuDAO.deleteBatchIds(idList);
log.info("SystemMenuService-batchDeleteSystemMenu idList: {}, effect: {}", JSON.toJSONString(idList), effect);
// 查询
SystemMenuDO record = systemMenuDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 从缓存中查询
List<SystemMenuCacheDTO> cache = permissionService.getMenuCache();
// 获取要删除的id
List<Long> deletedIdList = this.getChildrenIdList(id, cache, record.getType());
// 删除菜单缓存
cache.removeIf(s -> deletedIdList.contains(s.getId()));
// 删除引用缓存
permissionService.getRoleMenuCache()
.values()
.forEach(roleMenus -> roleMenus.removeIf(s -> deletedIdList.contains(s.getId())));
// 删除
int effect = systemMenuDAO.deleteBatchIds(deletedIdList);
log.info("SystemMenuService-deleteSystemMenu deletedIdList: {}, effect: {}", JSON.toJSONString(deletedIdList), effect);
return effect;
}
/**
* 检测对象是否存在
* 获取子节点id集合
*
* @param domain domain
* @param id id
* @param cache cache
* @param type type
* @return childrenId
*/
private void checkSystemMenuPresent(SystemMenuDO domain) {
// 构造条件
LambdaQueryWrapper<SystemMenuDO> wrapper = systemMenuDAO.wrapper()
// 更新时忽略当前记录
.ne(SystemMenuDO::getId, domain.getId())
// 用其他字段做重复校验
.eq(SystemMenuDO::getParentId, domain.getParentId())
.eq(SystemMenuDO::getName, domain.getName())
.eq(SystemMenuDO::getPermission, domain.getPermission())
.eq(SystemMenuDO::getType, domain.getType())
.eq(SystemMenuDO::getSort, domain.getSort())
.eq(SystemMenuDO::getStatus, domain.getStatus())
.eq(SystemMenuDO::getCache, domain.getCache())
.eq(SystemMenuDO::getIcon, domain.getIcon())
.eq(SystemMenuDO::getPath, domain.getPath())
.eq(SystemMenuDO::getComponentName, domain.getComponentName())
.eq(SystemMenuDO::getComponent, domain.getComponent());
// 检查是否存在
boolean present = systemMenuDAO.of().wrapper(wrapper).present();
Valid.isFalse(present, ErrorMessage.DATA_PRESENT);
private List<Long> getChildrenIdList(Long id, List<SystemMenuCacheDTO> cache, Integer type) {
// 需要移除的菜单id
List<Long> idList = new ArrayList<>();
idList.add(id);
// 级联查询
if (MenuTypeEnum.PARENT_MENU.getType().equals(type)) {
// 查询子节点
List<Long> childrenIdList = cache.stream()
.filter(s -> s.getParentId().equals(id))
.map(SystemMenuCacheDTO::getId)
.collect(Collectors.toList());
idList.addAll(childrenIdList);
// 级联查询子节点
cache.stream()
.filter(s -> childrenIdList.contains(s.getParentId()))
.map(SystemMenuCacheDTO::getId)
.forEach(idList::add);
} else if (MenuTypeEnum.SUB_MENU.getType().equals(type)) {
// 查询子节点
cache.stream()
.filter(s -> s.getParentId().equals(id))
.map(SystemMenuCacheDTO::getId)
.forEach(idList::add);
}
return idList;
}
/**
* 验证创建菜单参数 不进行重复性校验
*
* @param request request
*/
private void validateCreateRequest(SystemMenuCreateRequest request) {
Long parentId = request.getParentId();
MenuTypeEnum type = Valid.valid(MenuTypeEnum::of, request.getType());
// 检查必填参数
if (MenuTypeEnum.PARENT_MENU.equals(type)) {
// 父菜单创建的 parentId 为 0
request.setParentId(Const.ROOT_MENU_ID);
// 验证必填参数
Valid.valid(SystemMenuConvert.MAPPER.toMenuValidate(request));
} else if (MenuTypeEnum.SUB_MENU.equals(type)) {
// 验证必填参数
Valid.valid(SystemMenuConvert.MAPPER.toMenuValidate(request));
// 检查 parentId 是否为为父菜单
SystemMenuDO parent = Valid.notNull(systemMenuDAO.selectById(parentId), ErrorMessage.PARENT_MENU_ABSENT);
Valid.eq(parent.getType(), MenuTypeEnum.PARENT_MENU.getType());
} else if (MenuTypeEnum.FUNCTION.equals(type)) {
// 验证必填参数
Valid.valid(SystemMenuConvert.MAPPER.toFunctionValidate(request));
// 检查 parentId 是否存在
Valid.notNull(systemMenuDAO.selectById(parentId), ErrorMessage.PARENT_MENU_ABSENT);
}
}
}