Commit 69f29bb5e7fa5db78e7e6ebfc242775c4aa00185

Authored by 黄 x
1 parent 11c7f4a9

feat: add customer device mapping

@@ -64,6 +64,7 @@ import org.thingsboard.server.common.data.page.PageData; @@ -64,6 +64,7 @@ import org.thingsboard.server.common.data.page.PageData;
64 import org.thingsboard.server.common.data.page.PageLink; 64 import org.thingsboard.server.common.data.page.PageLink;
65 import org.thingsboard.server.common.data.page.TimePageLink; 65 import org.thingsboard.server.common.data.page.TimePageLink;
66 import org.thingsboard.server.common.data.security.DeviceCredentials; 66 import org.thingsboard.server.common.data.security.DeviceCredentials;
  67 +import org.thingsboard.server.common.data.yunteng.dto.TkCustomerDeviceDTO;
67 import org.thingsboard.server.common.msg.TbMsg; 68 import org.thingsboard.server.common.msg.TbMsg;
68 import org.thingsboard.server.common.msg.TbMsgDataType; 69 import org.thingsboard.server.common.msg.TbMsgDataType;
69 import org.thingsboard.server.common.msg.TbMsgMetaData; 70 import org.thingsboard.server.common.msg.TbMsgMetaData;
@@ -72,6 +73,7 @@ import org.thingsboard.server.dao.device.claim.ClaimResult; @@ -72,6 +73,7 @@ import org.thingsboard.server.dao.device.claim.ClaimResult;
72 import org.thingsboard.server.dao.device.claim.ReclaimResult; 73 import org.thingsboard.server.dao.device.claim.ReclaimResult;
73 import org.thingsboard.server.dao.exception.IncorrectParameterException; 74 import org.thingsboard.server.dao.exception.IncorrectParameterException;
74 import org.thingsboard.server.dao.model.ModelConstants; 75 import org.thingsboard.server.dao.model.ModelConstants;
  76 +import org.thingsboard.server.dao.yunteng.service.TkCustomerDevice;
