TopSysDeptController.java 6.1 KB

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