|
@@ -0,0 +1,105 @@
|
|
|
|
+package com.management.platform.service.impl;
|
|
|
|
+
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.management.platform.entity.Department;
|
|
|
|
+import com.management.platform.entity.vo.DepartmentVO;
|
|
|
|
+import com.management.platform.mapper.DepartmentMapper;
|
|
|
|
+import com.management.platform.mapper.UserMapper;
|
|
|
|
+import com.management.platform.service.DepartmentService;
|
|
|
|
+import com.management.platform.util.HttpRespMsg;
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Stack;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * <p>
|
|
|
|
+ * 部门 服务实现类
|
|
|
|
+ * </p>
|
|
|
|
+ *
|
|
|
|
+ * @author 吴涛涛
|
|
|
|
+ * @since 2020-02-11
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private UserMapper userMapper;
|
|
|
|
+ @Resource
|
|
|
|
+ private DepartmentMapper departmentMapper;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public HttpRespMsg getDepartmentList(HttpServletRequest request) {
|
|
|
|
+ HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
|
+ try {
|
|
|
|
+ //筛选公司下所有的部门
|
|
|
|
+ Integer companyId = userMapper.selectById(request.getHeader("Token")).getCompanyId();
|
|
|
|
+ List<Department> departmentList = departmentMapper.selectList(new QueryWrapper<Department>()
|
|
|
|
+ .eq("company_id", companyId));
|
|
|
|
+ List<DepartmentVO> list = new ArrayList<>();
|
|
|
|
+ Stack<DepartmentVO> stack1 = new Stack<>(); //存放上依赖下的栈
|
|
|
|
+ Stack<DepartmentVO> stack2 = new Stack<>(); //临时存放对1中的依赖
|
|
|
|
+ Stack<DepartmentVO> stack3 = new Stack<>(); //存放所有要塞进去的东西
|
|
|
|
+ for (int i = 0; i < departmentList.size(); i++) {
|
|
|
|
+ Department department = departmentList.get(i);
|
|
|
|
+ if (department.getSuperiorId() == null) {
|
|
|
|
+ DepartmentVO departmentVO = new DepartmentVO();
|
|
|
|
+ BeanUtils.copyProperties(department, departmentVO);
|
|
|
|
+ stack1.push(departmentVO);
|
|
|
|
+ departmentList.remove(i--);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ while (departmentList.size() > 0) {
|
|
|
|
+ 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.getDepartmentId())) {
|
|
|
|
+ isPushed = true;
|
|
|
|
+ DepartmentVO departmentVO = new DepartmentVO();
|
|
|
|
+ BeanUtils.copyProperties(department, departmentVO);
|
|
|
|
+ stack1.push(departmentVO);
|
|
|
|
+ departmentList.remove(i--);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (!isPushed) {
|
|
|
|
+ stack2.push(stack1.pop());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ while (!stack1.isEmpty()) {
|
|
|
|
+ DepartmentVO first = stack1.pop();
|
|
|
|
+ Integer superiorId = first.getSuperiorId();
|
|
|
|
+ if (superiorId != null) {
|
|
|
|
+ stack3.push(first);
|
|
|
|
+ while (stack2.peek().getSuperiorId() != null && stack2.peek().getSuperiorId().equals(superiorId)) {
|
|
|
|
+ stack3.push(stack2.pop());
|
|
|
|
+ }
|
|
|
|
+ while (stack1.peek().getSuperiorId() != null && stack1.peek().getSuperiorId().equals(superiorId)) {
|
|
|
|
+ stack3.push(stack1.pop());
|
|
|
|
+ }
|
|
|
|
+ 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());
|
|
|
|
+ }
|
|
|
|
+ httpRespMsg.data = list;
|
|
|
|
+ } catch (NullPointerException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ httpRespMsg.setError("验证失败");
|
|
|
|
+ return httpRespMsg;
|
|
|
|
+ }
|
|
|
|
+ return httpRespMsg;
|
|
|
|
+ }
|
|
|
|
+}
|