郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.gkhy.safePlatform.account.utils;
 
import com.gkhy.safePlatform.account.entity.enterprise.DepartmentInfo;
import com.gkhy.safePlatform.account.enums.DepartmentLevelEnum;
import com.gkhy.safePlatform.account.enums.DepartmentStatusEnum;
import com.gkhy.safePlatform.account.model.dto.resp.DepartmentRespDTO;
import com.gkhy.safePlatform.account.entity.enterprise.DepartmentInfoDO;
import org.springframework.beans.BeanUtils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
public class DepartmentUtil {
 
 
    /**
     * @Description: 生成树
     */
    public static List<DepartmentRespDTO> generateMenuTree(List<DepartmentInfoDO> allDepartment) {
 
        // 区分父子节点
        List<DepartmentRespDTO> rootDepartment = new ArrayList<>();
        List<DepartmentRespDTO> childDepartment = new ArrayList<>();
        DepartmentRespDTO tmp;
        DepartmentStatusEnum statusEnum;
        DepartmentLevelEnum levelEnum;
        for (DepartmentInfoDO departmentDo : allDepartment) {
            // 转换dto
            tmp = new DepartmentRespDTO();
            tmp.setDepId(departmentDo.getId());
            tmp.setDepName(departmentDo.getName());
            tmp.setDepInfo(departmentDo.getInfo());
            tmp.setDepLevel(departmentDo.getLevel());
            tmp.setDepCode(departmentDo.getCode());
            levelEnum = DepartmentLevelEnum.parse(departmentDo.getLevel());
            if (levelEnum != null) {
                tmp.setDepLevelDesc(levelEnum.value);
            }
            tmp.setStatus(departmentDo.getStatus());
            statusEnum = DepartmentStatusEnum.parse(departmentDo.getStatus());
            assert statusEnum != null;
            tmp.setStatusDesc(statusEnum.getValue());
            tmp.setParentDepId(departmentDo.getParentId());
            if (departmentDo.getParentId() == null) {
                rootDepartment.add(tmp);
            }else{
                childDepartment.add(tmp);
            }
        }
        // 遍历父节点
        if (rootDepartment.size() > 0) {
            for (DepartmentRespDTO root : rootDepartment) {
                // 当前父节点的子节点
                List<DepartmentRespDTO> children = getChildren(root, childDepartment);
                root.setChildren(children);
            }
        }
 
        return rootDepartment;
    }
 
    private static List<DepartmentRespDTO> getChildren(DepartmentRespDTO parent, List<DepartmentRespDTO> childDepartment) {
        List<DepartmentRespDTO> children = new ArrayList<>();
        if (childDepartment.size() > 0) {
            for (DepartmentRespDTO nav : childDepartment) {
                if (nav.getParentDepId().equals(parent.getDepId())) {
                    children.add(nav);
                }
            }
        }
        if (children.size() > 0) {
            for (DepartmentRespDTO nav:children) {
                List<DepartmentRespDTO> subChildren = getChildren(nav, childDepartment);
                nav.setChildren(subChildren);
            }
        }
        return children;
    }
 
 
    public static DepartmentInfoDO getDepartment(Long depId, List<DepartmentInfoDO> allDeps) {
        if (depId == null) return null;
        for (DepartmentInfoDO departmentInfoDO : allDeps) {
            if (departmentInfoDO.getId().equals(depId)) {
                return departmentInfoDO;
            }
        }
        return null;
 
    }
 
    public static boolean isUnderDepartment(Long depId,Long depId1, List<DepartmentInfoDO> allDeps) {
        DepartmentInfoDO department = getDepartment(depId1, allDeps);
        if (department.getParentId() != null) {
            if (department.getParentId().equals(depId)) {
                return true;
            }else{
                return isUnderDepartment(depId, department.getParentId(), allDeps);
            }
        }else{
            return false;
        }
    }
 
 
    public static List<DepartmentInfoDO> generateDepListByDepId(Long depId, List<DepartmentInfoDO> allDeps) {
        List<DepartmentInfoDO> result = new ArrayList<>();
        for (DepartmentInfoDO departmentInfoDO : allDeps) {
            if (departmentInfoDO.getId().equals(depId)) {
                result.add(departmentInfoDO);
            }
        }
        if (result.size() == 1) {
            // 递归寻找子部门
            addChildren(result.get(0), result, allDeps);
 
        }
        return result;
    }
 
    private static void addChildren(DepartmentInfoDO root, List<DepartmentInfoDO> result, List<DepartmentInfoDO> allDeps) {
        List<DepartmentInfoDO> children = new ArrayList<>();
        for (DepartmentInfoDO departmentInfo : allDeps) {
            if (root.getId().equals(departmentInfo.getParentId())) {
                children.add(departmentInfo);
                result.add(departmentInfo);
            }
        }
        if (children.size() > 0) {
            for (DepartmentInfoDO child : children) {
                addChildren(child, result, allDeps);
            }
        }
    }
 
 
    public static void main(String[] args) {
 
    }
 
    public static List<Long> generateDepIdListByDepId(Long depId, List<DepartmentInfoDO> allDeps) {
        List<Long> result = new ArrayList<>();
        for (DepartmentInfoDO departmentInfoDO : allDeps) {
            if (departmentInfoDO.getId().equals(depId)) {
                result.add(departmentInfoDO.getId());
            }
        }
        if (result.size() == 1) {
            // 递归寻找子部门
            addChildrenId(result.get(0), result, allDeps);
 
        }
        return result;
    }
 
 
    private static void addChildrenId(Long rootId, List<Long> result, List<DepartmentInfoDO> allDeps) {
        List<DepartmentInfoDO> children = new ArrayList<>();
        for (DepartmentInfoDO departmentInfo : allDeps) {
            if (rootId.equals(departmentInfo.getParentId())) {
                children.add(departmentInfo);
                result.add(departmentInfo.getId());
            }
        }
        if (children.size() > 0) {
            for (DepartmentInfoDO child : children) {
                addChildrenId(child.getId(), result, allDeps);
            }
        }
    }
}