定义基本树结构
package com.un.common.utils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.List;
@ApiModel("社区结构树")
public class StructTree {
@ApiModelProperty("社区结构id")
private String csId;
@ApiModelProperty("社区id")
private String csCoId;
@ApiModelProperty("社区结构parentId")
private String csParentId;
@ApiModelProperty("社区结构名称")
private String csName;
@ApiModelProperty("社区结构全名称")
private String csFullName;
@ApiModelProperty("是否为最后一级,1是 0否")
private Integer isLast;
@ApiModelProperty("子树")
List structTreeList;
public String getCsFullName() {
return csFullName;
}
public void setCsFullName(String csFullName) {
this.csFullName = csFullName;
}
public String getCsCoId() {
return csCoId;
}
public void setCsCoId(String csCoId) {
this.csCoId = csCoId;
}
public String getCsName() {
return csName;
}
public void setCsName(String csName) {
this.csName = csName;
}
public String getCsId() {
return csId;
}
public void setCsId(String csId) {
this.csId = csId;
}
public String getCsParentId() {
return csParentId;
}
public void setCsParentId(String csParentId) {
this.csParentId = csParentId;
}
public Integer getIsLast() {
return isLast;
}
public void setIsLast(Integer isLast) {
this.isLast = isLast;
}
public List getStructTreeList() {
return structTreeList;
}
public void setStructTreeList(List structTreeList) {
this.structTreeList = structTreeList;
}
@Override
public String toString() {
return "StructTree{" +
"csId="" + csId + """ +
", csCoId="" + csCoId + """ +
", csParentId="" + csParentId + """ +
", csName="" + csName + """ +
", csFullName="" + csFullName + """ +
", isLast=" + isLast +
", struceTreeList=" + structTreeList +
"}";
}
}
定义工具类:
package com.un.common.utils;
import com.unwulian.communitystruct.domain.StructTree;
import java.util.ArrayList;
import java.util.List;
public class CsTreeNodeUtils {
public static List buildByRecursive(List treeNodes) {
List trees = new ArrayList();
for (StructTree treeNode : treeNodes) {
if ("CS0".equals(treeNode.getCsParentId())) {
trees.add(findChildren(treeNode, treeNodes));
}
}
return trees;
}
public static com.unwulian.communitystruct.domain.StructTree findChildren(StructTree treeNode, List treeNodes) {
for (StructTree it : treeNodes) {
if (treeNode.getCsId().equals(it.getCsParentId())) {
if (treeNode.getStructTreeList() == null) {
treeNode.setStructTreeList(new ArrayList());
}
treeNode.getStructTreeList().add(findChildren(it, treeNodes));
}
}
return treeNode;
}
public static StructTree getTreeNode(String csId, List treeList) {
for (StructTree structTree : treeList) {
if (csId.equals(structTree.getCsId())) {
return structTree;
} else {
List tempStructTreeList = structTree.getStructTreeList();
if(tempStructTreeList != null){
StructTree treeNode = getTreeNode(csId, tempStructTreeList);
if (treeNode != null) {
return treeNode;
}
}
}
}
return null;
}
}
测试方式
//把普通的list结构转换成树节点
List treeList = CsTreeNodeUtils.buildByRecursive(structTreeList);
//根据节点中的摸一个节点获取当前节点以及所有子节点
StructTree treeNode = CsTreeNodeUtils.getTreeNode(csId, treeList);
//跟新选中节点以及其下所有子节点
public void updateCsFullName(List treeNodeList, StructTree parentNode) {
if (CollectionUtils.isEmpty(treeNodeList)) {
return;
}
for (StructTree structTree : treeNodeList) {
String csFullName = parentNode.getCsFullName() + structTree.getCsName();
TBasCommunitystruct communitystruct = new TBasCommunitystruct();
communitystruct.setCsId(structTree.getCsId());
communitystruct.setCsFullname(csFullName);
tBasCommunitystructMapper.updateTBasCommunitystruct(communitystruct);
//当前节点的csFullName也跟新掉
structTree.setCsFullName(csFullName);
if (!CollectionUtils.isEmpty(structTree.getStructTreeList())) {
//自旋跟新子数据节点
updateCsFullName(structTree.getStructTreeList(), structTree);
}
}
}