<?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="cn.com.ctop.kuaishou.modules.batch.mapper.DelKuaiShouVideoGetMapper">
    <insert id="replaceBatch">
        replace into ctop_del_kuaishou_video_get(
        account_id,
        photo_id,
        signature,
        photo_name
        )
        values
        <foreach collection="videos" item="video" separator=",">
            (
            #{video.accountId},
            #{video.photoId},
            #{video.signature},
            #{video.photoName}
            )
        </foreach>
    </insert>

    <delete id="deleteByAccountId">
        DELETE
        FROM ctop_del_kuaishou_video_get
        WHERE account_id = #{accountId}
    </delete>

    <select id="queryPhototIdsByPhotoName" resultType="String">
        select photo_id
        from ctop_del_kuaishou_video_get
        where account_id = #{accountId}
          AND photo_name Like CONCAT('%', #{photoName}, '%')
    </select>

    <insert id="replaceBatchVideoRelateCreatives">
        replace
        into ctop_del_kuaishou_video_relate_creatives(
        account_id,
        photo_id,
        creative_id,
        create_name)
        values
        <foreach collection="creatives" item="creative" separator=",">
            (
            #{object.account_id},
            #{object.photo_id},
            #{creative.creative_id},
            #{creative.creative_name}
            )
        </foreach>
    </insert>

    <insert id="replaceBatchVideoRelateAdvancedCreatives">
        replace
        into ctop_del_kuaishou_video_relate_advanced_creatives(
        account_id,
        photo_id,
        advanced_creative_id)
        values
        <foreach collection="advancedCreativeIds" item="advancedCreativeId" separator=",">
            (
            #{object.account_id},
            #{object.photo_id},
            #{advancedCreativeId}
            )
        </foreach>
    </insert>

    <select id="queryCreativeIds" resultType="Long">
        select creative_id
        from ctop_del_kuaishou_video_relate_creatives
        where put_status = 0
          AND account_id = #{accountId}
    </select>

    <select id="queryAdvancedCreativeIds" resultType="Long">
        select advanced_creative_id
        from ctop_del_kuaishou_video_relate_advanced_creatives
        where put_status = 0
          AND account_id = #{accountId}
    </select>

    <select id="getPhotoIds" resultType="String">
        select photo_id
        from ctop_del_kuaishou_video_relate_creatives
        where put_status = #{putStatus}
          AND account_id = #{accountId}
        group by photo_id
    </select>

    <select id="getPhotoIds2" resultType="String">
        select photo_id
        from ctop_del_kuaishou_video_relate_advanced_creatives
        where put_status = #{putStatus}
          AND account_id = #{accountId}
        group by photo_id
    </select>

    <update id="updateCreativesStatus">
        UPDATE ctop_del_kuaishou_video_relate_creatives
        SET put_status = 3
        WHERE account_id = #{accountId}
        AND creative_id IN
        <foreach item="creativeId" index="index" collection="creativeIds"
                 open="(" separator="," close=")">
            #{creativeId}
        </foreach>
    </update>

    <update id="updateAdvancedCreativesStatus">
        UPDATE ctop_del_kuaishou_video_relate_advanced_creatives
        SET put_status = 3
        WHERE account_id = #{accountId}
        AND advanced_creative_id IN
        <foreach item="creativeId" index="index" collection="unitIds"
                 open="(" separator="," close=")">
            #{creativeId}
        </foreach>
    </update>

    <insert id="insertDelPhotoIdLog">
        REPLACE INTO ctop_del_kuaishou_delete_video_record (account_id, photo_id, photo_name,r_code,r_message)
        SELECT account_id, photo_id, photo_name,'${code}','${message}'
        FROM ctop_del_kuaishou_video_get
        WHERE account_id = #{accountId}
        and photo_id in
        <foreach collection="photoIds" item="photoId" separator=","
                 open="(" close=")">
            #{photoId}
        </foreach>
    </insert>

</mapper>
