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 500")
    List<FsTagUpdateQueue> selectPending();

    @Select("<script>" +
            "SELECT * FROM fs_tag_update_queue " +
            "<where>" +
            "<if test='id != null'> AND id = #{id} </if>" +
            "<if test='courseLogId != null'> AND course_log_id = #{courseLogId} </if>" +
            "<if test='courseId != null'> AND course_id = #{courseId} </if>" +
            "<if test='tagId != null'> AND tag_id = #{tagId} </if>" +
            "<if test='tagName != null'> AND tag_name = #{tagName} </if>" +
            "<if test='operationType != null'> AND operation_type = #{operationType} </if>" +
            "<if test='videoId != null'> AND video_id = #{videoId} </if>" +
            "<if test='status != null'> AND status = #{status} </if>" +
            "<if test='retryCount != null'> AND retry_count = #{retryCount} </if>" +
            "<if test='corpId != null'> AND corp_id = #{corpId} </if>" +
            "<if test='qwUserId != null'> AND qw_user_id = #{qwUserId} </if>" +
            "</where>" +
            "</script>")
    List<FsTagUpdateQueue> selectByConditions(FsTagUpdateQueue condition);

    @Insert("<script>" +
            "INSERT INTO fs_tag_update_queue " +
            "(course_log_id, course_id, tag_id, tag_name, operation_type, video_id, status, retry_count, corp_id, qw_user_id, fail_msg, payload, response, create_time, update_time, update_by, create_by,log_type) " +
            "VALUES " +
            "<trim prefix='(' suffix=')' suffixOverrides=','>" +
            "<if test='courseLogId != null'>course_log_id,</if>" +
            "<if test='courseId != null'>course_id,</if>" +
            "<if test='tagId != null'>tag_id,</if>" +
            "<if test='tagName != null'>tag_name,</if>" +
            "<if test='operationType != null'>operation_type,</if>" +
            "<if test='videoId != null'>video_id,</if>" +
            "<if test='status != null'>status,</if>" +
            "<if test='retryCount != null'>retry_count,</if>" +
            "<if test='corpId != null'>corp_id,</if>" +
            "<if test='qwUserId != null'>qw_user_id,</if>" +
            "<if test='qwExternalContactId != null'>qw_external_contact_id,</if>" +
            "<if test='failMsg != null'>fail_msg,</if>" +
            "<if test='payload != null'>payload,</if>" +
            "<if test='response != null'>response,</if>" +
            "<if test='createTime != null'>create_time,</if>" +
            "<if test='updateTime != null'>update_time,</if>" +
            "<if test='updateBy != null'>update_by,</if>" +
            "<if test='createBy != null'>create_by,</if>" +
            "<if test='logType != null'>log_type,</if>" +
            "</trim>" +
            "<trim prefix='VALUES (' suffix=')' suffixOverrides=','>" +
            "<if test='courseLogId != null'>#{courseLogId},</if>" +
            "<if test='courseId != null'>#{courseId},</if>" +
            "<if test='tagId != null'>#{tagId},</if>" +
            "<if test='tagName != null'>#{tagName},</if>" +
            "<if test='operationType != null'>#{operationType},</if>" +
            "<if test='videoId != null'>#{videoId},</if>" +
            "<if test='status != null'>#{status},</if>" +
            "<if test='retryCount != null'>#{retryCount},</if>" +
            "<if test='corpId != null'>#{corpId},</if>" +
            "<if test='qwExternalContactId != null'>#{qwExternalContactId},</if>" +
            "<if test='qwUserId != null'>#{qwUserId},</if>" +
            "<if test='failMsg != null'>#{failMsg},</if>" +
            "<if test='payload != null'>#{payload},</if>" +
            "<if test='response != null'>#{response},</if>" +
            "<if test='createTime != null'>#{createTime},</if>" +
            "<if test='updateTime != null'>#{updateTime},</if>" +
            "<if test='updateBy != null'>#{updateBy},</if>" +
            "<if test='createBy != null'>#{createBy},</if>" +
            "<if test='log_type != null'>#{logType},</if>" +
            "</trim>" +
            "</script>")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    int insertSelective(FsTagUpdateQueue record);



    @Insert("<script>" +
            "INSERT IGNORE INTO fs_tag_update_queue (" +
            "course_log_id, is_first, course_id, tag_id, tag_name, 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,tg_id,watching_tg_id,watched_tg_id,watching_tag_id,watched_tag_id,tag_group_id" +
            ") VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(" +
            "#{item.courseLogId}, #{item.isFirst}, #{item.courseId}, #{item.tagId}, #{item.tagName}, #{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.tgId},#{item.watchingTgId},#{item.watchedTgId},#{item.watchingTagId},#{item.watchedTagId},#{item.tagGroupId}" +
            ")" +
            "</foreach>" +
            "</script>")
    int batchInsert(@Param("list") List<FsTagUpdateQueue> list);


    @Update("<script>" +
            "UPDATE fs_tag_update_queue " +
            "<set>" +
            "<if test='courseLogId != null'>course_log_id = #{courseLogId},</if>" +
            "<if test='courseId != null'>course_id = #{courseId},</if>" +
            "<if test='tagId != null'>tag_id = #{tagId},</if>" +
            "<if test='tagName != null'>tag_name = #{tagName},</if>" +
            "<if test='operationType != null'>operation_type = #{operationType},</if>" +
            "<if test='videoId != null'>video_id = #{videoId},</if>" +
            "<if test='status != null'>status = #{status},</if>" +
            "<if test='retryCount != null'>retry_count = #{retryCount},</if>" +
            "<if test='corpId != null'>corp_id = #{corpId},</if>" +
            "<if test='qwUserId != null'>qw_user_id = #{qwUserId},</if>" +
            "<if test='failMsg != null'>fail_msg = #{failMsg},</if>" +
            "<if test='payload != null'>payload = #{payload},</if>" +
            "<if test='response != null'>response = #{response},</if>" +
            "<if test='createTime != null'>create_time = #{createTime},</if>" +
            "<if test='updateTime != null'>update_time = #{updateTime},</if>" +
            "<if test='updateBy != null'>update_by = #{updateBy},</if>" +
            "<if test='createBy != null'>create_by = #{createBy},</if>" +
            "<if test='logType != null'>log_type = #{logType},</if>" +
            "</set> " +
            "WHERE id = #{id}" +
            "</script>")
    int updateSelective(FsTagUpdateQueue record);



    @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.tagId != null'>tag_id = #{item.tagId},</if>" +
            "<if test='item.tagName != null'>tag_name = #{item.tagName},</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);

}
