SysDeptController.java 4.2 KB

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