父子树状结构图

遍历获取父子树状结构的MAP集合。
用Map集合通用性比较高。

private List<Map<String, Object>> treeMenuList(List<Map<String, Object>> childMenu, List<Map<String, Object>> allMenu, String pid) {
        for (Map<String, Object> map : allMenu) {
            String supdepid = map.get("parentId") == null ? null : map.get("parentId").toString();
            String departmentId = map.get("id") == null ? null : map.get("id").toString();
            if (pid.equals(supdepid)) {
                List<Map<String, Object>> childrenList = treeMenuList(new ArrayList<>(), allMenu, departmentId);
                map.put("childrenDepartment", childrenList);
                childMenu.add(map);
            }
        }
        return childMenu;
    }

当父子树状图需要对一个对象集合来操作的时候

 /**
     * 根据工号,和系统的id(mark) 来获取菜单的框架,分级那种。
     * @param menuQueryBo
     * @return
     */
    /**
     * 利用递归循环查询
     * 每一层查询其员工封装到一个对象map集合中
     * 员工是根据部门的id来查询的,一对多
     * 和查询部门封装到一个对象map集合中
     * 当根据父id查询不到部门的时候循环结束。
     */
    //全局变量
    private List<MenuVO> allDepartment = new ArrayList<>();  //全局变量

    @Override
    public Response<List<MenuVO>> getMenuByworkCodeAndSystemIdForFrame(MenuQueryBo menuQueryBo) {


        //先获取所有的数据  该数据取 没有做父子类的数据,
        List<MenuBO> data = this.getMenuByworkCodeAndSystemId(menuQueryBo).getData();

        if (data == null || data.size() == 0) {
            Response response = new Response();
            response.setCode(0);
            response.setMessage("没有关联的菜单");
            return response;
        }
        /**
         * 找到符合条件的角色的所有父类直到parentId为0;
         */
        List<MenuVO> datumList = new ArrayList<>();
        //把获取的值给全值集合
        for (MenuBO datum : data) {

            //getMenuVOParent(datum);
            allDepartment.add(MenuBOTransformVO.boToVO(datum));
        }
        List<MenuVO> menuVOList = new ArrayList<>();
       /* for (MenuVO datum : allDepartment) {
            //得到所有符合条件的menuVO集合
            // allDepartment.add(MenuBOTransformVO.boToVO(datum));
            if (datum.getParentId() == 0) {
                menuVOList.add(datum);
            }
        }*/
        for (int i = 0; i < allDepartment.size(); i++) {
            if (allDepartment.get(i).getParentId() == 0) {
                menuVOList.add(allDepartment.get(i));
            }

        }

        if (!menuVOList.isEmpty()) {

            List<MenuVO> list = new ArrayList<>();
            if (menuVOList != null) {
                for (int i = 0; i < menuVOList.size(); i++) {
                    list.add(recursiveTree(menuVOList.get(i).getId())); //获取一级节点的对象id
                }
            }
            allDepartment.clear();
            return Response.success(list);
        } else {
            //此时父id不是0;
            for (MenuVO menuVO : allDepartment) {
                List<MenuVO> menuVOList1 = new ArrayList<>();
                for (MenuVO menuVO1 : allDepartment) {
                    if (menuVO.getParentId() == menuVO1.getId()) {
                        menuVOList1.add(menuVO);

                    }
                }
                if (menuVOList1.isEmpty()) {
                    menuVOList.add(menuVO);
                }
            }
            List<MenuVO> list = new ArrayList<>();
            if (menuVOList != null) {
                for (int i = 0; i < menuVOList.size(); i++) {
                    list.add(recursiveTree(menuVOList.get(i).getId())); //获取一级节点的对象id
                }
            }
            allDepartment.clear();
            return Response.success(list);

        }

    }


    /**
     * 递归算法解析成树形结构
     *
     * @param cid
     */
    public MenuVO recursiveTree(Integer cid) {
        MenuVO node = new MenuVO();
        for (MenuVO menuVO : allDepartment) {
            if (menuVO.getId() == cid) {
                BeanUtils.copyProperties(menuVO, node);
            }
        }
        List<MenuVO> childTreeNodes = getChildTreeById(cid);

        for (MenuVO child : childTreeNodes) {
            MenuVO n = recursiveTree(child.getId());
            node.getChildren().add(n);
        }
        return node;
    }

    /**
     * 根据CID查询节点对象
     */
    public HrmDepartmentVO HrmDepartmentPO(Integer cid) {
        Map map = getTreeMap();
        return (HrmDepartmentVO) map.get(cid);
    }

    /**
     * 一次性取所有数据,为了减少对数据库查询操作
     *
     * @return
     */
    public Map getTreeMap() {
        Map map = new HashMap<Integer, HrmDepartmentPO>();
        if (null != allDepartment) {
            for (MenuVO d : allDepartment) {
                map.put(d.getId(), d);
            }
        }
        return map;
    }

    /**
     * 根据父节点CID获取所有了节点
     */
    public List<MenuVO> getChildTreeById(Integer cid) {
        List<MenuVO> list = new ArrayList<>();
        if (null != allDepartment) {
            for (MenuVO d : allDepartment) {
                if (null != cid) {
                    if (cid.equals(d.getParentId())) {
                        list.add(d);
                    }
                }
            }
        }

        return list;
    }

你可能感兴趣的:(Java)