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

    <insert id="replaceBatch">
        replace into
        ctop_kuaishou_project_video_get(
        id,
        project_id,
        status,
        width,
        height,
        url,
        cover_url,
        material_type,
        channel_type,
        stat_date,
        signature,
        photo_name,
        update_time,
        material_tag,
        supplier_account_id,
        supplier_account_name
        )
        values
        <foreach collection="dataList" item="video" separator=",">
            (
            #{video.id},
            #{video.projectId},
            #{video.status},
            #{video.width},
            #{video.height},
            #{video.url},
            #{video.coverUrl},
            #{video.materialType},
            #{video.channelType},
            #{video.statDate},
            #{video.signature},
            #{video.photoName},
            #{video.updateTime},
            #{video.materialTag},
            #{video.supplierAccountId},
            #{video.supplierAccountName}
            )
        </foreach>
    </insert>

    <select id="queryLatestUpdateTime" resultType="java.lang.String">
        SELECT update_time
        FROM ctop_kuaishou_project_video_get
        WHERE project_id = #{projectId}
        ORDER BY update_time DESC LIMIT 1
    </select>

    <select id="queryPageListTotal" resultType="java.lang.Long">
        SELECT IFNULL(COUNT(1),0)
        FROM ctop_kuaishou_project_video_get
        <where>
            <if test="projectId != null">
                and project_id = #{projectId}
            </if>
            <if test="signature != null">
                and signature = #{signature}
            </if>
            <if test="status != null">
                and `status` = #{status}
            </if>
            <if test="channelType != null">
                and channel_type = #{channelType}
            </if>
        </where>
    </select>
    <select id="queryPageList" resultType="cn.com.ctop.kuaishou.modules.batch.entity.KuaishouProjectVideoGet">
        SELECT
        id,
        project_id,
        status,
        width,
        height,
        url,
        cover_url,
        material_type,
        channel_type,
        stat_date,
        signature,
        photo_name
        FROM ctop_kuaishou_project_video_get
        <where>
            <if test="projectId != null">
                and project_id = #{projectId}
            </if>
            <if test="signature != null">
                and signature = #{signature}
            </if>
            <if test="status != null">
                and `status` = #{status}
            </if>
            <if test="channelType != null">
                and channel_type = #{channelType}
            </if>
        </where>
        ORDER BY stat_date desc
    </select>

    <select id="getVideoTotalNumber" resultType="com.alibaba.fastjson.JSONObject">
        SELECT IFNULL(t1.total, 0) 'total', IFNULL(t2.validCount, 0) 'validCount', IFNULL(tt.yesterdayValidCount, 0) 'yesterdayValidCount', IFNULL(t3.todayUpCount, 0) 'todayUpCount', IFNULL(t4.yesterdayUpCount, 0) 'yesterdayUpCount', IFNULL(t5.thisWeekCount, 0) 'thisWeekCount', IFNULL(t6.lastWeekCount, 0) 'lastWeekCount'
        FROM (
                 SELECT COUNT(1) total
                 FROM ctop_kuaishou_project_video_get
                 where project_id = #{projectId}
                   and status = 0
             ) t1
                 LEFT JOIN
             (
                 SELECT COUNT(1) validCount
                 FROM ctop_kuaishou_report_daily_material t1
                          INNER JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
                 WHERE t2.project_id = #{projectId}
                   AND t1.stat_date = #{today}
                   AND t1.charge > 0
             ) t2 ON 1 = 1
                 LEFT JOIN
             (
                 SELECT COUNT(1) yesterdayValidCount
                 FROM ctop_kuaishou_report_daily_material t1
                          INNER JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
                 WHERE t2.project_id = #{projectId}
                   AND t1.stat_date = DATE_SUB(#{today}, INTERVAL 1 day)
                   AND t1.charge > 0
             ) tt ON 1 = 1
                 LEFT JOIN
             (
                 SELECT COUNT(1) todayUpCount
                 FROM ctop_kuaishou_project_video_get
                 where status = 0
                   AND project_id = #{projectId}
                   AND stat_date LIKE CONCAT(#{today}, '%')
             ) t3 ON 1 = 1
                 LEFT JOIN
             (
                 SELECT COUNT(1) yesterdayUpCount
                 FROM ctop_kuaishou_project_video_get
                 where status = 0
                   AND project_id = #{projectId}
                   AND stat_date LIKE CONCAT(DATE_SUB(#{today}, INTERVAL 1 day), '%')
             ) t4 ON 1 = 1
                 LEFT JOIN
             (
                 SELECT COUNT(1) thisWeekCount
                 FROM ctop_kuaishou_project_video_get
                 where status = 0
                   AND project_id = #{projectId}
                   AND stat_date >= #{monday}
                   AND stat_date &lt;= now()
             ) t5 ON 1 = 1
                 LEFT JOIN
             (
                 SELECT COUNT(1) lastWeekCount
                 FROM ctop_kuaishou_project_video_get
                 where status = 0
                   AND project_id = #{projectId}
                   AND stat_date >= #{lastStartDate}
                   AND stat_date &lt;= CONCAT(#{lastEndDate}, ' 23:59:59')
             ) t6 ON 1 = 1
    </select>

    <select id="getVideoDetails" resultType="com.alibaba.fastjson.JSONObject">
        SELECT IFNULL(t1.id, '') 'id', IFNULL(t1.photo_name, '') 'photoName', IFNULL(t1.signature, '') 'signature', IFNULL(t1.`status`, '') 'status', IFNULL(t1.channel_type, '') 'channelType', IFNULL(t1.stat_date, '') 'statDate', IFNULL(t1.create_time, '') 'createTime', IFNULL(t1.put_start_time, '') 'putStartTime', IFNULL(t1.put_end_time, '') 'putEndTime', IFNULL(t2.plan_name, '') 'planName', IFNULL(t2.shot_name, '') 'shotName', IFNULL(t2.clip_name, '') 'clipName', IFNULL(t1.supplier_account_name, '无') 'supplierAccountName'
        FROM ctop_kuaishou_project_video_get t1
                 LEFT JOIN ctop_material_ascription t2 ON t1.signature = t2.material_id
        WHERE t1.signature = #{signature}
          and t1.id = #{id}
    </select>

    <select id="getAccountVideosTotal" resultType="java.lang.Long">
        SELECT IFNULL(COUNT(1), 0)
        FROM (
                 select t1.account_id
                 FROM ctop_kuaishou_video_get t1
                          LEFT JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
                 WHERE t2.project_id = #{projectId}
                   AND t1.signature = #{signature}
                 GROUP BY t1.account_id
             ) t
    </select>

    <select id="getAccountVideos" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t2.auth_name 'authName',
               t0.account_id 'accountId',
               t0.stat_date 'statDate',
               t3.rejectCount,
               (SELECT COUNT(0) FROM ctop_kuaishou_creative WHERE account_id = t1.account_id
                AND photo_id = t0.photo_id) 'creativeCount',
               IFNULL(SUM(t1.charge), 0) 'charge',
               IFNULL(SUM(t1.photo_show), 0) 'photoShow',
               IFNULL(SUM(t1.photo_click), 0) 'photoClick',
               IFNULL(SUM(t1.aclick), 0) 'aclick',
               IFNULL(SUM(t1.bclick), 0) 'bclick',
               CONCAT(IFNULL(CAST(ROUND(SUM(t1.photo_click) / SUM(t1.photo_show) * 100, 2) AS CHAR), 0),
                      '%') 'photoClickRatio',
               CONCAT(IFNULL(CAST(ROUND(SUM(t1.bclick) / SUM(t1.aclick) * 100, 2) AS CHAR), 0), '%') 'actionRatio',
               IFNULL(ROUND(SUM(t1.charge) / SUM(t1.aclick) * 1000, 2), 0) 'impression1kCost',
               IFNULL(ROUND(SUM(t1.charge) / SUM(t1.photo_click), 2), 0) 'photoClickCost',
               IFNULL(ROUND(SUM(t1.charge) / SUM(t1.bclick), 2), 0) 'actionCost'
        FROM ctop_kuaishou_video_get t0
                 LEFT JOIN ctop_kuaishou_report_daily_material t1 ON t0.account_id = t1.account_id
                 LEFT JOIN ctop_user_allocation t2 ON t0.account_id = t2.account_id
                 LEFT JOIN (
            SELECT IFNULL(COUNT(1), 0) 'rejectCount',account_id
            FROM ctop_kuaishou_creative
            WHERE `status` = 42
              and account_id IN (SELECT account_id FROM ctop_user_allocation where project_id = #{projectId})
            GROUP BY account_id
        ) t3 ON t0.account_id = t3.account_id
        WHERE t2.project_id = #{projectId}
          AND t0.signature = #{signature}
        GROUP BY t0.account_id
    </select>

    <select id="getNewVideoBetweenTime" resultType="cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet">
        select t2.*,t1.put_end_time,t1.put_start_time
        from ctop_kuaishou_project_video_get t1
                 INNER JOIN
             (
                 SELECT tt1.*, tt2.project_id
                 FROM ctop_user_allocation tt2
                 LEFT JOIN ctop_kuaishou_video_get tt1 ON tt2.account_id = tt1.account_id
                 WHERE tt2.account_id = #{accountId}
                 and tt1.signature is not null
                 group by tt1.signature
             ) t2 ON t1.project_id = t2.project_id AND t1.signature = t2.signature
        WHERE t1.status = 0
        <if test="startTime!=null">
            AND t1.stat_date &gt;= #{startTime}
        </if>
        <if test="endTime!=null">
            and t1.stat_date &lt;= #{endTime}
        </if>
        <if test="channelType!=null">
            AND t1.channel_type = #{channelType}
        </if>
    </select>

    <select id="getZeroVideoByParams" resultType="cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet">
        select t1.* from (
        select t2.*,t1.put_end_time,t1.put_start_time
        from ctop_kuaishou_project_video_get t1
        INNER JOIN
        (
        SELECT tt1.*, tt2.project_id
        FROM ctop_user_allocation tt2
        LEFT JOIN ctop_kuaishou_video_get tt1 ON tt2.account_id = tt1.account_id
        WHERE tt2.account_id = #{accountId}
        and tt1.signature is not null
        <if test="appVersion != null and appVersion != '' ">
            and tt1.photo_name like concat(#{appVersion},'%')
        </if>
        group by tt1.signature
        ) t2 ON t1.project_id = t2.project_id AND t1.signature = t2.signature
        WHERE t1.status = 0
        <if test="startTime!=null">
            AND t1.stat_date &gt;= #{startTime}
        </if>
        <if test="endTime!=null">
            and t1.stat_date &lt;= #{endTime}
        </if>
        <if test="channelType!=null">
            AND t1.channel_type = #{channelType}
        </if>
        ) t1
        left join ctop_kuaishou_video_relate_creatives t2 on t1.photo_id =
        t2.photo_id and t1.account_id = t2.account_id
        where
        1=1
        <if test="relativeCnt!=null">
            and t2.creative_count &lt;= #{relativeCnt}
        </if>
        order by t1.stat_date desc
        <if test="videoCnt!=null">
            limit #{videoCnt}
        </if>
    </select>

    <select id="getHistoryTopVideoByParams"
            resultType="cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet">
        select
        t2.*
        from
        ctop_kuaishou_report_daily_material t1
        left join (
        select t2.*,t1.put_end_time,t1.put_start_time
        from ctop_kuaishou_project_video_get t1
        INNER JOIN
        (
        SELECT tt1.*, tt2.project_id
        FROM ctop_user_allocation tt2
        LEFT JOIN ctop_kuaishou_video_get tt1 ON tt2.account_id = tt1.account_id
        WHERE tt2.account_id = #{accountId}
        and tt1.signature is not null
        <if test="appVersion != null and appVersion != '' ">
            and tt1.photo_name like concat(#{appVersion},'%')
        </if>
        group by tt1.signature
        ) t2 ON t1.project_id = t2.project_id AND t1.signature = t2.signature
        WHERE t1.status = 0
        <if test="startTime!=null">
            AND t1.stat_date &gt;= #{startTime}
        </if>
        <if test="endTime!=null">
            and t1.stat_date &lt;= #{endTime}
        </if>
        <if test="channelType!=null">
            AND t1.channel_type = #{channelType}
        </if>
        ) t2
        on t1.signature = t2.signature
        where t2.id is not null
        and t2.photo_id NOT IN (
        SELECT
        photo_id
        FROM
        ctop_ai_creative_limit
        WHERE
        account_id = #{accountId}
        )
        group by t2.signature
        order by sum(t1.charge) desc
        <if test="videoCnt!=null">
            limit #{videoCnt}
        </if>
    </select>
</mapper>