|
@@ -32,6 +32,91 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
|
@Resource
|
|
|
private DepartmentMapper departmentMapper;
|
|
|
|
|
|
+ //新增部门
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg insertDepartment(String departmentName, Integer superiorId, HttpServletRequest request) {
|
|
|
+ HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
+ try {
|
|
|
+ Integer companyId = userMapper.selectById(request.getHeader("Token")).getCompanyId();
|
|
|
+ if (superiorId == null || departmentMapper.selectCount(new QueryWrapper<Department>()
|
|
|
+ .eq("department_id", superiorId)
|
|
|
+ .eq("company_id", companyId)) == 1) {
|
|
|
+ Department department = new Department()
|
|
|
+ .setDepartmentName(departmentName)
|
|
|
+ .setSuperiorId(superiorId)
|
|
|
+ .setCompanyId(companyId);
|
|
|
+ if (departmentMapper.insert(department) == 0) {
|
|
|
+ httpRespMsg.setError("新增失败");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ httpRespMsg.setError("无所选父级部门");
|
|
|
+ }
|
|
|
+ } catch (NullPointerException e) {
|
|
|
+ httpRespMsg.setError("验证失败");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新部门
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg updateDepartment(Integer departmentId, String departmentName, Integer superiorId,
|
|
|
+ HttpServletRequest request) {
|
|
|
+ HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
+ try {
|
|
|
+ Integer companyId = userMapper.selectById(request.getHeader("Token")).getCompanyId();
|
|
|
+ Department department = departmentMapper.selectById(departmentId);
|
|
|
+ if (!department.getCompanyId().equals(companyId)) {
|
|
|
+ httpRespMsg.setError("不能修改其他公司的部门");
|
|
|
+ } else if (!department.getSuperiorId().equals(superiorId) && checkBranch(departmentId)) {
|
|
|
+ httpRespMsg.setError("不能在有子级部门时修改父级部门");
|
|
|
+ } else if (superiorId != null && departmentMapper.selectCount(new QueryWrapper<Department>()
|
|
|
+ .eq("department_id", superiorId)
|
|
|
+ .eq("company_id", companyId)) == 0) {
|
|
|
+ httpRespMsg.setError("无所选父级部门");
|
|
|
+ } else {
|
|
|
+ if (departmentMapper.updateById(department
|
|
|
+ .setDepartmentName(departmentName)
|
|
|
+ .setSuperiorId(superiorId)) == 0) {
|
|
|
+ httpRespMsg.setError("修改失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (NullPointerException e) {
|
|
|
+ httpRespMsg.setError("验证失败");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ //删除部门
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg deleteDepartment(Integer departmentId, HttpServletRequest request) {
|
|
|
+ HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
+ try {
|
|
|
+ Integer companyId = userMapper.selectById(request.getHeader("Token")).getCompanyId();
|
|
|
+ Department department = departmentMapper.selectById(departmentId);
|
|
|
+ if (!department.getCompanyId().equals(companyId)) {
|
|
|
+ httpRespMsg.setError("不能删除其他公司的部门");
|
|
|
+ } else if (checkBranch(departmentId)) {
|
|
|
+ httpRespMsg.setError("不能在有子级部门时删除部门");
|
|
|
+ } else {
|
|
|
+ if (departmentMapper.deleteById(department) == 0) {
|
|
|
+ httpRespMsg.setError("修改失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (NullPointerException e) {
|
|
|
+ httpRespMsg.setError("验证失败");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ //查看有无子节点
|
|
|
+ private boolean checkBranch(Integer id) {
|
|
|
+ return departmentMapper.selectCount(new QueryWrapper<Department>().eq("superior_id", id)) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取部门列表
|
|
|
@Override
|
|
|
public HttpRespMsg getDepartmentList(HttpServletRequest request) {
|
|
|
HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
@@ -91,11 +176,11 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|
|
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());
|
|
|
}
|