package com.dcsoft.system.utils;

import com.dcsoft.system.ladder.domain.DeviceTreeVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class TreeUtil {

    public static List<DeviceTreeVo> buildTree(List<DeviceTreeVo> list, String pid) {
        List<DeviceTreeVo> tree = new ArrayList<>();
        for (DeviceTreeVo node : list) {
            if (Objects.equals(node.getParentId(), pid)) {
                tree.add(findChild(node, list));
            }
        }
        return tree;
    }

    public static DeviceTreeVo findChild(DeviceTreeVo node, List<DeviceTreeVo> list) {
        for (DeviceTreeVo n : list) {
            if (Objects.equals(n.getParentId(), node.getId())) {
                if (node.getChildren() == null) {
                    node.setChildren(new ArrayList<>());
                }
                node.getChildren().add(findChild(n, list));
            }
        }
        return node;
    }
}
