Commit d4b5685c26909e2630aa51892e81701166585176

Authored by xp.Huang
2 parents a69d02c1 9ef3717d

Merge branch '20220507' into 'master'

fix: 设备删除,场景联动启动失败问题

See merge request huang/thingsboard3.3.2!94
... ... @@ -424,4 +424,10 @@ public class YtDeviceController extends BaseController {
424 424 sendRelationNotificationMsg(
425 425 getTenantId(), relation, EdgeEventActionType.RELATION_ADD_OR_UPDATE);
426 426 }
  427 + @GetMapping({"used/{id}"})
  428 + @ApiOperation("设备是否被占用")
  429 + public ResponseEntity<Boolean> otherUsing(@PathVariable("id") String deviceId) throws ThingsboardException {
  430 + Boolean result = deviceService.otherUsing(deviceId, getCurrentUser().getCurrentTenantId());
  431 + return ResponseEntity.ok(result);
  432 + }
427 433 }
... ...
... ... @@ -74,12 +74,19 @@ public class YtThirdPlatformController extends BaseController {
74 74 }
75 75
76 76 @DeleteMapping
77   - @ApiOperation("解绑")
  77 + @ApiOperation("解绑(第三方小程序)")
78 78 public boolean deleteAlarmProfile(@Validated(DeleteGroup.class) @RequestBody YtThirdUserDTO dto)
79 79 throws ThingsboardException {
80 80 return thirdService.unbindUser(getCurrentUser().getCurrentTenantId(), dto.getAppUserId(), dto.getThirdUserId());
81 81 }
82 82
  83 + @DeleteMapping("/unbind")
  84 + @ApiOperation("解绑(平台账号)")
  85 + public boolean deleteAlarmProfile()
  86 + throws ThingsboardException {
  87 + return thirdService.unbindUser(getCurrentUser().getCurrentUserId());
  88 + }
  89 +
