SysDeptController.java 5.1 KB

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