提交 | 用户 | 时间
|
e57a89
|
1 |
package com.jcdm.system.service.impl; |
懒 |
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.Iterator; |
|
5 |
import java.util.List; |
|
6 |
import java.util.stream.Collectors; |
|
7 |
import org.springframework.beans.factory.annotation.Autowired; |
|
8 |
import org.springframework.stereotype.Service; |
|
9 |
import com.jcdm.common.annotation.DataScope; |
|
10 |
import com.jcdm.common.constant.UserConstants; |
|
11 |
import com.jcdm.common.core.domain.TreeSelect; |
|
12 |
import com.jcdm.common.core.domain.entity.SysDept; |
|
13 |
import com.jcdm.common.core.domain.entity.SysRole; |
|
14 |
import com.jcdm.common.core.domain.entity.SysUser; |
|
15 |
import com.jcdm.common.core.text.Convert; |
|
16 |
import com.jcdm.common.exception.ServiceException; |
|
17 |
import com.jcdm.common.utils.SecurityUtils; |
|
18 |
import com.jcdm.common.utils.StringUtils; |
|
19 |
import com.jcdm.common.utils.spring.SpringUtils; |
|
20 |
import com.jcdm.system.mapper.SysDeptMapper; |
|
21 |
import com.jcdm.system.mapper.SysRoleMapper; |
|
22 |
import com.jcdm.system.service.ISysDeptService; |
|
23 |
|
|
24 |
/** |
|
25 |
* 部门管理 服务实现 |
|
26 |
* |
|
27 |
* @author jc |
|
28 |
*/ |
|
29 |
@Service |
|
30 |
public class SysDeptServiceImpl implements ISysDeptService |
|
31 |
{ |
|
32 |
@Autowired |
|
33 |
private SysDeptMapper deptMapper; |
|
34 |
|
|
35 |
@Autowired |
|
36 |
private SysRoleMapper roleMapper; |
|
37 |
|
|
38 |
/** |
|
39 |
* 查询部门管理数据 |
|
40 |
* |
|
41 |
* @param dept 部门信息 |
|
42 |
* @return 部门信息集合 |
|
43 |
*/ |
|
44 |
@Override |
|
45 |
@DataScope(deptAlias = "d") |
|
46 |
public List<SysDept> selectDeptList(SysDept dept) |
|
47 |
{ |
|
48 |
return deptMapper.selectDeptList(dept); |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* 查询部门树结构信息 |
|
53 |
* |
|
54 |
* @param dept 部门信息 |
|
55 |
* @return 部门树信息集合 |
|
56 |
*/ |
|
57 |
@Override |
|
58 |
public List<TreeSelect> selectDeptTreeList(SysDept dept) |
|
59 |
{ |
|
60 |
List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept); |
|
61 |
return buildDeptTreeSelect(depts); |
|
62 |
} |
|
63 |
|
|
64 |
/** |
|
65 |
* 构建前端所需要树结构 |
|
66 |
* |
|
67 |
* @param depts 部门列表 |
|
68 |
* @return 树结构列表 |
|
69 |
*/ |
|
70 |
@Override |
|
71 |
public List<SysDept> buildDeptTree(List<SysDept> depts) |
|
72 |
{ |
|
73 |
List<SysDept> returnList = new ArrayList<SysDept>(); |
|
74 |
List<Long> tempList = depts.stream().map(SysDept::getDeptId).collect(Collectors.toList()); |
|
75 |
for (SysDept dept : depts) |
|
76 |
{ |
|
77 |
// 如果是顶级节点, 遍历该父节点的所有子节点 |
|
78 |
if (!tempList.contains(dept.getParentId())) |
|
79 |
{ |
|
80 |
recursionFn(depts, dept); |
|
81 |
returnList.add(dept); |
|
82 |
} |
|
83 |
} |
|
84 |
if (returnList.isEmpty()) |
|
85 |
{ |
|
86 |
returnList = depts; |
|
87 |
} |
|
88 |
return returnList; |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* 构建前端所需要下拉树结构 |
|
93 |
* |
|
94 |
* @param depts 部门列表 |
|
95 |
* @return 下拉树结构列表 |
|
96 |
*/ |
|
97 |
@Override |
|
98 |
public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts) |
|
99 |
{ |
|
100 |
List<SysDept> deptTrees = buildDeptTree(depts); |
|
101 |
return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList()); |
|
102 |
} |
|
103 |
|
|
104 |
/** |
|
105 |
* 根据角色ID查询部门树信息 |
|
106 |
* |
|
107 |
* @param roleId 角色ID |
|
108 |
* @return 选中部门列表 |
|
109 |
*/ |
|
110 |
@Override |
|
111 |
public List<Long> selectDeptListByRoleId(Long roleId) |
|
112 |
{ |
|
113 |
SysRole role = roleMapper.selectRoleById(roleId); |
|
114 |
return deptMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly()); |
|
115 |
} |
|
116 |
|
|
117 |
/** |
|
118 |
* 根据部门ID查询信息 |
|
119 |
* |
|
120 |
* @param deptId 部门ID |
|
121 |
* @return 部门信息 |
|
122 |
*/ |
|
123 |
@Override |
|
124 |
public SysDept selectDeptById(Long deptId) |
|
125 |
{ |
|
126 |
return deptMapper.selectDeptById(deptId); |
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* 根据ID查询所有子部门(正常状态) |
|
131 |
* |
|
132 |
* @param deptId 部门ID |
|
133 |
* @return 子部门数 |
|
134 |
*/ |
|
135 |
@Override |
|
136 |
public int selectNormalChildrenDeptById(Long deptId) |
|
137 |
{ |
|
138 |
return deptMapper.selectNormalChildrenDeptById(deptId); |
|
139 |
} |
|
140 |
|
|
141 |
/** |
|
142 |
* 是否存在子节点 |
|
143 |
* |
|
144 |
* @param deptId 部门ID |
|
145 |
* @return 结果 |
|
146 |
*/ |
|
147 |
@Override |
|
148 |
public boolean hasChildByDeptId(Long deptId) |
|
149 |
{ |
|
150 |
int result = deptMapper.hasChildByDeptId(deptId); |
|
151 |
return result > 0; |
|
152 |
} |
|
153 |
|
|
154 |
/** |
|
155 |
* 查询部门是否存在用户 |
|
156 |
* |
|
157 |
* @param deptId 部门ID |
|
158 |
* @return 结果 true 存在 false 不存在 |
|
159 |
*/ |
|
160 |
@Override |
|
161 |
public boolean checkDeptExistUser(Long deptId) |
|
162 |
{ |
|
163 |
int result = deptMapper.checkDeptExistUser(deptId); |
|
164 |
return result > 0; |
|
165 |
} |
|
166 |
|
|
167 |
/** |
|
168 |
* 校验部门名称是否唯一 |
|
169 |
* |
|
170 |
* @param dept 部门信息 |
|
171 |
* @return 结果 |
|
172 |
*/ |
|
173 |
@Override |
|
174 |
public boolean checkDeptNameUnique(SysDept dept) |
|
175 |
{ |
|
176 |
Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId(); |
|
177 |
SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId()); |
|
178 |
if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue()) |
|
179 |
{ |
|
180 |
return UserConstants.NOT_UNIQUE; |
|
181 |
} |
|
182 |
return UserConstants.UNIQUE; |
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* 校验部门是否有数据权限 |
|
187 |
* |
|
188 |
* @param deptId 部门id |
|
189 |
*/ |
|
190 |
@Override |
|
191 |
public void checkDeptDataScope(Long deptId) |
|
192 |
{ |
|
193 |
if (!SysUser.isAdmin(SecurityUtils.getUserId())) |
|
194 |
{ |
|
195 |
SysDept dept = new SysDept(); |
|
196 |
dept.setDeptId(deptId); |
|
197 |
List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept); |
|
198 |
if (StringUtils.isEmpty(depts)) |
|
199 |
{ |
|
200 |
throw new ServiceException("没有权限访问部门数据!"); |
|
201 |
} |
|
202 |
} |
|
203 |
} |
|
204 |
|
|
205 |
/** |
|
206 |
* 新增保存部门信息 |
|
207 |
* |
|
208 |
* @param dept 部门信息 |
|
209 |
* @return 结果 |
|
210 |
*/ |
|
211 |
@Override |
|
212 |
public int insertDept(SysDept dept) |
|
213 |
{ |
|
214 |
SysDept info = deptMapper.selectDeptById(dept.getParentId()); |
|
215 |
// 如果父节点不为正常状态,则不允许新增子节点 |
|
216 |
if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) |
|
217 |
{ |
|
218 |
throw new ServiceException("部门停用,不允许新增"); |
|
219 |
} |
|
220 |
dept.setAncestors(info.getAncestors() + "," + dept.getParentId()); |
|
221 |
return deptMapper.insertDept(dept); |
|
222 |
} |
|
223 |
|
|
224 |
/** |
|
225 |
* 修改保存部门信息 |
|
226 |
* |
|
227 |
* @param dept 部门信息 |
|
228 |
* @return 结果 |
|
229 |
*/ |
|
230 |
@Override |
|
231 |
public int updateDept(SysDept dept) |
|
232 |
{ |
|
233 |
SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId()); |
|
234 |
SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId()); |
|
235 |
if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept)) |
|
236 |
{ |
|
237 |
String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId(); |
|
238 |
String oldAncestors = oldDept.getAncestors(); |
|
239 |
dept.setAncestors(newAncestors); |
|
240 |
updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors); |
|
241 |
} |
|
242 |
int result = deptMapper.updateDept(dept); |
|
243 |
if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors()) |
|
244 |
&& !StringUtils.equals("0", dept.getAncestors())) |
|
245 |
{ |
|
246 |
// 如果该部门是启用状态,则启用该部门的所有上级部门 |
|
247 |
updateParentDeptStatusNormal(dept); |
|
248 |
} |
|
249 |
return result; |
|
250 |
} |
|
251 |
|
|
252 |
/** |
|
253 |
* 修改该部门的父级部门状态 |
|
254 |
* |
|
255 |
* @param dept 当前部门 |
|
256 |
*/ |
|
257 |
private void updateParentDeptStatusNormal(SysDept dept) |
|
258 |
{ |
|
259 |
String ancestors = dept.getAncestors(); |
|
260 |
Long[] deptIds = Convert.toLongArray(ancestors); |
|
261 |
deptMapper.updateDeptStatusNormal(deptIds); |
|
262 |
} |
|
263 |
|
|
264 |
/** |
|
265 |
* 修改子元素关系 |
|
266 |
* |
|
267 |
* @param deptId 被修改的部门ID |
|
268 |
* @param newAncestors 新的父ID集合 |
|
269 |
* @param oldAncestors 旧的父ID集合 |
|
270 |
*/ |
|
271 |
public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) |
|
272 |
{ |
|
273 |
List<SysDept> children = deptMapper.selectChildrenDeptById(deptId); |
|
274 |
for (SysDept child : children) |
|
275 |
{ |
|
276 |
child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors)); |
|
277 |
} |
|
278 |
if (children.size() > 0) |
|
279 |
{ |
|
280 |
deptMapper.updateDeptChildren(children); |
|
281 |
} |
|
282 |
} |
|
283 |
|
|
284 |
/** |
|
285 |
* 删除部门管理信息 |
|
286 |
* |
|
287 |
* @param deptId 部门ID |
|
288 |
* @return 结果 |
|
289 |
*/ |
|
290 |
@Override |
|
291 |
public int deleteDeptById(Long deptId) |
|
292 |
{ |
|
293 |
return deptMapper.deleteDeptById(deptId); |
|
294 |
} |
|
295 |
|
|
296 |
/** |
|
297 |
* 递归列表 |
|
298 |
*/ |
|
299 |
private void recursionFn(List<SysDept> list, SysDept t) |
|
300 |
{ |
|
301 |
// 得到子节点列表 |
|
302 |
List<SysDept> childList = getChildList(list, t); |
|
303 |
t.setChildren(childList); |
|
304 |
for (SysDept tChild : childList) |
|
305 |
{ |
|
306 |
if (hasChild(list, tChild)) |
|
307 |
{ |
|
308 |
recursionFn(list, tChild); |
|
309 |
} |
|
310 |
} |
|
311 |
} |
|
312 |
|
|
313 |
/** |
|
314 |
* 得到子节点列表 |
|
315 |
*/ |
|
316 |
private List<SysDept> getChildList(List<SysDept> list, SysDept t) |
|
317 |
{ |
|
318 |
List<SysDept> tlist = new ArrayList<SysDept>(); |
|
319 |
Iterator<SysDept> it = list.iterator(); |
|
320 |
while (it.hasNext()) |
|
321 |
{ |
|
322 |
SysDept n = (SysDept) it.next(); |
|
323 |
if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue()) |
|
324 |
{ |
|
325 |
tlist.add(n); |
|
326 |
} |
|
327 |
} |
|
328 |
return tlist; |
|
329 |
} |
|
330 |
|
|
331 |
/** |
|
332 |
* 判断是否有子节点 |
|
333 |
*/ |
|
334 |
private boolean hasChild(List<SysDept> list, SysDept t) |
|
335 |
{ |
|
336 |
return getChildList(list, t).size() > 0; |
|
337 |
} |
|
338 |
} |