Commit da458ece8983a236c65117c25c88c24a8dbcc2d2
1 parent
f141050a
fix: 租户管理员进行组织调整时,对应组织的源用户和目标用户映射关系表进行更新
Showing
1 changed file
with
26 additions
and
56 deletions
... | ... | @@ -20,7 +20,6 @@ import org.thingsboard.server.dao.yunteng.entities.*; |
20 | 20 | import org.thingsboard.server.dao.yunteng.mapper.*; |
21 | 21 | import org.thingsboard.server.dao.yunteng.service.AbstractBaseService; |
22 | 22 | import org.thingsboard.server.dao.yunteng.service.TkOrganizationService; |
23 | -import org.thingsboard.server.dao.yunteng.service.UserOrganizationMappingService; | |
24 | 23 | |
25 | 24 | import java.util.*; |
26 | 25 | import java.util.stream.Collectors; |
... | ... | @@ -47,9 +46,10 @@ public class TkOrganizationServiceImpl extends AbstractBaseService<OrganizationM |
47 | 46 | boolean isCustomerOrPlatform, |
48 | 47 | String userId) { |
49 | 48 | Assert.notNull(organizationDTO.getName(), "organization name cannot be null"); |
50 | - if (StringUtils.isNotBlank(organizationDTO.getParentId())) { | |
49 | + String parentId = organizationDTO.getParentId(); | |
50 | + if (StringUtils.isNotBlank(parentId)) { | |
51 | 51 | // 新增有父组织,拿到父组织 |
52 | - TkOrganizationEntity organization = baseMapper.selectById(organizationDTO.getParentId()); | |
52 | + TkOrganizationEntity organization = baseMapper.selectById(parentId); | |
53 | 53 | if (organization == null) { |
54 | 54 | // 父组织不存在 |
55 | 55 | throw new TkDataValidationException("parent organization not exist!"); |
... | ... | @@ -64,13 +64,12 @@ public class TkOrganizationServiceImpl extends AbstractBaseService<OrganizationM |
64 | 64 | organizationDTO.copyToEntity(organization, "id"); |
65 | 65 | // 如果当前账号为平台管理员或者用户 |
66 | 66 | if (isCustomerOrPlatform) { |
67 | - if (StringUtils.isEmpty(organizationDTO.getParentId())) { | |
67 | + if (StringUtils.isEmpty(parentId)) { | |
68 | 68 | // 获取当前用户的最顶级组织ID的父ID |
69 | 69 | List<OrganizationDTO> myOrganizations = getMyOrganizations(false, false, false,false,tenantId, userId,null); |
70 | 70 | if (null != myOrganizations && myOrganizations.size() > 0) { |
71 | 71 | OrganizationDTO organizationDto = myOrganizations.get(0); |
72 | - String parentId = organizationDto.getParentId(); | |
73 | - organization.setParentId(parentId); | |
72 | + organization.setParentId(organizationDto.getParentId()); | |
74 | 73 | } |
75 | 74 | } |
76 | 75 | } |
... | ... | @@ -94,6 +93,8 @@ public class TkOrganizationServiceImpl extends AbstractBaseService<OrganizationM |
94 | 93 | } |
95 | 94 | organization.copyToDTO(organizationDTO); |
96 | 95 | invalidateParentAndMeCacheByTenantIdAndId(tenantId,true,null,organization.getId()); |
96 | + //查询目标组织的用户列表,新增当前组织关联的目标用户映射 | |
97 | + addUserOrganizationMapping(parentId,organization.getId()); | |
97 | 98 | return organizationDTO; |
98 | 99 | } |
99 | 100 | |
... | ... | @@ -162,13 +163,14 @@ public class TkOrganizationServiceImpl extends AbstractBaseService<OrganizationM |
162 | 163 | boolean isCustomerOrPlatform, |
163 | 164 | String userId) { |
164 | 165 | Assert.notNull(organizationDTO.getName(), "organization name cannot be null"); |
166 | + String organizationId = organizationDTO.getId(); | |
165 | 167 | // 判断自己父id不能为本身的id |
166 | 168 | if (organizationDTO.getParentId() != null |
167 | - && organizationDTO.getParentId().equals(organizationDTO.getId())) { | |
169 | + && organizationDTO.getParentId().equals(organizationId)) { | |
168 | 170 | throw new TkDataValidationException(ErrorMessage.IT_CANT_EQUAL_ITSELF.name()); |
169 | 171 | } |
170 | 172 | // 选择的组织 |
171 | - TkOrganizationEntity organization = baseMapper.selectById(organizationDTO.getId()); | |
173 | + TkOrganizationEntity organization = baseMapper.selectById(organizationId); | |
172 | 174 | TkOrganizationEntity parent = null; |
173 | 175 | // 父级组织不为空,则查询到父级组织 |
174 | 176 | if (StringUtils.isNotBlank(organizationDTO.getParentId())) { |
... | ... | @@ -205,63 +207,31 @@ public class TkOrganizationServiceImpl extends AbstractBaseService<OrganizationM |
205 | 207 | (StringUtils.isNotEmpty(parentId)&&!parentId.equals(sourceParentOrganizationId))){//正常变化 |
206 | 208 | invalidateParentAndMeCacheByTenantIdAndId(tenantId,false,sourceParentOrganizationId,parentId); |
207 | 209 | } |
208 | - //获取组织修改之前原上级与目标上级所绑定的用户 | |
209 | - List<Map<String,String>> userOrgList = getUserOrgMap(getUserByOrganizationIds(parentId),getUserByOrganizationIds(sourceParentOrganizationId)); | |
210 | - baseMapper.updateById(organization); | |
211 | - //组织修改过后更新原上级与目标上级用户绑定的组织 | |
212 | - if(null!=userOrgList){ | |
213 | - for (Map<String,String> userOrg:userOrgList){ | |
214 | - String user = userOrg.get("userId"); | |
215 | - String org = userOrg.get("orgId"); | |
216 | - List<String>orgIds = organizationAllIds(tenantId, org); | |
217 | - addOrUpdateUserOrganizationMapping(user,orgIds); | |
218 | - } | |
210 | + | |
211 | + //如果源上级组织与目标组织有变更,则需要更新用户组织映射表信息 | |
212 | + if(!Objects.equals(sourceParentOrganizationId,parentId)){ | |
213 | + //删除当前组织关联的源用户映射 | |
214 | + deleteUserOrganizationMappingByOrganizationId(organizationId); | |
215 | + //查询目标组织的用户列表,新增当前组织关联的目标用户映射 | |
216 | + addUserOrganizationMapping(parentId,organizationId); | |
219 | 217 | } |
218 | + baseMapper.updateById(organization); | |
220 | 219 | return organizationDTO; |
221 | 220 | } |
222 | 221 | |
223 | - public void addOrUpdateUserOrganizationMapping( | |
224 | - String userId, List<String> organizationIds) { | |
225 | - if (StringUtils.isEmpty(userId) || null == organizationIds || organizationIds.size() < 1) { | |
226 | - throw new TkDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage()); | |
227 | - } | |
228 | - // 先删除,再新增 | |
229 | - userOrganizationMappingMapper.delete( | |
230 | - new LambdaQueryWrapper<TkUserOrganizationMappingEntity>() | |
231 | - .eq(TkUserOrganizationMappingEntity::getUserId, userId)); | |
232 | - organizationIds.forEach( | |
233 | - organizationId -> { | |
234 | - TkUserOrganizationMappingEntity userOrganizationMapping = new TkUserOrganizationMappingEntity(); | |
235 | - userOrganizationMapping.setUserId(userId); | |
236 | - userOrganizationMapping.setOrganizationId(organizationId); | |
237 | - userOrganizationMappingMapper.insert(userOrganizationMapping); | |
238 | - }); | |
222 | + private void deleteUserOrganizationMappingByOrganizationId(String organizationId){ | |
223 | + userOrganizationMappingMapper.delete(new LambdaQueryWrapper<TkUserOrganizationMappingEntity>() | |
224 | + .eq(TkUserOrganizationMappingEntity::getOrganizationId,organizationId)); | |
239 | 225 | } |
240 | 226 | |
241 | - List<Map<String,String>> getUserOrgMap(List<String>newUserIds ,List<String> oldUserIds){ | |
242 | - //目标上级原本绑定的用户 newUserIds | |
243 | - //原上级原本绑定的用户 oldUserIds | |
244 | - List<Map<String,String>> userOrgList = new ArrayList<>(); | |
245 | - if(null!=newUserIds){ | |
246 | - for (String newUserId:newUserIds) { | |
247 | - Map<String,String> map = new HashMap<>(); | |
248 | - map.put("userId",newUserId); | |
249 | - map.put("orgId", getOrganizationIdsByUser(newUserId).get(0)); | |
250 | - userOrgList.add(map); | |
251 | - } | |
252 | - } | |
253 | - if(null!=oldUserIds){ | |
254 | - for (String oldUserId:oldUserIds) { | |
255 | - Map<String,String> map = new HashMap<>(); | |
256 | - map.put("userId",oldUserId); | |
257 | - map.put("orgId", getOrganizationIdsByUser(oldUserId).get(0)); | |
258 | - userOrgList.add(map); | |
259 | - } | |
227 | + private void addUserOrganizationMapping(String targetParentId,String organizationId){ | |
228 | + List<String> userIds = getUsersByOrganizationId(targetParentId); | |
229 | + if(null != userIds && !userIds.isEmpty()){ | |
230 | + userIds.forEach(id->userOrganizationMappingMapper.insert(new TkUserOrganizationMappingEntity(id,organizationId))); | |
260 | 231 | } |
261 | - return userOrgList; | |
262 | 232 | } |
263 | 233 | // 获取当前组织的用户id集 |
264 | - List<String> getUserByOrganizationIds(String ogrId){ | |
234 | + List<String> getUsersByOrganizationId(String ogrId){ | |
265 | 235 | if (StringUtils.isEmpty(ogrId)) { |
266 | 236 | return null; |
267 | 237 | } | ... | ... |