SysDeptController.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package com.ruoyi.system.controller;
  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.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.core.constant.UserConstants;
  16. import com.ruoyi.common.core.utils.StringUtils;
  17. import com.ruoyi.common.core.web.controller.BaseController;
  18. import com.ruoyi.common.core.web.domain.AjaxResult;
  19. import com.ruoyi.common.log.annotation.Log;
  20. import com.ruoyi.common.log.enums.BusinessType;
  21. import com.ruoyi.common.security.annotation.RequiresPermissions;
  22. import com.ruoyi.common.security.utils.SecurityUtils;
  23. import com.ruoyi.system.api.domain.SysDept;
  24. import com.ruoyi.system.service.ISysDeptService;
  25. /**
  26. * 部门信息
  27. *
  28. * @author ruoyi
  29. */
  30. @RestController
  31. @RequestMapping("/dept")
  32. public class SysDeptController extends BaseController
  33. {
  34. @Autowired
  35. private ISysDeptService deptService;
  36. /**
  37. * 获取部门列表
  38. */
  39. @RequiresPermissions("system:dept:list")
  40. @GetMapping("/list")
  41. public AjaxResult list(SysDept dept)
  42. {
  43. List<SysDept> depts = deptService.selectDeptList(dept);
  44. return AjaxResult.success(depts);
  45. }
  46. /**
  47. * 查询部门列表(排除节点)
  48. */
  49. @RequiresPermissions("system:dept:list")
  50. @GetMapping("/list/exclude/{deptId}")
  51. public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId)
  52. {
  53. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  54. depts.removeIf(d -> d.getDeptId().intValue() == deptId
  55. || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
  56. return AjaxResult.success(depts);
  57. }
  58. /**
  59. * 根据部门编号获取详细信息
  60. */
  61. @RequiresPermissions("system:dept:query")
  62. @GetMapping(value = "/{deptId}")
  63. public AjaxResult getInfo(@PathVariable Long deptId)
  64. {
  65. deptService.checkDeptDataScope(deptId);
  66. return AjaxResult.success(deptService.selectDeptById(deptId));
  67. }
  68. /**
  69. * 获取部门下拉树列表
  70. */
  71. @GetMapping("/treeselect")
  72. public AjaxResult treeselect(SysDept dept)
  73. {
  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. {
  83. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  84. AjaxResult ajax = AjaxResult.success();
  85. ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  86. ajax.put("depts", deptService.buildDeptTreeSelect(depts));
  87. return ajax;
  88. }
  89. /**
  90. * 新增部门
  91. */
  92. @RequiresPermissions("system:dept:add")
  93. @Log(title = "部门管理", businessType = BusinessType.INSERT)
  94. @PostMapping
  95. public AjaxResult add(@Validated @RequestBody SysDept dept)
  96. {
  97. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  98. {
  99. return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  100. }
  101. dept.setCreateBy(SecurityUtils.getUsername());
  102. return toAjax(deptService.insertDept(dept));
  103. }
  104. /**
  105. * 修改部门
  106. */
  107. @RequiresPermissions("system:dept:edit")
  108. @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  109. @PutMapping
  110. public AjaxResult edit(@Validated @RequestBody SysDept dept)
  111. {
  112. Long deptId = dept.getDeptId();
  113. deptService.checkDeptDataScope(deptId);
  114. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  115. {
  116. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  117. }
  118. else if (dept.getParentId().equals(deptId))
  119. {
  120. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  121. }
  122. else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0)
  123. {
  124. return AjaxResult.error("该部门包含未停用的子部门!");
  125. }
  126. dept.setUpdateBy(SecurityUtils.getUsername());
  127. return toAjax(deptService.updateDept(dept));
  128. }
  129. /**
  130. * 删除部门
  131. */
  132. @RequiresPermissions("system:dept:remove")
  133. @Log(title = "部门管理", businessType = BusinessType.DELETE)
  134. @DeleteMapping("/{deptId}")
  135. public AjaxResult remove(@PathVariable Long deptId)
  136. {
  137. if (deptService.hasChildByDeptId(deptId))
  138. {
  139. return AjaxResult.error("存在下级部门,不允许删除");
  140. }
  141. if (deptService.checkDeptExistUser(deptId))
  142. {
  143. return AjaxResult.error("部门存在用户,不允许删除");
  144. }
  145. deptService.checkDeptDataScope(deptId);
  146. return toAjax(deptService.deleteDeptById(deptId));
  147. }
  148. }