75 import org.thingsboard.server.queue.util.TbCoreComponent; 77 import org.thingsboard.server.queue.util.TbCoreComponent;
76 import org.thingsboard.server.service.device.DeviceBulkImportService; 78 import org.thingsboard.server.service.device.DeviceBulkImportService;
77 import org.thingsboard.server.service.gateway_device.GatewayNotificationsService; 79 import org.thingsboard.server.service.gateway_device.GatewayNotificationsService;
@@ -132,6 +134,8 @@ public class DeviceController extends BaseController { @@ -132,6 +134,8 @@ public class DeviceController extends BaseController {
132 134
133 private final GatewayNotificationsService gatewayNotificationsService; 135 private final GatewayNotificationsService gatewayNotificationsService;
134 136
  137 + private final TkCustomerDevice tkCustomerDevice;
  138 +
135 @ApiOperation(value = "Get Device (getDeviceById)", 139 @ApiOperation(value = "Get Device (getDeviceById)",
136 notes = "Fetch the Device object based on the provided Device Id. " + 140 notes = "Fetch the Device object based on the provided Device Id. " +
137 "If the user has the authority of 'TENANT_ADMIN', the server checks that the device is owned by the same tenant. " + 141 "If the user has the authority of 'TENANT_ADMIN', the server checks that the device is owned by the same tenant. " +
@@ -311,6 +315,8 @@ public class DeviceController extends BaseController { @@ -311,6 +315,8 @@ public class DeviceController extends BaseController {
311 sendEntityAssignToCustomerNotificationMsg(savedDevice.getTenantId(), savedDevice.getId(), 315 sendEntityAssignToCustomerNotificationMsg(savedDevice.getTenantId(), savedDevice.getId(),
312 customerId, EdgeEventActionType.ASSIGNED_TO_CUSTOMER); 316 customerId, EdgeEventActionType.ASSIGNED_TO_CUSTOMER);
313 317
  318 + //ThingsKit save
  319 + tkCustomerDevice.saveCustomerDeviceMapping(new TkCustomerDeviceDTO(strCustomerId,strDeviceId));
314 return savedDevice; 320 return savedDevice;
315 } catch (Exception e) { 321 } catch (Exception e) {
316 logEntityAction(emptyId(EntityType.DEVICE), null, 322 logEntityAction(emptyId(EntityType.DEVICE), null,
@@ -345,6 +351,8 @@ public class DeviceController extends BaseController { @@ -345,6 +351,8 @@ public class DeviceController extends BaseController {
345 sendEntityAssignToCustomerNotificationMsg(savedDevice.getTenantId(), savedDevice.getId(), 351 sendEntityAssignToCustomerNotificationMsg(savedDevice.getTenantId(), savedDevice.getId(),
346 customer.getId(), EdgeEventActionType.UNASSIGNED_FROM_CUSTOMER); 352 customer.getId(), EdgeEventActionType.UNASSIGNED_FROM_CUSTOMER);
347 353
  354 + //ThingsKit delete
  355 + tkCustomerDevice.deleteCustomerDeviceMapping(List.of(strDeviceId));
348 return savedDevice; 356 return savedDevice;
349 } catch (Exception e) { 357 } catch (Exception e) {
350 logEntityAction(emptyId(EntityType.DEVICE), null, 358 logEntityAction(emptyId(EntityType.DEVICE), null,
@@ -108,6 +108,8 @@ public final class ModelConstants { @@ -108,6 +108,8 @@ public final class ModelConstants {
108 public static final String TK_DATA_COMPONENT_NAME = "tk_data_component"; 108 public static final String TK_DATA_COMPONENT_NAME = "tk_data_component";
109 /** 物模型 */ 109 /** 物模型 */
110 public static final String TK_THING_MODEL = "tk_things_model"; 110 public static final String TK_THING_MODEL = "tk_things_model";
  111 + /** 客户和设备的映射表 */
  112 + public static final String TK_CUSTOMER_DEVICE_NAME = "tk_customer_device";
111 } 113 }
112 114
113 public static class TableFields { 115 public static class TableFields {
  1 +package org.thingsboard.server.common.data.yunteng.dto;
  2 +
  3 +import io.swagger.annotations.ApiModelProperty;
  4 +import lombok.Data;
  5 +import lombok.RequiredArgsConstructor;
  6 +import org.thingsboard.server.common.data.yunteng.common.AddGroup;
  7 +import org.thingsboard.server.common.data.yunteng.common.DeleteGroup;
  8 +
  9 +import javax.validation.constraints.NotEmpty;
  10 +
  11 +@Data
  12 +public class TkCustomerDeviceDTO {
  13 +
  14 + @ApiModelProperty(value = "客户ID", required = true)
  15 + @NotEmpty(
  16 + message = "客户ID不能为空或空字符串",
  17 + groups = {AddGroup.class, DeleteGroup.class})
  18 + private String customerId;
  19 + @ApiModelProperty(value = "TB设备ID", required = true)
  20 + @NotEmpty(
  21 + message = "设备ID不能为空或空字符串",
  22 + groups = {AddGroup.class, DeleteGroup.class})
  23 + private String deviceId;
  24 +
  25 + public TkCustomerDeviceDTO(String customerId, String deviceId) {
  26 + this.customerId = customerId;
  27 + this.deviceId = deviceId;
  28 + }
  29 + public TkCustomerDeviceDTO(){
  30 +
  31 +
  32 + }
  33 +}
  1 +package org.thingsboard.server.dao.yunteng.entities;
  2 +
  3 +import com.baomidou.mybatisplus.annotation.TableName;
  4 +import lombok.Data;
  5 +import org.thingsboard.server.common.data.yunteng.constant.ModelConstants;
  6 +
  7 +import java.io.Serializable;
  8 +
  9 +@Data
  10 +@TableName(ModelConstants.Table.TK_CUSTOMER_DEVICE_NAME)
  11 +public class TkCustomerDeviceEntity implements Serializable {
  12 +
  13 + private static final long serialVersionUID = -3419023927666636379L;
  14 + private String customerId;
  15 + private String deviceId;
  16 +}
  1 +package org.thingsboard.server.dao.yunteng.impl;
  2 +
  3 +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4 +import lombok.RequiredArgsConstructor;
  5 +import org.springframework.beans.BeanUtils;
  6 +import org.springframework.stereotype.Service;
  7 +import org.thingsboard.server.common.data.StringUtils;
  8 +import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
  9 +import org.thingsboard.server.common.data.yunteng.core.exception.YtDataValidationException;
  10 +import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
  11 +import org.thingsboard.server.common.data.yunteng.dto.TkCustomerDeviceDTO;
  12 +import org.thingsboard.server.dao.yunteng.entities.TkCustomerDeviceEntity;
  13 +import org.thingsboard.server.dao.yunteng.mapper.TkCustomerDeviceMapper;
  14 +import org.thingsboard.server.dao.yunteng.service.TkCustomerDevice;
  15 +import java.util.List;
  16 +import java.util.Optional;
  17 +import java.util.stream.Collectors;
  18 +
  19 +@Service
  20 +@RequiredArgsConstructor
  21 +public class TkCustomerDeviceImpl implements TkCustomerDevice {
  22 + private final TkCustomerDeviceMapper tkCustomerDeviceMapper;
  23 +
  24 + @Override
  25 + public List<TkCustomerDeviceDTO> getMappingByCustomerId(String customerId) {
  26 + if (StringUtils.isEmpty(customerId)) {
  27 + throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
  28 + }
  29 + List<TkCustomerDeviceEntity> entities =
  30 + tkCustomerDeviceMapper.selectList(
  31 + new LambdaQueryWrapper<TkCustomerDeviceEntity>()
  32 + .eq(TkCustomerDeviceEntity::getCustomerId, customerId));
  33 + return Optional.ofNullable(entities)
  34 + .map(
  35 + list ->
  36 + list.stream()
  37 + .map(
  38 + obj -> {
  39 + TkCustomerDeviceDTO target = new TkCustomerDeviceDTO();
  40 + BeanUtils.copyProperties(obj, target);
  41 + return target;
  42 + })
  43 + .collect(Collectors.toList()))
  44 + .orElse(null);
  45 + }
  46 +
  47 + @Override
  48 + public TkCustomerDeviceDTO saveCustomerDeviceMapping(TkCustomerDeviceDTO tkCustomerDeviceDTO) {
  49 + if (StringUtils.isEmpty(tkCustomerDeviceDTO.getCustomerId())
  50 + || StringUtils.isEmpty(tkCustomerDeviceDTO.getDeviceId())) {
  51 + throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
  52 + }
  53 + TkCustomerDeviceEntity entity = new TkCustomerDeviceEntity();
  54 + BeanUtils.copyProperties(tkCustomerDeviceDTO, entity);
  55 + tkCustomerDeviceMapper.insert(entity);
  56 + return tkCustomerDeviceDTO;
  57 + }
  58 +
  59 + @Override
  60 + public boolean deleteCustomerDeviceMapping(List<String> deviceList) {
  61 + if (null == deviceList || deviceList.isEmpty()) {
  62 + throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
  63 + }
  64 + return tkCustomerDeviceMapper.delete(
  65 + new LambdaQueryWrapper<TkCustomerDeviceEntity>()
  66 + .in(TkCustomerDeviceEntity::getDeviceId, deviceList))
  67 + > FastIotConstants.MagicNumber.ZERO;
  68 + }
  69 +}
  1 +package org.thingsboard.server.dao.yunteng.mapper;
  2 +
  3 +import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  4 +import org.apache.ibatis.annotations.Mapper;
  5 +import org.thingsboard.server.dao.yunteng.entities.TkCustomerDeviceEntity;
  6 +
  7 +@Mapper
  8 +public interface TkCustomerDeviceMapper extends BaseMapper<TkCustomerDeviceEntity> {}
  1 +package org.thingsboard.server.dao.yunteng.service;
  2 +
  3 +import org.thingsboard.server.common.data.yunteng.dto.TkCustomerDeviceDTO;
  4 +
  5 +import java.util.List;
  6 +
  7 +public interface TkCustomerDevice {
  8 +
  9 + List<TkCustomerDeviceDTO> getMappingByCustomerId(String customerId);
  10 +
  11 + TkCustomerDeviceDTO saveCustomerDeviceMapping(TkCustomerDeviceDTO tkCustomerDeviceDTO);
  12 +
  13 + boolean deleteCustomerDeviceMapping(List<String> deviceList);
  14 +}