java实现组织架构

JAVA实现简单的组织架构

主代码块:

@Override
    public Map<String, Object> findTAreaMenu(QueryRequest request, TArea tArea) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            List<TArea> tAreas = findTAreas2(request, tArea);
            List<TAreaTree> trees = new ArrayList<>();
            buildTrees(trees, tAreas);
            List<? extends Tree<?>> tAreaTree = TreeUtil.build(trees);

            result.put(PageConstant.ROWS, tAreaTree);
            result.put(PageConstant.TOTAL, tAreas.size());
        } catch (Exception e) {
            log.error("获取地域失败", e);
            result.put(PageConstant.ROWS, null);
            result.put(PageConstant.TOTAL, 0);
        }
        return result;
    }

查询架构里面的数据:

  @Override
    public List<TArea> findTAreas2(QueryRequest request, TArea tArea) {
        QueryWrapper<TArea> queryWrapper = new QueryWrapper<>();
        SortUtil.handleWrapperSort(request, queryWrapper, "orderNum", SysConstant.ORDER_ASC, true);
        return this.baseMapper.selectList(queryWrapper);
    }

java实现组织架构_第1张图片
将数据放进集合里面:

private void buildTrees(List<TAreaTree> trees, List<TArea> tAreas) {
        tAreas.forEach(tArea -> {
            TAreaTree tree = new TAreaTree();
            tree.setId(tArea.getAreaCode());
            tree.setParentId(tArea.getParentCode());
            tree.setLabel(tArea.getAreaName());
            tree.setOrderNum(tArea.getOrderNum());
            trees.add(tree);
        });
    }

构建树

 /**
     * 用于构建菜单或部门树
     *
     * @param nodes nodes
     * @return  List
     */
    public static <T> List<? extends Tree<?>> build(List<? extends Tree<T>> nodes) {
        if (nodes == null) {
            return null;
        }
        List<Tree<T>> topNodes = new ArrayList<>();
        nodes.forEach(node -> {
            String pid = node.getParentId();
            if (pid == null || TOP_NODE_ID.equals(pid)) {
                topNodes.add(node);
                return;
            }
            for (Tree<T> n : nodes) {
                String id = n.getId();
                if (id != null && id.equals(pid)) {
                    if (n.getChildren() == null) {
                        n.initChildren();
                    }
                    n.getChildren().add(node);
                    node.setHasParent(true);
                    n.setHasChildren(true);
                    n.setHasParent(true);
                    return;
                }
            }
            if (topNodes.isEmpty()) {
                topNodes.add(node);
            }
        });
        return topNodes;
    }

前端展示:
java实现组织架构_第2张图片

你可能感兴趣的:(java语法,sql,架构)