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

This commit is contained in:
YunaiV 2024-12-26 09:55:19 +08:00
parent 064b3381df
commit 245ab4e62d
4 changed files with 32 additions and 7 deletions

View File

@ -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,

View File

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

View File

@ -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);
}

View File

@ -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);
}