重构dept和menu构建树过程:封装构建过程到TreeBuilder类中简化代码,解耦实体对象和TreeSelect的强依赖。
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user