package com.common.mapper; import com.common.entity.DeviceCollectHeartbeat; import org.apache.ibatis.annotations.*; import java.util.List; /** * 探针心跳状态 Mapper 接口 */ @Mapper public interface DeviceCollectHeartbeatMapper { /** * 根据探针ID查询 */ @Select("SELECT * FROM device_collect_heartbeat WHERE collect_id = #{collectId}") DeviceCollectHeartbeat selectByCollectId(@Param("collectId") String collectId); /** * 查询所有探针 */ @Select("SELECT * FROM device_collect_heartbeat ORDER BY update_time DESC") List selectAll(); /** * 查询所有在线探针 */ @Select("SELECT * FROM device_collect_heartbeat WHERE status = 'online' ORDER BY update_time DESC") List selectAllOnline(); /** * 查询所有离线探针 */ @Select("SELECT * FROM device_collect_heartbeat WHERE status = 'offline' ORDER BY update_time DESC") List selectAllOffline(); /** * 插入或更新(根据collect_id) * 达梦数据库使用 MERGE INTO 实现 upsert */ @Update("MERGE INTO device_collect_heartbeat t " + "USING (SELECT " + "#{collectId} AS collect_id, " + "#{collectName} AS collect_name, " + "#{deviceIp} AS device_ip, " + "#{appVersion} AS app_version, " + "#{lastHeartbeat} AS last_heartbeat, " + "#{heartbeatCount} AS heartbeat_count, " + "#{status} AS status, " + "#{failCount} AS fail_count, " + "#{updateTime} AS update_time " + "FROM DUAL) s " + "ON (t.collect_id = s.collect_id) " + "WHEN MATCHED THEN UPDATE SET " + "t.collect_name = s.collect_name, " + "t.device_ip = s.device_ip, " + "t.app_version = s.app_version, " + "t.last_heartbeat = s.last_heartbeat, " + "t.heartbeat_count = s.heartbeat_count, " + "t.status = s.status, " + "t.fail_count = s.fail_count, " + "t.update_time = s.update_time " + "WHEN NOT MATCHED THEN INSERT (" + "collect_id, collect_name, device_ip, app_version, last_heartbeat, " + "heartbeat_count, status, fail_count, update_time" + ") VALUES (" + "s.collect_id, s.collect_name, s.device_ip, s.app_version, s.last_heartbeat, " + "s.heartbeat_count, s.status, s.fail_count, s.update_time" + ")") int upsert(DeviceCollectHeartbeat heartbeat); /** * 更新探针状态 */ @Update("UPDATE device_collect_heartbeat SET " + "status = #{status}, fail_count = #{failCount}, update_time = #{updateTime} " + "WHERE collect_id = #{collectId}") int updateStatus(@Param("collectId") String collectId, @Param("status") String status, @Param("failCount") Integer failCount, @Param("updateTime") java.time.LocalDateTime updateTime); /** * 查询超过指定时间未心跳的探针(用于检测离线) */ @Select("SELECT * FROM device_collect_heartbeat " + "WHERE status = 'online' AND last_heartbeat < #{thresholdTime} " + "ORDER BY last_heartbeat ASC") List selectOfflineCandidates(@Param("thresholdTime") java.time.LocalDateTime thresholdTime); /** * 删除指定日期之前的记录 */ @Delete("DELETE FROM device_collect_heartbeat WHERE update_time < #{beforeTime}") int deleteBeforeTime(@Param("beforeTime") java.time.LocalDateTime beforeTime); }