提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.dept; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import cn.hutool.core.util.ObjectUtil; |
|
5 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
6 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
7 |
import com.iailab.framework.datapermission.core.annotation.DataPermission; |
|
8 |
import com.iailab.module.system.controller.admin.dept.vo.dept.DeptListReqVO; |
|
9 |
import com.iailab.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO; |
|
10 |
import com.iailab.module.system.dal.dataobject.dept.DeptDO; |
|
11 |
import com.iailab.module.system.dal.mysql.dept.DeptMapper; |
|
12 |
import com.iailab.module.system.dal.redis.RedisKeyConstants; |
|
13 |
import com.google.common.annotations.VisibleForTesting; |
|
14 |
import lombok.extern.slf4j.Slf4j; |
|
15 |
import org.springframework.cache.annotation.CacheEvict; |
|
16 |
import org.springframework.cache.annotation.Cacheable; |
|
17 |
import org.springframework.stereotype.Service; |
|
18 |
import org.springframework.validation.annotation.Validated; |
|
19 |
|
|
20 |
import javax.annotation.Resource; |
|
21 |
import java.util.*; |
|
22 |
|
|
23 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
24 |
import static com.iailab.framework.common.util.collection.CollectionUtils.convertSet; |
|
25 |
import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
|
26 |
|
|
27 |
/** |
|
28 |
* 部门 Service 实现类 |
|
29 |
* |
|
30 |
* @author iailab |
|
31 |
*/ |
|
32 |
@Service |
|
33 |
@Validated |
|
34 |
@Slf4j |
|
35 |
public class DeptServiceImpl implements DeptService { |
|
36 |
|
|
37 |
@Resource |
|
38 |
private DeptMapper deptMapper; |
|
39 |
|
|
40 |
@Override |
|
41 |
@CacheEvict(cacheNames = RedisKeyConstants.DEPT_CHILDREN_ID_LIST, |
|
42 |
allEntries = true) // allEntries 清空所有缓存,因为操作一个部门,涉及到多个缓存 |
|
43 |
public Long createDept(DeptSaveReqVO createReqVO) { |
|
44 |
if (createReqVO.getParentId() == null) { |
|
45 |
createReqVO.setParentId(DeptDO.PARENT_ID_ROOT); |
|
46 |
} |
|
47 |
// 校验父部门的有效性 |
|
48 |
validateParentDept(null, createReqVO.getParentId()); |
|
49 |
// 校验部门名的唯一性 |
|
50 |
validateDeptNameUnique(null, createReqVO.getParentId(), createReqVO.getName()); |
|
51 |
|
|
52 |
// 插入部门 |
|
53 |
DeptDO dept = BeanUtils.toBean(createReqVO, DeptDO.class); |
|
54 |
deptMapper.insert(dept); |
|
55 |
return dept.getId(); |
|
56 |
} |
|
57 |
|
|
58 |
@Override |
|
59 |
@CacheEvict(cacheNames = RedisKeyConstants.DEPT_CHILDREN_ID_LIST, |
|
60 |
allEntries = true) // allEntries 清空所有缓存,因为操作一个部门,涉及到多个缓存 |
|
61 |
public void updateDept(DeptSaveReqVO updateReqVO) { |
|
62 |
if (updateReqVO.getParentId() == null) { |
|
63 |
updateReqVO.setParentId(DeptDO.PARENT_ID_ROOT); |
|
64 |
} |
|
65 |
// 校验自己存在 |
|
66 |
validateDeptExists(updateReqVO.getId()); |
|
67 |
// 校验父部门的有效性 |
|
68 |
validateParentDept(updateReqVO.getId(), updateReqVO.getParentId()); |
|
69 |
// 校验部门名的唯一性 |
|
70 |
validateDeptNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName()); |
|
71 |
|
|
72 |
// 更新部门 |
|
73 |
DeptDO updateObj = BeanUtils.toBean(updateReqVO, DeptDO.class); |
|
74 |
deptMapper.updateById(updateObj); |
|
75 |
} |
|
76 |
|
|
77 |
@Override |
|
78 |
@CacheEvict(cacheNames = RedisKeyConstants.DEPT_CHILDREN_ID_LIST, |
|
79 |
allEntries = true) // allEntries 清空所有缓存,因为操作一个部门,涉及到多个缓存 |
|
80 |
public void deleteDept(Long id) { |
|
81 |
// 校验是否存在 |
|
82 |
validateDeptExists(id); |
|
83 |
// 校验是否有子部门 |
|
84 |
if (deptMapper.selectCountByParentId(id) > 0) { |
|
85 |
throw exception(DEPT_EXITS_CHILDREN); |
|
86 |
} |
|
87 |
// 删除部门 |
|
88 |
deptMapper.deleteById(id); |
|
89 |
} |
|
90 |
|
|
91 |
@VisibleForTesting |
|
92 |
void validateDeptExists(Long id) { |
|
93 |
if (id == null) { |
|
94 |
return; |
|
95 |
} |
|
96 |
DeptDO dept = deptMapper.selectById(id); |
|
97 |
if (dept == null) { |
|
98 |
throw exception(DEPT_NOT_FOUND); |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
@VisibleForTesting |
|
103 |
void validateParentDept(Long id, Long parentId) { |
|
104 |
if (parentId == null || DeptDO.PARENT_ID_ROOT.equals(parentId)) { |
|
105 |
return; |
|
106 |
} |
|
107 |
// 1. 不能设置自己为父部门 |
|
108 |
if (Objects.equals(id, parentId)) { |
|
109 |
throw exception(DEPT_PARENT_ERROR); |
|
110 |
} |
|
111 |
// 2. 父部门不存在 |
|
112 |
DeptDO parentDept = deptMapper.selectById(parentId); |
|
113 |
if (parentDept == null) { |
|
114 |
throw exception(DEPT_PARENT_NOT_EXITS); |
|
115 |
} |
|
116 |
// 3. 递归校验父部门,如果父部门是自己的子部门,则报错,避免形成环路 |
|
117 |
if (id == null) { // id 为空,说明新增,不需要考虑环路 |
|
118 |
return; |
|
119 |
} |
|
120 |
for (int i = 0; i < Short.MAX_VALUE; i++) { |
|
121 |
// 3.1 校验环路 |
|
122 |
parentId = parentDept.getParentId(); |
|
123 |
if (Objects.equals(id, parentId)) { |
|
124 |
throw exception(DEPT_PARENT_IS_CHILD); |
|
125 |
} |
|
126 |
// 3.2 继续递归下一级父部门 |
|
127 |
if (parentId == null || DeptDO.PARENT_ID_ROOT.equals(parentId)) { |
|
128 |
break; |
|
129 |
} |
|
130 |
parentDept = deptMapper.selectById(parentId); |
|
131 |
if (parentDept == null) { |
|
132 |
break; |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
@VisibleForTesting |
|
138 |
void validateDeptNameUnique(Long id, Long parentId, String name) { |
|
139 |
DeptDO dept = deptMapper.selectByParentIdAndName(parentId, name); |
|
140 |
if (dept == null) { |
|
141 |
return; |
|
142 |
} |
|
143 |
// 如果 id 为空,说明不用比较是否为相同 id 的部门 |
|
144 |
if (id == null) { |
|
145 |
throw exception(DEPT_NAME_DUPLICATE); |
|
146 |
} |
|
147 |
if (ObjectUtil.notEqual(dept.getId(), id)) { |
|
148 |
throw exception(DEPT_NAME_DUPLICATE); |
|
149 |
} |
|
150 |
} |
|
151 |
|
|
152 |
@Override |
|
153 |
public DeptDO getDept(Long id) { |
|
154 |
return deptMapper.selectById(id); |
|
155 |
} |
|
156 |
|
|
157 |
@Override |
|
158 |
public List<DeptDO> getDeptList(Collection<Long> ids) { |
|
159 |
if (CollUtil.isEmpty(ids)) { |
|
160 |
return Collections.emptyList(); |
|
161 |
} |
|
162 |
return deptMapper.selectBatchIds(ids); |
|
163 |
} |
|
164 |
|
|
165 |
@Override |
|
166 |
public List<DeptDO> getDeptList(DeptListReqVO reqVO) { |
|
167 |
List<DeptDO> list = deptMapper.selectList(reqVO); |
|
168 |
list.sort(Comparator.comparing(DeptDO::getSort)); |
|
169 |
return list; |
|
170 |
} |
|
171 |
|
|
172 |
@Override |
|
173 |
public List<DeptDO> getChildDeptList(Long id) { |
|
174 |
List<DeptDO> children = new LinkedList<>(); |
|
175 |
// 遍历每一层 |
|
176 |
Collection<Long> parentIds = Collections.singleton(id); |
|
177 |
for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环 |
|
178 |
// 查询当前层,所有的子部门 |
|
179 |
List<DeptDO> depts = deptMapper.selectListByParentId(parentIds); |
|
180 |
// 1. 如果没有子部门,则结束遍历 |
|
181 |
if (CollUtil.isEmpty(depts)) { |
|
182 |
break; |
|
183 |
} |
|
184 |
// 2. 如果有子部门,继续遍历 |
|
185 |
children.addAll(depts); |
|
186 |
parentIds = convertSet(depts, DeptDO::getId); |
|
187 |
} |
|
188 |
return children; |
|
189 |
} |
|
190 |
|
|
191 |
@Override |
|
192 |
@DataPermission(enable = false) // 禁用数据权限,避免建立不正确的缓存 |
|
193 |
@Cacheable(cacheNames = RedisKeyConstants.DEPT_CHILDREN_ID_LIST, key = "#id") |
|
194 |
public Set<Long> getChildDeptIdListFromCache(Long id) { |
|
195 |
List<DeptDO> children = getChildDeptList(id); |
|
196 |
return convertSet(children, DeptDO::getId); |
|
197 |
} |
|
198 |
|
|
199 |
@Override |
|
200 |
public void validateDeptList(Collection<Long> ids) { |
|
201 |
if (CollUtil.isEmpty(ids)) { |
|
202 |
return; |
|
203 |
} |
|
204 |
// 获得科室信息 |
|
205 |
Map<Long, DeptDO> deptMap = getDeptMap(ids); |
|
206 |
// 校验 |
|
207 |
ids.forEach(id -> { |
|
208 |
DeptDO dept = deptMap.get(id); |
|
209 |
if (dept == null) { |
|
210 |
throw exception(DEPT_NOT_FOUND); |
|
211 |
} |
|
212 |
if (!CommonStatusEnum.ENABLE.getStatus().equals(dept.getStatus())) { |
|
213 |
throw exception(DEPT_NOT_ENABLE, dept.getName()); |
|
214 |
} |
|
215 |
}); |
|
216 |
} |
|
217 |
|
|
218 |
} |