SysDeptController.java
6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.huaheng.system.controller;
import java.util.Iterator;
import java.util.List;
import cn.hutool.core.util.StrUtil;
import com.huaheng.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huaheng.common.core.constant.UserConstants;
import com.huaheng.common.core.web.controller.BaseController;
import com.huaheng.common.core.web.domain.AjaxResult;
import com.huaheng.common.log.annotation.Log;
import com.huaheng.common.log.enums.BusinessType;
import com.huaheng.common.security.annotation.PreAuthorize;
import com.huaheng.common.security.utils.SecurityUtils;
import com.huaheng.system.api.domain.SysDept;
/**
* 部门信息
*/
@RestController
@RequestMapping("/dept")
@Api(value = "部门信息", tags = "部门信息")
public class SysDeptController extends BaseController {
@Autowired
private ISysDeptService deptService;
/**
* 获取部门列表
*/
@PreAuthorize(hasPermi = "system:dept:list")
@GetMapping("/list")
@ApiOperation(value = "获取部门列表")
public AjaxResult list(SysDept dept) {
List<SysDept> depts = deptService.selectDeptList(dept);
return AjaxResult.success(depts);
}
/**
* 查询部门列表(排除节点)
*/
@PreAuthorize(hasPermi = "system:dept:list")
@GetMapping("/list/exclude/{deptId}")
@ApiOperation(value = "查询部门列表(排除节点)")
public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
List<SysDept> depts = deptService.selectDeptList(new SysDept());
Iterator<SysDept> it = depts.iterator();
while (it.hasNext()) {
SysDept d = (SysDept) it.next();
if (d.getDeptId().intValue() == deptId
|| ArrayUtils.contains(StrUtil.split(d.getAncestors(), ","), deptId + "")) {
it.remove();
}
}
return AjaxResult.success(depts);
}
/**
* 根据部门编号获取详细信息
*/
@PreAuthorize(hasPermi = "system:dept:query")
@GetMapping(value = "/{deptId}")
@ApiOperation(value = "根据部门编号获取详细信息")
public AjaxResult getInfo(@PathVariable Long deptId) {
return AjaxResult.success(deptService.selectDeptById(deptId));
}
/**
* 获取部门下拉树列表
*/
@GetMapping("/treeselect")
@ApiOperation(value = "获取部门下拉树列表")
public AjaxResult treeselect(SysDept dept) {
List<SysDept> depts = deptService.selectDeptList(dept);
return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
}
/**
* 加载对应角色部门列表树
*/
@GetMapping(value = "/roleDeptTreeselect/{roleId}")
@ApiOperation(value = "加载对应角色部门列表树")
public AjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId) {
List<SysDept> depts = deptService.selectDeptList(new SysDept());
AjaxResult ajax = AjaxResult.success();
ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
ajax.put("depts", deptService.buildDeptTreeSelect(depts));
return ajax;
}
/**
* 新增部门
*/
@PreAuthorize(hasPermi = "system:dept:add")
@Log(title = "部门管理", businessType = BusinessType.INSERT)
@PostMapping
@ApiOperation(value = "新增部门")
public AjaxResult add(@Validated @RequestBody SysDept dept) {
if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
}
dept.setCreateBy(SecurityUtils.getUsername());
return toAjax(deptService.insertDept(dept));
}
/**
* 修改部门
*/
@PreAuthorize(hasPermi = "system:dept:edit")
@Log(title = "部门管理", businessType = BusinessType.UPDATE)
@PutMapping
@ApiOperation(value = "修改部门")
public AjaxResult edit(@Validated @RequestBody SysDept dept) {
if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
} else if (dept.getParentId().equals(dept.getDeptId())) {
return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
} else if (StrUtil.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
&& deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0) {
return AjaxResult.error("该部门包含未停用的子部门!");
}
dept.setUpdateBy(SecurityUtils.getUsername());
return toAjax(deptService.updateDept(dept));
}
/**
* 删除部门
*/
@PreAuthorize(hasPermi = "system:dept:remove")
@Log(title = "部门管理", businessType = BusinessType.DELETE)
@DeleteMapping("/{deptId}")
@ApiOperation(value = "删除部门")
public AjaxResult remove(@PathVariable Long deptId) {
if (deptService.hasChildByDeptId(deptId)) {
return AjaxResult.error("存在下级部门,不允许删除");
}
if (deptService.checkDeptExistUser(deptId)) {
return AjaxResult.error("部门存在用户,不允许删除");
}
return toAjax(deptService.deleteDeptById(deptId));
}
}