重构dept和menu构建树过程:封装构建过程到TreeBuilder类中简化代码,解耦实体对象和TreeSelect的强依赖。

This commit is contained in:
likun5 2022-05-02 21:00:47 +08:00
parent 77d5afdffd
commit 2e3c779e69
7 changed files with 151 additions and 164 deletions

View File

@ -0,0 +1,101 @@
package com.ruoyi.common.core.domain;
import com.ruoyi.common.utils.StringUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author likun5
*/
public class TreeBuilder<T extends TreeEntity>
{
/**
* 构建前端所需要下拉树结构
*
* @param treeNodes 部门列表
* @return 下拉树结构列表
*/
public List<TreeSelect> buildTreeSelect(List<T> treeNodes, Function<T, Long> getPkMethod, Function<T, TreeSelect> toTreeSelectMethod)
{
List<T> deptTrees = buildTree(treeNodes, getPkMethod);
return deptTrees.stream().map(toTreeSelectMethod).collect(Collectors.toList());
}
/**
* 构建前端所需要树结构
*
* @param treeNodes 部门列表
* @return 树结构列表
*/
public List<T> buildTree(List<T> treeNodes, Function<T, Long> getPkMethod)
{
List<T> returnList = new ArrayList<T>();
List<Long> tempList = new ArrayList<Long>();
for (T dept : treeNodes)
{
tempList.add(getPkMethod.apply(dept));
}
for (Iterator<T> iterator = treeNodes.iterator(); iterator.hasNext(); )
{
T dept = (T) iterator.next();
// 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.contains(dept.getParentId()))
{
recursionFn(treeNodes, dept, getPkMethod);
returnList.add(dept);
}
}
if (returnList.isEmpty())
{
returnList = treeNodes;
}
return returnList;
}
/**
* 递归列表
*/
public void recursionFn(List<T> list, T t, Function<T, Long> getPkMethod)
{
// 得到子节点列表
List<T> childList = getChildList(list, t, getPkMethod);
t.setChildren(childList);
for (T tChild : childList)
{
if (hasChild(list, tChild, getPkMethod))
{
recursionFn(list, tChild, getPkMethod);
}
}
}
/**
* 得到子节点列表
*/
private List<T> getChildList(List<T> list, T t, Function<T, Long> getPkMethod)
{
List<T> tlist = new ArrayList<T>();
Iterator<T> it = list.iterator();
while (it.hasNext())
{
T n = (T) it.next();
if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == getPkMethod.apply(t).longValue()) {
tlist.add(n);
}
}
return tlist;
}
/**
* 判断是否有子节点
*/
private boolean hasChild(List<T> list, T t, Function<T, Long> getPkMethod)
{
return getChildList(list, t, getPkMethod).size() > 0 ? true : false;
}
}

View File

@ -1,5 +1,6 @@
package com.ruoyi.common.core.domain;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@ -8,7 +9,7 @@ import java.util.List;
*
* @author ruoyi
*/
public class TreeEntity extends BaseEntity
public abstract class TreeEntity<T> extends BaseEntity implements Serializable
{
private static final long serialVersionUID = 1L;
@ -25,7 +26,7 @@ public class TreeEntity extends BaseEntity
private String ancestors;
/** 子部门 */
private List<?> children = new ArrayList<>();
private List<T> children = new ArrayList<>();
public String getParentName()
{
@ -67,13 +68,16 @@ public class TreeEntity extends BaseEntity
this.ancestors = ancestors;
}
public List<?> getChildren()
public List<T> getChildren()
{
return children;
}
public void setChildren(List<?> children)
public void setChildren(List<T> children)
{
this.children = children;
}
public abstract TreeSelect toTreeSelect(T entity);
}

View File

