SysDeptController.java 6.2 KB

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