1.org实体类

public class Org  implements Serializable {

    private static final long serialVersionUID = 1L;

    private String title;
    private Long parent;
    private List<User> userList;
    private List<Org> nodes;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Long getParent() {
        return parent;
    }

    public void setParent(Long parent) {
        this.parent = parent;
    }

    public List<Org> getNodes() {
        if(nodes == null){
            nodes = new ArrayList<Org>();
        }
        return nodes;
    }

    public void setNodes(List<Org> nodes) {
        this.nodes = nodes;
    }

    public List<User> getUserList() {
        if(userList == null){
            userList = new ArrayList<User>();
        }
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    @Override
    public String toString() {
        return "Org{" +
                "createTime=" + createTime +
                ", title='" + title + '\'' +
                ", parent=" + parent +
                ", userList=" + userList +
                ", nodes=" + nodes +
                '}';
    }
}

2.查询该部门下的子部门

private List<Org> hasChilds(Long id){
        List<Org> orgs = super.selectList(
                new EntityWrapper<Org>().eq(Config.ABLE_CONFIG.ABLE_COLUMN,Config.ABLE_CONFIG.ABLE)
                    .eq("parent",id)
        );
        List<Org> orgList1 = new ArrayList<>();
        for(Org org : orgs){
            List<User> userList = userService.getByOrgId(org.getId());
            org.setUserList(userList);   //同时也查出了该部门下的所有人员
            orgList1.add(org);
        }
        return orgList1;
    }

3.查询该部门下的所有部门的id。---应用递归查询出所有的id,放到set集合中

 public Set<Long> getAllChildIds(Long id,Set<Long> set) {
        if(set == null){
            set = new HashSet<>();
        }
        List<Org> orgList = hasChilds(id);
        set.add(id);
        for (int i = 0; i < orgList.size(); i++) {
            set.add(orgList.get(i).getId());
            getAllChildIds(orgList.get(i).getId(),set);
        }
        return set;
    }

4.查询该部门下的所有部门部门---应用递归查询出该部门下的所有部门,放到Org实体类的List<Org> node属性中。(已经在实体类中定义过了)

    public Org  getAllChildOrgs(Long id) {
        Org org = null;
        if(id == 0){
            org = new Org();
        }else {
            org = get(id);
        }
        List<Org> orgList = hasChilds(id);
        for(Org org1 : orgList){
            Org org2 = getAllChildOrgs(org1.getId());
            org.getNodes().add(org2);
        }
        return org;
    }

5.递归删除该部门下所有部门

//递归删除该部门下所有部门
public boolean del(Long id) {
        if (id != null){
            List<Org> hasResponses = hasChilds(id);
            if (!CollectionUtils.isEmpty(hasResponses)){
                for (Org orgRsp:hasResponses){
                    del(orgRsp.getId());
                }
            }
            return delCurrent(id);

        }else {
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(), StatusCode.BAD_REQUEST.getMessage());
        }
    }

//根据id删除对象
 private boolean delCurrent(Long id){
        Org org = new Org();
        org.setId(id);
        org.setIsDeleted(Config.ABLE_CONFIG.UNABLE);
        org.setUpdateTime(new Date());
        return super.updateById(org);
    }
05-11 22:35