package com.erp.platformsync.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.erp.platformsync.entity.OrderPullLog;
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 OrderPullLogMapper extends BaseMapper<OrderPullLog> {

    /**
     * 查询配置的最新日志
     */
    @Select("SELECT * FROM order_pull_log WHERE config_id = #{configId} ORDER BY created_at DESC LIMIT #{limit}")
    List<OrderPullLog> findLatestByConfigId(@Param("configId") Long configId, @Param("limit") int limit);

    /**
     * 查询配置在某时间范围内的日志
     */
    @Select("SELECT * FROM order_pull_log WHERE config_id = #{configId} AND created_at >= #{startTime} AND created_at <= #{endTime} ORDER BY created_at DESC")
    List<OrderPullLog> findByConfigIdAndTimeRange(@Param("configId") Long configId, 
                                                   @Param("startTime") LocalDateTime startTime, 
                                                   @Param("endTime") LocalDateTime endTime);

    /**
     * 查询失败日志
     */
    @Select("SELECT * FROM order_pull_log WHERE status = 'FAILED' ORDER BY created_at DESC LIMIT #{limit}")
    List<OrderPullLog> findFailedLogs(@Param("limit") int limit);

    /**
     * 统计某配置的拉单次数
     */
    @Select("SELECT COUNT(*) FROM order_pull_log WHERE config_id = #{configId}")
    int countByConfigId(@Param("configId") Long configId);
}
