SysDeptController.java 4.0 KB

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