package com.ruoyi.energymeter.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.ruoyi.energymeter.pojo.EnergyMeterSyncRun; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.time.LocalDateTime; import java.util.List; /** * 同步批次 Mapper。 */ @Mapper public interface EnergyMeterSyncRunMapper extends BaseMapper { @Select("SELECT * FROM energy_meter_sync_run WHERE run_key = #{runKey} LIMIT 1") EnergyMeterSyncRun selectByRunKey(@Param("runKey") String runKey); /** * 汇总批次任务状态并回写统计字段。 */ int refreshTotals(@Param("runId") Long runId); /** * 查询长时间停在 RUNNING、但已没有待执行任务的批次,用于崩溃后兜底汇总。 */ @Select("SELECT r.* FROM energy_meter_sync_run r WHERE r.status = 'RUNNING' AND r.create_time < #{deadline} " + "AND NOT EXISTS (SELECT 1 FROM energy_meter_sync_task t WHERE t.run_id = r.id " + "AND t.status IN ('PENDING', 'RUNNING', 'RETRY')) ORDER BY r.id LIMIT #{limit}") List selectStuckRuns(@Param("deadline") LocalDateTime deadline, @Param("limit") int limit); }