|
@@ -44,12 +44,13 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
|
//结果列表
|
|
|
List<DepartmentVO> list = new ArrayList<>();
|
|
|
Stack<DepartmentVO>
|
|
|
- //存放顺序依赖的主栈
|
|
|
+ //主栈 存放顺序依赖下半段
|
|
|
stack1 = new Stack<>(),
|
|
|
- //存放主栈中的依赖的上半段的栈
|
|
|
+ //副栈 存放主栈中依赖被切开后的上半段
|
|
|
stack2 = new Stack<>(),
|
|
|
- //存放最后塞入列表前的栈
|
|
|
+ //最终栈 存放最后存入列表前
|
|
|
stack3 = new Stack<>();
|
|
|
+ //遍历一次 把其中无父级的部门PO取出来转换成部门VO放入主栈中 并将其从原始部门数据中移除
|
|
|
for (int i = 0; i < departmentList.size(); i++) {
|
|
|
Department department = departmentList.get(i);
|
|
|
if (department.getSuperiorId() == null) {
|
|
@@ -57,32 +58,49 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
|
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) {
|
|
|
+ //有父部门的话 那么下方很可能也是相同的父部门 所以要把类似的它们存入最终栈中
|
|
|
stack3.push(first);
|
|
|
+ //首先把主栈中所有相同父部门的部门存入最终栈
|
|
|
while (stack2.peek().getParentId() != null && stack2.peek().getParentId().equals(superiorId)) {
|
|
|
stack3.push(stack2.pop());
|
|
|
}
|
|
|
+ //首先把副栈中所有相同父部门的部门存入最终栈
|
|
|
while (stack1.peek().getParentId() != null && stack1.peek().getParentId().equals(superiorId)) {
|
|
|
stack3.push(stack1.pop());
|
|
|
}
|
|
|
+ //把这些部门装入父部门 也就是当前主栈栈顶的那个部门的children中
|
|
|
List<DepartmentVO> targetList = new ArrayList<>();
|
|
|
while (!stack3.isEmpty()) {
|
|
|
targetList.add(stack3.pop());
|
|
@@ -92,9 +110,11 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
|
list.add(first);
|
|
|
}
|
|
|
}
|
|
|
+ //这个时候主栈已经清空了 副栈中应该还剩下几个没有父部门的部门 全都直接扔进列表
|
|
|
while (!stack2.isEmpty()) {
|
|
|
list.add(stack2.pop());
|
|
|
}
|
|
|
+ //返回数据
|
|
|
httpRespMsg.data = list;
|
|
|
} catch (NullPointerException e) {
|
|
|
httpRespMsg.setError("验证失败");
|
|
@@ -103,7 +123,9 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
|
return httpRespMsg;
|
|
|
}
|
|
|
|
|
|
+ //将部门PO转化为部门VO
|
|
|
private DepartmentVO formatDepartmentToVO(Department department) {
|
|
|
+ //这俩东西并没有继承关系
|
|
|
return new DepartmentVO()
|
|
|
.setId(department.getDepartmentId())
|
|
|
.setLabel(department.getDepartmentName())
|