SysDeptController.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package com.yijia.web.controller.system;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import com.yijia.common.core.domain.TreeSelect;
  6. import com.yijia.common.core.domain.model.LoginUser;
  7. import com.yijia.common.utils.bean.BeanUtils;
  8. import com.yijia.system.domain.SysDeptDemo;
  9. import org.apache.commons.lang3.ArrayUtils;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.security.access.prepost.PreAuthorize;
  12. import org.springframework.validation.annotation.Validated;
  13. import org.springframework.web.bind.annotation.DeleteMapping;
  14. import org.springframework.web.bind.annotation.GetMapping;
  15. import org.springframework.web.bind.annotation.PathVariable;
  16. import org.springframework.web.bind.annotation.PostMapping;
  17. import org.springframework.web.bind.annotation.PutMapping;
  18. import org.springframework.web.bind.annotation.RequestBody;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RestController;
  21. import com.yijia.common.annotation.Log;
  22. import com.yijia.common.constant.UserConstants;
  23. import com.yijia.common.core.controller.BaseController;
  24. import com.yijia.common.core.domain.AjaxResult;
  25. import com.yijia.common.core.domain.entity.SysDept;
  26. import com.yijia.common.enums.BusinessType;
  27. import com.yijia.common.utils.SecurityUtils;
  28. import com.yijia.common.utils.StringUtils;
  29. import com.yijia.system.service.ISysDeptService;
  30. /**
  31. * 部门信息
  32. *
  33. * @author yijia
  34. */
  35. @RestController
  36. @RequestMapping("/system/dept")
  37. public class SysDeptController extends BaseController
  38. {
  39. @Autowired
  40. private ISysDeptService deptService;
  41. /**
  42. * 获取部门列表
  43. */
  44. @GetMapping("/list")
  45. public AjaxResult list(SysDept dept)
  46. {
  47. List<SysDept> depts = deptService.selectDeptList(dept);
  48. return AjaxResult.success(depts);
  49. }
  50. @GetMapping("/listTree")
  51. public AjaxResult listTree(SysDept dept)
  52. {
  53. List<SysDept> depts = deptService.selectDeptListTree(dept);
  54. return AjaxResult.success(depts);
  55. }
  56. /**
  57. * 查询部门列表(排除节点)
  58. */
  59. @GetMapping("/list/exclude/{deptId}")
  60. public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId)
  61. {
  62. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  63. Iterator<SysDept> it = depts.iterator();
  64. while (it.hasNext())
  65. {
  66. SysDept d = (SysDept) it.next();
  67. if (d.getDeptId().intValue() == deptId
  68. || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""))
  69. {
  70. it.remove();
  71. }
  72. }
  73. return AjaxResult.success(depts);
  74. }
  75. /**
  76. * 根据部门编号获取详细信息
  77. */
  78. @GetMapping(value = "/{deptId}")
  79. public AjaxResult getInfo(@PathVariable Long deptId)
  80. {
  81. return AjaxResult.success(deptService.selectDeptById(deptId));
  82. }
  83. @GetMapping(value = "/getGroupInfo")
  84. public AjaxResult getGroupInfo(SysDept dept)
  85. {
  86. SysDept sysDepts =new SysDept();
  87. LoginUser currentUser = SecurityUtils.getLoginUser();
  88. SysDept sysDept=deptService.selectDeptById(currentUser.getUser().getDeptId());
  89. if(sysDept.getJiBie()!=null && sysDept.getJiBie()==1){
  90. dept.setDeptId(currentUser.getUser().getDeptId());
  91. sysDepts=deptService.getGroupInfo(dept);
  92. }else if(sysDept.getJiBie()!=null && sysDept.getJiBie()==2){
  93. dept.setParentId(currentUser.getUser().getDeptId());
  94. sysDepts=deptService.getGroupInfo(dept);
  95. }else{
  96. sysDepts=new SysDept();
  97. }
  98. // SysDeptDemo sysDeptDemo =new SysDeptDemo();
  99. // BeanUtils.copyProperties(sysDeptDemo, sysDepts);
  100. return AjaxResult.success(sysDepts);
  101. }
  102. /**
  103. * 获取部门下拉树列表
  104. */
  105. @GetMapping("/treeselect")
  106. public AjaxResult treeselect(SysDept dept)
  107. {
  108. List<SysDept> depts = deptService.selectDeptList(dept);
  109. return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
  110. }
  111. /**
  112. * 获取部门下拉树列表
  113. */
  114. @GetMapping("/userdepttree")
  115. public AjaxResult userdepttree(SysDept dept)
  116. {
  117. LoginUser currentUser = SecurityUtils.getLoginUser();
  118. //获取当前登陆人级别
  119. SysDept deptJb=deptService.selectDeptById(currentUser.getUser().getDeptId());
  120. List<SysDept> depts =new ArrayList<>();
  121. if(deptJb!=null ){
  122. if(deptJb.getJiBie()==0){
  123. dept.setDeptId(null);
  124. depts = deptService.selectDeptList(dept);
  125. }else if(deptJb.getJiBie()==1){
  126. dept.setParentId(deptJb.getDeptId());
  127. dept.setDeptId(deptJb.getDeptId());
  128. depts= deptService.queryDeptList(dept);
  129. }else if(deptJb.getJiBie()==2){
  130. dept.setDeptId(deptJb.getDeptId());
  131. depts= deptService.selectDeptList(dept);
  132. }
  133. }
  134. List<TreeSelect> list = deptService.buildDeptTreeSelect(depts);
  135. return AjaxResult.success(list);
  136. }
  137. /**
  138. * 加载对应角色部门列表树
  139. */
  140. @GetMapping(value = "/roleDeptTreeselect/{roleId}")
  141. public AjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId)
  142. {
  143. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  144. AjaxResult ajax = AjaxResult.success();
  145. ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  146. ajax.put("depts", deptService.buildDeptTreeSelect(depts));
  147. return ajax;
  148. }
  149. /**
  150. * 新增部门
  151. */
  152. @PreAuthorize("@ss.hasPermi('system:dept:add')")
  153. @Log(title = "部门管理", businessType = BusinessType.INSERT)
  154. @PostMapping
  155. public AjaxResult add(@Validated @RequestBody SysDept dept)
  156. {
  157. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  158. {
  159. return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  160. }
  161. dept.setCreateBy(SecurityUtils.getUsername());
  162. return toAjax(deptService.insertDept(dept));
  163. }
  164. /**
  165. * 修改部门
  166. */
  167. @PreAuthorize("@ss.hasPermi('system:dept:edit')")
  168. @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  169. @PutMapping
  170. public AjaxResult edit(@Validated @RequestBody SysDept dept)
  171. {
  172. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  173. {
  174. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  175. }
  176. else if (dept.getParentId().equals(dept.getDeptId()))
  177. {
  178. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  179. }
  180. else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
  181. && deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0)
  182. {
  183. return AjaxResult.error("该部门包含未停用的子部门!");
  184. }
  185. dept.setUpdateBy(SecurityUtils.getUsername());
  186. return toAjax(deptService.updateDept(dept));
  187. }
  188. /**
  189. * 删除部门
  190. */
  191. @PreAuthorize("@ss.hasPermi('system:dept:remove')")
  192. @Log(title = "部门管理", businessType = BusinessType.DELETE)
  193. @DeleteMapping("/{deptId}")
  194. public AjaxResult remove(@PathVariable Long deptId)
  195. {
  196. if (deptService.hasChildByDeptId(deptId))
  197. {
  198. return AjaxResult.error("存在下级部门,不允许删除");
  199. }
  200. if (deptService.checkDeptExistUser(deptId))
  201. {
  202. return AjaxResult.error("部门存在用户,不允许删除");
  203. }
  204. return toAjax(deptService.deleteDeptById(deptId));
  205. }
  206. }