zoukankan      html  css  js  c++  java
  • 递归解决员工所属部门遍历

    1.部门表结构

    CREATE TABLE `dept_info` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `dept_code` varchar(32) DEFAULT NULL COMMENT '部门编号',
      `dept_name` varchar(64) DEFAULT NULL COMMENT '部门名称',
      `pcode` varchar(32) DEFAULT NULL COMMENT '父部门code',
      `creator_id` int(11) DEFAULT NULL,
      `gmt_create` timestamp NULL DEFAULT NULL,
      `update_id` int(11) DEFAULT NULL,
      `gmt_update` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`) USING BTREE
    ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

    2.递归实现列表层级集合

      public List<DeptInfo> getLevelDeptInfoList() {
    
            DeptInfo deptInfo = new DeptInfo();
            deptInfo.setPageNum(null);
            deptInfo.setPageSize(null);
            List<DeptInfo> deptInfos = deptInfoMapper.getDeptInfoList(deptInfo);
    
            //层级集合
            List<DeptInfo> rootDeptInfos = new ArrayList<>();
    
            if (!CollectionUtils.isEmpty(deptInfos)) {
                for (DeptInfo deptInfo1:deptInfos) {
                    if (StringUtils.isBlank(deptInfo1.getPcode())) {
                        rootDeptInfos.add(deptInfo1);
                    }
                }
    
                for (DeptInfo deptInfo1:rootDeptInfos) {
                    deptInfo1.setChildDeptList(getChild(deptInfo1.getId().toString(),deptInfos));
                }
    
            }
    
            return rootDeptInfos;
        }
    
        /**
         * @Author ldg
         * @param id 顶层部门的id
         * @param deptInfos 所有部门
         * @return
         */
        private List<DeptInfo> getChild(String id,List<DeptInfo> deptInfos){
    
            List<DeptInfo> childList = new ArrayList<>();
    
            for (DeptInfo deptInfo:deptInfos) {
                if (StringUtils.isNotBlank(deptInfo.getPcode())) {
                    if (deptInfo.getPcode().equals(id)) {
    
                        childList.add(deptInfo);
                    }
                }
            }
    
            for (DeptInfo deptInfo:childList) {
                deptInfo.setChildDeptList(getChild(deptInfo.getId().toString(),deptInfos));
            }
    
            if (childList.size() == 0){
                return null;
            }
    
            return childList;
        }
  • 相关阅读:
    加入Tomcat插件到ECLIPSE中的方法
    Coursera, Big Data 5, Graph Analytics for Big Data, Week 4
    视觉技术在列车上的应用场景
    AWS Data Analytics Fundamentals 官方课程笔记
    AWS Data Analytics Fundamentals 官方课程笔记
    spark 新建一个column并用另一column的最大值赋值
    AWS Cloud Practioner 官方课程笔记
    spark 自定义 accumulator
    windows 上用 VS Code 编译 C/C++
    spark 问题
  • 原文地址:https://www.cnblogs.com/beixiaoyi/p/13820177.html
Copyright © 2011-2022 走看看