SysDeptController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.ruoyi.web.controller.system;
  2. import cn.hutool.core.util.StrUtil;
  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.SecurityUtils;
  10. import com.ruoyi.system.service.ISysDeptService;
  11. import org.apache.commons.lang3.ArrayUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.security.access.prepost.PreAuthorize;
  14. import org.springframework.validation.annotation.Validated;
  15. import org.springframework.web.bind.annotation.*;
  16. import java.util.HashMap;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import java.util.Map;
  20. /**
  21. * 部门信息
  22. *
  23. * @author ruoyi
  24. */
  25. @RestController
  26. @RequestMapping("/system/dept")
  27. public class SysDeptController extends BaseController
  28. {
  29. @Autowired
  30. private ISysDeptService deptService;
  31. /**
  32. * 获取部门列表
  33. */
  34. @PreAuthorize("@ss.hasPermi('system:dept:list')")
  35. @GetMapping("/list")
  36. public AjaxResult list(SysDept dept)
  37. {
  38. List<SysDept> depts = deptService.selectDeptList(dept);
  39. return AjaxResult.success(depts);
  40. }
  41. /**
  42. * 查询部门列表(排除节点)
  43. */
  44. @PreAuthorize("@ss.hasPermi('system:dept:list')")
  45. @GetMapping("/list/exclude/{deptId}")
  46. public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId)
  47. {
  48. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  49. Iterator<SysDept> it = depts.iterator();
  50. while (it.hasNext())
  51. {
  52. SysDept d = (SysDept) it.next();
  53. if (d.getDeptId().intValue() == deptId
  54. || ArrayUtils.contains(StrUtil.splitToArray(d.getAncestors(), ","), deptId + ""))
  55. {
  56. it.remove();
  57. }
  58. }
  59. return AjaxResult.success(depts);
  60. }
  61. /**
  62. * 根据部门编号获取详细信息
  63. */
  64. @PreAuthorize("@ss.hasPermi('system:dept:query')")
  65. @GetMapping(value = "/{deptId}")
  66. public AjaxResult getInfo(@PathVariable Long deptId)
  67. {
  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(SecurityUtils.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 (StrUtil.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
  123. && deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0)
  124. {
  125. return AjaxResult.error("该部门包含未停用的子部门!");
  126. }
  127. dept.setUpdateBy(SecurityUtils.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. }