TreeBuilder.java 3.81 KB
package com.lhcredit.project.business.apiDataService.common;

import com.lhcredit.project.business.apiDataService.domain.ApiDataService;
import com.lhcredit.project.business.apiDataService.domain.ApiDataTreeNode;
import com.lhcredit.project.business.zareaInfo.domain.ZareaInfo;
import com.lhcredit.project.business.zindustryInfo.domain.ZindustryInfo;

import java.util.*;
import java.util.stream.Collectors;

public class TreeBuilder {

    public static List<ApiDataTreeNode> buildTree(List<ApiDataService> flatList) {
        Map<Integer, ApiDataTreeNode> nodeMap = new HashMap<>();
        flatList.forEach(data -> nodeMap.put(data.getId(), new ApiDataTreeNode(data)));

        List<ApiDataTreeNode> rootNodes = new ArrayList<>();

        for (ApiDataService data : flatList) {
            ApiDataTreeNode node = nodeMap.get(data.getId());
            if (data.getParentId() == null || data.getParentId() == 0) {
                rootNodes.add(node);
            } else {
                ApiDataTreeNode parent = nodeMap.get(data.getParentId());
                if (parent != null) {
                    parent.addChild(node);
                }
            }
        }

        return rootNodes;
    }

    /**
     * 构建树形结构
     * @param dataList 所有地区数据
     * @return 根节点列表(省级)
     */
    public static List<ZareaInfo> buildAreaTree(List<ZareaInfo> dataList) {
        if (dataList == null || dataList.isEmpty()) {
            return null;
        }
        List<ZareaInfo> collect20 = dataList.stream().filter(item -> Objects.equals(item.getCType(),"20") && item.getParentId() != -1).collect(Collectors.toList());
        List<ZareaInfo> collect30 = dataList.stream().filter(item -> Objects.equals(item.getCType(),"30")).collect(Collectors.toList());
        List<ZareaInfo> collect40 = dataList.stream().filter(item -> Objects.equals(item.getCType(),"40")).collect(Collectors.toList());

        collect20.forEach(item -> {
            List<ZareaInfo> collect = collect30.stream().filter(item30 -> item30.getParentId().equals(item.getCId())).collect(Collectors.toList());
            item.setChildren(collect);
        });

        collect30.forEach(item -> {
            List<ZareaInfo> collect = collect40.stream().filter(item40 -> item40.getParentId().equals(item.getCId())).collect(Collectors.toList());
            item.setChildren(collect);
        });

        return collect20;
    }

    // 打印树结构(可选)
    public static void printTree(List<ApiDataTreeNode> nodes, int depth) {
        for (ApiDataTreeNode node : nodes) {
            for (int i = 0; i < depth; i++) {
                System.out.print("--");
            }
            System.out.println(node.getDataName());
            printTree(node.getChildren(), depth + 1);
        }
    }

    public static List<ZindustryInfo> buildIndustryTree(List<ZindustryInfo> industryType) {
        if (industryType == null || industryType.isEmpty()) {
            return null;
        }
       // List<ZindustryInfo> garade1 = industryType.stream().filter(item -> item.getGarade() == 1).collect(Collectors.toList());
        List<ZindustryInfo> garade2 = industryType.stream().filter(item -> item.getGarade() == 2).collect(Collectors.toList());
        List<ZindustryInfo> garade3 = industryType.stream().filter(item -> item.getGarade() == 3).collect(Collectors.toList());

        /*garade1.forEach(item -> {
            List<ZindustryInfo> collect = garade2.stream().filter(item2 -> item2.getParentId().equals(item.getId())).collect(Collectors.toList());
            item.setChildren(collect);
        });*/

        garade2.forEach(item -> {
            List<ZindustryInfo> collect = garade3.stream().filter(item3 -> item3.getParentId().equals(item.getId())).collect(Collectors.toList());
            item.setChildren(collect);
        });

        return garade2;
    }
}