@@ -2,13 +2,17 @@ package com.ningdatech.pmapi.sys.manage; | |||||
import cn.hutool.core.bean.BeanUtil; | import cn.hutool.core.bean.BeanUtil; | ||||
import com.ningdatech.basic.util.CollUtils; | 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.common.util.TreeUtil; | ||||
import com.ningdatech.pmapi.sys.model.entity.Menu; | import com.ningdatech.pmapi.sys.model.entity.Menu; | ||||
import com.ningdatech.pmapi.sys.model.vo.MenuRoleVO; | import com.ningdatech.pmapi.sys.model.vo.MenuRoleVO; | ||||
import com.ningdatech.pmapi.sys.service.IMenuService; | |||||
import lombok.RequiredArgsConstructor; | import lombok.RequiredArgsConstructor; | ||||
import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||
import java.util.List; | |||||
import java.util.*; | |||||
import java.util.stream.Collectors; | |||||
/** | /** | ||||
* <p> | * <p> | ||||
@@ -22,9 +26,40 @@ import java.util.List; | |||||
@RequiredArgsConstructor | @RequiredArgsConstructor | ||||
public class MenuManage { | public class MenuManage { | ||||
private final IMenuService menuService; | |||||
public List<MenuRoleVO> buildUserMenu(List<Menu> list) { | 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); | 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); | |||||
} | |||||
} | } |
@@ -21,7 +21,7 @@ import java.io.Serializable; | |||||
*/ | */ | ||||
@TableName("nd_role_menu") | @TableName("nd_role_menu") | ||||
@Data | @Data | ||||
@ApiModel(value = "RoleMenu对象", description = "") | |||||
@ApiModel(value = "RoleMenu对象") | |||||
@AllArgsConstructor | @AllArgsConstructor | ||||
@NoArgsConstructor | @NoArgsConstructor | ||||
public class RoleMenu implements Serializable { | public class RoleMenu implements Serializable { | ||||
@@ -178,6 +178,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR | |||||
roleMenuService.remove(Wrappers.lambdaQuery(RoleMenu.class).eq(RoleMenu::getRoleId, roleId)); | roleMenuService.remove(Wrappers.lambdaQuery(RoleMenu.class).eq(RoleMenu::getRoleId, roleId)); | ||||
if (CollUtil.isNotEmpty(menuIds)) { | if (CollUtil.isNotEmpty(menuIds)) { | ||||
Set<RoleMenu> toAddMenus = new HashSet<>(); | Set<RoleMenu> toAddMenus = new HashSet<>(); | ||||
// Set<Long> addParentIds = new HashSet<>(); | |||||
for (Long menuId : menuIds) { | for (Long menuId : menuIds) { | ||||
Menu menu = menuService.getById(menuId); | Menu menu = menuService.getById(menuId); | ||||
if (Objects.isNull(menu)) { | if (Objects.isNull(menu)) { | ||||
@@ -186,7 +187,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR | |||||
RoleMenu roleMenu = new RoleMenu(null, menuId, roleId); | RoleMenu roleMenu = new RoleMenu(null, menuId, roleId); | ||||
toAddMenus.add(roleMenu); | toAddMenus.add(roleMenu); | ||||
// 父级菜单 | // 父级菜单 | ||||
// addParent(toAddMenus,menu,roleId); | |||||
// addParent(toAddMenus, menu, roleId, addParentIds); | |||||
} | } | ||||
if (CollUtil.isNotEmpty(toAddMenus)) { | if (CollUtil.isNotEmpty(toAddMenus)) { | ||||
roleMenuService.saveBatch(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)) { | if (Objects.isNull(menu.getPid()) || menu.getPid().equals(0L)) { | ||||
return; | return; | ||||
} | } | ||||
if (addParentIds.contains(menu.getPid())) { | |||||
return; | |||||
} | |||||
Menu parent = menuService.getById(menu.getPid()); | Menu parent = menuService.getById(menu.getPid()); | ||||
if (Objects.isNull(parent)) { | if (Objects.isNull(parent)) { | ||||
return; | return; | ||||
} | } | ||||
addParentIds.add(menu.getPid()); | |||||
RoleMenu roleMenu = new RoleMenu(null, parent.getId(), roleId); | RoleMenu roleMenu = new RoleMenu(null, parent.getId(), roleId); | ||||
toAddMenus.add(roleMenu); | toAddMenus.add(roleMenu); | ||||
addParent(toAddMenus, parent, roleId); | |||||
addParent(toAddMenus, parent, roleId, addParentIds); | |||||
} | } | ||||
} | } |