SysDeptController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. @GetMapping("/treeselect")
  59. public AjaxResult treeselect(SysDept dept)
  60. {
  61. List<SysDept> depts = deptService.selectDeptList(dept);
  62. return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
  63. }
  64. /**
  65. * 根据部门编号获取详细信息
  66. */
  67. @PreAuthorize("@ss.hasPermi('system:dept:query')")
  68. @GetMapping(value = "/{deptId}")
  69. public AjaxResult getInfo(@PathVariable Long deptId)
  70. {
  71. deptService.checkDeptDataScope(deptId);
  72. return success(deptService.selectDeptById(deptId));
  73. }
  74. /**
  75. * 新增部门
  76. */
  77. @PreAuthorize("@ss.hasPermi('system:dept:add')")
  78. @Log(title = "部门管理", businessType = BusinessType.INSERT)
  79. @PostMapping
  80. public AjaxResult add(@Validated @RequestBody SysDept dept)
  81. {
  82. if (!deptService.checkDeptNameUnique(dept))
  83. {
  84. return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  85. }
  86. dept.setCreateBy(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. {
  97. Long deptId = dept.getDeptId();
  98. deptService.checkDeptDataScope(deptId);
  99. if (!deptService.checkDeptNameUnique(dept))
  100. {
  101. return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  102. }
  103. else if (dept.getParentId().equals(deptId))
  104. {
  105. return error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  106. }
  107. else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0)
  108. {
  109. return error("该部门包含未停用的子部门!");
  110. }
  111. dept.setUpdateBy(getUsername());
  112. return toAjax(deptService.updateDept(dept));
  113. }
  114. /**
  115. * 删除部门
  116. */
  117. @PreAuthorize("@ss.hasPermi('system:dept:remove')")
  118. @Log(title = "部门管理", businessType = BusinessType.DELETE)
  119. @DeleteMapping("/{deptId}")
  120. public AjaxResult remove(@PathVariable Long deptId)
  121. {
  122. if (deptService.hasChildByDeptId(deptId))
  123. {
  124. return warn("存在下级部门,不允许删除");
  125. }
  126. if (deptService.checkDeptExistUser(deptId))
  127. {
  128. return warn("部门存在用户,不允许删除");
  129. }
  130. deptService.checkDeptDataScope(deptId);
  131. return toAjax(deptService.deleteDeptById(deptId));
  132. }
  133. }