83 90 @GetMapping("login/{loginCode}")
84 91 @ApiOperation("第三方登录")
85 92 public YtLoginResponse login(@PathVariable("loginCode") String loginCode)
... ...
... ... @@ -62,6 +62,7 @@ public enum ErrorMessage {
62 62 SCENE_REACT_USED_ALARM_PROFILE(400042,"场景联动正在使用该告警配置"),
63 63 APP_USER_BINDED(400043,"平台用户【%s】已被其它账号绑定"),
64 64 THIRD_PLATFORM_EXCEPTION(400044,"【%s】,第三方异常【%s】"),
  65 + DEVICE_NOT_EXTIED(400045,"设备不存在"),
65 66 HAVE_NO_PERMISSION(500002,"没有修改权限");
66 67 private final int code;
67 68 private String message;
... ...
... ... @@ -64,6 +64,7 @@ public class DeviceDTO extends TenantDTO {
64 64
65 65 @ApiModelProperty(value = "分配客户")
66 66 private String customerId;
  67 + private String customerName;
67 68
68 69 private DeviceProfileDTO deviceProfile;
69 70 private OrganizationDTO organizationDTO;
... ...
... ... @@ -179,7 +179,7 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap
179 179 }
180 180
181 181 // 先删除触发器
182   - triggerMapper.delete(
  182 + int result = triggerMapper.delete(
183 183 new QueryWrapper<Trigger>()
184 184 .lambda()
185 185 .eq(
... ... @@ -469,8 +469,10 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap
469 469 List<DeviceDTO> organizationDevices =
470 470 findDeviceList(self.getOrganizationId(), tenantId, customerId);
471 471 List<String> allDevices = new ArrayList<>();
472   - for (DeviceDTO item : organizationDevices) {
473   - allDevices.add(item.getTbDeviceId());
  472 + if(organizationDevices != null && !organizationDevices.isEmpty()){
  473 + for (DeviceDTO item : organizationDevices) {
  474 + allDevices.add(item.getTbDeviceId());
  475 + }
474 476 }
475 477
476 478 Map<String, List<String>> matchedDevices = new HashMap<>();
... ... @@ -487,8 +489,17 @@ public class SceneLinkageServiceImpl extends AbstractBaseService<SceneLinkageMap
487 489 trigger -> {
488 490 String scenId = trigger.getSceneLinkageId();
489 491 List<String> devices = trigger.getEntityId();
490   - if (ScopeEnum.ALL.equals(trigger.getEntityType()) && currentSceneId.equals(scenId)) {
491   - devices = allDevices;
  492 + if(currentSceneId.equals(scenId)){
  493 + if (ScopeEnum.ALL.equals(trigger.getEntityType()) ) {
  494 + devices = allDevices;
  495 + }else{
  496 + devices = devices.stream()
  497 + .filter(t -> allDevices.contains(t))
  498 + .collect(Collectors.toList());
  499 + }
  500 + if (devices == null || devices.isEmpty()) {
  501 + throw new YtDataValidationException(ErrorMessage.DEVICE_NOT_EXTIED.getMessage());
  502 + }
492 503 }
493 504 deviceSceneMap(matchedDevices, devices, scenId);
494 505 });
... ...
... ... @@ -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.Nullable;
9 10 import org.springframework.stereotype.Service;
10 11 import org.springframework.transaction.annotation.Transactional;
11 12 import org.thingsboard.server.common.data.DeviceProfile;
... ... @@ -355,4 +356,63 @@ public class YtDeviceServiceImpl extends AbstractBaseService<DeviceMapper, YtDev
355 356 return snBuilder.substring(0, 16);
356 357 }
357 358
  359 +
  360 + @Override
  361 + public Boolean otherUsing(String deviceId,String tenantId) {
  362 + YtDevice device = baseMapper.selectById(deviceId);
  363 + if(device == null){
  364 + throw new YtDataValidationException(ErrorMessage.DEVICE_NOT_EXTIED.getMessage());
  365 + }
  366 + String tbDeviceId = device.getTbDeviceId();
  367 +
  368 + /**场景联动*/
  369 + Boolean userd = usedBySceneLinkage(tenantId, tbDeviceId);
  370 +
  371 + return userd;
  372 + }
  373 +
  374 + @Nullable
  375 + private Boolean usedBySceneLinkage(String tenantId, String tbDeviceId) {
  376 + LambdaQueryWrapper<SceneLinkage> sceneFilter =
  377 + new QueryWrapper<SceneLinkage>()
  378 + .lambda()
  379 + .eq(SceneLinkage::getTenantId, tenantId)
  380 + .eq(SceneLinkage::getStatus, 1);
  381 + Optional<List<SceneLinkage>> list= Optional.ofNullable(sceneLinkageMapper.selectList(sceneFilter));
  382 + if(list.isEmpty()){
  383 + return false;
  384 + }
  385 + List<String> sceneIds = list.get().stream()
  386 + .map(i -> i.getId()).collect(Collectors.toList());
  387 + LambdaQueryWrapper<Trigger> triggerFilter =
  388 + new QueryWrapper<Trigger>()
  389 + .lambda()
  390 + .in(sceneIds.size()>0,Trigger::getSceneLinkageId, sceneIds)
  391 + .eq(Trigger::getEntityId, tbDeviceId);
  392 + Optional<List<Trigger>> triggers = Optional.ofNullable(triggerMapper.selectList(triggerFilter));
  393 + if(triggers.isPresent() && triggers.get().size()>0){
  394 + return true;
  395 + }
  396 +
  397 + LambdaQueryWrapper<DoCondition> conditionFilter =
  398 + new QueryWrapper<DoCondition>()
  399 + .lambda()
  400 + .in(sceneIds.size()>0,DoCondition::getSceneLinkageId, sceneIds)
  401 + .eq(DoCondition::getEntityId, tbDeviceId);
  402 + Optional<List<DoCondition>> doConditions = Optional.ofNullable(conditionMapper.selectList(conditionFilter));
  403 + if(doConditions.isPresent()&& doConditions.get().size()>0){
  404 + return true;
  405 + }
  406 +
  407 + LambdaQueryWrapper<DoAction> actionFilter =
  408 + new QueryWrapper<DoAction>()
  409 + .lambda()
  410 + .in(sceneIds.size()>0,DoAction::getSceneLinkageId, sceneIds)
  411 + .eq(DoAction::getDeviceId, tbDeviceId);
  412 + Optional<List<DoAction>> doActions = Optional.ofNullable(actionMapper.selectList(actionFilter));
  413 + if(doActions.isPresent()&& doActions.get().size()>0){
  414 + return true;
  415 + }
  416 + return false;
  417 + }
358 418 }
... ...
... ... @@ -178,6 +178,15 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf
178 178 return baseMapper.delete(filter) > 0;
179 179 }
180 180
  181 + @Override
  182 + @Transactional(rollbackFor = Exception.class)
  183 + public boolean unbindUser(String appUserId) {
  184 + Wrapper filter = new QueryWrapper<YtThirdUserEntity>().lambda()
  185 + .eq(YtThirdUserEntity::getAppUserId,appUserId);
  186 + baseMapper.delete(filter);
  187 + return true;
  188 + }
  189 +
181 190 private static final String loginApi="/sns/jscode2session?appid={appid}&secret={appid}&js_code={appid}&grant_type=authorization_code";
182 191 @Override
183 192 public String thirdLogin(String loginCode) {
... ...
... ... @@ -94,4 +94,13 @@ public interface YtDeviceService extends BaseService<YtDevice> {
94 94 * @return
95 95 */
96 96 String generateSn();
  97 +
  98 + /**
  99 + *
  100 + * 设备是否被其它资源使用,例如:场景联动
  101 + * @param deviceId 平台设备ID
  102 + * @param tenantId 租户ID
  103 + * @return
  104 + */
  105 + Boolean otherUsing(String deviceId,String tenantId);
97 106 }
... ...
... ... @@ -35,12 +35,20 @@ public interface YtThirdPlatformService extends BaseService<YtThirdUserEntity> {
35 35
36 36 /**
37 37 * 第三方平台用户与系统用户解绑
38   - * @param appUserId
39   - * @param thirdUserId
  38 + * @param appUserId 平台用户ID
  39 + * @param thirdUserId 第三方用户ID
40 40 * @return
41 41 */
42 42 boolean unbindUser(String tenantId,String appUserId,String thirdUserId);
43 43
  44 +
  45 + /**
  46 + * 第三方平台用户与系统用户解绑
  47 + * @param appUserId 平台用户ID
  48 + * @return
  49 + */
  50 + boolean unbindUser(String appUserId);
  51 +
44 52 /**
45 53 * 第三方登录
46 54 *
... ...
... ... @@ -25,6 +25,7 @@
25 25 <result property="updater" column="updater"/>
26 26 <result property="description" column="description"/>
27 27 <result property="customerId" column="customer_id"/>
  28 + <result property="customerName" column="cusotomer_name"/>
28 29 <result property="organizationId" column="organization_id"/>
29 30 <result property="gatewayId" column="gateway_id"/>
30 31 <result property="gatewayName" column="gateway_name"/>
... ... @@ -72,7 +73,7 @@
72 73 </sql>
73 74 <select id="getDevicePage" resultMap="deviceMap">
74 75 SELECT
75   - <include refid="pageColumns"/>,d.customer_id::TEXT AS customer_id
  76 + <include refid="pageColumns"/>,d.customer_id::TEXT AS customer_id,cus.title AS cusotomer_name
76 77 FROM iotfs_device ifd
77 78 LEFT JOIN device_profile ifdp ON ifd.profile_id = ifdp.id::TEXT
78 79 LEFT JOIN iotfs_organization io ON io.id = ifd.organization_id
... ... @@ -83,6 +84,7 @@
83 84 LEFT JOIN attribute_kv c ON ifd.tb_device_id = c.entity_id::TEXT AND c.entity_type ='DEVICE' AND
84 85 c.attribute_key='inactivityAlarmTime'
85 86 LEFT JOIN device d ON d.id::TEXT = ifd.tb_device_id
  87 + LEFT JOIN customer cus ON cus.id = d.customer_id
86 88 <where>
87 89 <if test="queryMap.tenantId !=null and queryMap.tenantId !=''">
88 90 AND ifd.tenant_id = #{queryMap.tenantId}
... ...