SysDeptController.java 5.0 KB

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