生成部门树

 List<BiUser> userList = thirdPartyApiServiceFactory.getInstance(systemId).getAllUser(systemId, getTenantParam());
        List<BiDepartment> departList = thirdPartyApiServiceFactory.getInstance(systemId).getAllDepart(systemId, getTenantParam());
        DepartTree departTree = createDepartTree(departList);
        List<JSONObject> userJsonList = new ArrayList<>();
        List<JSONObject> departJsonList = new ArrayList<>();
        Long departId = null;
        if(departId == null){
            for(DepartNode node:departTree.getRoot().getChildren()){
                departJsonList.add(createDepartJson(node.getDepartment()));
            }
            for(BiUser user:userList){
                userJsonList.add(createUserJson(user));
            }
        }else if(departTree.getDepartNodeMap().containsKey(departId)) {
            for(DepartNode node:departTree.getDepartNodeMap().get(departId).getChildren()){
                departJsonList.add(createDepartJson(node.getDepartment()));
            }
            Set<Long> departIdSet = getAllDescendant(departTree,departId,new HashSet<>());
            for(BiUser user:userList){
                if(departIdSet.contains(user.getDepartId())) {
                    userJsonList.add(createUserJson(user));
                }
            }
        }
        JSONObject rstJson = new JSONObject();
        rstJson.put("group",departJsonList);
        rstJson.put("user",userJsonList);
class DepartTree{
        private DepartNode root = new DepartNode(null);
        private Map<Long, DepartNode> departNodeMap = new HashMap<>();

        public DepartNode getRoot() {
            return root;
        }

        public Map<Long, DepartNode> getDepartNodeMap() {
            return departNodeMap;
        }
    }
    class DepartNode{
        private List<DepartNode> children = new ArrayList<>();
        private BiDepartment department;

        public DepartNode(BiDepartment department) {
            this.department = department;
        }

        public List<DepartNode> getChildren() {
            return children;
        }
        public BiDepartment getDepartment() {
            return department;
        }
    }
    private DepartTree createDepartTree(List<BiDepartment> departList){
        DepartTree departTree = new DepartTree();
        for(BiDepartment depart:departList){
            departTree.getDepartNodeMap().put(depart.getId(),new DepartNode(depart));
        }
        for(BiDepartment depart:departList){
            Long parentId = depart.getParentId();
            DepartNode parentNode = departTree.getDepartNodeMap().get(parentId);
            if(parentNode == null){
                parentNode = departTree.getRoot();
            }
            parentNode.getChildren().add(departTree.getDepartNodeMap().get(depart.getId()));
        }
        return departTree;
    }
    private JSONObject createDepartJson(BiDepartment depart){
        JSONObject json = new JSONObject();
        json.put("id",depart.getId());
        json.put("name",depart.getName());
        return json;
    }
    private JSONObject createUserJson(BiUser user){
        JSONObject json = new JSONObject();
        json.put("userId",user.getId());
        json.put("userName",user.getUserName());
        return json;
    }
    private Set<Long> getAllDescendant(DepartTree departTree, Long id, Set<Long> descendant){
        if(departTree.getDepartNodeMap().containsKey(id) && !descendant.contains(id)){
            descendant.add(id);
            for(DepartNode child:departTree.getDepartNodeMap().get(id).getChildren()){
                getAllDescendant(departTree,child.getDepartment().getId(),descendant);
            }
        }
        return descendant;
    }

部门树型结构

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值