<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tzld.piaoquan.api.dao.mapper.contentplatform.ExternalChannelMapperExt">

    <resultMap id="BaseResultMap" type="com.tzld.piaoquan.api.model.po.contentplatform.ExternalChannel">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="root_source_id" jdbcType="VARCHAR" property="rootSourceId" />
        <result column="channel" jdbcType="VARCHAR" property="channel" />
        <result column="partner_id" jdbcType="VARCHAR" property="partnerId" />
        <result column="account_id" jdbcType="VARCHAR" property="accountId" />
        <result column="creative_id" jdbcType="VARCHAR" property="creativeId" />
        <result column="article_id" jdbcType="VARCHAR" property="articleId" />
        <result column="card_id" jdbcType="VARCHAR" property="cardId" />
        <result column="package_id" jdbcType="VARCHAR" property="packageId" />
        <result column="status" jdbcType="INTEGER" property="status" />
        <result column="is_delete" jdbcType="INTEGER" property="isDelete" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    </resultMap>

    <sql id="Base_Column_List">
        id, root_source_id, channel, partner_id, account_id, creative_id, article_id, card_id, 
        package_id, `status`, is_delete, create_time, update_time
    </sql>

    <!-- 查询待处理的记录列表（status=0），仅查询创建时间在指定时间之后的记录 -->
    <select id="selectPendingList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from external_channel
        where `status` = 0
        and is_delete = 0
        and id > #{lastId}
        and create_time >= #{startTime}
        order by id asc
        limit #{limit}
    </select>

    <!-- 将创建时间早于指定时间的待处理记录标记为失败 -->
    <update id="markFailedForTimeoutRecords">
        update external_channel
        set `status` = 2,
            update_time = NOW()
        where `status` = 0
          and is_delete = 0
          and create_time &lt; #{timeoutTime}
    </update>

    <!-- 检查 rootSourceId 是否已存在 -->
    <select id="checkRootSourceIdExists" resultType="int">
        select count(1)
        from external_channel
        where root_source_id = #{rootSourceId}
          and is_delete = 0
    </select>

    <!-- 批量查询已存在的rootSourceId -->
    <select id="selectExistingRootSourceIds" resultType="java.lang.String">
        select root_source_id
        from external_channel
        where is_delete = 0
        and root_source_id in
        <foreach collection="rootSourceIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <!-- 批量插入待处理记录（忽略重复） -->
    <insert id="batchInsertIgnore" parameterType="java.util.List">
        insert ignore into external_channel (
            root_source_id,
            `status`,
            is_delete,
            create_time,
            update_time
        ) values
        <foreach collection="list" item="item" separator=",">
            (
                #{item.rootSourceId,jdbcType=VARCHAR},
                0,
                0,
                #{item.createTime,jdbcType=TIMESTAMP},
                #{item.updateTime,jdbcType=TIMESTAMP}
            )
        </foreach>
    </insert>

    <!-- 查询 video_id 为空的记录列表 -->
    <select id="selectEmptyVideoIdList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from external_channel
        where is_delete = 0
        and (video_id is null or video_id = 0)
        and id > #{lastId}
        order by id asc
        limit #{limit}
    </select>

    <!-- 批量更新记录的 video_id -->
    <update id="batchUpdateVideoId" parameterType="java.util.List">
        <foreach collection="list" item="item" separator=";">
            update external_channel
            set video_id = #{item.videoId,jdbcType=BIGINT},
                update_time = NOW()
            where id = #{item.id,jdbcType=BIGINT}
        </foreach>
    </update>

</mapper>
