SysDeptController.java 4.9 KB

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