SysDeptController.java 4.7 KB

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