<?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.ai.mapper.AiKuaishouAccountAutoVideoMapper">

    <insert id="insertBatch">
        replace into ctop_ai_kuaishou_account_auto_video(
        account_id,
        custom_status,
        program_status,
        photo_id,
        photo_name,
        material_type,
        channel_type,
        stat_date,
        signature
        )
        values
        <foreach collection="videoGetList" item="video" separator=",">
            (
            #{video.accountId},
            #{video.customStatus},
            #{video.programStatus},
            #{video.photoId},
            #{video.photoName},
            #{video.materialType},
            #{video.channelType},
            #{video.statDate},
            #{video.signature}
            )
        </foreach>
    </insert>


    <select id="getUpNewVideos" resultType="cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAccountAutoVideo">
        SELECT * FROM ctop_ai_kuaishou_account_auto_video
        WHERE account_id = #{accountId}
        <if test="createType == 'custom' ">
            AND custom_status = 1
        </if>
        <if test="createType == 'program'">
            AND program_status = 1
        </if>
        <if test="channelType!=null and channelType!=2">
            AND channel_type = #{channelType}
        </if>
        <if test="statDate!=null">
            AND stat_date = #{statDate}
        </if>
        <if test="keyword != null and keyword.size>0 ">
            AND
            <foreach item="item" index="index" collection="keyword"
                     open="(" separator="OR" close=")">
                photo_name LIKE concat(#{item},'%')
            </foreach>
        </if>
        LIMIT #{videoCnt}
    </select>

    <select id="getZeroVideoByParams" resultType="cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAccountAutoVideo">
        SELECT * FROM ctop_ai_kuaishou_account_auto_video
        WHERE custom_status = 1
        AND account_id = #{accountId}
        <if test="channelType!=null and channelType!=2">
            AND channel_type = #{channelType}
        </if>
        <if test="statDate!=null">
            AND stat_date &lt; #{statDate}
            AND stat_date &gt;= DATE_SUB(#{statDate},INTERVAL 7 day)
        </if>
        <if test="keyword != null and keyword.size>0 ">
            AND
            <foreach item="item" index="index" collection="keyword"
                     open="(" separator="OR" close=")">
                photo_name LIKE concat(#{item},'%')
            </foreach>
        </if>
        LIMIT #{videoCnt}
    </select>

    <select id="getHistoryTopVideos" resultType="cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAccountAutoVideo">
        select
        account_id,
        md5 as 'signature',
        photo_id,
        material_name as 'photoName',
        channel_type,
        material_type
        from ctop_material_top_cost
        where account_id = #{accountId}
        and md5 not in
        (
        SELECT signature FROM ctop_ai_kuaishou_account_auto_video_used
        WHERE account_id =#{accountId}
        AND video_type = 2
        AND usage_times &gt;= 5
        <if test="createType !=null">
            AND create_type = #{createType}
        </if>
        AND stat_date &gt;= DATE_SUB(#{statDate},INTERVAL 7 day)
        group by signature
        )
        <if test="channelType!=null and channelType!=2">
            and channel_type = #{channelType}
        </if>
        <if test="keyword != null and keyword.size>0 ">
            AND
            <foreach item="item" index="index" collection="keyword"
                     open="(" separator="OR" close=")">
                material_name LIKE concat(#{item},'%')
            </foreach>
        </if>
        group by signature
        ORDER BY 7day_cost DESC
        LIMIT #{videoCnt}
    </select>

    <select id="getHistoryCatchVideos" resultType="cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAccountAutoVideo">
        select
        account_id,
        md5 as 'signature',
        photo_id,
        material_name as 'photoName',
        channel_type,
        material_type
        from ctop_history_material_catch
        where account_id = #{accountId}
        and md5 not in
        (
        SELECT signature FROM ctop_ai_kuaishou_account_auto_video_used
        WHERE account_id =#{accountId}
        AND video_type = 4
        AND create_type = 'custom'
        AND usage_times &gt;= 2
        AND stat_date &gt;= DATE_SUB(#{statDate},INTERVAL 60 day)
        group by signature
        )
        <if test="channelType!=null and channelType!=2">
            and channel_type = #{channelType}
        </if>
        <if test="keyword != null and keyword.size>0 ">
            AND
            <foreach item="item" index="index" collection="keyword"
                     open="(" separator="OR" close=")">
                material_name LIKE concat(#{item},'%')
            </foreach>
        </if>
        group by signature
        ORDER BY sum_cost DESC
        LIMIT #{videoCnt}
    </select>


    <select id="getAutoVideoUsed" resultType="java.lang.Integer">
        SELECT usage_times
        FROM ctop_ai_kuaishou_account_auto_video_used
        WHERE signature = #{signature}
          AND account_id = #{accountId}
          AND video_type = #{videoType}
          AND create_type = #{createType} limit 1
    </select>


    <insert id="addAutoVideoUsed">
        replace
        into ctop_ai_kuaishou_account_auto_video_used
        (
        account_id,
        signature,
        video_type,
        create_type,
        stat_date,
        usage_times)
        values
        (
        #{accountId},
        #{signature},
        #{videoType},
        #{createType},
        #{date},
        #{usageTimes}
        )
    </insert>

    <update id="updateAutoVideoUsed">
        UPDATE ctop_ai_kuaishou_account_auto_video_used
        SET usage_times = #{usageTimes},
            stat_date   = #{date}
        WHERE signature = #{signature}
          AND account_id = #{accountId}
          AND video_type = #{videoType}
          AND create_type = #{createType}
    </update>

    <select id="selectBatchByPhotoIds" resultType="java.lang.String">
        SELECT photo_id
        FROM ctop_ai_kuaishou_account_auto_video
        WHERE account_id = #{accountId}
        AND stat_date = #{statDate}
        <if test="details!=null">
            AND photo_id in
            <foreach item="item" collection="details"
                     open="(" separator="," close=")">
                #{item.photo_id}
            </foreach>
        </if>
    </select>

</mapper>