Commit 60d8071ebb1b7ae621e3ca352de8ab05a361098b

Authored by xp.Huang
1 parent 1b2aeb24

fix: 修改TCP网关子设备时,验证设备地址码或设备标识是否重复的bug

@@ -4,7 +4,6 @@ import com.fasterxml.jackson.databind.JsonNode; @@ -4,7 +4,6 @@ import com.fasterxml.jackson.databind.JsonNode;
4 import com.fasterxml.jackson.databind.ObjectMapper; 4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.fasterxml.jackson.databind.node.ObjectNode; 5 import com.fasterxml.jackson.databind.node.ObjectNode;
6 import io.swagger.annotations.Api; 6 import io.swagger.annotations.Api;
7 -import io.swagger.annotations.ApiModelProperty;  
8 import io.swagger.annotations.ApiOperation; 7 import io.swagger.annotations.ApiOperation;
9 import io.swagger.annotations.ApiParam; 8 import io.swagger.annotations.ApiParam;
10 import lombok.RequiredArgsConstructor; 9 import lombok.RequiredArgsConstructor;
@@ -78,19 +77,12 @@ public class TkDeviceController extends BaseController { @@ -78,19 +77,12 @@ public class TkDeviceController extends BaseController {
78 DeviceDTO gateWay = null; 77 DeviceDTO gateWay = null;
79 if (StringUtils.isNotEmpty(gatewayId)) { 78 if (StringUtils.isNotEmpty(gatewayId)) {
80 gateWay = 79 gateWay =
81 - tkdeviceService.checkDeviceByTenantIdAndDeviceId(  
82 - getCurrentUser().getCurrentTenantId(), gatewayId); 80 + tkdeviceService.checkDeviceByTenantIdAndDeviceId(currentTenantId, gatewayId);
83 if (null == gateWay) { 81 if (null == gateWay) {
84 throw new TkDataValidationException( 82 throw new TkDataValidationException(
85 ErrorMessage.DEVICE_NOT_EXISTENCE_IN_TENANT.getMessage()); 83 ErrorMessage.DEVICE_NOT_EXISTENCE_IN_TENANT.getMessage());
86 } 84 }
87 } 85 }
88 - String identifier = StringUtils.isBlank(deviceDTO.getCode())?deviceDTO.getName():deviceDTO.getCode();  
89 - String markResult =tkdeviceService.markExisted(getCurrentUser().getCurrentTenantId(), gatewayId,deviceDTO.getId(),identifier);  
90 - if(StringUtils.isNotEmpty(markResult)){  
91 - throw new TkDataValidationException(markResult);  
92 - }  
93 -  
94 /** 86 /**
95 * 子设备编辑业务逻辑: 设备地址码必须同时设置到附加信息字段内。 1、新增或编辑网关和直连设备 2、新增网关子设备 3、编辑网关子设备时,关联关系已存在(未切换网关) 87 * 子设备编辑业务逻辑: 设备地址码必须同时设置到附加信息字段内。 1、新增或编辑网关和直连设备 2、新增网关子设备 3、编辑网关子设备时,关联关系已存在(未切换网关)
96 * 4、编辑网关子设备时,关联关系不存在(切换网关) 5、编辑网关子设备时,修改其它设备信息,例如:设备名称等。 88 * 4、编辑网关子设备时,关联关系不存在(切换网关) 5、编辑网关子设备时,修改其它设备信息,例如:设备名称等。
@@ -500,19 +492,6 @@ public class TkDeviceController extends BaseController { @@ -500,19 +492,6 @@ public class TkDeviceController extends BaseController {
500 result.setMessage(str); 492 result.setMessage(str);
501 return result; 493 return result;
502 } 494 }
503 - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{})")  
504 - @GetMapping("exist")  
505 - @ApiOperation("设备标识符是否已经被使用")  
506 - public ResponseResult<Boolean> existed(  
507 - @ApiParam(value = "网关设备主键") @RequestParam(name="gateway",required = false) String gatewayId,  
508 - @ApiParam(value = "当前设备主键,编辑时使用") @RequestParam(name="devId",required = false) String devId,  
509 - @ApiParam(value = "设备标识符,例如:设备名称、地址码", required = true)@RequestParam("identifier") String identifier)  
510 - throws ThingsboardException {  
511 - String str = tkdeviceService.markExisted(getCurrentUser().getCurrentTenantId(),gatewayId,devId,identifier);  
512 - ResponseResult result = ResponseResult.success(StringUtils.isEmpty(str) ? true : false);  
513 - result.setMessage(str);  
514 - return result;  
515 - }  
516 @GetMapping({"/attributes/{deviceProfileId}"}) 495 @GetMapping({"/attributes/{deviceProfileId}"})
517 @ApiOperation("获取设备的属性") 496 @ApiOperation("获取设备的属性")
518 public ResponseEntity<JsonNode> getDeviceAttributes( 497 public ResponseEntity<JsonNode> getDeviceAttributes(
@@ -100,8 +100,7 @@ public enum ErrorMessage { @@ -100,8 +100,7 @@ public enum ErrorMessage {
100 TASK_CENTER_IS_DISABLED(400076,"【%s】任务是禁用状态,不能执行"), 100 TASK_CENTER_IS_DISABLED(400076,"【%s】任务是禁用状态,不能执行"),
101 NAME_OR_IDENTIFIER_IS_EMPTY(400077,"【%s】的名称或标识符不能为空或空字符串"), 101 NAME_OR_IDENTIFIER_IS_EMPTY(400077,"【%s】的名称或标识符不能为空或空字符串"),
102 IDENTIFIER_ALREADY_EXISTS(400078,"标识符【%s】已经存在"), 102 IDENTIFIER_ALREADY_EXISTS(400078,"标识符【%s】已经存在"),
103 - DEVICE_GATEWAY_NOT_EXTIED(400079,"网关设备【%s】不存在"),  
104 - DEVICE_IDENTIFIER_REPEATED(400079,"设备标识【%s】与设备【%s】重复"), 103 + DEVICE_IDENTIFIER_REPEATED(400079,"设备地址码或标识码【%s】与设备【%s】重复"),
105 HAVE_NO_PERMISSION(500002,"没有修改权限"); 104 HAVE_NO_PERMISSION(500002,"没有修改权限");
106 private final int code; 105 private final int code;
107 private String message; 106 private String message;
@@ -140,6 +140,23 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev @@ -140,6 +140,23 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev
140 } else if (!organization.getTenantId().equals(deviceTenantId)) { 140 } else if (!organization.getTenantId().equals(deviceTenantId)) {
141 throw new TkDataValidationException(ErrorMessage.TENANT_MISMATCHING.getMessage()); 141 throw new TkDataValidationException(ErrorMessage.TENANT_MISMATCHING.getMessage());
142 } 142 }
  143 + //如果是TCP的网关子设备,要验证其Code是否已在网关的其他子设备存在
  144 + if(Objects.equals(deviceDTO.getDeviceType(),DeviceTypeEnum.SENSOR) && Objects.equals(deviceProfile.getTransportType(),
  145 + DeviceTransportType.TCP)){
  146 + String gateWayId = deviceDTO.getGatewayId();
  147 + List<TkDeviceEntity> entities = baseMapper.selectList(new LambdaQueryWrapper<TkDeviceEntity>()
  148 + .eq(TkDeviceEntity::getTenantId,currentTenantId)
  149 + .eq(TkDeviceEntity::getGatewayId,gateWayId)
  150 + .ne(!insert,TkDeviceEntity::getId,deviceDTO.getId()));
  151 + if(null != entities && !entities.isEmpty()){
  152 + for (TkDeviceEntity entity:entities) {
  153 + if(Objects.equals(deviceDTO.getCode(),entity.getCode())){
  154 + throw new TkDataValidationException(String.format(ErrorMessage.DEVICE_IDENTIFIER_REPEATED.getMessage(),
  155 + deviceDTO.getCode(),StringUtils.isEmpty(entity.getAlias())?entity.getName():entity.getAlias()));
  156 + }
  157 + }
  158 + }
  159 + }
143 } 160 }
144 161
145 /** 162 /**
@@ -758,49 +775,4 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev @@ -758,49 +775,4 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev
758 } 775 }
759 return result; 776 return result;
760 } 777 }
761 -  
762 - @Override  
763 - public String markExisted(String tenantId,String gatewayId,String deviceId, String identifier) {  
764 - if(StringUtils.isNotBlank(deviceId)){//编辑设备时,未修改设备标识符  
765 - TkDeviceEntity self = baseMapper.selectOne(  
766 - new LambdaQueryWrapper<TkDeviceEntity>()  
767 - .eq(TkDeviceEntity::getTenantId, tenantId)  
768 - .eq(TkDeviceEntity::getId, deviceId));  
769 - if(self.getGatewayId().equals(gatewayId)  
770 - &&( self.getCode().equals(identifier) || self.getName().equals(identifier))){  
771 - return "";  
772 - }  
773 - }  
774 - TkDeviceEntity gateway =null;  
775 - if(StringUtils.isNotBlank(gatewayId)){  
776 - gateway = baseMapper.selectOne(  
777 - new LambdaQueryWrapper<TkDeviceEntity>()  
778 - .eq(TkDeviceEntity::getTenantId, tenantId)  
779 - .eq(TkDeviceEntity::getTbDeviceId, gatewayId));  
780 - if (gateway ==null ) {  
781 - return String.format(ErrorMessage.DEVICE_GATEWAY_NOT_EXTIED.getMessage(),gatewayId);  
782 - }  
783 - }  
784 - LambdaQueryWrapper<TkDeviceEntity> filter =new LambdaQueryWrapper<>();  
785 - filter.eq(TkDeviceEntity::getTenantId, tenantId);  
786 - DeviceProfileDTO profile=null;  
787 - if(gateway !=null){  
788 - filter.eq(TkDeviceEntity::getGatewayId, gatewayId);  
789 - profile =tkProfileMapper.selectDetail(tenantId,gateway.getProfileId());  
790 - }  
791 -  
792 - if(profile !=null && TransportTypeEnum.TCP.name().equals(profile.getTransportType())){  
793 - filter.eq(TkDeviceEntity::getCode, identifier);  
794 - }else{  
795 - filter.eq(TkDeviceEntity::getName, identifier);  
796 - }  
797 - List<TkDeviceEntity> self = baseMapper.selectList(filter);  
798 - if(self ==null || self.isEmpty()){  
799 - return "";  
800 - }else{  
801 - String names = String.join(",",self.stream().map(f->f.getName()).collect(Collectors.toList()));  
802 - return String.format(ErrorMessage.DEVICE_IDENTIFIER_REPEATED.getMessage(),identifier  
803 - ,names);  
804 - }  
805 - }  
806 } 778 }
@@ -109,16 +109,6 @@ public interface TkDeviceService extends BaseService<TkDeviceEntity> { @@ -109,16 +109,6 @@ public interface TkDeviceService extends BaseService<TkDeviceEntity> {
109 */ 109 */
110 String otherUsing(String deviceId, String tenantId); 110 String otherUsing(String deviceId, String tenantId);
111 /** 111 /**
112 - * 设备标识符是否已经被使用  
113 - *  
114 - * @param tenantId 租户ID  
115 - * @param gatewayId 网关设备主键  
116 - * @param deviceId 网关设备主键  
117 - * @param identifier 设备标识符,例如:设备名称、TCP子设备的地址码  
118 - * @return 场景联动ID  
119 - */  
120 - String markExisted(String tenantId,String gatewayId,String deviceId, String identifier);  
121 - /**  
122 * 主设备信息 112 * 主设备信息
123 * 113 *
124 * @param tenantId 租户ID 114 * @param tenantId 租户ID