SysDeptController.java 5.8 KB

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