SysDeptController.java 5.7 KB

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