Jelajahi Sumber

[fix]:code review

alwayssuper 6 bulan lalu
induk
melakukan
9f3730d5d9

+ 6 - 4
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/vo/deviceData/IotDeviceDataSimulatorSaveReqVO.java

@@ -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:中文写作规范,中英文之间,要有空格。例如说,设备 ID。ps:这里应该是设备标识
-    @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 @super:type、subType,是不是不用传递,因为模拟只有属性???
+    // TODO 讨论: 不只模拟属性
     @Schema(description = "消息/日志类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "property")
     @NotEmpty(message = "消息类型不能为空")
     private String type;

+ 5 - 0
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/device/IotDeviceLogDO.java

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

+ 16 - 3
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/IotDeviceLogDataMapper.java

@@ -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);
 
     /**
      * 查询设备日志表是否存在

+ 4 - 2
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceLogDataServiceImpl.java

@@ -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 @super:TS在SQL中直接NOW   咱们的TS数据获取是走哪一种;走 now()
+//        iotDeviceLogDO.setTs(currentTime); // TODO @super:TS在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) {
         // 查询数据

+ 43 - 43
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/plugin/ExampleService.java

@@ -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;
+//    }
+//}

+ 3 - 3
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDeviceLogDataMapper.xml

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