feat(portal): 构建官网菜单树

- 在 HwWebMenu 模型中添加 children 属性,用于存储子菜单
- 实现 HwWebMenuServiceImpl 中的 buildWebMenuTree 方法,用于构建菜单树
- 添加 recursionFn、getChildList 和 hasChild 等辅助方法,用于递归构建菜单树
master
zangch@mesnac.com 4 months ago
parent 96320bd1f4
commit d433d0660d

@ -5,6 +5,8 @@ import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.core.annotation.Excel; import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.web.domain.BaseEntity; import com.ruoyi.common.core.web.domain.BaseEntity;
import java.util.List;
/** /**
* haiwei hw_web_menu * haiwei hw_web_menu
* *
@ -42,6 +44,8 @@ public class HwWebMenu extends BaseEntity
@Excel(name = "图片地址") @Excel(name = "图片地址")
private String webMenuPic; private String webMenuPic;
private List<HwWebMenu> children;
public void setWebMenuId(Long webMenuId) public void setWebMenuId(Long webMenuId)
{ {
this.webMenuId = webMenuId; this.webMenuId = webMenuId;
@ -106,6 +110,14 @@ public class HwWebMenu extends BaseEntity
return webMenuPic; return webMenuPic;
} }
public List<HwWebMenu> getChildren() {
return children;
}
public void setChildren(List<HwWebMenu> children) {
this.children = children;
}
@Override @Override
public String toString() { public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)

@ -1,11 +1,15 @@
package com.ruoyi.portal.service.impl; package com.ruoyi.portal.service.impl;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.ruoyi.portal.mapper.HwWebMenuMapper; import com.ruoyi.portal.mapper.HwWebMenuMapper;
import com.ruoyi.portal.domain.HwWebMenu; import com.ruoyi.portal.domain.HwWebMenu;
import com.ruoyi.portal.service.IHwWebMenuService; import com.ruoyi.portal.service.IHwWebMenuService;
import com.ruoyi.common.core.utils.StringUtils;
/** /**
* haiweiService * haiweiService
@ -90,4 +94,62 @@ public class HwWebMenuServiceImpl implements IHwWebMenuService
{ {
return hwWebMenuMapper.deleteHwWebMenuByWebMenuId(webMenuId); return hwWebMenuMapper.deleteHwWebMenuByWebMenuId(webMenuId);
} }
/**
*
*
* @param menus
* @return
*/
public List<HwWebMenu> buildWebMenuTree(List<HwWebMenu> menus) {
List<HwWebMenu> returnList = new ArrayList<>();
List<Long> tempList = menus.stream().map(HwWebMenu::getWebMenuId).collect(Collectors.toList());
for (HwWebMenu menu : menus) {
// 如果是顶级节点(parent为null、0或者不在当前列表中), 遍历该父节点的所有子节点
if (menu.getParent() == null || menu.getParent() == 0L || !tempList.contains(menu.getParent())) {
recursionFn(menus, menu);
returnList.add(menu);
}
}
if (returnList.isEmpty()) {
returnList = menus;
}
return returnList;
}
/**
*
*/
private void recursionFn(List<HwWebMenu> list, HwWebMenu t) {
// 得到子节点列表
List<HwWebMenu> childList = getChildList(list, t);
t.setChildren(childList);
for (HwWebMenu tChild : childList) {
if (hasChild(list, tChild)) {
recursionFn(list, tChild);
}
}
}
/**
*
*/
private List<HwWebMenu> getChildList(List<HwWebMenu> list, HwWebMenu t) {
List<HwWebMenu> tlist = new ArrayList<HwWebMenu>();
Iterator<HwWebMenu> it = list.iterator();
while (it.hasNext()) {
HwWebMenu n = it.next();
if (StringUtils.isNotNull(n.getParent()) && n.getParent().longValue() == t.getWebMenuId().longValue()) {
tlist.add(n);
}
}
return tlist;
}
/**
*
*/
private boolean hasChild(List<HwWebMenu> list, HwWebMenu t) {
return !getChildList(list, t).isEmpty();
}
} }

Loading…
Cancel
Save