package com.ycwl.basic.mapper.task;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ycwl.basic.model.task.entity.TaskRenderJobMappingEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

/**
 * Task与RenderJob关联Mapper
 */
@Mapper
public interface TaskRenderJobMappingMapper extends BaseMapper<TaskRenderJobMappingEntity> {

    /**
     * 根据taskId查询mapping
     */
    TaskRenderJobMappingEntity selectByTaskId(@Param("taskId") Long taskId);

    /**
     * 根据renderJobId查询mapping
     */
    TaskRenderJobMappingEntity selectByRenderJobId(@Param("renderJobId") Long renderJobId);

    /**
     * 查询需要轮询的记录
     * 条件：状态为PENDING或PREVIEW_READY，且最后检查时间超过指定间隔
     */
    List<TaskRenderJobMappingEntity> selectPendingForPolling(
            @Param("statuses") List<String> statuses,
            @Param("checkIntervalSeconds") int checkIntervalSeconds,
            @Param("limit") int limit
    );

    /**
     * 更新渲染状态和片段信息
     */
    int updateRenderStatus(
            @Param("id") Long id,
            @Param("renderStatus") String renderStatus,
            @Param("publishedCount") Integer publishedCount,
            @Param("segmentCount") Integer segmentCount,
            @Param("previewUrl") String previewUrl,
            @Param("mp4Url") String mp4Url,
            @Param("lastCheckTime") Date lastCheckTime
    );

    /**
     * 更新为失败状态
     */
    int updateToFailed(
            @Param("id") Long id,
            @Param("errorCode") String errorCode,
            @Param("errorMessage") String errorMessage,
            @Param("lastCheckTime") Date lastCheckTime
    );

    /**
     * 增加重试次数
     */
    int incrementRetryCount(@Param("id") Long id);
}
