package com.common.mapper;


import com.common.entity.DeviceCollectTaskTime;
import com.common.entity.DeviceReceiveLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface DeviceReceiveLogMapper {

    /**
     * 插入单条记录
     */
    int insert(DeviceReceiveLog log);

    /**
     * 批量插入记录
     */
    int batchInsert(@Param("list") List<DeviceReceiveLog> logs);

    /**
     * 根据ID查询
     */
    DeviceReceiveLog selectById(@Param("id") Long id);

    /**
     * 根据设备ID查询
     */
    List<DeviceReceiveLog> selectByDeviceId(@Param("deviceId") Integer deviceId);

    /**
     * 根据采集探针ID查询
     */
    List<DeviceReceiveLog> selectByCollectId(@Param("collectId") Integer collectId);

    /**
     * 根据IP地址查询
     */
    List<DeviceReceiveLog> selectByDeviceIp(@Param("deviceIp") String deviceIp);

    /**
     * 根据时间范围查询
     */
    List<DeviceReceiveLog> selectByTimeRange(
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 多条件组合查询
     */
    List<DeviceReceiveLog> selectByCondition(DeviceReceiveLog condition);

    /**
     * 统计设备接收日志数量
     */
    Long countByCondition(DeviceReceiveLog condition);

    /**
     * 根据时间范围删除旧数据
     */
    int deleteByTimeRange(
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 获取最近N条记录
     */
    List<DeviceReceiveLog> selectRecent(@Param("limit") Integer limit);

    /**
     * 按设备分组统计日志数量
     */
    List<Map<String, Object>> countByDeviceGroup();


    /**
     * 查询当天每个设备的最新成功时间
     */
    @Select("SELECT device_collect_id, MAX(created_at) AS last_success_time " +
            "FROM device_receive_log " +
            "WHERE push_success = 1 " +
            "AND created_at >= CURRENT_DATE " +
            "GROUP BY device_collect_id")
    List<DeviceCollectTaskTime> selectDailySuccessTimes();

    /**
     * 查询当天每个设备的最新失败时间
     */
    @Select("SELECT device_collect_id, MAX(created_at) AS last_fail_time " +
            "FROM device_receive_log " +
            "WHERE push_success = 0 " +
            "AND created_at >= CURRENT_DATE " +
            "GROUP BY device_collect_id")
    List<DeviceCollectTaskTime> selectDailyFailTimes();

    /**
     * 按 ID 列表精确查询首次成功时间（仅查 first_time 为空的任务，避免全表扫描）
     */
    @Select("<script>" +
            "SELECT device_collect_id, MIN(created_at) AS first_success_time " +
            "FROM device_receive_log " +
            "WHERE push_success = 1 " +
            "AND device_collect_id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            " GROUP BY device_collect_id" +
            "</script>")
    List<DeviceCollectTaskTime> selectFirstSuccessTimesByIds(@Param("ids") List<Integer> ids);

    /**
     * 查询指定时间范围内的成功记录
     */
    @Select("SELECT device_collect_id, MIN(created_at) AS first_time, " +
            "MAX(created_at) AS last_success_time " +
            "FROM device_receive_log " +
            "WHERE push_success = 1 " +
            "AND created_at >= #{startTime} " +
            "AND created_at < #{endTime} " +
            "GROUP BY device_collect_id")
    List<DeviceCollectTaskTime> selectSuccessTimesByRange(
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);
}
