SysDeptController.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.ruoyi.system.controller;
  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.validation.annotation.Validated;
  7. import org.springframework.web.bind.annotation.DeleteMapping;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.bind.annotation.PutMapping;
  12. import org.springframework.web.bind.annotation.RequestBody;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import com.ruoyi.common.core.constant.UserConstants;
  16. import com.ruoyi.common.core.utils.StringUtils;
  17. import com.ruoyi.common.core.web.controller.BaseController;
  18. import com.ruoyi.common.core.web.domain.AjaxResult;
  19. import com.ruoyi.common.log.annotation.Log;
  20. import com.ruoyi.common.log.enums.BusinessType;
  21. import com.ruoyi.common.security.annotation.RequiresPermissions;
  22. import com.ruoyi.common.security.utils.SecurityUtils;
  23. import com.ruoyi.system.api.domain.SysDept;
  24. import com.ruoyi.system.service.ISysDeptService;
  25. /**
  26. * 部门信息
  27. *
  28. * @author ruoyi
  29. */
  30. @RestController
  31. @RequestMapping("/dept")
  32. public class SysDeptController extends BaseController
  33. {
  34. @Autowired
  35. private ISysDeptService deptService;
  36. /**
  37. * 获取部门列表
  38. */
  39. @RequiresPermissions("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. @RequiresPermissions("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. @RequiresPermissions("system:dept:query")
  70. @GetMapping(value = "/{deptId}")
  71. public AjaxResult getInfo(@PathVariable Long deptId)
  72. {
  73. deptService.checkDeptDataScope(deptId);
  74. return AjaxResult.success(deptService.selectDeptById(deptId));
  75. }
  76. /**
  77. * 获取部门下拉树列表
  78. */
  79. @GetMapping("/treeselect")
  80. public AjaxResult treeselect(SysDept dept)
  81. {
  82. List<SysDept> depts = deptService.selectDeptList(dept);
  83. return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
  84. }
  85. /**
  86. * 加载对应角色部门列表树
  87. */
  88. @GetMapping(value = "/roleDeptTreeselect/{roleId}")
  89. public AjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId)
  90. {
  91. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  92. AjaxResult ajax = AjaxResult.success();
  93. ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  94. ajax.put("depts", deptService.buildDeptTreeSelect(depts));
  95. return ajax;
  96. }
  97. /**
  98. * 新增部门
  99. */
  100. @RequiresPermissions("system:dept:add")
  101. @Log(title = "部门管理", businessType = BusinessType.INSERT)
  102. @PostMapping
  103. public AjaxResult add(@Validated @RequestBody SysDept dept)
  104. {
  105. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  106. {
  107. return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  108. }
  109. dept.setCreateBy(SecurityUtils.getUsername());
  110. return toAjax(deptService.insertDept(dept));
  111. }
  112. /**
  113. * 修改部门
  114. */
  115. @RequiresPermissions("system:dept:edit")
  116. @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  117. @PutMapping
  118. public AjaxResult edit(@Validated @RequestBody SysDept dept)
  119. {
  120. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  121. {
  122. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  123. }
  124. else if (dept.getParentId().equals(dept.getDeptId()))
  125. {
  126. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  127. }
  128. else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
  129. && deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0)
  130. {
  131. return AjaxResult.error("该部门包含未停用的子部门!");
  132. }
  133. dept.setUpdateBy(SecurityUtils.getUsername());
  134. return toAjax(deptService.updateDept(dept));
  135. }
  136. /**
  137. * 删除部门
  138. */
  139. @RequiresPermissions("system:dept:remove")
  140. @Log(title = "部门管理", businessType = BusinessType.DELETE)
  141. @DeleteMapping("/{deptId}")
  142. public AjaxResult remove(@PathVariable Long deptId)
  143. {
  144. if (deptService.hasChildByDeptId(deptId))
  145. {
  146. return AjaxResult.error("存在下级部门,不允许删除");
  147. }
  148. if (deptService.checkDeptExistUser(deptId))
  149. {
  150. return AjaxResult.error("部门存在用户,不允许删除");
  151. }
  152. return toAjax(deptService.deleteDeptById(deptId));
  153. }
  154. }