Newer
Older
huludao / src / main / java / com / newfiber / api / config / CameraNodeTree.java
package com.newfiber.api.config;

import com.newfiber.api.pc.model.hikvsion.RegionStation;
import org.springframework.util.StringUtils;

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

/**
 * @Author:zzh
 * @CreateDate:2020/11/19 16:48
 * @Description:
 */
public class CameraNodeTree {
    private List<RegionStation> menuList = new ArrayList<>();

    public CameraNodeTree(List<RegionStation> menuList) {
        this.menuList = menuList;
    }

    /**
     * 递归构建子树型结构
     * @return
     */
    private RegionStation buildChilTree(RegionStation projectNode){
        List<RegionStation> childNodes = new ArrayList<>();
        for(RegionStation node : menuList){
            if(StringUtils.isEmpty(node.getRegionIndexCode())){
                continue;
            }
            if(node.getParentIndexCode().equals(projectNode.getRegionIndexCode())){
                childNodes.add(buildChilTree(node));
            }
        }
        projectNode.setChildern(childNodes);
        return projectNode;
    }

    /**
     * 构建树结构
     * @return
     */
    public List<RegionStation> buildTree(){
        List<RegionStation> treeNodes = new ArrayList<>();
        for(RegionStation node : getRootNodes()){
            RegionStation projectNode = buildChilTree(node);
            treeNodes.add(projectNode);
        }
        return treeNodes;
    }

    /**
     * 获取根节点
     * @return
     */
    private List<RegionStation> getRootNodes(){
        List<RegionStation> rootTreeNodes = new ArrayList<>();
        for(RegionStation node :menuList){
            //如果为0  就表示为根节点
            //因为可能会有脏数据所以要判断一下 然后去除
            if(StringUtils.isEmpty(node.getRegionIndexCode())){
                continue;
            }
            if(node.getParentIndexCode().equals("-1")){
                rootTreeNodes.add(node);
            }
        }
        return rootTreeNodes;
    }
}