|
@@ -135,6 +135,8 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
|
return httpRespMsg;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
//删除部门
|
|
|
@Override
|
|
|
public HttpRespMsg deleteDepartment(Integer departmentId, HttpServletRequest request) {
|
|
@@ -185,6 +187,23 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
|
return departmentMapper.selectCount(new QueryWrapper<Department>().eq("superior_id", id)) > 0;
|
|
|
}
|
|
|
|
|
|
+ private void fillSubDepartmentList(List<Department> allDepts, DepartmentVO parentDept) {
|
|
|
+ Integer id = parentDept.getId();
|
|
|
+ List<Department> collect = allDepts.stream().filter(all -> all.getSuperiorId() != null && all.getSuperiorId().intValue() == id).collect(Collectors.toList());
|
|
|
+ List<DepartmentVO> subResult = new ArrayList<>();
|
|
|
+ if (collect.size() > 0) {
|
|
|
+ collect.forEach(c->{
|
|
|
+ DepartmentVO vo = formatDepartmentToVO(c);
|
|
|
+ subResult.add(vo);
|
|
|
+ //继续添加当前部门的子部门
|
|
|
+ fillSubDepartmentList(allDepts, vo);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (subResult.size() > 0) {
|
|
|
+ parentDept.setChildren(subResult);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
//获取部门列表
|
|
|
@Override
|
|
|
public HttpRespMsg getDepartmentList(HttpServletRequest request) {
|
|
@@ -196,94 +215,102 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
|
.eq("company_id", companyId));
|
|
|
//结果列表
|
|
|
List<DepartmentVO> list = new ArrayList<>();
|
|
|
- Stack<DepartmentVO>
|
|
|
- //主栈 存放顺序依赖下半段
|
|
|
- stack1 = new Stack<>(),
|
|
|
- //副栈 存放主栈中依赖被切开后的上半段
|
|
|
- stack2 = new Stack<>();
|
|
|
- //遍历一次 把其中无父级的部门PO取出来转换成部门VO放入主栈中 并将其从原始部门数据中移除
|
|
|
- for (int i = 0; i < departmentList.size(); i++) {
|
|
|
- Department department = departmentList.get(i);
|
|
|
- if (department.getSuperiorId() == null) {
|
|
|
- stack1.push(formatDepartmentToVO(department));
|
|
|
- departmentList.remove(i--);
|
|
|
- }
|
|
|
- }
|
|
|
+ List<Department> rootDepartments = departmentList.stream().filter(dept -> dept.getSuperiorId() == null).collect(Collectors.toList());
|
|
|
+ rootDepartments.forEach(root->{
|
|
|
+ DepartmentVO rootDeptVO = formatDepartmentToVO(root);
|
|
|
+ list.add(rootDeptVO);
|
|
|
+ fillSubDepartmentList(departmentList, rootDeptVO);
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+// Stack<DepartmentVO>
|
|
|
+// //主栈 存放顺序依赖下半段
|
|
|
+// stack1 = new Stack<>(),
|
|
|
+// //副栈 存放主栈中依赖被切开后的上半段
|
|
|
+// stack2 = new Stack<>();
|
|
|
+// //遍历一次 把其中无父级的部门PO取出来转换成部门VO放入主栈中 并将其从原始部门数据中移除
|
|
|
+// for (int i = 0; i < departmentList.size(); i++) {
|
|
|
+// Department department = departmentList.get(i);
|
|
|
+// if (department.getSuperiorId() == null) {
|
|
|
+// stack1.push(formatDepartmentToVO(department));
|
|
|
+// departmentList.remove(i--);
|
|
|
+// }
|
|
|
+// }
|
|
|
//在原始部门数据移除之前无限循环 将列表里的元素全部按照顺序放进主栈和副栈当中
|
|
|
- while (departmentList.size() > 0) {
|
|
|
- //用来判断主栈最上方那个部门是 false-本来就在的 还是 true-刚被加入进去的
|
|
|
- Boolean isPushed = false;
|
|
|
- //偷看顶部元素
|
|
|
- DepartmentVO temp = stack1.peek();
|
|
|
- //遍历一次原始数据列表
|
|
|
- for (int i = 0; i < departmentList.size(); i++) {
|
|
|
- //如果列表中有任何主栈顶部门的子部门
|
|
|
- Department department = departmentList.get(i);
|
|
|
- if (department.getSuperiorId().equals(temp.getId())) {
|
|
|
- //设定主栈最上方的部门是刚放进去的 本次循环中不要向外排
|
|
|
- isPushed = true;
|
|
|
- //那就转换成部门VO放入主栈
|
|
|
- stack1.push(formatDepartmentToVO(department));
|
|
|
- //从列表中移除部门
|
|
|
- departmentList.remove(i--);
|
|
|
- }
|
|
|
- }
|
|
|
- //如果主栈最上方部门不是刚被加进去的而是一开始循环遍历时的那个
|
|
|
- if (!isPushed) {
|
|
|
- //也就是说这个最上方部门并没有子部门 所以排出来扔进副栈
|
|
|
- stack2.push(stack1.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 (!stack2.isEmpty() &&
|
|
|
- stack2.peek().getParentId() != null &&
|
|
|
- stack2.peek().getParentId().equals(superiorId)) {
|
|
|
- targetList.add(stack2.pop());
|
|
|
- }
|
|
|
- //把主栈中所有相同父部门的部门存入最终栈
|
|
|
- while (stack1.peek().getParentId() != null && stack1.peek().getParentId().equals(superiorId)) {
|
|
|
- targetList.add(stack1.pop());
|
|
|
- }
|
|
|
- //把这些部门装入父部门 也就是当前主栈栈顶的那个部门的children中
|
|
|
- stack1.peek().setChildren(targetList);
|
|
|
- } else {
|
|
|
- list.add(first);
|
|
|
- }
|
|
|
- }
|
|
|
- //这个时候主栈已经清空了 副栈中元素加入主栈中再来一次
|
|
|
- while (!stack2.isEmpty()) {
|
|
|
- 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);
|
|
|
- }
|
|
|
- }
|
|
|
+// while (departmentList.size() > 0) {
|
|
|
+// //用来判断主栈最上方那个部门是 false-本来就在的 还是 true-刚被加入进去的
|
|
|
+// Boolean isPushed = false;
|
|
|
+// //偷看顶部元素
|
|
|
+// DepartmentVO temp = stack1.peek();
|
|
|
+// //遍历一次原始数据列表
|
|
|
+// for (int i = 0; i < departmentList.size(); i++) {
|
|
|
+// //如果列表中有任何主栈顶部门的子部门
|
|
|
+// Department department = departmentList.get(i);
|
|
|
+// if (department.getSuperiorId().equals(temp.getId())) {
|
|
|
+// //设定主栈最上方的部门是刚放进去的 本次循环中不要向外排
|
|
|
+// isPushed = true;
|
|
|
+// //那就转换成部门VO放入主栈
|
|
|
+// stack1.push(formatDepartmentToVO(department));
|
|
|
+// //从列表中移除部门
|
|
|
+// departmentList.remove(i--);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// //如果主栈最上方部门不是刚被加进去的而是一开始循环遍历时的那个
|
|
|
+// if (!isPushed) {
|
|
|
+// //也就是说这个最上方部门并没有子部门 所以排出来扔进副栈
|
|
|
+// stack2.push(stack1.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 (!stack2.isEmpty() &&
|
|
|
+// stack2.peek().getParentId() != null &&
|
|
|
+// stack2.peek().getParentId().equals(superiorId)) {
|
|
|
+// targetList.add(stack2.pop());
|
|
|
+// }
|
|
|
+// //把主栈中所有相同父部门的部门存入最终栈
|
|
|
+// while (stack1.peek().getParentId() != null && stack1.peek().getParentId().equals(superiorId)) {
|
|
|
+// targetList.add(stack1.pop());
|
|
|
+// }
|
|
|
+// //把这些部门装入父部门 也就是当前主栈栈顶的那个部门的children中
|
|
|
+// stack1.peek().setChildren(targetList);
|
|
|
+// } else {
|
|
|
+// list.add(first);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// //这个时候主栈已经清空了 副栈中元素加入主栈中再来一次
|
|
|
+// while (!stack2.isEmpty()) {
|
|
|
+// 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;
|
|
|
} catch (NullPointerException e) {
|