AlarmDFS.java 2.4 KB
package com.skua.modules.entity;

import com.alibaba.fastjson.JSONObject;
import com.skua.modules.vo.AlarmVO;
import com.skua.tool.util.BeanExtUtils;
import org.apache.commons.lang.StringUtils;

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

/**
 * <pre>
 * <请输入描述信息>
 * </pre>
 *
 * @author sonin
 * @version 1.0 2023/4/10 10:17
 */
public class AlarmDFS {

    private LinkedList<LinkedList<Alarm>> routeList = new LinkedList<>();

    private void backtrack(List<AlarmVO> tree, LinkedList<AlarmVO> track) {
        if (!track.isEmpty() && track.getLast().getChildren().isEmpty()) {
            List<Alarm> route;
            try {
                route = BeanExtUtils.beans2Beans(track, Alarm.class);
            } catch (Exception e) {
                route = new ArrayList<>();
            }
            routeList.add(new LinkedList<>(route));
        }
        for (AlarmVO alarmVO : tree) {
            track.addLast(alarmVO);
            backtrack(alarmVO.getChildren(), track);
            track.removeLast();
        }
    }

    public List<AlarmVO> buildTree(List<Alarm> equipCategoryList) {
        List<AlarmVO> tree = new ArrayList<>();
        List<AlarmVO> alarmVOList;
        try {
            alarmVOList = BeanExtUtils.beans2Beans(equipCategoryList, AlarmVO.class);
        } catch (Exception e) {
            alarmVOList = new ArrayList<>();
        }
        for (AlarmVO k : alarmVOList) {
            for (AlarmVO v : alarmVOList) {
                if (k.getId().equals(v.getParentId())) {
                    k.getChildren().add(v);
                }
            }
            // 先按照departOrder排序,再按照departName排序
            k.getChildren().sort((o1, o2) -> {
                if (o1.getDepartOrder() == null || o2.getDepartOrder() == null) {
                    return o1.getDepartName().compareTo(o2.getDepartName());
                } else {
                    if (o1.getDepartOrder().equals(o2.getDepartOrder())) {
                        return o1.getDepartName().compareTo(o2.getDepartName());
                    } else {
                        return o1.getDepartOrder().compareTo(o2.getDepartOrder());
                    }
                }
            });
            // 提取出父节点
            if (StringUtils.isBlank(k.getParentId()) || "0".equals(k.getParentId()) || "root".equals(k.getParentId())) {
                tree.add(k);
            }
        }
        return tree;
    }

}