SysDeptController.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package com.boman.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.boman.domain.constant.UserConstants;
  16. import com.boman.common.core.utils.SecurityUtils;
  17. import com.boman.common.core.utils.StringUtils;
  18. import com.boman.common.core.web.controller.BaseController;
  19. import com.boman.domain.dto.AjaxResult;
  20. import com.boman.common.log.annotation.Log;
  21. import com.boman.common.log.enums.BusinessType;
  22. import com.boman.common.security.annotation.PreAuthorize;
  23. import com.boman.domain.SysDept;
  24. import com.boman.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. // @PreAuthorize(hasPermi = "system:dept:list")
  40. @GetMapping("/list")
  41. public AjaxResult list(SysDept dept) {
  42. List<SysDept> depts = deptService.selectDeptList(dept);
  43. return AjaxResult.success(depts);
  44. }
  45. /**
  46. * 查询部门列表(排除节点)
  47. */
  48. @PreAuthorize(hasPermi = "system:dept:list")
  49. @GetMapping("/list/exclude/{id}")
  50. public AjaxResult excludeChild(@PathVariable(value = "id", required = false) Long id)
  51. {
  52. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  53. Iterator<SysDept> it = depts.iterator();
  54. while (it.hasNext())
  55. {
  56. SysDept d = (SysDept) it.next();
  57. if (d.getId().intValue() == id
  58. || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), id + ""))
  59. {
  60. it.remove();
  61. }
  62. }
  63. return AjaxResult.success(depts);
  64. }
  65. /**
  66. * 功能描述: 拿到部门下所有的部门, 包含传过来的deptId
  67. *
  68. * @param deptId deptId
  69. * @return com.boman.domain.dto.AjaxResult
  70. */
  71. @GetMapping("/list/children/depts/{deptId}")
  72. public List<SysDept> listChildrenDepts(@PathVariable(value = "deptId") Long deptId) {
  73. return deptService.listChildrenDepts(deptId);
  74. }
  75. /**
  76. * 根据部门编号获取详细信息
  77. */
  78. @PreAuthorize(hasPermi = "system:dept:query")
  79. @GetMapping(value = "/{id}")
  80. public AjaxResult getInfo(@PathVariable Long id)
  81. {
  82. return AjaxResult.success(deptService.selectDeptById(id));
  83. }
  84. /**
  85. * 根据部门编号获取详细信息
  86. */
  87. @PreAuthorize(hasPermi = "system:dept:query")
  88. @GetMapping(value = "/getById/{id}")
  89. public SysDept getById(@PathVariable("id") Long id)
  90. {
  91. return deptService.selectDeptById(id);
  92. }
  93. /**
  94. * 获取部门下拉树列表
  95. */
  96. @GetMapping("/treeselect")
  97. public AjaxResult treeselect(SysDept dept)
  98. {
  99. List<SysDept> depts = deptService.selectDeptList(dept);
  100. return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
  101. }
  102. /**
  103. * 获取部门下拉树列表
  104. */
  105. @GetMapping("/depts")
  106. public AjaxResult depts(SysDept dept)
  107. {
  108. List<SysDept> depts = deptService.selectDepts(dept);
  109. return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
  110. }
  111. /**
  112. * 加载对应角色部门列表树
  113. */
  114. @GetMapping(value = "/roleDeptTreeselect/{roleId}")
  115. public AjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId)
  116. {
  117. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  118. AjaxResult ajax = AjaxResult.success();
  119. ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  120. ajax.put("depts", deptService.buildDeptTreeSelect(depts));
  121. return ajax;
  122. }
  123. /**
  124. * 新增部门
  125. */
  126. @PreAuthorize(hasPermi = "system:dept:add")
  127. @Log(title = "部门管理", businessType = BusinessType.INSERT)
  128. @PostMapping
  129. public AjaxResult add(@Validated @RequestBody SysDept dept)
  130. {
  131. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  132. {
  133. return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  134. }
  135. dept.setCreateBy(SecurityUtils.getUsername());
  136. return toAjax(deptService.insertDept(dept));
  137. }
  138. /**
  139. * 修改部门
  140. */
  141. @PreAuthorize(hasPermi = "system:dept:edit")
  142. @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  143. @PutMapping
  144. public AjaxResult edit(@Validated @RequestBody SysDept dept)
  145. {
  146. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  147. {
  148. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  149. }
  150. else if (dept.getParentId().equals(dept.getId()))
  151. {
  152. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  153. }
  154. else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
  155. && deptService.selectNormalChildrenDeptById(dept.getId()) > 0)
  156. {
  157. return AjaxResult.error("该部门包含未停用的子部门!");
  158. }
  159. dept.setUpdateBy(SecurityUtils.getUsername());
  160. return toAjax(deptService.updateDept(dept));
  161. }
  162. /**
  163. * 删除部门
  164. */
  165. @PreAuthorize(hasPermi = "system:dept:remove")
  166. @Log(title = "部门管理", businessType = BusinessType.DELETE)
  167. @DeleteMapping("/{id}")
  168. public AjaxResult remove(@PathVariable Long id)
  169. {
  170. if (deptService.hasChildById(id))
  171. {
  172. return AjaxResult.error("存在下级部门,不允许删除");
  173. }
  174. if (deptService.checkDeptExistUser(id))
  175. {
  176. return AjaxResult.error("部门存在用户,不允许删除");
  177. }
  178. return toAjax(deptService.deleteDeptById(id));
  179. }
  180. }