SysDeptController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. return AjaxResult.success(deptService.selectDeptById(deptId));
  68. }
  69. /**
  70. * 获取部门下拉树列表
  71. */
  72. @GetMapping("/treeselect")
  73. public AjaxResult treeselect(SysDept dept)
  74. {
  75. List<SysDept> depts = deptService.selectDeptList(dept);
  76. return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
  77. }
  78. /**
  79. * 加载对应角色部门列表树
  80. */
  81. @GetMapping(value = "/roleDeptTreeselect/{roleId}")
  82. public AjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId)
  83. {
  84. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  85. Map<String,Object> ajax = new HashMap<>();
  86. ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  87. ajax.put("depts", deptService.buildDeptTreeSelect(depts));
  88. return AjaxResult.success(ajax);
  89. }
  90. /**
  91. * 新增部门
  92. */
  93. @PreAuthorize("@ss.hasPermi('system:dept:add')")
  94. @Log(title = "部门管理", businessType = BusinessType.INSERT)
  95. @PostMapping
  96. public AjaxResult add(@Validated @RequestBody SysDept dept)
  97. {
  98. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  99. {
  100. return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  101. }
  102. dept.setCreateBy(getUsername());
  103. return toAjax(deptService.insertDept(dept));
  104. }
  105. /**
  106. * 修改部门
  107. */
  108. @PreAuthorize("@ss.hasPermi('system:dept:edit')")
  109. @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  110. @PutMapping
  111. public AjaxResult edit(@Validated @RequestBody SysDept dept)
  112. {
  113. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  114. {
  115. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  116. }
  117. else if (dept.getParentId().equals(dept.getDeptId()))
  118. {
  119. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  120. }
  121. else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
  122. && deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0)
  123. {
  124. return AjaxResult.error("该部门包含未停用的子部门!");
  125. }
  126. dept.setUpdateBy(getUsername());
  127. return toAjax(deptService.updateDept(dept));
  128. }
  129. /**
  130. * 删除部门
  131. */
  132. @PreAuthorize("@ss.hasPermi('system:dept:remove')")
  133. @Log(title = "部门管理", businessType = BusinessType.DELETE)
  134. @DeleteMapping("/{deptId}")
  135. public AjaxResult remove(@PathVariable Long deptId)
  136. {
  137. if (deptService.hasChildByDeptId(deptId))
  138. {
  139. return AjaxResult.error("存在下级部门,不允许删除");
  140. }
  141. if (deptService.checkDeptExistUser(deptId))
  142. {
  143. return AjaxResult.error("部门存在用户,不允许删除");
  144. }
  145. return toAjax(deptService.deleteDeptById(deptId));
  146. }
  147. }