Commit c7b45e618e29082dd6f52ff2459d40f5b359e6b4

Authored by xp.Huang
2 parents 5b41f39d 1fb64e05

Merge branch '20220517' into 'master'

20220517

See merge request huang/thingsboard3.3.2!101
... ... @@ -353,8 +353,8 @@ public class YtDeviceController extends BaseController {
353 353 @ApiOperation("获取该组织的所有设备")
354 354 public List<DeviceDTO> getGatewayDevices(
355 355 @ApiParam(value = "组织ID") @RequestParam("organizationId") String organizationId,
356   - @ApiParam(value = "设备类型") @PathVariable("deviceType") DeviceTypeEnum deviceType) {
357   - return deviceService.findDevicesByDeviceTypeAndOrganizationId(deviceType, organizationId);
  356 + @ApiParam(value = "设备类型") @PathVariable("deviceType") DeviceTypeEnum deviceType) throws ThingsboardException {
  357 + return deviceService.findDevicesByDeviceTypeAndOrganizationId(deviceType,getCurrentUser().getCurrentTenantId(), organizationId);
358 358 }
359 359
360 360 @GetMapping("/gateway/{tbDeviceId}")
... ...
... ... @@ -92,7 +92,7 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap
92 92 new QueryWrapper<SceneLinkage>()
93 93 .lambda()
94 94 .eq(SceneLinkage::getTenantId, tenantId)
95   - .eq(SceneLinkage::getCreator, currentUserId)
  95 + .eq(StringUtils.isNotBlank(currentUserId),SceneLinkage::getCreator, currentUserId)
96 96 .in(SceneLinkage::getId, ids);
97 97 int result = sceneLinkageMapper.delete(Wrapper);
98 98 if (result != ids.size()) {
... ...
... ... @@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
6 6 import lombok.RequiredArgsConstructor;
7 7 import lombok.extern.slf4j.Slf4j;
8 8 import org.apache.commons.lang3.StringUtils;
  9 +import org.jetbrains.annotations.NotNull;
9 10 import org.jetbrains.annotations.Nullable;
10 11 import org.springframework.stereotype.Service;
11 12 import org.springframework.transaction.annotation.Transactional;
... ... @@ -217,12 +218,13 @@ public class YtDeviceServiceImpl extends AbstractBaseService<DeviceMapper, YtDev
217 218
218 219 @Override
219 220 public List<DeviceDTO> findDevicesByDeviceTypeAndOrganizationId(
220   - DeviceTypeEnum deviceType, String organizationId) {
  221 + DeviceTypeEnum deviceType, String tenantId, String organizationId) {
  222 + List<String> orgIds = organizationAllIds(tenantId,organizationId);
221 223 return ReflectUtils.sourceToTarget(
222 224 baseMapper.selectList(
223 225 new LambdaQueryWrapper<YtDevice>()
224 226 .eq(YtDevice::getDeviceType, deviceType)
225   - .eq(YtDevice::getOrganizationId, organizationId)),
  227 + .in(YtDevice::getOrganizationId, orgIds)),
226 228 DeviceDTO.class);
227 229 }
228 230
... ... @@ -286,16 +288,7 @@ public class YtDeviceServiceImpl extends AbstractBaseService<DeviceMapper, YtDev
286 288 queryMap.put("tenantId", tenantId);
287 289 String organizationId = (String) queryMap.get("organizationId");
288 290 if (!StringUtils.isEmpty(organizationId)) {
289   - List<String> organizationIds = new ArrayList<>();
290   - organizationIds.add(organizationId);
291   - // 查询该组织的所有子类
292   - List<OrganizationDTO> organizationDTOS =
293   - ytOrganizationMapper.findOrganizationTreeList(tenantId, organizationIds);
294   - List<String> queryOrganizationIds = new ArrayList<>();
295   - organizationDTOS.forEach(
296   - item -> {
297   - queryOrganizationIds.add(item.getId());
298   - });
  291 + List<String> queryOrganizationIds = organizationAllIds(tenantId, organizationId);
299 292 queryMap.put("organizationIds", queryOrganizationIds);
300 293 }
301 294 IPage<YtDevice> page = getPage(queryMap, FastIotConstants.DefaultOrder.CREATE_TIME, false);
... ... @@ -311,6 +304,27 @@ public class YtDeviceServiceImpl extends AbstractBaseService<DeviceMapper, YtDev
311 304 return new YtPageData<>(records, deviceIPage.getTotal());
312 305 }
313 306
  307 + /**
  308 + * 组织结构下的所有组织ID
  309 + * @param tenantId 租户ID
  310 + * @param organizationId 组织ID
  311 + * @return
  312 + */
  313 + @NotNull
  314 + private List<String> organizationAllIds(String tenantId, String organizationId) {
  315 + List<String> organizationIds = new ArrayList<>();
  316 + organizationIds.add(organizationId);
  317 + // 查询该组织的所有子类
  318 + List<OrganizationDTO> organizationDTOS =
  319 + ytOrganizationMapper.findOrganizationTreeList(tenantId, organizationIds);
  320 + List<String> queryOrganizationIds = new ArrayList<>();
  321 + organizationDTOS.forEach(
  322 + item -> {
  323 + queryOrganizationIds.add(item.getId());
  324 + });
  325 + return queryOrganizationIds;
  326 + }
  327 +
