SysDeptController.java 5.1 KB

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