SysDeptController.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package com.dgtly.web.controller.system;
  2. import com.dgtly.common.annotation.Log;
  3. import com.dgtly.common.constant.UserConstants;
  4. import com.dgtly.common.core.controller.BaseController;
  5. import com.dgtly.common.core.domain.AjaxResult;
  6. import com.dgtly.common.core.domain.Ztree;
  7. import com.dgtly.common.enums.BusinessType;
  8. import com.dgtly.common.exception.BusinessException;
  9. import com.dgtly.common.utils.StringUtils;
  10. import com.dgtly.framework.util.ShiroUtils;
  11. import com.dgtly.system.domain.SysDept;
  12. import com.dgtly.system.domain.SysRole;
  13. import com.dgtly.system.domain.SysUser;
  14. import com.dgtly.system.service.ISysConfigService;
  15. import com.dgtly.system.service.ISysDeptService;
  16. import org.apache.shiro.authz.annotation.RequiresPermissions;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.ui.ModelMap;
  20. import org.springframework.validation.annotation.Validated;
  21. import org.springframework.web.bind.annotation.*;
  22. import java.util.List;
  23. /**
  24. * 部门信息
  25. *
  26. * @author ruoyi
  27. */
  28. @Controller
  29. @RequestMapping("/system/dept")
  30. public class SysDeptController extends BaseController
  31. {
  32. private String prefix = "system/dept";
  33. @Autowired
  34. private ISysDeptService deptService;
  35. @Autowired
  36. private ISysConfigService configService;
  37. @RequiresPermissions("system:dept:view")
  38. @GetMapping()
  39. public String dept(ModelMap mmap)
  40. {
  41. // 取身份信息
  42. SysUser user = ShiroUtils.getSysUser();
  43. mmap.put("user",user);
  44. return prefix + "/dept";
  45. }
  46. @RequiresPermissions("system:dept:list")
  47. @PostMapping("/list")
  48. @ResponseBody
  49. public List<SysDept> list(SysDept dept)
  50. {
  51. SysUser user = ShiroUtils.getSysUser();
  52. dept.setCompanyId(user.getCompanyId());
  53. List<SysDept> deptList = deptService.selectDeptList(dept);
  54. return deptList;
  55. }
  56. /**
  57. * 新增部门
  58. */
  59. @GetMapping("/add/{deptId}")
  60. public String add(@PathVariable("deptId") Long deptId, ModelMap mmap)
  61. {
  62. SysDept info =deptService.selectDeptById(deptId);
  63. SysDept dept = new SysDept();
  64. if(info.getParentId()==0){
  65. dept.setDeptId(info.getDeptId());
  66. dept.setDeptName(info.getDeptName());
  67. }else{
  68. dept.setDeptId(info.getParentId());
  69. dept.setDeptName(info.getParentName());
  70. }
  71. mmap.put("dept",dept);
  72. return prefix + "/add";
  73. }
  74. /**
  75. * 新增保存部门
  76. */
  77. @Log(title = "部门管理", businessType = BusinessType.INSERT)
  78. @RequiresPermissions("system:dept:add")
  79. @PostMapping("/add")
  80. @ResponseBody
  81. public AjaxResult addSave(@Validated SysDept dept)
  82. {
  83. SysUser user = ShiroUtils.getSysUser();
  84. if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  85. {
  86. return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  87. }else if(dept.getDeptName().length()>9){
  88. return error("修改部门'" + dept.getDeptName() + "'失败,部门名称长度过长(9个字符)");
  89. }
  90. dept.setCompanyId(user.getCompanyId());
  91. dept.setCreateBy(user.getUserId().toString());
  92. dept.setDeptId(dept.getDeptId());
  93. SysDept parent = deptService.selectDeptById(dept.getParentId());
  94. return toAjax(deptService.insertDept(dept));
  95. }
  96. /**
  97. * 修改
  98. */
  99. @GetMapping("/edit/{deptId}")
  100. public String edit(@PathVariable("deptId") Long deptId, ModelMap mmap)
  101. {
  102. SysDept dept = deptService.selectDeptById(deptId);
  103. if (StringUtils.isNotNull(dept) && 100L == deptId)
  104. {
  105. dept.setParentName("无");
  106. }
  107. mmap.put("dept", dept);
  108. return prefix + "/edit";
  109. }
  110. /**
  111. * 保存
  112. */
  113. @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  114. @RequiresPermissions("system:dept:edit")
  115. @PostMapping("/edit")
  116. @ResponseBody
  117. public AjaxResult editSave(@Validated SysDept dept)
  118. {
  119. if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  120. {
  121. return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  122. }
  123. else if (dept.getParentId().equals(dept.getDeptId()))
  124. {
  125. return error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  126. }else if(dept.getDeptName().length()>9){
  127. return error("修改部门'" + dept.getDeptName() + "'失败,部门名称长度过长(9个字符)");
  128. }
  129. dept.setUpdateBy(ShiroUtils.getUserId().toString());
  130. return toAjax(deptService.updateDept(dept));
  131. }
  132. /**
  133. * 删除
  134. */
  135. @Log(title = "部门管理", businessType = BusinessType.DELETE)
  136. @RequiresPermissions("system:dept:remove")
  137. @GetMapping("/remove/{deptId}")
  138. @ResponseBody
  139. public AjaxResult remove(@PathVariable("deptId") Long deptId)
  140. {
  141. if (deptService.selectDeptCount(deptId) > 0)
  142. {
  143. throw new BusinessException("存在下级部门,不允许删除");
  144. }
  145. if (deptService.checkDeptExistUser(deptId))
  146. {
  147. throw new BusinessException("部门存在用户,不允许删除");
  148. }
  149. return toAjax(deptService.deleteDeptById(deptId));
  150. }
  151. /**
  152. * 校验部门名称
  153. */
  154. @PostMapping("/checkDeptNameUnique")
  155. @ResponseBody
  156. public String checkDeptNameUnique(SysDept dept)
  157. {
  158. return deptService.checkDeptNameUnique(dept);
  159. }
  160. /**
  161. * 选择部门树
  162. */
  163. @GetMapping("/selectDeptTree/{deptId}")
  164. public String selectDeptTree(@PathVariable("deptId") Long deptId, ModelMap mmap)
  165. {
  166. mmap.put("dept", deptService.selectDeptById(deptId));
  167. return prefix + "/tree";
  168. }
  169. /**
  170. * 加载部门列表树
  171. */
  172. @GetMapping("/treeData")
  173. @ResponseBody
  174. public List<Ztree> treeData()
  175. {
  176. SysUser user = ShiroUtils.getSysUser();
  177. List<Ztree> ztrees;
  178. SysDept dept = new SysDept();
  179. /*判断是否是系统内部人员*/
  180. SysRole role = new SysRole();
  181. role.setRoleId(user.getRoleId());
  182. ztrees= deptService.roleDeptTreeData(role);
  183. /*if(ShiroUtils.getCompanyId()==Long.parseLong(configService.selectConfigByKey("sys.company.id"))){
  184. ztrees= deptService.selectDeptTree(dept);
  185. }else{
  186. ztrees= deptService.roleDeptTreeData(role);
  187. }*/
  188. return ztrees;
  189. }
  190. /**
  191. * 加载角色部门(数据权限)列表树
  192. */
  193. @GetMapping("/roleDeptTreeData")
  194. @ResponseBody
  195. public List<Ztree> deptTreeData(SysRole role)
  196. {
  197. List<Ztree> ztrees = deptService.roleDeptTreeData(role);
  198. return ztrees;
  199. }
  200. }