zhangherong
3 天以前 8904f9e6005e7e1f3cc06f415fdcde0033c32332
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package org.jeecg.modules.eam.tree;
 
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.eam.vo.EamEquipmentTree;
import org.jeecg.modules.system.entity.MdcProduction;
import org.jeecg.modules.system.model.MdcProductionTreeModel;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 产线设备树
 * @author: LiuS
 * @create: 2023-04-03 15:59
 */
public class FindsEquipmentProductionUtil {
 
    /**
     * 获取 MdcEquipmentTree
     */
    public static List<EamEquipmentTree> wrapEquipmentProductionTreeList(List<MdcProduction> recordList) {
        List<EamEquipmentTree> idList = new ArrayList<>();
        List<MdcProductionTreeModel> records = new ArrayList<>();
        for (MdcProduction mdcProduction : recordList) {
            records.add(new MdcProductionTreeModel(mdcProduction));
        }
        getChildren(records, idList);
        setEmptyChildrenAsNull(idList);
        return idList;
    }
 
    /**
     * 该方法是找到并封装顶级父类的节点到TreeList集合
     */
    private static void getChildren(List<MdcProductionTreeModel> recordList, List<EamEquipmentTree> idList) {
        List<MdcProductionTreeModel> treeList = new ArrayList<>();
        for (MdcProductionTreeModel mdcProductionTreeModel : recordList) {
            if (oConvertUtils.isEmpty(mdcProductionTreeModel.getParentId())) {
//                mdcProductionTreeModel.setType(1);
                treeList.add(mdcProductionTreeModel);
                idList.add(new EamEquipmentTree().convertByProduction(mdcProductionTreeModel));
            }
        }
        getGrandChildren(treeList, recordList, idList);
    }
 
    /**
     *该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合
     */
    private static void getGrandChildren(List<MdcProductionTreeModel> treeList, List<MdcProductionTreeModel> recordList, List<EamEquipmentTree> idList) {
        for (int i = 0; i < treeList.size(); i++) {
            MdcProductionTreeModel model = treeList.get(i);
            EamEquipmentTree mdcEquipmentTree = idList.get(i);
            for (int i1 = 0; i1 < recordList.size(); i1++) {
                MdcProductionTreeModel m = recordList.get(i1);
                if (m.getParentId() != null && m.getParentId().equals(model.getId())) {
                    model.getChildren().add(m);
//                    m.setType(1);
                    mdcEquipmentTree.getChildren().add(new EamEquipmentTree().convertByProduction(m));
                }
            }
            getGrandChildren(treeList.get(i).getChildren(), recordList, idList.get(i).getChildren());
        }
    }
 
    /**
     * 该方法是将子节点为空的List集合设置为Null值
     */
    private static void setEmptyChildrenAsNull(List<EamEquipmentTree> idList) {
        for (EamEquipmentTree mdcEquipmentTree : idList) {
            if (mdcEquipmentTree.getChildren().isEmpty()) {
                mdcEquipmentTree.setLeaf(true);
            } else {
                setEmptyChildrenAsNull(mdcEquipmentTree.getChildren());
                mdcEquipmentTree.setLeaf(false);
            }
        }
    }
}