314 328 @Override
315 329 public YtPageData<RelationDeviceDTO> pageRelation(Map<String, Object> queryMap) {
316 330 IPage<YtDevice> page = getPage(queryMap, "last_online_time", false);
... ...
... ... @@ -198,44 +198,37 @@ public class YtTenantServiceImpl extends AbstractBaseService<TenantMapper, Tenan
198 198
199 199 /**
200 200 * 刷新租户管理员与角色关系
  201 + * 1、1个租户1个角色;1个角色多个租户
  202 + * 2、1个角色多个用户(租户管理员)
201 203 *
202 204 * @param tenantIds 租户ID
203 205 * @param newRoles 新增的角色ID
204 206 */
205 207 private void freshTenantAdminRole(Set<String> tenantIds, List<String> newRoles) {
206   - AtomicBoolean notChanged = new AtomicBoolean(false);
207 208 LambdaQueryWrapper<TenantRole> roleFilter = new QueryWrapper<TenantRole>().lambda()
208 209 .in(TenantRole::getTenantId, tenantIds);
209   - List<String> oldRoleIds = tenantRoleMapper.selectList(roleFilter).stream()
  210 + List<String> deleteIds = tenantRoleMapper.selectList(roleFilter).stream()
210 211 .map(i -> i.getRoleId())
211 212 .collect(Collectors.toList());
212   - List<String> deleteIds = oldRoleIds.stream()
213   - .filter(f -> {
214   - if(newRoles == null || newRoles.isEmpty()){
215   - return true;
216   - }
217   - if(newRoles.contains(f)){
218   - notChanged.set(true);
219   - return false;
220   - }
221   - return true;
222   - })
223   - .collect(Collectors.toList());
224   - if(!deleteIds.isEmpty()){
225   - LambdaQueryWrapper<UserRole> filter = new QueryWrapper<UserRole>().lambda()
226   - .in(UserRole::getRoleId, deleteIds);
227   - userRoleMapper.delete(filter);
228   - }
229 213
230   - if (notChanged.get() || newRoles == null || newRoles.isEmpty()) {
231   - return;
232   - }
233 214 LambdaQueryWrapper<User> userFilter = new QueryWrapper<User>().lambda()
234 215 .eq(User::getLevel,2)
235 216 .in(User::getTenantId, tenantIds);
236 217 List<String> userIds = userMapper.selectList(userFilter).stream()
237 218 .map(m -> m.getId())
238 219 .collect(Collectors.toList());
  220 +
  221 + if(!deleteIds.isEmpty() && !userIds.isEmpty()){
  222 + LambdaQueryWrapper<UserRole> filter = new QueryWrapper<UserRole>().lambda()
  223 + .in(UserRole::getRoleId, deleteIds)
  224 + .in(UserRole::getUserId,userIds);
  225 + userRoleMapper.delete(filter);
  226 + }
  227 +
  228 + if (newRoles == null || newRoles.isEmpty()
  229 + || userIds == null || userIds.isEmpty()) {
  230 + return;
  231 + }
239 232 userIds.forEach(item -> {
240 233 for (String roleId : newRoles) {
241 234 UserRole role = new UserRole();
... ...
1 1 package org.thingsboard.server.dao.yunteng.impl;
2 2
  3 +import com.baomidou.mybatisplus.core.conditions.Wrapper;
3 4 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
4 5 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
5 6 import com.baomidou.mybatisplus.core.metadata.IPage;
... ... @@ -70,6 +71,8 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User>
70 71 private final YtMessageTemplateService messageTemplateService;
71 72 private final YtOrganizationService organizationService;
72 73 private final UserOrganizationMappingService userOrganizationMappingService;
  74 + private final SceneLinkageService sceneLinkageService;
  75 + private final SceneLinkageMapper sceneLinkageMapper;
73 76
74 77 public static final String ACTIVATE_URL_PATTERN = "%s/api/noauth/activate?activateToken=%s";
75 78 private final PasswordEncoder passwordEncoder;
... ... @@ -390,6 +393,20 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User>
390 393 // 4. 删除人员
391 394 baseMapper.deleteBatchIds(userIds);
392 395
  396 + // 5、删除场景联动
  397 + Wrapper filter = new QueryWrapper<SceneLinkage>().lambda()
  398 + .in(SceneLinkage::getCreator,userIds);
  399 + List<SceneLinkage> sceneLinkages =sceneLinkageMapper.selectList(filter);
  400 + if(sceneLinkages ==null || sceneLinkages.isEmpty()){
  401 + return true;
  402 + }
  403 + Set<String> sceneIds = sceneLinkages.stream()
  404 + .map(i -> i.getId())
  405 + .collect(Collectors.toSet());
  406 + sceneLinkageService.deleteSceneLinkage(sceneIds, tenantId,null);
  407 +
  408 +
  409 +
393 410 return true;
394 411 }
395 412
... ...
... ... @@ -48,7 +48,7 @@ public interface YtDeviceService extends BaseService<YtDevice> {
48 48 * @return 设备列表
49 49 */
50 50 List<DeviceDTO> findDevicesByDeviceTypeAndOrganizationId(
51   - DeviceTypeEnum deviceType, String organizationId);
  51 + DeviceTypeEnum deviceType, String tenantId, String organizationId);
52 52
53 53 /**
54 54 * 通过设备ID和租户ID判断该设备是否存在
... ...
... ... @@ -41,8 +41,13 @@
41 41 SELECT
42 42 <include refid="menuDTOMapColumn">
43 43 </include>
44   - FROM sys_menu
45   - WHERE id IN (SELECT menu_id FROM sys_tenant_menu WHERE tenant_id = #{tenantId})
  44 + FROM sys_menu WHERE id IN (
  45 + SELECT menu_id FROM sys_role_menu WHERE role_id IN (
  46 + SELECT id FROM sys_role WHERE id in (
  47 + SELECT role_id FROM sys_tenant_role WHERE tenant_id = #{tenantId}
  48 + ) AND enabled=true
  49 + )
  50 + )
46 51 </select>
47 52
48 53 <select id="selectMyMenu" resultMap="menuDTOMap">
... ... @@ -50,10 +55,13 @@
50 55 <include refid="menuDTOMapColumn">
51 56 </include>
52 57 FROM sys_menu
53   - WHERE id IN (SELECT menu_id
54   - FROM sys_role_menu
55   - WHERE role_id IN (SELECT id FROM sys_role WHERE id in (SELECT role_id FROM sys_user_role WHERE user_id
56   - = #{userId}) AND enabled=true))
  58 + WHERE id IN (
  59 + SELECT menu_id FROM sys_role_menu WHERE role_id IN (
  60 + SELECT id FROM sys_role WHERE id in (
  61 + SELECT role_id FROM sys_user_role WHERE user_id = #{userId}
  62 + ) AND enabled=true
  63 + )
  64 + )
57 65 </select>
58 66
59 67 <delete id="deleteTenantMenuMappingByMenuIds">
... ...