SysDeptController.java 5.1 KB

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