SysDeptController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package com.zd.system.controller;
  2. import com.zd.common.core.annotation.Log;
  3. import com.zd.common.core.annotation.PreAuthorize;
  4. import com.zd.common.core.log.BusinessType;
  5. import com.zd.common.core.security.TokenService;
  6. import com.zd.common.core.utils.SecurityUtils;
  7. import com.zd.common.core.utils.StringUtils;
  8. import com.zd.common.core.web.controller.BaseController;
  9. import com.zd.model.constant.UserConstants;
  10. import com.zd.model.domain.AjaxResult;
  11. import com.zd.model.domain.ResultData;
  12. import com.zd.model.domain.per.PerFun;
  13. import com.zd.model.domain.per.PerPrefix;
  14. import com.zd.model.entity.SysDept;
  15. import com.zd.system.api.vo.SysDeptListVO;
  16. import com.zd.system.api.vo.SysDeptVO;
  17. import com.zd.system.domain.vo.TreeSelect;
  18. import com.zd.system.service.ISysDeptService;
  19. import com.zd.system.service.impl.SysDeptManager;
  20. import io.swagger.annotations.ApiOperation;
  21. import org.apache.commons.collections4.CollectionUtils;
  22. import org.apache.commons.lang3.ArrayUtils;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.validation.annotation.Validated;
  25. import org.springframework.web.bind.annotation.*;
  26. import java.util.*;
  27. import java.util.function.Predicate;
  28. import java.util.stream.Collectors;
  29. /**
  30. * 部门信息
  31. *
  32. * @author zd
  33. */
  34. @RestController
  35. @RequestMapping("/dept")
  36. public class SysDeptController extends BaseController {
  37. @Autowired
  38. private ISysDeptService deptService;
  39. @Autowired
  40. private SysDeptManager deptManager;
  41. @Autowired
  42. private TokenService tokenService;
  43. /**
  44. * 获取部门列表 "system:dept:list"
  45. */
  46. @PreAuthorize(hasPermi = PerPrefix.SYSTEM_DEPT+ PerFun.LIST)
  47. @GetMapping("/list")
  48. public AjaxResult list(SysDept dept) {
  49. List<SysDept> depts = deptService.selectDeptList(dept);
  50. return AjaxResult.success(depts);
  51. }
  52. @PostMapping("/listOption")
  53. public AjaxResult listOption(@RequestBody SysDept dept) {
  54. List<SysDept> depts = deptService.selectDeptListByCollege(dept);
  55. return AjaxResult.success(depts);
  56. }
  57. /**
  58. * 获取部门列表VO - 2.6 弃用
  59. */
  60. // @PreAuthorize(hasPermi = "system:dept:list")
  61. @GetMapping("/listVO")
  62. public ResultData<List<SysDeptListVO>> listVO(SysDept dept) {
  63. List<SysDeptListVO> depts = deptManager.querySysDeptListVO(dept);
  64. return ResultData.success(depts);
  65. }
  66. /**
  67. * 获取院系列表-不分页(按目前的需求约定第二级组织机构为院系)-应用数据权限
  68. */
  69. @ApiOperation("获取院系列表-不分页(按目前的需求约定第二级组织机构为院系)-应用数据权限")
  70. @GetMapping("/departments/list")
  71. public AjaxResult departments() {
  72. List<SysDept> depts = deptService.selectDeptListByCollege(new SysDept());
  73. return AjaxResult.success(depts);
  74. }
  75. @ApiOperation("通过ids 查询所有的本节点和父节点ID集合")
  76. @PostMapping("/allParentId")
  77. public AjaxResult allParentId(@RequestBody List<Long> deptIds) {
  78. if(CollectionUtils.isEmpty(deptIds)){
  79. return AjaxResult.success(Collections.emptyList());
  80. }
  81. List<Long> ids = deptService.selectAllParentId(deptIds);
  82. return AjaxResult.success(ids);
  83. }
  84. /**
  85. * 获取楼栋列表-不分页(按目前的需求约定第三级组织机构为楼栋)-切勿应用数据权限
  86. */
  87. @ApiOperation("获取楼栋列表-不分页(按目前的需求约定第三级组织机构为楼栋)-切勿应用数据权限")
  88. @GetMapping("/{deptId}/building/list")
  89. public AjaxResult building(@PathVariable("deptId") Long deptId) {
  90. SysDept sysDept = new SysDept();
  91. sysDept.setParentId(deptId);
  92. //这里不要有数据权限
  93. List<SysDept> depts = deptService.selectDeptListNoAuth(sysDept);
  94. return AjaxResult.success(depts);
  95. }
  96. /**
  97. * 查询部门列表(排除节点) "system:dept:list"
  98. */
  99. @PreAuthorize(hasPermi = PerPrefix.SYSTEM_DEPT+ PerFun.LIST)
  100. @GetMapping("/list/exclude/{deptId}")
  101. public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
  102. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  103. Iterator<SysDept> it = depts.iterator();
  104. while (it.hasNext()) {
  105. SysDept d = (SysDept) it.next();
  106. if (d.getDeptId().intValue() == deptId
  107. || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + "")) {
  108. it.remove();
  109. }
  110. }
  111. return AjaxResult.success(depts);
  112. }
  113. /**
  114. * 根据部门编号获取详细信息 "system:dept:query"
  115. */
  116. @PreAuthorize(hasPermi = PerPrefix.SYSTEM_DEPT+ PerFun.QUERY)
  117. @GetMapping(value = "/{deptId}")
  118. public AjaxResult getInfo(@PathVariable Long deptId) {
  119. return AjaxResult.success(deptService.selectDeptById(deptId));
  120. }
  121. /**
  122. * 根据部门编号获取详细信息 "system:dept:query"-无权限
  123. */
  124. @GetMapping(value = "/info/{deptId}")
  125. public AjaxResult getInfoById(@PathVariable Long deptId) {
  126. return AjaxResult.success(deptService.selectDeptById(deptId));
  127. }
  128. /**
  129. * 获取部门下拉树列表
  130. */
  131. @GetMapping("/treeselect")
  132. public AjaxResult treeselect(SysDept dept) {
  133. List<SysDept> depts = deptService.selectDeptList(dept);
  134. return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
  135. }
  136. /**
  137. * 获取部门和实验室下拉树列表
  138. */
  139. @GetMapping("/treeDeptSubSel")
  140. public AjaxResult treeDeptSubSel(SysDept dept) {
  141. List<SysDept> depts = deptService.treeDeptSubSel(dept);
  142. return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
  143. }
  144. /**
  145. * 获取部门下拉树列表(获取登录人的部门父级部门)
  146. */
  147. @GetMapping("/treeselectByUser")
  148. public AjaxResult treeselectByUser(SysDept dept) {
  149. List<SysDept> depts = deptService.treeselectByUser(dept);
  150. return AjaxResult.success(deptService.buildDeptTreeSelectByNo(depts));
  151. }
  152. /**
  153. * 加载对应角色部门列表树
  154. */
  155. @GetMapping(value = "/roleDeptTreeselect/{roleId}")
  156. public AjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId) {
  157. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  158. AjaxResult ajax = AjaxResult.success();
  159. ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  160. ajax.put("depts", deptService.buildDeptTreeSelect(depts));
  161. return ajax;
  162. }
  163. /**
  164. * 新增部门 "system:dept:add"
  165. */
  166. @PreAuthorize(hasPermi = PerPrefix.SYSTEM_DEPT+ PerFun.ADD)
  167. @Log(title = "部门管理", businessType = BusinessType.INSERT)
  168. @PostMapping
  169. public AjaxResult add(@Validated @RequestBody SysDept dept) {
  170. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
  171. return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  172. }
  173. dept.setCreateBy(SecurityUtils.getUsername());
  174. return toAjax(deptService.insertDept(dept));
  175. }
  176. /**
  177. * 新增部门 "system:dept:add" (教师专用)
  178. */
  179. @PreAuthorize(hasPermi = PerPrefix.SYSTEM_DEPT+ PerFun.ADD)
  180. @Log(title = "部门管理", businessType = BusinessType.INSERT)
  181. @PostMapping("/addDeptByTeacher")
  182. public AjaxResult addDeptByTeacher(@Validated @RequestBody SysDeptVO deptVO) {
  183. for (SysDept sysDept : deptVO.getTeaCherDpetList()){
  184. //未删除的数据
  185. sysDept.setDelFlag("0");
  186. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(sysDept))) {
  187. return AjaxResult.error("新增部门'" + sysDept.getDeptName() + "'失败,部门名称已存在");
  188. }
  189. }
  190. return toAjax(deptService.insertDeptByTeaCher(deptVO));
  191. }
  192. /**
  193. * 部门排序 "system:dept:edit" (教师专用)
  194. */
  195. @PreAuthorize(hasPermi = PerPrefix.SYSTEM_DEPT+ PerFun.EDIT)
  196. @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  197. @PutMapping("/editDeptOrder")
  198. public AjaxResult editDeptOrder(@Validated @RequestBody SysDeptVO deptVO) {
  199. return toAjax(deptService.editDeptOrder(deptVO));
  200. }
  201. /**
  202. * 修改部门 "system:dept:edit"
  203. */
  204. @PreAuthorize(hasPermi = PerPrefix.SYSTEM_DEPT+ PerFun.EDIT)
  205. @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  206. @PutMapping
  207. public AjaxResult edit(@RequestBody SysDept dept) {
  208. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
  209. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  210. } else if (dept.getParentId().equals(dept.getDeptId())) {
  211. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  212. } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
  213. && deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0) {
  214. return AjaxResult.error("该部门包含未停用的子部门!");
  215. }
  216. dept.setUpdateBy(SecurityUtils.getUsername());
  217. return toAjax(deptService.updateDept(dept));
  218. }
  219. /**
  220. * 修改部门名称 "system:dept:edit"
  221. */
  222. @PreAuthorize(hasPermi = PerPrefix.SYSTEM_DEPT+ PerFun.EDIT)
  223. @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  224. @PutMapping("/updateDeptName")
  225. public AjaxResult updateDeptName(@RequestBody SysDept dept) {
  226. dept.setUpdateBy(SecurityUtils.getUsername());
  227. return toAjax(deptService.updateDeptName(dept));
  228. }
  229. /**
  230. * 删除部门 "system:dept:remove"
  231. */
  232. @PreAuthorize(hasPermi = PerPrefix.SYSTEM_DEPT+ PerFun.REMOVE)
  233. @Log(title = "部门管理", businessType = BusinessType.DELETE)
  234. @DeleteMapping("/{deptId}")
  235. public AjaxResult remove(@PathVariable Long deptId) {
  236. if (deptService.hasChildByDeptId(deptId)) {
  237. return AjaxResult.error("存在下级部门,不允许删除");
  238. }
  239. if (deptService.checkDeptExistUser(deptId)) {
  240. return AjaxResult.error("部门存在用户,不允许删除");
  241. }
  242. return toAjax(deptService.deleteDeptById(deptId));
  243. }
  244. /**
  245. * 查询所有的楼栋-- 默认写死第三级
  246. */
  247. @GetMapping("/builds/list")
  248. public AjaxResult buildsList() {
  249. List<SysDept> depts = deptService.selectBuildsList(new SysDept());
  250. return AjaxResult.success(depts);
  251. }
  252. /**
  253. * 获取部门下拉树列表(处理过滤,如果为父级,返回下级列表,如果为子集,返回当前。)
  254. */
  255. @GetMapping("/filterDept")
  256. public AjaxResult filterDept() {
  257. SysDept sysDept1 = deptService.selectDeptById(tokenService.getLoginUser().getSysUser().getDeptId());
  258. if(sysDept1 == null){
  259. return AjaxResult.success();
  260. }
  261. if(sysDept1.getParentId()==0){
  262. SysDept sysDept = new SysDept();
  263. sysDept.setParentId(sysDept1.getDeptId());
  264. List<SysDept> depts = deptService.selectDeptList(sysDept);
  265. return AjaxResult.success(depts);
  266. }
  267. List<SysDept> depts = new ArrayList<>();
  268. depts.add(sysDept1);
  269. return AjaxResult.success(depts);
  270. }
  271. }