SysDeptController.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package com.ruoyi.project.system.controller;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.security.access.prepost.PreAuthorize;
  5. import org.springframework.validation.annotation.Validated;
  6. import org.springframework.web.bind.annotation.DeleteMapping;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.PutMapping;
  11. import org.springframework.web.bind.annotation.RequestBody;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import com.ruoyi.common.constant.UserConstants;
  15. import com.ruoyi.common.utils.SecurityUtils;
  16. import com.ruoyi.framework.aspectj.lang.annotation.Log;
  17. import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
  18. import com.ruoyi.framework.web.controller.BaseController;
  19. import com.ruoyi.framework.web.domain.AjaxResult;
  20. import com.ruoyi.project.system.domain.SysDept;
  21. import com.ruoyi.project.system.service.ISysDeptService;
  22. /**
  23. * 部门信息
  24. *
  25. * @author ruoyi
  26. */
  27. @RestController
  28. @RequestMapping("/system/dept")
  29. public class SysDeptController extends BaseController
  30. {
  31. @Autowired
  32. private ISysDeptService deptService;
  33. /**
  34. * 获取部门列表
  35. */
  36. @PreAuthorize("@ss.hasPermi('system:dept:list')")
  37. @GetMapping("/list")
  38. public AjaxResult list(SysDept dept)
  39. {
  40. List<SysDept> depts = deptService.selectDeptList(dept);
  41. return AjaxResult.success(deptService.buildDeptTree(depts));
  42. }
  43. /**
  44. * 根据部门编号获取详细信息
  45. */
  46. @PreAuthorize("@ss.hasPermi('system:dept:query')")
  47. @GetMapping(value = "/{deptId}")
  48. public AjaxResult getInfo(@PathVariable Long deptId)
  49. {
  50. return AjaxResult.success(deptService.selectDeptById(deptId));
  51. }
  52. /**
  53. * 获取部门下拉树列表
  54. */
  55. @GetMapping("/treeselect")
  56. public AjaxResult treeselect(SysDept dept)
  57. {
  58. List<SysDept> depts = deptService.selectDeptList(dept);
  59. return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
  60. }
  61. /**
  62. * 加载对应角色部门列表树
  63. */
  64. @GetMapping(value = "/roleDeptTreeselect/{roleId}")
  65. public AjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId)
  66. {
  67. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  68. AjaxResult ajax = AjaxResult.success();
  69. ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  70. ajax.put("depts", deptService.buildDeptTreeSelect(depts));
  71. return ajax;
  72. }
  73. /**
  74. * 新增部门
  75. */
  76. @PreAuthorize("@ss.hasPermi('system:dept:add')")
  77. @Log(title = "部门管理", businessType = BusinessType.INSERT)
  78. @PostMapping
  79. public AjaxResult add(@Validated @RequestBody SysDept dept)
  80. {
  81. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  82. {
  83. return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  84. }
  85. dept.setCreateBy(SecurityUtils.getUsername());
  86. return toAjax(deptService.insertDept(dept));
  87. }
  88. /**
  89. * 修改部门
  90. */
  91. @PreAuthorize("@ss.hasPermi('system:dept:edit')")
  92. @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  93. @PutMapping
  94. public AjaxResult edit(@Validated @RequestBody SysDept dept)
  95. {
  96. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  97. {
  98. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  99. }
  100. else if (dept.getParentId().equals(dept.getDeptId()))
  101. {
  102. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  103. }
  104. dept.setUpdateBy(SecurityUtils.getUsername());
  105. return toAjax(deptService.updateDept(dept));
  106. }
  107. /**
  108. * 删除部门
  109. */
  110. @PreAuthorize("@ss.hasPermi('system:dept:remove')")
  111. @Log(title = "部门管理", businessType = BusinessType.DELETE)
  112. @DeleteMapping("/{deptId}")
  113. public AjaxResult remove(@PathVariable Long deptId)
  114. {
  115. if (deptService.hasChildByDeptId(deptId))
  116. {
  117. return AjaxResult.error("存在下级部门,不允许删除");
  118. }
  119. if (deptService.checkDeptExistUser(deptId))
  120. {
  121. return AjaxResult.error("部门存在用户,不允许删除");
  122. }
  123. return toAjax(deptService.deleteDeptById(deptId));
  124. }
  125. }