Commit 245ab4e62d6b29addc266f6f176f912c262c3e1b
1 parent
064b3381
【代码优化】IoT:修复 device 建表时,tdengine 分成 length 和 type 的情况
Showing
4 changed files
with
32 additions
and
7 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/IotDevicePropertyDataMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.dal.tdengine; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.collection.CollUtil; | |
| 3 | 4 | import cn.hutool.core.util.ObjectUtil; |
| 4 | 5 | import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField; |
| 5 | 6 | import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS; |
| ... | ... | @@ -7,6 +8,7 @@ import com.baomidou.mybatisplus.annotation.InterceptorIgnore; |
| 7 | 8 | import org.apache.ibatis.annotations.Mapper; |
| 8 | 9 | import org.apache.ibatis.annotations.Param; |
| 9 | 10 | |
| 11 | +import java.util.ArrayList; | |
| 10 | 12 | import java.util.List; |
| 11 | 13 | import java.util.stream.Collectors; |
| 12 | 14 | |
| ... | ... | @@ -29,18 +31,38 @@ public interface IotDevicePropertyDataMapper { |
| 29 | 31 | List<TDengineTableField> addFields = newFields.stream().filter( // 新增的字段 |
| 30 | 32 | newField -> oldFields.stream().noneMatch(oldField -> oldField.getField().equals(newField.getField()))) |
| 31 | 33 | .collect(Collectors.toList()); |
| 32 | - List<TDengineTableField> modifyFields = newFields.stream().filter( // 更新的字段 | |
| 33 | - newField -> oldFields.stream().anyMatch(oldField -> oldField.getField().equals(newField.getField()) | |
| 34 | - && (ObjectUtil.notEqual(oldField.getType(), newField.getType()) | |
| 35 | - || (newField.getLength() != null && ObjectUtil.notEqual(oldField.getLength(), newField.getLength()))))) | |
| 36 | - .collect(Collectors.toList()); | |
| 37 | 34 | List<TDengineTableField> dropFields = oldFields.stream().filter( // 删除的字段 |
| 38 | 35 | oldField -> newFields.stream().noneMatch(n -> n.getField().equals(oldField.getField()))) |
| 39 | 36 | .collect(Collectors.toList()); |
| 37 | + List<TDengineTableField> modifyTypeFields = new ArrayList<>(); // 变更类型的字段 | |
| 38 | + List<TDengineTableField> modifyLengthFields = new ArrayList<>(); // 变更长度的字段 | |
| 39 | + newFields.forEach(newField -> { | |
| 40 | + TDengineTableField oldField = CollUtil.findOne(oldFields, field -> field.getField().equals(newField.getField())); | |
| 41 | + if (oldField == null) { | |
| 42 | + return; | |
| 43 | + } | |
| 44 | + if (ObjectUtil.notEqual(oldField.getType(), newField.getType())) { | |
| 45 | + modifyTypeFields.add(newField); | |
| 46 | + return; | |
| 47 | + } | |
| 48 | + if (newField.getLength()!= null) { | |
| 49 | + if (newField.getLength() > oldField.getLength()) { | |
| 50 | + modifyLengthFields.add(newField); | |
| 51 | + } else if (newField.getLength() < oldField.getLength()) { | |
| 52 | + // 特殊:TDengine 长度修改时,只允许变长,所以此时认为是修改类型 | |
| 53 | + modifyTypeFields.add(newField); | |
| 54 | + } | |
| 55 | + } | |
| 56 | + }); | |
| 57 | + | |
| 58 | + // 执行 | |
| 40 | 59 | addFields.forEach(field -> alterProductPropertySTableAddField(productKey, field)); |
| 41 | - // TODO 芋艿:tdengine 只允许 modify 长度;如果 type 变化,只能 drop + add | |
| 42 | - modifyFields.forEach(field -> alterProductPropertySTableModifyField(productKey, field)); | |
| 43 | 60 | dropFields.forEach(field -> alterProductPropertySTableDropField(productKey, field)); |
| 61 | + modifyLengthFields.forEach(field -> alterProductPropertySTableModifyField(productKey, field)); | |
| 62 | + modifyTypeFields.forEach(field -> { | |
| 63 | + alterProductPropertySTableDropField(productKey, field); | |
| 64 | + alterProductPropertySTableAddField(productKey, field); | |
| 65 | + }); | |
| 44 | 66 | } |
| 45 | 67 | |
| 46 | 68 | void alterProductPropertySTableAddField(@Param("productKey") String productKey, | ... | ... |
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 |
| 100 | 100 | } |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | + // TODO 芋艿:建表的时候,表名要小写么? | |
| 103 | 104 | // 2.1 情况一:如果是新增的时候,需要创建表 |
| 104 | 105 | List<TDengineTableField> newFields = buildTableFieldList(thingModels); |
| 105 | 106 | if (CollUtil.isEmpty(oldFields)) { | ... | ... |
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 { |
| 60 | 60 | throw exception(PRODUCT_NOT_EXISTS); |
| 61 | 61 | } |
| 62 | 62 | // 1.2 校验设备标识是否唯一 |
| 63 | + // TODO 芋艿:校验时,需要跨租户唯一,避免 TDEngine 无法处理;并且要忽略大小写 | |
| 63 | 64 | if (deviceMapper.selectByDeviceKey(createReqVO.getDeviceKey()) != null) { |
| 64 | 65 | throw exception(DEVICE_KEY_EXISTS); |
| 65 | 66 | } | ... | ... |
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 { |
| 47 | 47 | @Override |
| 48 | 48 | public Long createProduct(IotProductSaveReqVO createReqVO) { |
| 49 | 49 | // 1. 生成 ProductKey |
| 50 | + // TODO 芋艿:校验时,需要跨租户唯一,避免 TDEngine 无法处理;并且要忽略大小写 | |
| 50 | 51 | if (productMapper.selectByProductKey(createReqVO.getProductKey()) != null) { |
| 51 | 52 | throw exception(PRODUCT_KEY_EXISTS); |
| 52 | 53 | } | ... | ... |