@ -1,11 +1,8 @@
package com.ruoyi.common.core.domain;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysMenu;
/**
* Treeselect树结构实体类
@ -31,20 +28,6 @@ public class TreeSelect implements Serializable
}
public TreeSelect(SysDept dept)
{
this.id = dept.getDeptId();
this.label = dept.getDeptName();
this.children = dept.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
}
public TreeSelect(SysMenu menu)
{
this.id = menu.getMenuId();
this.label = menu.getMenuName();
this.children = menu.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
}
public Long getId()
{
return id;

View File

@ -2,20 +2,23 @@ package com.ruoyi.common.core.domain.entity;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.ruoyi.common.core.domain.TreeEntity;
import com.ruoyi.common.core.domain.TreeSelect;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.core.domain.BaseEntity;
/**
* 部门表 sys_dept
*
* @author ruoyi
*/
public class SysDept extends BaseEntity
public class SysDept extends TreeEntity<SysDept>
{
private static final long serialVersionUID = 1L;
@ -181,6 +184,16 @@ public class SysDept extends BaseEntity
this.children = children;
}
@Override
public TreeSelect toTreeSelect(SysDept entity)
{
TreeSelect treeSelect = new TreeSelect();
treeSelect.setId(entity.getDeptId());
treeSelect.setLabel(entity.getDeptName());
treeSelect.setChildren(entity.getChildren().stream().map(o->o.toTreeSelect(o)).collect(Collectors.toList()));
return treeSelect;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)

View File

@ -2,9 +2,12 @@ package com.ruoyi.common.core.domain.entity;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.ruoyi.common.core.domain.TreeEntity;
import com.ruoyi.common.core.domain.TreeSelect;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.core.domain.BaseEntity;
@ -14,7 +17,7 @@ import com.ruoyi.common.core.domain.BaseEntity;
*
* @author ruoyi
*/
public class SysMenu extends BaseEntity
public class SysMenu extends TreeEntity<SysMenu>
{
private static final long serialVersionUID = 1L;
@ -232,7 +235,16 @@ public class SysMenu extends BaseEntity
{
this.children = children;
}
@Override
public TreeSelect toTreeSelect(SysMenu entity) {
TreeSelect treeSelect = new TreeSelect();
treeSelect.setId(entity.getMenuId());
treeSelect.setLabel(entity.getMenuName());
treeSelect.setChildren(entity.getChildren().stream().map(o->o.toTreeSelect(o)).collect(Collectors.toList()));
return treeSelect;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)

View File

@ -1,13 +1,11 @@
package com.ruoyi.system.service.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.TreeBuilder;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
@ -57,26 +55,8 @@ public class SysDeptServiceImpl implements ISysDeptService
@Override
public List<SysDept> buildDeptTree(List<SysDept> depts)
{
List<SysDept> returnList = new ArrayList<SysDept>();
List<Long> tempList = new ArrayList<Long>();
for (SysDept dept : depts)
{
tempList.add(dept.getDeptId());
}
for (SysDept dept : depts)
{
// 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.contains(dept.getParentId()))
{
recursionFn(depts, dept);
returnList.add(dept);
}
}
if (returnList.isEmpty())
{
returnList = depts;
}
return returnList;
TreeBuilder<SysDept> treeBuilder = new TreeBuilder<>();
return treeBuilder.buildTree(depts,SysDept::getDeptId);
}
/**
@ -88,8 +68,8 @@ public class SysDeptServiceImpl implements ISysDeptService
@Override
public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts)
{
List<SysDept> deptTrees = buildDeptTree(depts);
return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
TreeBuilder<SysDept> treeBuilder = new TreeBuilder<>();
return treeBuilder.buildTreeSelect(depts,SysDept::getDeptId,o->o.toTreeSelect(o));
}
/**
@ -284,46 +264,4 @@ public class SysDeptServiceImpl implements ISysDeptService
return deptMapper.deleteDeptById(deptId);
}
/**
* 递归列表
*/
private void recursionFn(List<SysDept> list, SysDept t)
{
// 得到子节点列表
List<SysDept> childList = getChildList(list, t);
t.setChildren(childList);
for (SysDept tChild : childList)
{
if (hasChild(list, tChild))
{
recursionFn(list, tChild);
}
}
}
/**
* 得到子节点列表
*/
private List<SysDept> getChildList(List<SysDept> list, SysDept t)
{
List<SysDept> tlist = new ArrayList<SysDept>();
Iterator<SysDept> it = list.iterator();
while (it.hasNext())
{
SysDept n = (SysDept) it.next();
if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue())
{
tlist.add(n);
}
}
return tlist;
}
/**
* 判断是否有子节点
*/
private boolean hasChild(List<SysDept> list, SysDept t)
{
return getChildList(list, t).size() > 0;
}
}

