commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.sys.modular.org.service.impl; |
|
26 |
|
|
27 |
import cn.hutool.core.bean.BeanUtil; |
|
28 |
import cn.hutool.core.collection.CollectionUtil; |
|
29 |
import cn.hutool.core.convert.Convert; |
|
30 |
import cn.hutool.core.util.ObjectUtil; |
|
31 |
import cn.hutool.core.util.StrUtil; |
|
32 |
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
33 |
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
|
34 |
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
35 |
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
36 |
import org.springframework.stereotype.Service; |
|
37 |
import org.springframework.transaction.annotation.Transactional; |
|
38 |
import vip.xiaonuo.core.consts.SymbolConstant; |
|
39 |
import vip.xiaonuo.core.context.login.LoginContextHolder; |
|
40 |
import vip.xiaonuo.core.enums.CommonStatusEnum; |
|
41 |
import vip.xiaonuo.core.exception.PermissionException; |
|
42 |
import vip.xiaonuo.core.exception.ServiceException; |
|
43 |
import vip.xiaonuo.core.exception.enums.PermissionExceptionEnum; |
|
44 |
import vip.xiaonuo.core.factory.PageFactory; |
|
45 |
import vip.xiaonuo.core.factory.TreeBuildFactory; |
|
46 |
import vip.xiaonuo.core.pojo.node.AntdBaseTreeNode; |
|
47 |
import vip.xiaonuo.core.pojo.page.PageResult; |
|
48 |
import vip.xiaonuo.core.util.PoiUtil; |
|
49 |
import vip.xiaonuo.sys.core.enums.DataScopeTypeEnum; |
|
50 |
import vip.xiaonuo.sys.modular.auth.service.AuthService; |
|
51 |
import vip.xiaonuo.sys.modular.emp.service.SysEmpExtOrgPosService; |
|
52 |
import vip.xiaonuo.sys.modular.emp.service.SysEmpService; |
|
53 |
import vip.xiaonuo.sys.modular.org.entity.SysOrg; |
|
54 |
import vip.xiaonuo.sys.modular.org.enums.SysOrgExceptionEnum; |
|
55 |
import vip.xiaonuo.sys.modular.org.mapper.SysOrgMapper; |
|
56 |
import vip.xiaonuo.sys.modular.org.param.SysOrgParam; |
|
57 |
import vip.xiaonuo.sys.modular.org.service.SysOrgService; |
|
58 |
import vip.xiaonuo.sys.modular.role.service.SysRoleDataScopeService; |
|
59 |
import vip.xiaonuo.sys.modular.user.service.SysUserDataScopeService; |
|
60 |
|
|
61 |
import javax.annotation.Resource; |
|
62 |
import java.util.List; |
|
63 |
import java.util.Set; |
|
64 |
|
|
65 |
/** |
|
66 |
* 系统组织机构service接口实现类 |
|
67 |
* |
|
68 |
* @author xuyuxiang |
|
69 |
* @date 2020/3/13 16:02 |
|
70 |
*/ |
|
71 |
@Service |
|
72 |
public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> implements SysOrgService { |
|
73 |
|
|
74 |
@Resource |
|
75 |
private SysEmpService sysEmpService; |
|
76 |
|
|
77 |
@Resource |
|
78 |
private SysEmpExtOrgPosService sysEmpExtOrgPosService; |
|
79 |
|
|
80 |
@Resource |
|
81 |
private SysRoleDataScopeService sysRoleDataScopeService; |
|
82 |
|
|
83 |
@Resource |
|
84 |
private SysUserDataScopeService sysUserDataScopeService; |
|
85 |
|
|
86 |
@Resource |
|
87 |
private AuthService authService; |
|
88 |
|
|
89 |
@Override |
|
90 |
public PageResult<SysOrg> page(SysOrgParam sysOrgParam) { |
|
91 |
LambdaQueryWrapper<SysOrg> queryWrapper = new LambdaQueryWrapper<>(); |
|
92 |
if (ObjectUtil.isNotNull(sysOrgParam)) { |
|
93 |
|
|
94 |
// 根据机构名称模糊查询 |
|
95 |
if (ObjectUtil.isNotEmpty(sysOrgParam.getName())) { |
|
96 |
queryWrapper.like(SysOrg::getName, sysOrgParam.getName()); |
|
97 |
} |
|
98 |
|
|
99 |
// 根据机构id查询 |
|
100 |
if (ObjectUtil.isNotEmpty(sysOrgParam.getId())) { |
|
101 |
queryWrapper.eq(SysOrg::getId, sysOrgParam.getId()); |
|
102 |
} |
|
103 |
|
|
104 |
// 根据父机构id查询 |
|
105 |
if (ObjectUtil.isNotEmpty(sysOrgParam.getPid())) { |
|
106 |
queryWrapper |
|
107 |
.eq(SysOrg::getId, sysOrgParam.getPid()) |
|
108 |
.or() |
|
109 |
.like(SysOrg::getPids, sysOrgParam.getPid()); |
|
110 |
} |
|
111 |
} |
|
112 |
|
|
113 |
boolean superAdmin = LoginContextHolder.me().isSuperAdmin(); |
|
114 |
|
|
115 |
// 如果是超级管理员则获取所有组织机构,否则只获取其数据范围的机构数据 |
|
116 |
if (!superAdmin) { |
|
117 |
List<Long> dataScope = sysOrgParam.getDataScope(); |
|
118 |
if (ObjectUtil.isEmpty(dataScope)) { |
|
119 |
return new PageResult<>(new Page<>()); |
|
120 |
} else { |
|
121 |
Set<Long> dataScopeSet = CollectionUtil.newHashSet(dataScope); |
|
122 |
dataScope.forEach(orgId -> { |
|
123 |
//此处获取所有的上级节点,放入set,用于构造完整树 |
|
124 |
List<Long> parentAndChildIdListWithSelf = this.getParentIdListById(orgId); |
|
125 |
dataScopeSet.addAll(parentAndChildIdListWithSelf); |
|
126 |
}); |
|
127 |
queryWrapper.in(SysOrg::getId, dataScopeSet); |
|
128 |
} |
|
129 |
} |
|
130 |
|
|
131 |
// 查询启用状态的 |
|
132 |
queryWrapper.eq(SysOrg::getStatus, CommonStatusEnum.ENABLE.getCode()); |
|
133 |
//根据排序升序排列,序号越小越在前 |
|
134 |
queryWrapper.orderByAsc(SysOrg::getSort); |
|
135 |
return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper)); |
|
136 |
} |
|
137 |
|
|
138 |
@Override |
|
139 |
public List<SysOrg> list(SysOrgParam sysOrgParam) { |
|
140 |
LambdaQueryWrapper<SysOrg> queryWrapper = new LambdaQueryWrapper<>(); |
|
141 |
if (ObjectUtil.isNotNull(sysOrgParam)) { |
|
142 |
//根据父机构id查询 |
|
143 |
if (ObjectUtil.isNotEmpty(sysOrgParam.getPid())) { |
|
144 |
queryWrapper.eq(SysOrg::getPid, sysOrgParam.getPid()); |
|
145 |
} |
|
146 |
} |
|
147 |
//如果是超级管理员则获取所有组织机构,否则只获取其数据范围的机构数据 |
|
148 |
boolean superAdmin = LoginContextHolder.me().isSuperAdmin(); |
|
149 |
if (!superAdmin) { |
|
150 |
List<Long> dataScope = sysOrgParam.getDataScope(); |
|
151 |
if (ObjectUtil.isEmpty(dataScope)) { |
|
152 |
return CollectionUtil.newArrayList(); |
|
153 |
} else { |
|
154 |
Set<Long> dataScopeSet = CollectionUtil.newHashSet(dataScope); |
|
155 |
dataScope.forEach(orgId -> { |
|
156 |
//此处获取所有的上级节点,放入set,用于构造完整树 |
|
157 |
List<Long> parentAndChildIdListWithSelf = this.getParentIdListById(orgId); |
|
158 |
dataScopeSet.addAll(parentAndChildIdListWithSelf); |
|
159 |
}); |
|
160 |
queryWrapper.in(SysOrg::getId, dataScopeSet); |
|
161 |
} |
|
162 |
} |
|
163 |
queryWrapper.eq(SysOrg::getStatus, CommonStatusEnum.ENABLE.getCode()); |
|
164 |
//根据排序升序排列,序号越小越在前 |
|
165 |
queryWrapper.orderByAsc(SysOrg::getSort); |
|
166 |
return this.list(queryWrapper); |
|
167 |
} |
|
168 |
|
|
169 |
@Override |
|
170 |
public void add(SysOrgParam sysOrgParam) { |
|
171 |
//校验参数,检查是否存在相同的名称和编码 |
|
172 |
checkParam(sysOrgParam, false); |
|
173 |
//获取父id |
|
174 |
Long pid = sysOrgParam.getPid(); |
|
175 |
boolean superAdmin = LoginContextHolder.me().isSuperAdmin(); |
|
176 |
//如果登录用户不是超级管理员 |
|
177 |
if (!superAdmin) { |
|
178 |
//如果新增的机构父id不是0,则进行数据权限校验 |
|
179 |
if (!pid.equals(0L)) { |
|
180 |
List<Long> dataScope = sysOrgParam.getDataScope(); |
|
181 |
//数据范围为空 |
|
182 |
if (ObjectUtil.isEmpty(dataScope)) { |
|
183 |
throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE); |
|
184 |
} else if (!dataScope.contains(pid)) { |
|
185 |
//所添加的组织机构的父机构不在自己的数据范围内 |
|
186 |
throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE); |
|
187 |
} |
|
188 |
} else { |
|
189 |
//如果新增的机构父id是0,则根本没权限,只有超级管理员能添加父id为0的节点 |
|
190 |
throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE); |
|
191 |
} |
|
192 |
} |
|
193 |
|
|
194 |
SysOrg sysOrg = new SysOrg(); |
|
195 |
BeanUtil.copyProperties(sysOrgParam, sysOrg); |
|
196 |
this.fillPids(sysOrg); |
|
197 |
sysOrg.setStatus(CommonStatusEnum.ENABLE.getCode()); |
|
198 |
this.save(sysOrg); |
|
199 |
this.authService.refreshUserDataScope(sysOrg.getId()); |
|
200 |
} |
|
201 |
|
|
202 |
@Transactional(rollbackFor = Exception.class) |
|
203 |
@Override |
|
204 |
public void delete(List<SysOrgParam> sysOrgParamList) { |
|
205 |
sysOrgParamList.forEach(sysOrgParam -> { |
|
206 |
SysOrg sysOrg = this.querySysOrg(sysOrgParam); |
|
207 |
Long id = sysOrg.getId(); |
|
208 |
boolean superAdmin = LoginContextHolder.me().isSuperAdmin(); |
|
209 |
|
|
210 |
// 级联删除子节点 |
|
211 |
List<Long> childIdList = this.getChildIdListById(id); |
|
212 |
childIdList.add(id); |
|
213 |
|
|
214 |
childIdList.forEach(item ->{ |
|
215 |
if (!superAdmin) { |
|
216 |
List<Long> dataScope = LoginContextHolder.me().getLoginUserDataScopeIdList(); |
|
217 |
//数据范围为空 |
|
218 |
if (ObjectUtil.isEmpty(dataScope)) { |
|
219 |
throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE); |
|
220 |
} else if (!dataScope.contains(item)) { |
|
221 |
//所操作的数据不在自己的数据范围内 |
|
222 |
throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE); |
|
223 |
} |
|
224 |
} |
|
225 |
// 该机构下有员工,则不能删 |
|
226 |
boolean hasOrgEmp = sysEmpService.hasOrgEmp(item); |
|
227 |
if (hasOrgEmp) { |
|
228 |
throw new ServiceException(SysOrgExceptionEnum.ORG_CANNOT_DELETE); |
|
229 |
} |
|
230 |
|
|
231 |
// 该附属机构下若有员工,则不能删除 |
|
232 |
boolean hasExtOrgEmp = sysEmpExtOrgPosService.hasExtOrgEmp(item); |
|
233 |
if (hasExtOrgEmp) { |
|
234 |
throw new ServiceException(SysOrgExceptionEnum.ORG_CANNOT_DELETE); |
|
235 |
} |
|
236 |
}); |
|
237 |
|
|
238 |
LambdaUpdateWrapper<SysOrg> updateWrapper = new LambdaUpdateWrapper<>(); |
|
239 |
updateWrapper.in(SysOrg::getId, childIdList) |
|
240 |
.set(SysOrg::getStatus, CommonStatusEnum.DELETED.getCode()); |
|
241 |
this.update(updateWrapper); |
|
242 |
|
|
243 |
// 级联删除该机构及子机构对应的角色-数据范围关联信息 |
|
244 |
sysRoleDataScopeService.deleteRoleDataScopeListByOrgIdList(childIdList); |
|
245 |
|
|
246 |
// 级联删除该机构子机构对应的用户-数据范围关联信息 |
|
247 |
sysUserDataScopeService.deleteUserDataScopeListByOrgIdList(childIdList); |
|
248 |
}); |
|
249 |
} |
|
250 |
|
|
251 |
@Transactional(rollbackFor = Exception.class) |
|
252 |
@Override |
|
253 |
public void edit(SysOrgParam sysOrgParam) { |
|
254 |
|
|
255 |
SysOrg sysOrg = this.querySysOrg(sysOrgParam); |
|
256 |
Long id = sysOrg.getId(); |
|
257 |
|
|
258 |
// 检测此人数据范围能不能操作这个公司 |
|
259 |
boolean superAdmin = LoginContextHolder.me().isSuperAdmin(); |
|
260 |
if (!superAdmin) { |
|
261 |
List<Long> dataScope = sysOrgParam.getDataScope(); |
|
262 |
//数据范围为空 |
|
263 |
if (ObjectUtil.isEmpty(dataScope)) { |
|
264 |
throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE); |
|
265 |
} |
|
266 |
//数据范围中不包含本公司 |
|
267 |
else if (!dataScope.contains(id)) { |
|
268 |
throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE); |
|
269 |
} |
|
270 |
} |
|
271 |
|
|
272 |
//校验参数,检查是否存在相同的名称和编码 |
|
273 |
checkParam(sysOrgParam, true); |
|
274 |
|
|
275 |
//如果名称有变化,则修改对应员工的机构相关信息 |
|
276 |
if (!sysOrg.getName().equals(sysOrgParam.getName())) { |
|
277 |
sysEmpService.updateEmpOrgInfo(sysOrg.getId(), sysOrg.getName()); |
|
278 |
} |
|
279 |
|
|
280 |
BeanUtil.copyProperties(sysOrgParam, sysOrg); |
|
281 |
this.fillPids(sysOrg); |
|
282 |
|
|
283 |
//不能修改状态,用修改状态接口修改状态 |
|
284 |
sysOrg.setStatus(null); |
|
285 |
this.updateById(sysOrg); |
|
286 |
//将所有子的父id进行更新 |
|
287 |
List<Long> childIdListById = this.getChildIdListById(sysOrg.getId()); |
|
288 |
childIdListById.forEach(subChildId -> { |
|
289 |
SysOrg child = this.getById(subChildId); |
|
290 |
SysOrgParam childParam = new SysOrgParam(); |
|
291 |
BeanUtil.copyProperties(child, childParam); |
|
292 |
this.edit(childParam); |
|
293 |
}); |
|
294 |
} |
|
295 |
|
|
296 |
@Override |
|
297 |
public SysOrg detail(SysOrgParam sysOrgParam) { |
|
298 |
return this.querySysOrg(sysOrgParam); |
|
299 |
} |
|
300 |
|
|
301 |
@Override |
|
302 |
public List<AntdBaseTreeNode> tree(SysOrgParam sysOrgParam) { |
|
303 |
List<AntdBaseTreeNode> treeNodeList = CollectionUtil.newArrayList(); |
|
304 |
LambdaQueryWrapper<SysOrg> queryWrapper = new LambdaQueryWrapper<>(); |
|
305 |
|
|
306 |
// 如果是超级管理员则获取所有组织机构,否则只获取其数据范围的机构数据 |
|
307 |
boolean superAdmin = LoginContextHolder.me().isSuperAdmin(); |
|
308 |
if (!superAdmin) { |
|
309 |
List<Long> dataScope = sysOrgParam.getDataScope(); |
|
310 |
if (ObjectUtil.isEmpty(dataScope)) { |
|
311 |
return treeNodeList; |
|
312 |
} else { |
|
313 |
Set<Long> dataScopeSet = CollectionUtil.newHashSet(dataScope); |
|
314 |
dataScope.forEach(orgId -> { |
|
315 |
//此处获取所有的上级节点,放入set,用于构造完整树 |
|
316 |
List<Long> parentAndChildIdListWithSelf = this.getParentIdListById(orgId); |
|
317 |
dataScopeSet.addAll(parentAndChildIdListWithSelf); |
|
318 |
}); |
|
319 |
queryWrapper.in(SysOrg::getId, dataScopeSet); |
|
320 |
} |
|
321 |
} |
|
322 |
|
|
323 |
// 只查询未删除的 |
|
324 |
queryWrapper.eq(SysOrg::getStatus, CommonStatusEnum.ENABLE.getCode()); |
|
325 |
//根据排序升序排列,序号越小越在前 |
|
326 |
queryWrapper.orderByAsc(SysOrg::getSort); |
|
327 |
this.list(queryWrapper).forEach(sysOrg -> { |
|
328 |
AntdBaseTreeNode orgTreeNode = new AntdBaseTreeNode(); |
|
329 |
orgTreeNode.setId(sysOrg.getId()); |
|
330 |
orgTreeNode.setParentId(sysOrg.getPid()); |
|
331 |
orgTreeNode.setTitle(sysOrg.getName()); |
|
332 |
orgTreeNode.setValue(String.valueOf(sysOrg.getId())); |
|
333 |
orgTreeNode.setWeight(sysOrg.getSort()); |
|
334 |
treeNodeList.add(orgTreeNode); |
|
335 |
}); |
|
336 |
|
|
337 |
return new TreeBuildFactory<AntdBaseTreeNode>().doTreeBuild(treeNodeList); |
|
338 |
} |
|
339 |
|
|
340 |
@Override |
|
341 |
public List<Long> getDataScopeListByDataScopeType(Integer dataScopeType, Long orgId) { |
|
342 |
List<Long> resultList = CollectionUtil.newArrayList(); |
|
343 |
|
|
344 |
if (ObjectUtil.isEmpty(orgId)) { |
|
345 |
return CollectionUtil.newArrayList(); |
|
346 |
} |
|
347 |
|
|
348 |
// 如果是范围类型是全部数据,则获取当前系统所有的组织架构id |
|
349 |
if (DataScopeTypeEnum.ALL.getCode().equals(dataScopeType)) { |
|
350 |
resultList = this.getOrgIdAll(); |
|
351 |
} |
|
352 |
// 如果范围类型是本部门及以下部门,则查询本节点和子节点集合,包含本节点 |
|
353 |
else if (DataScopeTypeEnum.DEPT_WITH_CHILD.getCode().equals(dataScopeType)) { |
|
354 |
resultList = this.getChildIdListWithSelfById(orgId); |
|
355 |
} |
|
356 |
// 如果数据范围是本部门,不含子节点,则直接返回本部门 |
|
357 |
else if (DataScopeTypeEnum.DEPT.getCode().equals(dataScopeType)) { |
|
358 |
resultList.add(orgId); |
|
359 |
} |
|
360 |
|
|
361 |
return resultList; |
|
362 |
} |
|
363 |
|
|
364 |
/** |
|
365 |
* 根据条件获取组织机构id集合 |
|
366 |
* |
|
367 |
* @author xuyuxiang |
|
368 |
* @date 2020/4/5 18:35 |
|
369 |
*/ |
|
370 |
private List<Long> getOrgIdAll() { |
|
371 |
List<Long> resultList = CollectionUtil.newArrayList(); |
|
372 |
|
|
373 |
LambdaQueryWrapper<SysOrg> queryWrapper = new LambdaQueryWrapper<>(); |
|
374 |
|
|
375 |
queryWrapper.eq(SysOrg::getStatus, CommonStatusEnum.ENABLE.getCode()); |
|
376 |
|
|
377 |
this.list(queryWrapper).forEach(sysOrg -> resultList.add(sysOrg.getId())); |
|
378 |
return resultList; |
|
379 |
} |
|
380 |
|
|
381 |
/** |
|
382 |
* 校验参数,检查是否存在相同的名称和编码 |
|
383 |
* |
|
384 |
* @author xuyuxiang |
|
385 |
* @date 2020/3/25 21:23 |
|
386 |
*/ |
|
387 |
private void checkParam(SysOrgParam sysOrgParam, boolean isExcludeSelf) { |
|
388 |
Long id = sysOrgParam.getId(); |
|
389 |
String name = sysOrgParam.getName(); |
|
390 |
String code = sysOrgParam.getCode(); |
|
391 |
Long pid = sysOrgParam.getPid(); |
|
392 |
|
|
393 |
//如果父id不是根节点 |
|
394 |
if (!pid.equals(0L)) { |
|
395 |
SysOrg pOrg = this.getById(pid); |
|
396 |
if (ObjectUtil.isNull(pOrg)) { |
|
397 |
//父机构不存在 |
|
398 |
throw new ServiceException(SysOrgExceptionEnum.ORG_NOT_EXIST); |
|
399 |
} |
|
400 |
} |
|
401 |
|
|
402 |
// 如果是编辑,父id和自己的id不能一致 |
|
403 |
if (isExcludeSelf) { |
|
404 |
if (sysOrgParam.getId().equals(sysOrgParam.getPid())) { |
|
405 |
throw new ServiceException(SysOrgExceptionEnum.ID_CANT_EQ_PID); |
|
406 |
} |
|
407 |
|
|
408 |
// 如果是编辑,父id不能为自己的子节点 |
|
409 |
List<Long> childIdListById = this.getChildIdListById(sysOrgParam.getId()); |
|
410 |
if(ObjectUtil.isNotEmpty(childIdListById)) { |
|
411 |
if(childIdListById.contains(sysOrgParam.getPid())) { |
|
412 |
throw new ServiceException(SysOrgExceptionEnum.PID_CANT_EQ_CHILD_ID); |
|
413 |
} |
|
414 |
} |
|
415 |
} |
|
416 |
|
|
417 |
LambdaQueryWrapper<SysOrg> queryWrapperByName = new LambdaQueryWrapper<>(); |
|
418 |
queryWrapperByName.eq(SysOrg::getName, name) |
|
419 |
.ne(SysOrg::getStatus, CommonStatusEnum.DELETED.getCode()); |
|
420 |
|
|
421 |
LambdaQueryWrapper<SysOrg> queryWrapperByCode = new LambdaQueryWrapper<>(); |
|
422 |
queryWrapperByCode.eq(SysOrg::getCode, code) |
|
423 |
.ne(SysOrg::getStatus, CommonStatusEnum.DELETED.getCode()); |
|
424 |
|
|
425 |
if (isExcludeSelf) { |
|
426 |
queryWrapperByName.ne(SysOrg::getId, id); |
|
427 |
queryWrapperByCode.ne(SysOrg::getId, id); |
|
428 |
} |
|
429 |
int countByName = this.count(queryWrapperByName); |
|
430 |
int countByCode = this.count(queryWrapperByCode); |
|
431 |
|
|
432 |
if (countByName >= 1) { |
|
433 |
throw new ServiceException(SysOrgExceptionEnum.ORG_NAME_REPEAT); |
|
434 |
} |
|
435 |
if (countByCode >= 1) { |
|
436 |
throw new ServiceException(SysOrgExceptionEnum.ORG_CODE_REPEAT); |
|
437 |
} |
|
438 |
} |
|
439 |
|
|
440 |
/** |
|
441 |
* 获取系统组织机构 |
|
442 |
* |
|
443 |
* @author xuyuxiang |
|
444 |
* @date 2020/3/26 9:56 |
|
445 |
*/ |
|
446 |
private SysOrg querySysOrg(SysOrgParam sysOrgParam) { |
|
447 |
SysOrg sysOrg = this.getById(sysOrgParam.getId()); |
|
448 |
if (ObjectUtil.isNull(sysOrg)) { |
|
449 |
throw new ServiceException(SysOrgExceptionEnum.ORG_NOT_EXIST); |
|
450 |
} |
|
451 |
return sysOrg; |
|
452 |
} |
|
453 |
|
|
454 |
/** |
|
455 |
* 填充父ids |
|
456 |
* |
|
457 |
* @author xuyuxiang |
|
458 |
* @date 2020/3/26 11:28 |
|
459 |
*/ |
|
460 |
private void fillPids(SysOrg sysOrg) { |
|
461 |
if (sysOrg.getPid().equals(0L)) { |
|
462 |
sysOrg.setPids(SymbolConstant.LEFT_SQUARE_BRACKETS + |
|
463 |
0 + |
|
464 |
SymbolConstant.RIGHT_SQUARE_BRACKETS + |
|
465 |
SymbolConstant.COMMA); |
|
466 |
} else { |
|
467 |
//获取父组织机构 |
|
468 |
SysOrg pSysOrg = this.getById(sysOrg.getPid()); |
|
469 |
sysOrg.setPids(pSysOrg.getPids() + |
|
470 |
SymbolConstant.LEFT_SQUARE_BRACKETS + pSysOrg.getId() + |
|
471 |
SymbolConstant.RIGHT_SQUARE_BRACKETS + |
|
472 |
SymbolConstant.COMMA); |
|
473 |
} |
|
474 |
} |
|
475 |
|
|
476 |
/** |
|
477 |
* 根据节点id获取所有子节点id集合 |
|
478 |
* |
|
479 |
* @author xuyuxiang |
|
480 |
* @date 2020/3/26 11:31 |
|
481 |
*/ |
|
482 |
private List<Long> getChildIdListById(Long id) { |
|
483 |
List<Long> childIdList = CollectionUtil.newArrayList(); |
|
484 |
LambdaQueryWrapper<SysOrg> queryWrapper = new LambdaQueryWrapper<>(); |
|
485 |
|
|
486 |
queryWrapper.like(SysOrg::getPids, SymbolConstant.LEFT_SQUARE_BRACKETS + id + |
|
487 |
SymbolConstant.RIGHT_SQUARE_BRACKETS); |
|
488 |
|
|
489 |
this.list(queryWrapper).forEach(sysOrg -> childIdList.add(sysOrg.getId())); |
|
490 |
|
|
491 |
return childIdList; |
|
492 |
} |
|
493 |
|
|
494 |
/** |
|
495 |
* 根据节点id获取所有父节点id集合,不包含自己 |
|
496 |
* |
|
497 |
* @author xuyuxiang |
|
498 |
* @date 2020/4/6 14:53 |
|
499 |
*/ |
|
500 |
private List<Long> getParentIdListById(Long id) { |
|
501 |
List<Long> resultList = CollectionUtil.newArrayList(); |
|
502 |
SysOrg sysOrg = this.getById(id); |
|
503 |
String pids = sysOrg.getPids(); |
|
504 |
String pidsWithRightSymbol = StrUtil.removeAll(pids, SymbolConstant.LEFT_SQUARE_BRACKETS); |
|
505 |
String pidsNormal = StrUtil.removeAll(pidsWithRightSymbol, SymbolConstant.RIGHT_SQUARE_BRACKETS); |
|
506 |
String[] pidsNormalArr = pidsNormal.split(SymbolConstant.COMMA); |
|
507 |
for (String pid : pidsNormalArr) { |
|
508 |
resultList.add(Convert.toLong(pid)); |
|
509 |
} |
|
510 |
return resultList; |
|
511 |
} |
|
512 |
|
|
513 |
/** |
|
514 |
* 根据节点id获取所有子节点id集合,包含自己 |
|
515 |
* |
|
516 |
* @author xuyuxiang |
|
517 |
* @date 2020/4/6 14:54 |
|
518 |
*/ |
|
519 |
private List<Long> getChildIdListWithSelfById(Long id) { |
|
520 |
List<Long> childIdListById = this.getChildIdListById(id); |
|
521 |
List<Long> resultList = CollectionUtil.newArrayList(childIdListById); |
|
522 |
resultList.add(id); |
|
523 |
return resultList; |
|
524 |
} |
|
525 |
|
|
526 |
/** |
|
527 |
* 根据节点id获取父节点和子节点id集合,包含自己 |
|
528 |
* |
|
529 |
* @author xuyuxiang |
|
530 |
* @date 2020/4/7 16:50 |
|
531 |
*/ |
|
532 |
private List<Long> getParentAndChildIdListWithSelfById(Long id) { |
|
533 |
Set<Long> resultSet = CollectionUtil.newHashSet(); |
|
534 |
List<Long> parentIdListById = this.getParentIdListById(id); |
|
535 |
List<Long> childIdListById = this.getChildIdListWithSelfById(id); |
|
536 |
resultSet.addAll(parentIdListById); |
|
537 |
resultSet.addAll(childIdListById); |
|
538 |
return CollectionUtil.newArrayList(resultSet); |
|
539 |
} |
|
540 |
|
|
541 |
@Override |
|
542 |
public void export(SysOrgParam sysOrgParam) { |
|
543 |
List<SysOrg> list = this.list(sysOrgParam); |
|
544 |
PoiUtil.exportExcelWithStream("SysOrg.xls", SysOrg.class, list); |
|
545 |
} |
|
546 |
} |