FindsDepartsChildrenUtil.java
4.5 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.skua.modules.system.util;
import com.skua.core.util.ConvertUtils;
import com.skua.modules.system.entity.SysDepart;
import com.skua.modules.system.model.DepartIdModel;
import com.skua.modules.system.model.SysDepartTreeModel;
import java.util.ArrayList;
import java.util.List;
/**
* 对应部门的表,处理并查找树级数据
*/
public class FindsDepartsChildrenUtil {
//部门树信息-树结构
//private static List<SysDepartTreeModel> sysDepartTreeList = new ArrayList<SysDepartTreeModel>();
//部门树id-树结构
//private static List<DepartIdModel> idList = new ArrayList<>();
/**
* queryTreeList的子方法 ====1=====
* 该方法是s将SysDepart类型的list集合转换成SysDepartTreeModel类型的集合
*/
public static List<SysDepartTreeModel> wrapTreeDataToTreeList(List<SysDepart> recordList,String orgCode) {
// 在该方法每请求一次,都要对全局list集合进行一次清理
//idList.clear();
List<DepartIdModel> idList = new ArrayList<DepartIdModel>();
List<SysDepartTreeModel> records = new ArrayList<>();
for (int i = 0; i < recordList.size(); i++) {
SysDepart depart = recordList.get(i);
records.add(new SysDepartTreeModel(depart));
}
List<SysDepartTreeModel> tree = findChildren(records, idList,orgCode);
setEmptyChildrenAsNull(tree);
return tree;
}
/**
* 获取 DepartIdModel
* @param recordList
* @return
*/
public static List<DepartIdModel> wrapTreeDataToDepartIdTreeList(List<SysDepart> recordList,String orgCode) {
// 在该方法每请求一次,都要对全局list集合进行一次清理
//idList.clear();
List<DepartIdModel> idList = new ArrayList<DepartIdModel>();
List<SysDepartTreeModel> records = new ArrayList<>();
for (int i = 0; i < recordList.size(); i++) {
SysDepart depart = recordList.get(i);
records.add(new SysDepartTreeModel(depart));
}
findChildren(records, idList,orgCode);
return idList;
}
/**
* queryTreeList的子方法 ====2=====
* 该方法是找到并封装顶级父类的节点到TreeList集合
*/
private static List<SysDepartTreeModel> findChildren(List<SysDepartTreeModel> recordList,
List<DepartIdModel> departIdList,String orgCode) {
List<SysDepartTreeModel> treeList = new ArrayList<>();
for (int i = 0; i < recordList.size(); i++) {
SysDepartTreeModel branch = recordList.get(i);
if (branch.getOrgCode().equals(orgCode)) {
treeList.add(branch);
DepartIdModel departIdModel = new DepartIdModel().convert(branch);
departIdList.add(departIdModel);
}
}
getGrandChildren(treeList,recordList,departIdList);
//idList = departIdList;
return treeList;
}
/**
* queryTreeList的子方法====3====
*该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合
*/
private static void getGrandChildren(List<SysDepartTreeModel> treeList,List<SysDepartTreeModel> recordList,List<DepartIdModel> idList) {
for (int i = 0; i < treeList.size(); i++) {
SysDepartTreeModel model = treeList.get(i);
DepartIdModel idModel = idList.get(i);
for (int i1 = 0; i1 < recordList.size(); i1++) {
SysDepartTreeModel m = recordList.get(i1);
if (m.getParentId()!=null && m.getParentId().equals(model.getId())) {
model.getChildren().add(m);
DepartIdModel dim = new DepartIdModel().convert(m);
idModel.getChildren().add(dim);
}
}
getGrandChildren(treeList.get(i).getChildren(), recordList, idList.get(i).getChildren());
}
}
/**
* queryTreeList的子方法 ====4====
* 该方法是将子节点为空的List集合设置为Null值
*/
private static void setEmptyChildrenAsNull(List<SysDepartTreeModel> treeList) {
for (int i = 0; i < treeList.size(); i++) {
SysDepartTreeModel model = treeList.get(i);
if (model.getChildren().size() == 0) {
model.setChildren(null);
model.setIsLeaf(true);
}else{
setEmptyChildrenAsNull(model.getChildren());
model.setIsLeaf(false);
}
}
// sysDepartTreeList = treeList;
}
}