[fix]:code review

This commit is contained in:
alwayssuper 2025-01-20 16:31:37 +08:00
parent 495ae4526b
commit 9f3730d5d9
6 changed files with 77 additions and 55 deletions

View File

@ -14,16 +14,18 @@ public class IotDeviceDataSimulatorSaveReqVO {
private String id;
// TODO @super不用传递 productKey因为 deviceKey 可以推导出来
@Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "product123")
@NotEmpty(message = "产品ID不能为空")
// TODO 讨论: 日志记录的时候要记录一下productKey目前是前端已经有productKey了所以前端传入如果不传入的话后端要根据deviceKey查询productKey感觉直传是不是效率高一些
@Schema(description = "产品标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "product123")
@NotEmpty(message = "产品标识不能为空")
private String productKey;
// TODO @super中文写作规范中英文之间要有空格例如说设备 IDps这里应该是设备标识
@Schema(description = "设备ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "device123")
@NotEmpty(message = "设备ID不能为空")
@Schema(description = "设备标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "device123")
@NotEmpty(message = "设备标识不能为空")
private String deviceKey;
// TODO @supertypesubType是不是不用传递因为模拟只有属性
// TODO 讨论: 不只模拟属性
@Schema(description = "消息/日志类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "property")
@NotEmpty(message = "消息类型不能为空")
private String type;

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.iot.dal.dataobject.device;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@ -27,12 +28,16 @@ public class IotDeviceLogDO {
// TODO @super关联要 @下
/**
* 产品标识
* <p>
* 关联 {@link IotProductDO#getProductKey()}
*/
private String productKey;
// TODO @super关联要 @下
/**
* 设备标识
* <p>
* 关联 {@link IotDeviceDO#getDeviceKey()}}
*/
private String deviceKey;

View File

@ -27,6 +27,19 @@ public interface IotDeviceLogDataMapper {
// TODO @super单个参数不用加 @Param
//讨论艿菇这里有些特殊情况我也学习了一下这块知识
// 如果使用的是Java 8及以上版本并且编译器保留了参数名通过编译器选项-parameters启用则可以去掉@Param注解MyBatis会自动使用参数的实际名称
// 但在TDengine中 @Param去掉后TDengine会报错以下是大模型的回答
// 不用加 @Param在普通的 MySQL 场景下是正确的 - 对于 MyBatis当方法只有一个参数时确实可以不用添加 @Param 注解
//但是在 TDengine 的场景下情况不同
//TDengine 的特殊性
//TDengine 使用特殊的 SQL 语法
//需要处理超级表(STable)和子表的概念
//参数绑定的方式与普通 MySQL 不同
//为什么这里必须要 @Param
//XML 中使用了 ${log.deviceKey} 这样的参数引用方式
//需要在 SQL 中动态构建表名device_log_${log.deviceKey}
//没有 @Param("log") 的话MyBatis 无法正确解析参数
/**
* 插入设备日志数据
*
@ -34,7 +47,7 @@ public interface IotDeviceLogDataMapper {
*
* @param log 设备日志数据
*/
void insert(IotDeviceLogDO log);
void insert(@Param("log") IotDeviceLogDO log);
/**
* 获得设备日志分页
@ -42,7 +55,7 @@ public interface IotDeviceLogDataMapper {
* @param reqVO 分页查询条件
* @return 设备日志列表
*/
List<IotDeviceLogDO> selectPage(IotDeviceLogPageReqVO reqVO);
List<IotDeviceLogDO> selectPage(@Param("reqVO") IotDeviceLogPageReqVO reqVO);
/**
* 获得设备日志总数
@ -50,7 +63,7 @@ public interface IotDeviceLogDataMapper {
* @param reqVO 查询条件
* @return 日志总数
*/
Long selectCount(IotDeviceLogPageReqVO reqVO);
Long selectCount(@Param("reqVO") IotDeviceLogPageReqVO reqVO);
/**
* 查询设备日志表是否存在

View File

@ -55,16 +55,18 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{
// 2. 处理时间字段
// TODO @super一次性的字段不用单独给个变量
long currentTime = System.currentTimeMillis();
// long currentTime = System.currentTimeMillis();
// 2.1 设置时序时间为当前时间
iotDeviceLogDO.setTs(currentTime); // TODO @superTS在SQL中直接NOW 咱们的TS数据获取是走哪一种 now()
// iotDeviceLogDO.setTs(currentTime); // TODO @superTS在SQL中直接NOW 咱们的TS数据获取是走哪一种 now()
// 3. 插入数据
// TODO @super不要直接调用对方的 IotDeviceLogDataMapper通过 service
// 讨论艿菇 这就是iotDeviceLogDataService的Impl
iotDeviceLogDataMapper.insert(iotDeviceLogDO);
}
// TODO @super iotDeviceLogDataService
// 讨论艿菇 这就是iotDeviceLogDataService的Impl
@Override
public PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO) {
// 查询数据

View File

@ -1,43 +1,43 @@
package cn.iocoder.yudao.module.iot.service.plugin;
import cn.iocoder.yudao.module.iot.mqttrpc.server.RpcServer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@Service
@RequiredArgsConstructor
public class ExampleService {
private final RpcServer rpcServer;
@PostConstruct
public void registerMethods() {
rpcServer.registerMethod("add", params -> {
if (params.length != 2) {
throw new IllegalArgumentException("add方法需要两个参数");
}
int a = ((Number) params[0]).intValue();
int b = ((Number) params[1]).intValue();
return add(a, b);
});
rpcServer.registerMethod("concat", params -> {
if (params.length != 2) {
throw new IllegalArgumentException("concat方法需要两个参数");
}
String str1 = params[0].toString();
String str2 = params[1].toString();
return concat(str1, str2);
});
}
private int add(int a, int b) {
return a + b;
}
private String concat(String a, String b) {
return a + b;
}
}
//package cn.iocoder.yudao.module.iot.service.plugin;
//
//import cn.iocoder.yudao.module.iot.mqttrpc.server.RpcServer;
//import lombok.RequiredArgsConstructor;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.PostConstruct;
//
//@Service
//@RequiredArgsConstructor
//public class ExampleService {
//
// private final RpcServer rpcServer;
//
// @PostConstruct
// public void registerMethods() {
// rpcServer.registerMethod("add", params -> {
// if (params.length != 2) {
// throw new IllegalArgumentException("add方法需要两个参数");
// }
// int a = ((Number) params[0]).intValue();
// int b = ((Number) params[1]).intValue();
// return add(a, b);
// });
//
// rpcServer.registerMethod("concat", params -> {
// if (params.length != 2) {
// throw new IllegalArgumentException("concat方法需要两个参数");
// }
// String str1 = params[0].toString();
// String str2 = params[1].toString();
// return concat(str1, str2);
// });
// }
//
// private int add(int a, int b) {
// return a + b;
// }
//
// private String concat(String a, String b) {
// return a + b;
// }
//}

View File

@ -27,11 +27,11 @@
<!-- 插入设备日志数据 在子表不存在的情况下 可在数据插入时 自动建表 -->
<insert id="insert">
INSERT INTO device_log_${log.deviceKey} (ts, id, product_key, type, subType, content, report_time)
INSERT INTO device_log_${log.deviceKey} (id, product_key, type, subType, content, report_time)
USING device_log
TAGS ('${log.deviceKey}')
VALUES (
#{log.ts},
NOW,
#{log.id},
#{log.productKey},
#{log.type},
@ -77,7 +77,7 @@
<!-- 检查设备日志超级表是否存在 -->
<select id="checkDeviceLogTableExists" resultType="Object">
SHOW TABLES LIKE 'device_log';
SHOW STABLES LIKE 'device_log'
</select>
</mapper>