Newer
Older
huludao / src / main / java / com / newfiber / api / pc / model / entity / CameraGroupInfoTree.java
package com.newfiber.api.pc.model.entity;

import com.baomidou.mybatisplus.annotations.TableName;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @描述:自定义视频分组-分组表
 * @作者: luzhan
 * @日期: 2022-04-06
 */
@ApiModel(value = "自定义视频分组-分组表")
@TableName("camera_group_info")
@Data
public class CameraGroupInfoTree implements Serializable {

    private List<CameraGroupInfo> info = new ArrayList<>();

    public CameraGroupInfoTree(List<CameraGroupInfo> info) {
        this.info = info;
    }

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

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

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