SysDeptController.java 4.9 KB

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