SysDeptController.java 5.6 KB

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