@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;
}
public List<String> treeChildAndSelfList(List<SummarySchemeGroupNode> nodeList, String pid, Set<String> set) {
for (SummarySchemeGroupNode node : nodeList) {
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 {
@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;
@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,"初始化汇总方案树形失败");
}
}