SysDeptController.java 5.2 KB

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