Commit 767a26dd705df1bedc9aaf9001d14902d8e3791c
1 parent
b2434b7b
【代码优化】IOT: ThingModel 评审优化
Showing
4 changed files
with
13 additions
and
27 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/tdengine/FieldParser.java
| @@ -5,7 +5,8 @@ import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelR | @@ -5,7 +5,8 @@ import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelR | ||
| 5 | 5 | ||
| 6 | import java.util.HashMap; | 6 | import java.util.HashMap; |
| 7 | import java.util.List; | 7 | import java.util.List; |
| 8 | -import java.util.stream.Collectors; | 8 | + |
| 9 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | ||
| 9 | 10 | ||
| 10 | /** | 11 | /** |
| 11 | * FieldParser 类用于解析和转换物模型字段到 TDengine 字段 | 12 | * FieldParser 类用于解析和转换物模型字段到 TDengine 字段 |
| @@ -52,10 +53,7 @@ public class FieldParser { | @@ -52,10 +53,7 @@ public class FieldParser { | ||
| 52 | * @return 字段列表 | 53 | * @return 字段列表 |
| 53 | */ | 54 | */ |
| 54 | public static List<TdFieldDO> parse(ThingModelRespVO thingModel) { | 55 | public static List<TdFieldDO> parse(ThingModelRespVO thingModel) { |
| 55 | - // TODO @puhui999:是不是使用 convertList | ||
| 56 | - return thingModel.getModel().getProperties().stream() | ||
| 57 | - .map(FieldParser::parse) | ||
| 58 | - .collect(Collectors.toList()); | 56 | + return convertList(thingModel.getModel().getProperties(), FieldParser::parse); |
| 59 | } | 57 | } |
| 60 | 58 | ||
| 61 | /** | 59 | /** |
| @@ -65,13 +63,12 @@ public class FieldParser { | @@ -65,13 +63,12 @@ public class FieldParser { | ||
| 65 | * @return 转换后的 TDengine 字段对象列表 | 63 | * @return 转换后的 TDengine 字段对象列表 |
| 66 | */ | 64 | */ |
| 67 | public static List<TdFieldDO> parse(List<List<Object>> rows) { | 65 | public static List<TdFieldDO> parse(List<List<Object>> rows) { |
| 68 | - // TODO @puhui999:是不是使用 convertList | ||
| 69 | - return rows.stream().map(row -> { | 66 | + return convertList(rows, row -> { |
| 70 | String type = row.get(1).toString().toUpperCase(); | 67 | String type = row.get(1).toString().toUpperCase(); |
| 71 | // TODO @puhui999:"NCHAR" 最好枚举下 | 68 | // TODO @puhui999:"NCHAR" 最好枚举下 |
| 72 | int dataLength = "NCHAR".equals(type) ? Integer.parseInt(row.get(2).toString()) : -1; | 69 | int dataLength = "NCHAR".equals(type) ? Integer.parseInt(row.get(2).toString()) : -1; |
| 73 | return new TdFieldDO(row.get(0).toString(), type, dataLength); | 70 | return new TdFieldDO(row.get(0).toString(), type, dataLength); |
| 74 | - }).collect(Collectors.toList()); | 71 | + }); |
| 75 | } | 72 | } |
| 76 | 73 | ||
| 77 | /** | 74 | /** |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductServiceImpl.java
| @@ -122,7 +122,7 @@ public class IotProductServiceImpl implements IotProductService { | @@ -122,7 +122,7 @@ public class IotProductServiceImpl implements IotProductService { | ||
| 122 | thingModelFunctionService.createSuperTableDataModel(id); | 122 | thingModelFunctionService.createSuperTableDataModel(id); |
| 123 | // 3.2 创建物模型日志超级表数据模型 | 123 | // 3.2 创建物模型日志超级表数据模型 |
| 124 | thingModelMessageService.createSuperTable(id); | 124 | thingModelMessageService.createSuperTable(id); |
| 125 | - }x | 125 | + } |
| 126 | productMapper.updateById(updateObj); | 126 | productMapper.updateById(updateObj); |
| 127 | } | 127 | } |
| 128 | 128 |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/tdengine/IotThingModelMessageServiceImpl.java
| @@ -7,13 +7,9 @@ import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; | @@ -7,13 +7,9 @@ import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; | ||
| 7 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.device.IotDeviceStatusUpdateReqVO; | 7 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.device.IotDeviceStatusUpdateReqVO; |
| 8 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; | 8 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; |
| 9 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO; | 9 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO; |
| 10 | -import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.FieldParser; | ||
| 11 | -import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.TdFieldDO; | ||
| 12 | -import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.TdTableDO; | ||
| 13 | -import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage; | ||
| 14 | -import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotProductThingModelDO; | ||
| 15 | import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO; | 10 | import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO; |
| 16 | import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.*; | 11 | import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.*; |
| 12 | +import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotProductThingModelDO; | ||
| 17 | import cn.iocoder.yudao.module.iot.dal.redis.deviceData.DeviceDataRedisDAO; | 13 | import cn.iocoder.yudao.module.iot.dal.redis.deviceData.DeviceDataRedisDAO; |
| 18 | import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDDLMapper; | 14 | import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDDLMapper; |
| 19 | import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper; | 15 | import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper; |
| @@ -22,8 +18,8 @@ import cn.iocoder.yudao.module.iot.enums.IotConstants; | @@ -22,8 +18,8 @@ import cn.iocoder.yudao.module.iot.enums.IotConstants; | ||
| 22 | import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStatusEnum; | 18 | import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStatusEnum; |
| 23 | import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum; | 19 | import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum; |
| 24 | import cn.iocoder.yudao.module.iot.service.device.IotDeviceService; | 20 | import cn.iocoder.yudao.module.iot.service.device.IotDeviceService; |
| 25 | -import cn.iocoder.yudao.module.iot.service.thingmodel.IotProductThingModelService; | ||
| 26 | import cn.iocoder.yudao.module.iot.service.product.IotProductService; | 21 | import cn.iocoder.yudao.module.iot.service.product.IotProductService; |
| 22 | +import cn.iocoder.yudao.module.iot.service.thingmodel.IotProductThingModelService; | ||
| 27 | import cn.iocoder.yudao.module.iot.util.IotTdDatabaseUtils; | 23 | import cn.iocoder.yudao.module.iot.util.IotTdDatabaseUtils; |
| 28 | import jakarta.annotation.Resource; | 24 | import jakarta.annotation.Resource; |
| 29 | import lombok.extern.slf4j.Slf4j; | 25 | import lombok.extern.slf4j.Slf4j; |
| @@ -132,12 +128,8 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ | @@ -132,12 +128,8 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ | ||
| 132 | } | 128 | } |
| 133 | 129 | ||
| 134 | private List<IotProductThingModelDO> getValidFunctionList(String productKey) { | 130 | private List<IotProductThingModelDO> getValidFunctionList(String productKey) { |
| 135 | - // TODO @puhui999:使用 convertList 会好点哈 | ||
| 136 | - return iotProductThingModelService | ||
| 137 | - .getProductThingModelListByProductKey(productKey) | ||
| 138 | - .stream() | ||
| 139 | - .filter(function -> IotProductThingModelTypeEnum.PROPERTY.getType().equals(function.getType())) | ||
| 140 | - .toList(); | 131 | + return filterList(iotProductThingModelService.getProductThingModelListByProductKey(productKey), |
| 132 | + thingModel -> IotProductThingModelTypeEnum.PROPERTY.getType().equals(thingModel.getType())); | ||
| 141 | } | 133 | } |
| 142 | 134 | ||
| 143 | private List<TdFieldDO> filterAndCollectValidFields(Map<String, Object> params, List<IotProductThingModelDO> thingModelList, IotDeviceDO device, Long time) { | 135 | private List<TdFieldDO> filterAndCollectValidFields(Map<String, Object> params, List<IotProductThingModelDO> thingModelList, IotDeviceDO device, Long time) { |
| @@ -235,9 +227,8 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ | @@ -235,9 +227,8 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ | ||
| 235 | * @param productKey 产品 Key | 227 | * @param productKey 产品 Key |
| 236 | * @param deviceName 设备名称 | 228 | * @param deviceName 设备名称 |
| 237 | * @param deviceKey 设备 Key | 229 | * @param deviceKey 设备 Key |
| 238 | - * | ||
| 239 | */ | 230 | */ |
| 240 | - private void createThinkModelMessageDeviceTable(String productKey, String deviceName, String deviceKey){ | 231 | + private void createThinkModelMessageDeviceTable(String productKey, String deviceName, String deviceKey) { |
| 241 | 232 | ||
| 242 | // 1. 获取超级表的名称、数据库名称、设备日志表名称 | 233 | // 1. 获取超级表的名称、数据库名称、设备日志表名称 |
| 243 | String databaseName = iotTdDatabaseUtils.getDatabaseName(); | 234 | String databaseName = iotTdDatabaseUtils.getDatabaseName(); |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thingmodel/IotProductThingModelServiceImpl.java
| @@ -29,10 +29,10 @@ import org.springframework.transaction.annotation.Transactional; | @@ -29,10 +29,10 @@ import org.springframework.transaction.annotation.Transactional; | ||
| 29 | import org.springframework.validation.annotation.Validated; | 29 | import org.springframework.validation.annotation.Validated; |
| 30 | 30 | ||
| 31 | import java.util.*; | 31 | import java.util.*; |
| 32 | -import java.util.stream.Collectors; | ||
| 33 | 32 | ||
| 34 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 33 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 35 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList; | 34 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList; |
| 35 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; | ||
| 36 | import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*; | 36 | import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*; |
| 37 | 37 | ||
| 38 | /** | 38 | /** |
| @@ -261,9 +261,7 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ | @@ -261,9 +261,7 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ | ||
| 261 | } | 261 | } |
| 262 | }); | 262 | }); |
| 263 | // 过滤掉没有设置 ID 的对象 | 263 | // 过滤掉没有设置 ID 的对象 |
| 264 | - List<IotProductThingModelDO> validUpdateList = updateList.stream() | ||
| 265 | - .filter(func -> func.getId() != null) | ||
| 266 | - .collect(Collectors.toList()); | 264 | + List<IotProductThingModelDO> validUpdateList = filterList(updateList, thingModel -> thingModel.getId() != null); |
| 267 | // 执行批量更新 | 265 | // 执行批量更新 |
| 268 | if (CollUtil.isNotEmpty(validUpdateList)) { | 266 | if (CollUtil.isNotEmpty(validUpdateList)) { |
| 269 | productThingModelMapper.updateBatch(validUpdateList); | 267 | productThingModelMapper.updateBatch(validUpdateList); |