Przeglądaj źródła

部门 部门获取算法修改

Reiskuchen 5 lat temu
rodzic
commit
6febd9a30c

+ 29 - 13
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/DepartmentServiceImpl.java

@@ -138,7 +138,8 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
         HttpRespMsg httpRespMsg = new HttpRespMsg();
         try {
             //筛选公司下所有的部门
-            Integer companyId = userMapper.selectById(request.getHeader("Token")).getCompanyId();
+//            Integer companyId = userMapper.selectById(request.getHeader("Token")).getCompanyId();
+            Integer companyId = 6;
             List<Department> departmentList = departmentMapper.selectList(new QueryWrapper<Department>()
                     .eq("company_id", companyId));
             //结果列表
@@ -147,9 +148,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
                     //主栈 存放顺序依赖下半段
                     stack1 = new Stack<>(),
                     //副栈 存放主栈中依赖被切开后的上半段
-                    stack2 = new Stack<>(),
-                    //最终栈 存放最后存入列表前
-                    stack3 = new Stack<>();
+                    stack2 = new Stack<>();
             //遍历一次 把其中无父级的部门PO取出来转换成部门VO放入主栈中 并将其从原始部门数据中移除
             for (int i = 0; i < departmentList.size(); i++) {
                 Department department = departmentList.get(i);
@@ -190,29 +189,46 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
                 Integer superiorId = first.getParentId();
                 //如果没有父部门id 那就直接放进最终列表当中
                 if (superiorId != null) {
+                    List<DepartmentVO> targetList = new ArrayList<>();
                     //有父部门的话 那么下方很可能也是相同的父部门 所以要把类似的它们存入最终栈中
-                    stack3.push(first);
+                    targetList.add(first);
                     //首先把副栈中所有相同父部门的部门存入最终栈
                     while (stack2.peek().getParentId() != null && stack2.peek().getParentId().equals(superiorId)) {
-                        stack3.push(stack2.pop());
+                        targetList.add(stack2.pop());
                     }
                     //首先把主栈中所有相同父部门的部门存入最终栈
                     while (stack1.peek().getParentId() != null && stack1.peek().getParentId().equals(superiorId)) {
-                        stack3.push(stack1.pop());
+                        targetList.add(stack1.pop());
                     }
                     //把这些部门装入父部门 也就是当前主栈栈顶的那个部门的children中
-                    List<DepartmentVO> targetList = new ArrayList<>();
-                    while (!stack3.isEmpty()) {
-                        targetList.add(stack3.pop());
-                    }
                     stack1.peek().setChildren(targetList);
                 } else {
                     list.add(first);
                 }
             }
-            //这个时候主栈已经清空了 副栈中应该还剩下几个没有父部门的部门 全都直接扔进列表
+            //这个时候主栈已经清空了 副栈中元素加入主栈中再来一次
             while (!stack2.isEmpty()) {
-                list.add(stack2.pop());
+                stack1.push(stack2.pop());
+            }
+            //主栈再来一次 这次不用考虑副栈了
+            while (!stack1.isEmpty()) {
+                //取得栈顶部门及其父部门id
+                DepartmentVO first = stack1.pop();
+                Integer superiorId = first.getParentId();
+                //如果没有父部门id 那就直接放进最终列表当中
+                if (superiorId != null) {
+                    List<DepartmentVO> targetList = new ArrayList<>();
+                    //有父部门的话 那么下方很可能也是相同的父部门 所以要把类似的它们存入最终栈中
+                    targetList.add(first);
+                    //首先把主栈中所有相同父部门的部门存入最终栈
+                    while (stack1.peek().getParentId() != null && stack1.peek().getParentId().equals(superiorId)) {
+                        targetList.add(stack1.pop());
+                    }
+                    //把这些部门装入父部门 也就是当前主栈栈顶的那个部门的children中
+                    stack1.peek().setChildren(targetList);
+                } else {
+                    list.add(first);
+                }
             }
             //返回数据
             httpRespMsg.data = list;