View File

@ -7,11 +7,11 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.TreeBuilder;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysRole;
@ -201,27 +201,8 @@ public class SysMenuServiceImpl implements ISysMenuService
@Override
public List<SysMenu> buildMenuTree(List<SysMenu> menus)
{
List<SysMenu> returnList = new ArrayList<SysMenu>();
List<Long> tempList = new ArrayList<Long>();
for (SysMenu dept : menus)
{
tempList.add(dept.getMenuId());
}
for (Iterator<SysMenu> iterator = menus.iterator(); iterator.hasNext();)
{
SysMenu menu = (SysMenu) iterator.next();
// 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.contains(menu.getParentId()))
{
recursionFn(menus, menu);
returnList.add(menu);
}
}
if (returnList.isEmpty())
{
returnList = menus;
}
return returnList;
TreeBuilder<SysMenu> treeBuilder =new TreeBuilder<>();
return treeBuilder.buildTree(menus,SysMenu::getMenuId);
}
/**
@ -233,8 +214,8 @@ public class SysMenuServiceImpl implements ISysMenuService
@Override
public List<TreeSelect> buildMenuTreeSelect(List<SysMenu> menus)
{
List<SysMenu> menuTrees = buildMenuTree(menus);
return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
TreeBuilder<SysMenu> treeBuilder = new TreeBuilder<>();
return treeBuilder.buildTreeSelect(menus, SysMenu::getMenuId, o->o.toTreeSelect(o));
}
/**
@ -441,6 +422,7 @@ public class SysMenuServiceImpl implements ISysMenuService
*/
public List<SysMenu> getChildPerms(List<SysMenu> list, int parentId)
{
TreeBuilder<SysMenu> builder = new TreeBuilder<>();
List<SysMenu> returnList = new ArrayList<SysMenu>();
for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext();)
{
@ -448,59 +430,13 @@ public class SysMenuServiceImpl implements ISysMenuService
// 根据传入的某个父节点ID,遍历该父节点的所有子节点
if (t.getParentId() == parentId)
{
recursionFn(list, t);
builder.recursionFn(list, t, SysMenu::getMenuId);
returnList.add(t);
}
}
return returnList;
}
/**
* 递归列表
*
* @param list
* @param t
*/
private void recursionFn(List<SysMenu> list, SysMenu t)
{
// 得到子节点列表
List<SysMenu> childList = getChildList(list, t);
t.setChildren(childList);
for (SysMenu tChild : childList)
{
if (hasChild(list, tChild))
{
recursionFn(list, tChild);
}
}
}
/**
* 得到子节点列表
*/
private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t)
{
List<SysMenu> tlist = new ArrayList<SysMenu>();
Iterator<SysMenu> it = list.iterator();
while (it.hasNext())
{
SysMenu n = (SysMenu) it.next();
if (n.getParentId().longValue() == t.getMenuId().longValue())
{
tlist.add(n);
}
}
return tlist;
}
/**
* 判断是否有子节点
*/
private boolean hasChild(List<SysMenu> list, SysMenu t)
{
return getChildList(list, t).size() > 0;
}
/**
* 内链域名特殊字符替换
*