<?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.KuaishouVideoRelateCreativesMapper">

    <insert id="insertBatch">
        replace into ctop_kuaishou_video_relate_creatives(
        account_id,
        photo_id,
        creative_count,
        stat_date
        )
        values
        <foreach collection="videolist" item="video" separator=",">
            (
            #{video.accountId},
            #{video.photoId},
            #{video.creativeCount},
            #{video.statDate}
            )
        </foreach>
    </insert>


    <select id="getVideoListByMap" resultType="com.alibaba.fastjson.JSONObject">
        select
        photo_id as 'photo_id',
        stat_date as 'stat_date',
        creative_count as 'creative_count'
        FROM ctop_kuaishou_video_relate_creatives AS t1
        JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM
        ctop_kuaishou_video_relate_creatives
        where 1 = 1
        <if test="accountId != null and accountId != '' ">
            and account_id = #{accountId}
        </if>
        <if test="startTime != null and startTime != '' ">
            and DATE_FORMAT(stat_date,'%Y-%m-%d') &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != '' ">
            and DATE_FORMAT(stat_date,'%Y-%m-%d') &lt;= #{endTime}
        </if>
        )) AS id) AS t2
        WHERE t1.id >= t2.id
        <if test="accountId != null and accountId != '' ">
            and account_id = #{accountId}
        </if>
        <if test="createCount != null and createCount != '' ">
            AND creative_count &lt;= #{createCount}
        </if>
        ORDER BY t1.id ASC LIMIT #{num}
    </select>

    <select id="getVideoZeroListByMap" resultType="com.alibaba.fastjson.JSONObject">
        select
        photo_id as 'photo_id',
        stat_date as 'stat_date',
        creative_count as 'creative_count'
        FROM ctop_kuaishou_video_relate_creatives
        where 1 = 1
        <if test="accountId != null and accountId != '' ">
            and account_id = #{accountId}
        </if>
        <if test="creativeCount != null and creativeCount != '' ">
            AND creative_count = #{creativeCount}
        </if>
        <if test="startTime != null and startTime != '' ">
            AND stat_date &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != '' ">
            AND stat_date &lt;= #{endTime}
        </if>
        ORDER BY stat_date ASC
        LIMIT #{videoCnt}
    </select>

    <select id="getHistoryTopVideoList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t2.photo_id,
        t2.signature,
        t1.charge,
        t2.channel_type,
        t2.material_type as 'material_type',
        t2.url as 'video_url'
        FROM
        (
        SELECT
        sum(charge) as 'charge',
        signature
        FROM
        ctop_kuaishou_report_daily_material
        WHERE
        signature IN (
        SELECT DISTINCT
        (signature)
        FROM
        ctop_kuaishou_video_get
        WHERE
        account_id = #{accountId}
        and stat_date &gt;= #{startDate}
        and stat_date &lt;= #{endDate}
        and signature is  not  null
        )
        GROUP BY
        signature
        ORDER BY
        sum(charge) DESC
        LIMIT #{num}
        ) t1
        LEFT   JOIN ctop_kuaishou_video_get t2 ON t1.signature = t2.signature
        AND t2.account_id = #{accountId}
        GROUP BY t2.signature
        ORDER BY t1.charge DESC
    </select>
</mapper>