SysDeptController.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.ruoyi.web.controller.system;
  2. import java.util.List;
  3. import org.apache.shiro.authz.annotation.RequiresPermissions;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.ModelMap;
  7. import org.springframework.validation.annotation.Validated;
  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.RequestMapping;
  12. import org.springframework.web.bind.annotation.ResponseBody;
  13. import com.ruoyi.common.annotation.Log;
  14. import com.ruoyi.common.constant.UserConstants;
  15. import com.ruoyi.common.core.controller.BaseController;
  16. import com.ruoyi.common.core.domain.AjaxResult;
  17. import com.ruoyi.common.core.domain.Ztree;
  18. import com.ruoyi.common.core.domain.entity.SysDept;
  19. import com.ruoyi.common.enums.BusinessType;
  20. import com.ruoyi.common.utils.StringUtils;
  21. import com.ruoyi.system.service.ISysDeptService;
  22. /**
  23. * 部门信息
  24. *
  25. * @author ruoyi
  26. */
  27. @Controller
  28. @RequestMapping("/system/dept")
  29. public class SysDeptController extends BaseController
  30. {
  31. private String prefix = "system/dept";
  32. @Autowired
  33. private ISysDeptService deptService;
  34. @RequiresPermissions("system:dept:view")
  35. @GetMapping()
  36. public String dept()
  37. {
  38. return prefix + "/dept";
  39. }
  40. @RequiresPermissions("system:dept:list")
  41. @PostMapping("/list")
  42. @ResponseBody
  43. public List<SysDept> list(SysDept dept)
  44. {
  45. List<SysDept> deptList = deptService.selectDeptList(dept);
  46. return deptList;
  47. }
  48. /**
  49. * 新增部门
  50. */
  51. @GetMapping("/add/{parentId}")
  52. public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
  53. {
  54. if (!getSysUser().isAdmin())
  55. {
  56. parentId = getSysUser().getDeptId();
  57. }
  58. mmap.put("dept", deptService.selectDeptById(parentId));
  59. return prefix + "/add";
  60. }
  61. /**
  62. * 新增保存部门
  63. */
  64. @Log(title = "部门管理", businessType = BusinessType.INSERT)
  65. @RequiresPermissions("system:dept:add")
  66. @PostMapping("/add")
  67. @ResponseBody
  68. public AjaxResult addSave(@Validated SysDept dept)
  69. {
  70. if (!deptService.checkDeptNameUnique(dept))
  71. {
  72. return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  73. }
  74. dept.setCreateBy(getLoginName());
  75. return toAjax(deptService.insertDept(dept));
  76. }
  77. /**
  78. * 修改部门
  79. */
  80. @RequiresPermissions("system:dept:edit")
  81. @GetMapping("/edit/{deptId}")
  82. public String edit(@PathVariable("deptId") Long deptId, ModelMap mmap)
  83. {
  84. deptService.checkDeptDataScope(deptId);
  85. SysDept dept = deptService.selectDeptById(deptId);
  86. if (StringUtils.isNotNull(dept) && 100L == deptId)
  87. {
  88. dept.setParentName("无");
  89. }
  90. mmap.put("dept", dept);
  91. return prefix + "/edit";
  92. }
  93. /**
  94. * 修改保存部门
  95. */
  96. @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  97. @RequiresPermissions("system:dept:edit")
  98. @PostMapping("/edit")
  99. @ResponseBody
  100. public AjaxResult editSave(@Validated SysDept dept)
  101. {
  102. Long deptId = dept.getDeptId();
  103. deptService.checkDeptDataScope(deptId);
  104. if (!deptService.checkDeptNameUnique(dept))
  105. {
  106. return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  107. }
  108. else if (dept.getParentId().equals(deptId))
  109. {
  110. return error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  111. }
  112. else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0)
  113. {
  114. return AjaxResult.error("该部门包含未停用的子部门!");
  115. }
  116. dept.setUpdateBy(getLoginName());
  117. return toAjax(deptService.updateDept(dept));
  118. }
  119. /**
  120. * 删除
  121. */
  122. @Log(title = "部门管理", businessType = BusinessType.DELETE)
  123. @RequiresPermissions("system:dept:remove")
  124. @GetMapping("/remove/{deptId}")
  125. @ResponseBody
  126. public AjaxResult remove(@PathVariable("deptId") Long deptId)
  127. {
  128. if (deptService.selectDeptCount(deptId) > 0)
  129. {
  130. return AjaxResult.warn("存在下级部门,不允许删除");
  131. }
  132. if (deptService.checkDeptExistUser(deptId))
  133. {
  134. return AjaxResult.warn("部门存在用户,不允许删除");
  135. }
  136. deptService.checkDeptDataScope(deptId);
  137. return toAjax(deptService.deleteDeptById(deptId));
  138. }
  139. /**
  140. * 校验部门名称
  141. */
  142. @PostMapping("/checkDeptNameUnique")
  143. @ResponseBody
  144. public boolean checkDeptNameUnique(SysDept dept)
  145. {
  146. return deptService.checkDeptNameUnique(dept);
  147. }
  148. /**
  149. * 选择部门树
  150. *
  151. * @param deptId 部门ID
  152. * @param excludeId 排除ID
  153. */
  154. @GetMapping(value = { "/selectDeptTree/{deptId}", "/selectDeptTree/{deptId}/{excludeId}" })
  155. public String selectDeptTree(@PathVariable("deptId") Long deptId,
  156. @PathVariable(value = "excludeId", required = false) Long excludeId, ModelMap mmap)
  157. {
  158. mmap.put("dept", deptService.selectDeptById(deptId));
  159. mmap.put("excludeId", excludeId);
  160. return prefix + "/tree";
  161. }
  162. /**
  163. * 加载部门列表树(排除下级)
  164. */
  165. @GetMapping("/treeData/{excludeId}")
  166. @ResponseBody
  167. public List<Ztree> treeDataExcludeChild(@PathVariable(value = "excludeId", required = false) Long excludeId)
  168. {
  169. SysDept dept = new SysDept();
  170. dept.setExcludeId(excludeId);
  171. List<Ztree> ztrees = deptService.selectDeptTreeExcludeChild(dept);
  172. return ztrees;
  173. }
  174. }