SysDeptController.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 org.apache.commons.lang3.ArrayUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.security.access.prepost.PreAuthorize;
  14. import org.springframework.validation.annotation.Validated;
  15. import org.springframework.web.bind.annotation.DeleteMapping;
  16. import org.springframework.web.bind.annotation.GetMapping;
  17. import org.springframework.web.bind.annotation.PathVariable;
  18. import org.springframework.web.bind.annotation.PostMapping;
  19. import org.springframework.web.bind.annotation.PutMapping;
  20. import org.springframework.web.bind.annotation.RequestBody;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.RestController;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. /**
  26. * 部门信息
  27. *
  28. * @author ruoyi
  29. */
  30. @RestController
  31. @RequestMapping("/system/dept")
  32. public class SysDeptController extends BaseController {
  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. List<SysDept> depts = deptService.selectDeptList(dept);
  42. return AjaxResult.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) String deptId) {
  50. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  51. Iterator<SysDept> it = depts.iterator();
  52. while (it.hasNext()) {
  53. SysDept d = (SysDept) it.next();
  54. if (d.getDeptId().equals(deptId)
  55. || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId)) {
  56. it.remove();
  57. }
  58. }
  59. return AjaxResult.success(depts);
  60. }
  61. /**
  62. * 根据部门编号获取详细信息
  63. */
  64. @PreAuthorize("@ss.hasPermi('system:dept:query')")
  65. @GetMapping(value = "/{deptId}")
  66. public AjaxResult getInfo(@PathVariable String deptId) {
  67. return AjaxResult.success(deptService.selectDeptById(deptId));
  68. }
  69. /**
  70. * 获取部门下拉树列表
  71. */
  72. @GetMapping("/treeselect")
  73. public AjaxResult treeselect(SysDept dept) {
  74. List<SysDept> depts = deptService.selectDeptList(dept);
  75. return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
  76. }
  77. /**
  78. * 加载对应角色部门列表树
  79. */
  80. @GetMapping(value = "/roleDeptTreeselect/{roleId}")
  81. public AjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId) {
  82. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  83. AjaxResult ajax = AjaxResult.success();
  84. ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  85. ajax.put("depts", deptService.buildDeptTreeSelect(depts));
  86. return ajax;
  87. }
  88. /**
  89. * 新增部门
  90. */
  91. @PreAuthorize("@ss.hasPermi('system:dept:add')")
  92. @Log(title = "部门管理", businessType = BusinessType.INSERT)
  93. @PostMapping
  94. public AjaxResult add(@Validated @RequestBody SysDept dept) {
  95. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
  96. return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  97. }
  98. dept.setCreateBy(SecurityUtils.getUsername());
  99. return toAjax(deptService.insertDept(dept));
  100. }
  101. /**
  102. * 修改部门
  103. */
  104. @PreAuthorize("@ss.hasPermi('system:dept:edit')")
  105. @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  106. @PutMapping
  107. public AjaxResult edit(@Validated @RequestBody SysDept dept) {
  108. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
  109. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  110. } else if (dept.getParentId().equals(dept.getDeptId())) {
  111. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  112. } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
  113. && deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0) {
  114. return AjaxResult.error("该部门包含未停用的子部门!");
  115. }
  116. dept.setUpdateBy(SecurityUtils.getUsername());
  117. return toAjax(deptService.updateDept(dept));
  118. }
  119. /**
  120. * 删除部门
  121. */
  122. @PreAuthorize("@ss.hasPermi('system:dept:remove')")
  123. @Log(title = "部门管理", businessType = BusinessType.DELETE)
  124. @DeleteMapping("/{deptId}")
  125. public AjaxResult remove(@PathVariable String deptId) {
  126. if (deptService.hasChildByDeptId(deptId)) {
  127. return AjaxResult.error("存在下级部门,不允许删除");
  128. }
  129. if (deptService.checkDeptExistUser(deptId)) {
  130. return AjaxResult.error("部门存在用户,不允许删除");
  131. }
  132. return toAjax(deptService.deleteDeptById(deptId));
  133. }
  134. }