Browse Source

fix(mapper): 修复时间戳类型不兼容导致的查询异常

- 将 CAST(ts AS timestamp) 改为 CAST(ts AS timestamp without time zone)
- 将 CAST(NULL AS timestamp) 改为 CAST(NULL AS timestamp without time zone)
- 补充注释说明 Kingbase 兼容模式下 timestamp 类型混用问题
- 确保 COALESCE/MAX 中全链路类型一致避免转换报错
林仔 2 weeks ago
parent
commit
953082714d

+ 9 - 3
zk-api-service/src/main/java/com/zksy/api/mapper/CollectionLastReceiptMapper.java

@@ -9,7 +9,13 @@ import org.apache.ibatis.annotations.Options;
 import java.util.List;
 
 public interface CollectionLastReceiptMapper {
-    /** 仅恢复本类采集表里有记录且台账仍在线的设备;表名和列名均为固定白名单。 */
+    /**
+     * 仅恢复本类采集表里有记录且台账仍在线的设备;表名和列名均为固定白名单。
+     * 注意:Kingbase 中 CAST(x AS timestamp) 取决于数据库兼容模式,可能得到
+     * timestamp with time zone,与 create_time(timestamp without time zone)在
+     * COALESCE/MAX 中混用会报“COALESCE 无法转换类型 timestamp 为 timestamp without time zone”。
+     * 因此时间一律显式 CAST 为 timestamp without time zone,保证全链路类型一致。
+     */
     @Select({"<script>",
             "SELECT r.device_code, MAX(r.received_at) AS last_receive_time FROM (",
             "<choose>",
@@ -20,8 +26,8 @@ public interface CollectionLastReceiptMapper {
             "<when test=\"collector == 'radar'\">SELECT device_code, create_time AS received_at FROM app_user.radar_data</when>",
             "<when test=\"collector == 'telemetry'\">SELECT device_code, create_time AS received_at FROM app_user.telemetry_data</when>",
             "<when test=\"collector == 'water'\">SELECT device_code, create_time AS received_at FROM app_user.water_monitor_data</when>",
-            "<when test=\"collector == 'audio'\">SELECT encode AS device_code, COALESCE(create_time, CASE WHEN ts ~ '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN CAST(ts AS timestamp) END) AS received_at FROM app_user.noise_info</when>",
-            "<otherwise>SELECT CAST(NULL AS varchar) AS device_code, CAST(NULL AS timestamp) AS received_at WHERE 1=0</otherwise>",
+            "<when test=\"collector == 'audio'\">SELECT encode AS device_code, COALESCE(create_time, CASE WHEN ts ~ '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN CAST(ts AS timestamp without time zone) END) AS received_at FROM app_user.noise_info</when>",
+            "<otherwise>SELECT CAST(NULL AS varchar) AS device_code, CAST(NULL AS timestamp without time zone) AS received_at WHERE 1=0</otherwise>",
             "</choose>",
             ") r WHERE r.received_at IS NOT NULL AND EXISTS (",
             "SELECT 1 FROM app_user.equipment_base b JOIN app_user.equipment_status s ON s.equipment_id=b.equipment_id",