package com.fs.tag.mapper;

import com.fs.tag.domain.FsTagUpdateQueue;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 标签更新对列表 Mapper
 */
@Mapper
public interface FsTagUpdateQueueMapper {

    @Select("select * from fs_tag_update_queue where retry_count < 3 and status in (0,3) and (next_execute_time < now() or next_execute_time is null) limit 1000")
    List<FsTagUpdateQueue> selectPending();


    @Insert("<script>" +
            "INSERT IGNORE INTO fs_tag_update_queue (" +
            "course_log_id, is_first, course_id,  operation_type, video_id, status, retry_count, " +
            "corp_id, qw_user_id, qw_external_contact_id, fail_msg, payload, response, create_time, update_time, update_by, create_by, log_type,watching_tg_id,watched_tg_id,watching_tag_id,watched_tag_id,watching_tg_group_id,watched_tg_group_id" +
            ") VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(" +
            "#{item.courseLogId}, #{item.isFirst}, #{item.courseId},  #{item.operationType}, #{item.videoId}, #{item.status}, #{item.retryCount}, " +
            "#{item.corpId}, #{item.qwUserId}, #{item.qwExternalContactId}, #{item.failMsg}, #{item.payload}, #{item.response}, #{item.createTime}," +
            " #{item.updateTime}, #{item.updateBy}, #{item.createBy}, #{item.logType},#{item.watchingTgId},#{item.watchedTgId},#{item.watchingTagId},#{item.watchedTagId},#{item.watchingTgGroupId},#{item.watchedTgGroupId}" +
            ")" +
            "</foreach>" +
            "</script>")
    int batchInsert(@Param("list") List<FsTagUpdateQueue> list);


    @Update("<script>" +
            "<foreach collection='list' item='item' separator=';'>" +
            "UPDATE fs_tag_update_queue" +
            "<set>" +
            "<if test='item.courseLogId != null'>course_log_id = #{item.courseLogId},</if>" +
            "<if test='item.isFirst != null'>is_first = #{item.isFirst},</if>" +
            "<if test='item.courseId != null'>course_id = #{item.courseId},</if>" +
            "<if test='item.operationType != null'>operation_type = #{item.operationType},</if>" +
            "<if test='item.videoId != null'>video_id = #{item.videoId},</if>" +
            "<if test='item.status != null'>status = #{item.status},</if>" +
            "<if test='item.retryCount != null'>retry_count = #{item.retryCount},</if>" +
            "<if test='item.corpId != null'>corp_id = #{item.corpId},</if>" +
            "<if test='item.qwUserId != null'>qw_user_id = #{item.qwUserId},</if>" +
            "<if test='item.qwExternalContactId != null'>qw_external_contact_id = #{item.qwExternalContactId},</if>" +
            "<if test='item.failMsg != null'>fail_msg = #{item.failMsg},</if>" +
            "<if test='item.payload != null'>payload = #{item.payload},</if>" +
            "<if test='item.response != null'>response = #{item.response},</if>" +
            "<if test='item.createTime != null'>create_time = #{item.createTime},</if>" +
            "<if test='item.updateTime != null'>update_time = #{item.updateTime},</if>" +
            "<if test='item.updateBy != null'>update_by = #{item.updateBy},</if>" +
            "<if test='item.createBy != null'>create_by = #{item.createBy},</if>" +
            "<if test='item.logType != null'>log_type = #{item.logType},</if>" +
            "<if test='item.nextExecuteTime != null'>next_execute_time = #{item.nextExecuteTime},</if>" +
            "</set>" +
            " WHERE id = #{item.id}" +
            "</foreach>" +
            "</script>")
    int batchUpdateSelective(@Param("list") List<FsTagUpdateQueue> list);

}
