SysDeptController.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.ozs.web.controller.system;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import io.swagger.annotations.Api;
  5. import io.swagger.annotations.ApiOperation;
  6. import org.apache.commons.lang3.ArrayUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.security.access.prepost.PreAuthorize;
  9. import org.springframework.util.ObjectUtils;
  10. import org.springframework.validation.annotation.Validated;
  11. import org.springframework.web.bind.annotation.DeleteMapping;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.PathVariable;
  14. import org.springframework.web.bind.annotation.PostMapping;
  15. import org.springframework.web.bind.annotation.PutMapping;
  16. import org.springframework.web.bind.annotation.RequestBody;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import com.ozs.common.annotation.Log;
  20. import com.ozs.common.constant.UserConstants;
  21. import com.ozs.common.core.controller.BaseController;
  22. import com.ozs.common.core.domain.AjaxResult;
  23. import com.ozs.common.core.domain.entity.SysDept;
  24. import com.ozs.common.enums.BusinessType;
  25. import com.ozs.common.utils.StringUtils;
  26. import com.ozs.system.service.ISysDeptService;
  27. /**
  28. * 部门信息
  29. *
  30. * @author ruoyi
  31. */
  32. @Api(tags = "部门信息")
  33. @RestController
  34. @RequestMapping("/system/dept")
  35. public class SysDeptController extends BaseController {
  36. @Autowired
  37. private ISysDeptService deptService;
  38. /**
  39. * 获取部门列表
  40. */
  41. @PreAuthorize("@ss.hasPermi('system:dept:list')")
  42. @GetMapping("/list")
  43. @ApiOperation("获取部门列表")
  44. public AjaxResult list(SysDept dept) {
  45. List<SysDept> depts = deptService.selectDeptList(dept);
  46. return success(depts);
  47. }
  48. /**
  49. * 获取部门列表(包含自己上一级)
  50. */
  51. @GetMapping("/listAndUp")
  52. @ApiOperation("获取部门列表(包含自己上一级)")
  53. public AjaxResult listAndUp(SysDept dept) {
  54. List<SysDept> depts = deptService.selectDeptListAndUp(dept);
  55. return success(depts);
  56. }
  57. /**
  58. * 获取部门列表
  59. */
  60. @GetMapping("/listByUser")
  61. @ApiOperation("根据获取本级和子孙级部门列表")
  62. public AjaxResult listByUser() {
  63. Long deptId = getDeptId();
  64. List<SysDept> list = new ArrayList<>();
  65. SysDept s = deptService.selectById(deptId);
  66. if (!ObjectUtils.isEmpty(s)) {
  67. list.add(s);
  68. List<SysDept> sl = deptService.selectChildrenDeptById(deptId);
  69. if (!ObjectUtils.isEmpty(sl)) {
  70. list.addAll(sl);
  71. }
  72. }
  73. return success(list);
  74. }
  75. /**
  76. * 查询部门列表(排除节点)
  77. */
  78. @PreAuthorize("@ss.hasPermi('system:dept:list')")
  79. @GetMapping("/list/exclude/{deptId}")
  80. @ApiOperation("查询部门列表(排除节点)")
  81. public AjaxResult excludeChild(@PathVariable(value = "deptId" , required = false) Long deptId) {
  82. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  83. depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
  84. return success(depts);
  85. }
  86. /**
  87. * 根据部门编号获取详细信息
  88. */
  89. @GetMapping(value = "/{deptId}")
  90. @ApiOperation("根据部门编号获取详细信息")
  91. public AjaxResult getInfo(@PathVariable Long deptId) {
  92. deptService.checkDeptDataScope(deptId);
  93. return success(deptService.selectDeptById(deptId));
  94. }
  95. /**
  96. * 新增部门
  97. */
  98. @PreAuthorize("@ss.hasPermi('system:dept:add')")
  99. @Log(title = "部门管理" , businessType = BusinessType.INSERT)
  100. @PostMapping
  101. @ApiOperation("新增部门")
  102. public AjaxResult add(@Validated @RequestBody SysDept dept) {
  103. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
  104. return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  105. }
  106. dept.setCreateBy(getUsername());
  107. return deptService.insertDept(dept);
  108. }
  109. /**
  110. * 修改部门
  111. */
  112. @PreAuthorize("@ss.hasPermi('system:dept:edit')")
  113. @Log(title = "部门管理" , businessType = BusinessType.UPDATE)
  114. @PutMapping
  115. @ApiOperation("修改部门")
  116. public AjaxResult edit(@Validated @RequestBody SysDept dept) {
  117. Long deptId = dept.getDeptId();
  118. deptService.checkDeptDataScope(deptId);
  119. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
  120. return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  121. } else if (dept.getParentId().equals(deptId)) {
  122. return error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  123. } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0) {
  124. return error("该部门包含未停用的子部门!");
  125. }
  126. dept.setUpdateBy(getUsername());
  127. return toAjax(deptService.updateDept(dept));
  128. }
  129. /**
  130. * 删除部门
  131. */
  132. @PreAuthorize("@ss.hasPermi('system:dept:remove')")
  133. @Log(title = "部门管理" , businessType = BusinessType.DELETE)
  134. @DeleteMapping("/{deptId}")
  135. @ApiOperation("删除部门")
  136. public AjaxResult remove(@PathVariable Long deptId) {
  137. if (deptService.hasChildByDeptId(deptId)) {
  138. return warn("存在下级部门,不允许删除");
  139. }
  140. if (deptService.checkDeptExistUser(deptId)) {
  141. return warn("部门存在用户,不允许删除");
  142. }
  143. deptService.checkDeptDataScope(deptId);
  144. return toAjax(deptService.deleteDeptById(deptId));
  145. }
  146. }