java 遍历组装树形
程序员文章站
2022-05-08 19:16:16
...
/**
* @title: GroupNodeTree
* @Author: 相思子
* @Date: 2021/8/10 12:04
* @Description:组装树形
*/
@Component
public class GroupNodeTree {
private List<SummarySchemeGroupNode> groupNodeList;
public GroupNodeTree(List<SummarySchemeGroupNode> groupNodeList) {
this.groupNodeList=groupNodeList;
}
//建立树形结构
public List<SummarySchemeGroupNode> buildTree(){
List<SummarySchemeGroupNode> treeGroups = new ArrayList<>();
for(SummarySchemeGroupNode groupNode : getRootNode()) {
groupNode=buildChildTree(groupNode);
treeGroups.add(groupNode);
}
return treeGroups;
}
//递归,建立子树形结构
private SummarySchemeGroupNode buildChildTree(SummarySchemeGroupNode pNode){
List<SummarySchemeGroupNode> chilNodes = new ArrayList<>();
for(SummarySchemeGroupNode groupNode : groupNodeList) {
if(groupNode.getParentId() != null && groupNode.getParentId().equals(pNode.getKey())) {
chilNodes.add(buildChildTree(groupNode));
}
}
pNode.setChildren(chilNodes);
return pNode;
}
//获取根节点
private List<SummarySchemeGroupNode> getRootNode() {
List<SummarySchemeGroupNode> rootGroupLists = groupNodeList.stream().filter(node -> StringUtils.isEmpty(node.getParentId())).sorted(Comparator.comparing(SummarySchemeGroupNode::getOrder)).collect(Collectors.toList());
return rootGroupLists;
}
/**
* 获取某个父节点下面的所有子节点
* @param nodeList 所有节点
* @param pid 父级节点
* @param set 汇总集合
* @return List<String>
*/
public List<String> treeChildAndSelfList(List<SummarySchemeGroupNode> nodeList, String pid, Set<String> set) {
for (SummarySchemeGroupNode node : nodeList) {
//遍历出父id等于参数的id,add进子节点集合
if (node.getParentId() !=null && node.getParentId().equals(pid)) {
//递归遍历下一级
treeChildAndSelfList(nodeList, node.getKey(), set);
set.add(node.getKey());
}
}
return new ArrayList<>(set);
}
}
@ApiModel(description = "汇总方案分组节点")
public class SummarySchemeGroupNode {
/**
* 节点id
*/
@ApiModelProperty(value = "节点id")
private String key;
/**
* 标题
*/
@ApiModelProperty(value = "标题")
private String title;
/**
* 排序
*/
@ApiModelProperty(value = "排序")
private String order;
/**
* 父节点
*/
@ApiModelProperty(value = "父节点id")
private String parentId;
/**
* 子节点
*/
@ApiModelProperty(value = "子节点")
private List<SummarySchemeGroupNode> children = new ArrayList<>();
/**
* 是否叶子结点
*/
@ApiModelProperty(value = "是否叶子结点")
private boolean isLeaf;
/**
* 任务key
*/
@ApiModelProperty(value = "taskKey")
private String taskKey;
}
@GetMapping("/query/tree/schemes")
@ApiOperation(value = "查询汇总方案树形")
public List<SummarySchemeGroupNode> querySchemesTree() throws Exception {
LogHelper.info("数据汇总模块","查询汇总方案树形","查询汇总方案树形");
List<SummarySchemeGroupNode> tree = new ArrayList<>();
try {
List<SummarySchemeGroupNode> summarySchemeGroupNodes = groupService.queryAllGroupNode();
List<SummarySchemeGroupNode> schemeGroupNodes = schemeService.queryAllScheme();
if(schemeGroupNodes != null){
summarySchemeGroupNodes.addAll(schemeGroupNodes);
}
if (summarySchemeGroupNodes != null) {
GroupNodeTree groupNodeTree =new GroupNodeTree(summarySchemeGroupNodes);
tree=groupNodeTree.buildTree();
}
return tree;
} catch (Exception e) {
logger.error("初始化汇总方案树形失败",e);
throw new JQException(SummarySchemeErrorEnum.SUMMARYSCHEME_EXCEPTION_003,"初始化汇总方案树形失败");
}
}