Browse Source

登录用户菜单修复

tags/24082201
WendyYang 1 year ago
parent
commit
198ba0f1b2
3 changed files with 47 additions and 6 deletions
  1. +37
    -2
      pmapi/src/main/java/com/ningdatech/pmapi/sys/manage/MenuManage.java
  2. +1
    -1
      pmapi/src/main/java/com/ningdatech/pmapi/sys/model/entity/RoleMenu.java
  3. +9
    -3
      pmapi/src/main/java/com/ningdatech/pmapi/sys/service/impl/RoleServiceImpl.java

+ 37
- 2
pmapi/src/main/java/com/ningdatech/pmapi/sys/manage/MenuManage.java View File

@@ -2,13 +2,17 @@ package com.ningdatech.pmapi.sys.manage;

import cn.hutool.core.bean.BeanUtil;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.common.constant.BizConst;
import com.ningdatech.pmapi.common.model.entity.MenuTreeEntity;
import com.ningdatech.pmapi.common.util.TreeUtil;
import com.ningdatech.pmapi.sys.model.entity.Menu;
import com.ningdatech.pmapi.sys.model.vo.MenuRoleVO;
import com.ningdatech.pmapi.sys.service.IMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

/**
* <p>
@@ -22,9 +26,40 @@ import java.util.List;
@RequiredArgsConstructor
public class MenuManage {

private final IMenuService menuService;

public List<MenuRoleVO> buildUserMenu(List<Menu> list) {
List<MenuRoleVO> menuRoles = CollUtils.convert(list, w -> BeanUtil.copyProperties(w, MenuRoleVO.class));
Set<Long> menuIds = new HashSet<>();
List<MenuRoleVO> menuRoles = CollUtils.convert(list, w -> {
menuIds.add(w.getId());
return BeanUtil.copyProperties(w, MenuRoleVO.class);
});
List<Long> pidList = menuRoles.stream()
.map(MenuTreeEntity::getPid)
.filter(pid -> !menuIds.contains(pid) && pid != BizConst.PARENT_ID)
.collect(Collectors.toList());
parentMenuCheck(pidList, menuRoles, menuIds);
return TreeUtil.buildUserTree(menuRoles);
}

public void parentMenuCheck(List<Long> pidList, List<MenuRoleVO> menuRoles, Set<Long> menuIds) {
List<Menu> menus = menuService.listByIds(pidList);
if (menus.isEmpty()) {
return;
}
Set<Long> tmpSet = new HashSet<>();
menus.forEach(w -> {
menuIds.add(w.getId());
tmpSet.add(w.getPid());
menuRoles.add(BeanUtil.copyProperties(w, MenuRoleVO.class));
});
List<Long> tmpPidList = tmpSet.stream()
.filter(pid -> !menuIds.contains(pid) && pid != BizConst.PARENT_ID)
.collect(Collectors.toList());
if (tmpPidList.isEmpty()) {
return;
}
parentMenuCheck(tmpPidList, menuRoles, menuIds);
}

}

+ 1
- 1
pmapi/src/main/java/com/ningdatech/pmapi/sys/model/entity/RoleMenu.java View File

@@ -21,7 +21,7 @@ import java.io.Serializable;
*/
@TableName("nd_role_menu")
@Data
@ApiModel(value = "RoleMenu对象", description = "")
@ApiModel(value = "RoleMenu对象")
@AllArgsConstructor
@NoArgsConstructor
public class RoleMenu implements Serializable {


+ 9
- 3
pmapi/src/main/java/com/ningdatech/pmapi/sys/service/impl/RoleServiceImpl.java View File

@@ -178,6 +178,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR
roleMenuService.remove(Wrappers.lambdaQuery(RoleMenu.class).eq(RoleMenu::getRoleId, roleId));
if (CollUtil.isNotEmpty(menuIds)) {
Set<RoleMenu> toAddMenus = new HashSet<>();
// Set<Long> addParentIds = new HashSet<>();
for (Long menuId : menuIds) {
Menu menu = menuService.getById(menuId);
if (Objects.isNull(menu)) {
@@ -186,7 +187,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR
RoleMenu roleMenu = new RoleMenu(null, menuId, roleId);
toAddMenus.add(roleMenu);
// 父级菜单
// addParent(toAddMenus,menu,roleId);
// addParent(toAddMenus, menu, roleId, addParentIds);
}
if (CollUtil.isNotEmpty(toAddMenus)) {
roleMenuService.saveBatch(toAddMenus);
@@ -194,16 +195,21 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR
}
}

private void addParent(Set<RoleMenu> toAddMenus, Menu menu, Long roleId) {
private void addParent(Set<RoleMenu> toAddMenus, Menu menu, Long roleId, Set<Long> addParentIds) {
if (Objects.isNull(menu.getPid()) || menu.getPid().equals(0L)) {
return;
}
if (addParentIds.contains(menu.getPid())) {
return;
}
Menu parent = menuService.getById(menu.getPid());
if (Objects.isNull(parent)) {
return;
}
addParentIds.add(menu.getPid());
RoleMenu roleMenu = new RoleMenu(null, parent.getId(), roleId);
toAddMenus.add(roleMenu);
addParent(toAddMenus, parent, roleId);
addParent(toAddMenus, parent, roleId, addParentIds);
}

}

Loading…
Cancel
Save