SysDeptController.java 4.1 KB

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