AdaKing88
2023-08-23 ae35159387a55199e8ab150ebb97d89d68a235bd
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
package org.jeecg.modules.system.vo.thirdapp;
 
import com.jeecg.qywx.api.department.vo.Department;
import org.springframework.beans.BeanUtils;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 企业微信树结构的部门
 *
 * @author sunjianlei
 */
public class JwDepartmentTreeVo extends Department {
 
    private List<JwDepartmentTreeVo> children;
 
    public List<JwDepartmentTreeVo> getChildren() {
        return children;
    }
 
    public JwDepartmentTreeVo setChildren(List<JwDepartmentTreeVo> children) {
        this.children = children;
        return this;
    }
 
    public JwDepartmentTreeVo(Department department) {
        BeanUtils.copyProperties(department, this);
    }
 
    /**
     * 是否有子项
     */
    public boolean hasChildren() {
        return children != null && children.size() > 0;
    }
 
    @Override
    public String toString() {
        return "JwDepartmentTree{" +
                "children=" + children +
                "} " + super.toString();
    }
 
    /**
     * 静态辅助方法,将list转为tree结构
     */
    public static List<JwDepartmentTreeVo> listToTree(List<Department> allDepartment) {
        // 先找出所有的父级
        List<JwDepartmentTreeVo> treeList = getByParentId("1", allDepartment);
        getChildrenRecursion(treeList, allDepartment);
        return treeList;
    }
 
    private static List<JwDepartmentTreeVo> getByParentId(String parentId, List<Department> allDepartment) {
        List<JwDepartmentTreeVo> list = new ArrayList<>();
        for (Department department : allDepartment) {
            if (parentId.equals(department.getParentid())) {
                list.add(new JwDepartmentTreeVo(department));
            }
        }
        return list;
    }
 
    private static void getChildrenRecursion(List<JwDepartmentTreeVo> treeList, List<Department> allDepartment) {
        for (JwDepartmentTreeVo departmentTree : treeList) {
            // 递归寻找子级
            List<JwDepartmentTreeVo> children = getByParentId(departmentTree.getId(), allDepartment);
            if (children.size() > 0) {
                departmentTree.setChildren(children);
                getChildrenRecursion(children, allDepartment);
            }
        }
    }
 
}