소스 검색

【代码优化】IoT:修复 device 建表时,tdengine 分成 length 和 type 的情况

YunaiV 7 달 전
부모
커밋
245ab4e62d

+ 29 - 7
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/IotDevicePropertyDataMapper.java

@@ -1,5 +1,6 @@
 package cn.iocoder.yudao.module.iot.dal.tdengine;
 
+import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField;
 import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS;
@@ -7,6 +8,7 @@ import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -29,18 +31,38 @@ public interface IotDevicePropertyDataMapper {
         List<TDengineTableField> addFields = newFields.stream().filter( // 新增的字段
                         newField -> oldFields.stream().noneMatch(oldField -> oldField.getField().equals(newField.getField())))
                 .collect(Collectors.toList());
-        List<TDengineTableField> modifyFields = newFields.stream().filter( // 更新的字段
-                        newField -> oldFields.stream().anyMatch(oldField -> oldField.getField().equals(newField.getField())
-                                && (ObjectUtil.notEqual(oldField.getType(), newField.getType())
-                                    || (newField.getLength() != null && ObjectUtil.notEqual(oldField.getLength(), newField.getLength())))))
-                .collect(Collectors.toList());
         List<TDengineTableField> dropFields = oldFields.stream().filter( // 删除的字段
                         oldField -> newFields.stream().noneMatch(n -> n.getField().equals(oldField.getField())))
                 .collect(Collectors.toList());
+        List<TDengineTableField> modifyTypeFields = new ArrayList<>(); // 变更类型的字段
+        List<TDengineTableField> modifyLengthFields = new ArrayList<>(); // 变更长度的字段
+        newFields.forEach(newField -> {
+            TDengineTableField oldField = CollUtil.findOne(oldFields, field -> field.getField().equals(newField.getField()));
+            if (oldField == null) {
+                return;
+            }
+            if (ObjectUtil.notEqual(oldField.getType(), newField.getType())) {
+                modifyTypeFields.add(newField);
+                return;
+            }
+            if (newField.getLength()!= null) {
+                if (newField.getLength() > oldField.getLength()) {
+                    modifyLengthFields.add(newField);
+                } else if (newField.getLength() < oldField.getLength()) {
+                    // 特殊:TDengine 长度修改时,只允许变长,所以此时认为是修改类型
+                    modifyTypeFields.add(newField);
+                }
+            }
+        });
+
+        // 执行
         addFields.forEach(field -> alterProductPropertySTableAddField(productKey, field));
-        // TODO 芋艿:tdengine 只允许 modify 长度;如果 type 变化,只能 drop + add
-        modifyFields.forEach(field -> alterProductPropertySTableModifyField(productKey, field));
         dropFields.forEach(field -> alterProductPropertySTableDropField(productKey, field));
+        modifyLengthFields.forEach(field -> alterProductPropertySTableModifyField(productKey, field));
+        modifyTypeFields.forEach(field -> {
+            alterProductPropertySTableDropField(productKey, field);
+            alterProductPropertySTableAddField(productKey, field);
+        });
     }
 
     void alterProductPropertySTableAddField(@Param("productKey") String productKey,

+ 1 - 0
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDevicePropertyDataServiceImpl.java

@@ -100,6 +100,7 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
             }
         }
 
+        // TODO 芋艿:建表的时候,表名要小写么?
         // 2.1 情况一:如果是新增的时候,需要创建表
         List<TDengineTableField> newFields = buildTableFieldList(thingModels);
         if (CollUtil.isEmpty(oldFields)) {

+ 1 - 0
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java

@@ -60,6 +60,7 @@ public class IotDeviceServiceImpl implements IotDeviceService {
             throw exception(PRODUCT_NOT_EXISTS);
         }
         // 1.2 校验设备标识是否唯一
+        // TODO 芋艿:校验时,需要跨租户唯一,避免 TDEngine 无法处理;并且要忽略大小写
         if (deviceMapper.selectByDeviceKey(createReqVO.getDeviceKey()) != null) {
             throw exception(DEVICE_KEY_EXISTS);
         }

+ 1 - 0
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductServiceImpl.java

@@ -47,6 +47,7 @@ public class IotProductServiceImpl implements IotProductService {
     @Override
     public Long createProduct(IotProductSaveReqVO createReqVO) {
         // 1. 生成 ProductKey
+        // TODO 芋艿:校验时,需要跨租户唯一,避免 TDEngine 无法处理;并且要忽略大小写
         if (productMapper.selectByProductKey(createReqVO.getProductKey()) != null) {
             throw exception(PRODUCT_KEY_EXISTS);
         }