cuilei
9 天以前 3a36e69c9904ea1c5d6719382f4342a5b37db5a6
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.jeecg.modules.system.util;
 
import cn.hutool.core.util.StrUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.MdcProduction;
import org.jeecg.modules.system.model.MdcProductionTreeModel;
import org.jeecg.modules.system.model.ProductionIdModel;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 对应产线的表,处理并查找树结构数据
 *
 * @author: LiuS
 * @create: 2023-03-23 14:46
 */
public class FindsProductionsChildrenUtil {
 
    /**
     * queryTreeList的子方法 ====1=====
     * 该方法是s将MdcProduction类型的list集合转换成MdcProductionTreeModel类型的集合
     */
    public static List<MdcProductionTreeModel> wrapTreeDataToTreeList(List<MdcProduction> recordList) {
        List<ProductionIdModel> idList = new ArrayList<>();
        List<MdcProductionTreeModel> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            MdcProduction mdcProduction = recordList.get(i);
            if (StrUtil.isEmpty(mdcProduction.getDescription())){
                mdcProduction.setDescription("");
            }
            records.add(new MdcProductionTreeModel(mdcProduction));
        }
        List<MdcProductionTreeModel> tree = findChildren(records, idList);
        setEmptyChildrenAsNull(tree);
        return tree;
    }
 
    /**
     * queryTreeList的子方法 ====1=====
     * 该方法是找到并封装顶级父类的节点到TreeList集合
     */
    public static List<MdcProductionTreeModel> findChildren(List<MdcProductionTreeModel> recordList, List<ProductionIdModel> productionIdList) {
        List<MdcProductionTreeModel> treeList = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            MdcProductionTreeModel branch = recordList.get(i);
            if (oConvertUtils.isEmpty(branch.getParentId())) {
                treeList.add(branch);
                ProductionIdModel productionIdModel = new ProductionIdModel().convert(branch);
                productionIdList.add(productionIdModel);
            }
        }
        getGrandChildren(treeList, recordList, productionIdList);
        return treeList;
    }
 
    /**
     * queryTreeList的子方法====3====
     *该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合
     */
    private static void getGrandChildren(List<MdcProductionTreeModel> treeList, List<MdcProductionTreeModel> recordList, List<ProductionIdModel> idList) {
        for (int i = 0; i < treeList.size(); i++) {
            MdcProductionTreeModel model = treeList.get(i);
            ProductionIdModel idModel = 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);
                    ProductionIdModel pim = new ProductionIdModel().convert(m);
                    idModel.setSelectable(false);
                    idModel.getChildren().add(pim);
                }
            }
            getGrandChildren(treeList.get(i).getChildren(), recordList, idList.get(i).getChildren());
        }
    }
 
    /**
     * queryTreeList的子方法 ====4====
     * 该方法是将子节点为空的List集合设置为Null值
     */
    private static void setEmptyChildrenAsNull(List<MdcProductionTreeModel> treeList) {
        for (int i = 0; i < treeList.size(); i++) {
            MdcProductionTreeModel model = treeList.get(i);
            if (model.getChildren().size() == 0) {
                model.setChildren(null);
                model.setLeaf(true);
            } else {
                setEmptyChildrenAsNull(model.getChildren());
                model.setLeaf(false);
            }
        }
    }
 
    /**
     * 获取 DepartIdModel
     */
    public static List<ProductionIdModel> wrapTreeDataToProductionIdTreeList(List<MdcProduction> recordList) {
        List<ProductionIdModel> idList = new ArrayList<>();
        List<MdcProductionTreeModel> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            MdcProduction mdcProduction = recordList.get(i);
            records.add(new MdcProductionTreeModel(mdcProduction));
        }
        findChildren(records, idList);
        return idList;
    }
}