<?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.report.mapper.KsVideoReportMapper">

    <select id="getKuaiShouVideoReportByMap"  resultType="com.alibaba.fastjson.JSONObject" parameterType="map">
        SELECT
        IFNULL(t1.clip_name,'-') as clip,
        IFNULL(t1.plan_name,'-') as plan,
        IFNULL(t1.shot_name,'-') as shot,
        IFNULL(t1.leader_name,'-') as designLeader,
        IFNULL(t1.video_name,'-') as materialName,
        IFNULL(t1.cover_url,'-') as coverUrl,
        IFNULL(t1.photo_url,'-') as url ,
        IFNULL(t1.channel_name,'-') AS channel,
        DATE_FORMAT(stat_date,'%Y-%m-%d') as createTime,
        sum(key_action) as 'keyAction',
        IFNULL(round(SUM(charge)/sum(key_action),3),0) as 'keyActionCost',
        sum(event_app_invoked) as 'eventAppInvoked',
        IFNULL(round(SUM(charge)/sum(event_app_invoked),3),0) as 'eventAppInvokedCost',
        concat(CAST(IFNULL(round(sum(t1.event_app_invoked) / sum(t1.bclick)*100,2),0.0) AS CHAR) ,'%') as 'eventAppInvokedRatio',
        ${filed}
        FROM
        application.kuaishou_material_video_report_daily_dw t1
        where 1=1
        <if test="accountIds !=null and accountIds != ''">
            and t1.account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="startDate !=null and startDate != ''">
            AND t1.stat_date &gt;= DATE_FORMAT(#{startDate},'%Y%m%d')
        </if>
        <if test="endDate !=null and endDate != ''">
            AND t1.stat_date &lt;= DATE_FORMAT(#{endDate},'%Y%m%d')
        </if>
        <if test="leaderId !=null and leaderId != ''">
            AND t1.leader_id = #{leaderId}
        </if>
        <if test="channelType != null">
            AND t1.channel_type = #{channelType}
        </if>
        <if test="clipId !=null and clipId != ''">
            AND t1.clip_id = #{clipId}
        </if>
        <if test="planId !=null and planId != ''">
            AND t1.plan_id = #{planId}
        </if>
        <if test="shotId !=null and shotId != ''">
            AND t1.shot_id = #{shotId}
        </if>
        <if test="companyId !=null and companyId != ''">
            AND t1.company_id = #{companyId}
        </if>
        group by signature
        ORDER BY ${target} ${order}
    </select>


    <select id="getTotalByMap" resultType="java.lang.Long">
        SELECT IFNULL(sum(1),0)
        FROM (
        SELECT
        signature
        FROM
        application.kuaishou_material_video_report_daily_dw t1
        where 1=1
        <if test="accountIds !=null and accountIds != ''">
            and t1.account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="startDate !=null and startDate != ''">
            AND t1.stat_date &gt;= DATE_FORMAT(#{startDate},'%Y%m%d')
        </if>
        <if test="endDate !=null and endDate != ''">
            AND t1.stat_date &lt;= DATE_FORMAT(#{endDate},'%Y%m%d')
        </if>
        <if test="leaderId !=null and leaderId != ''">
            AND t1.leader_id = #{leaderId}
        </if>
        <if test="channelType != null">
            AND t1.channel_type = #{channelType}
        </if>
        <if test="clipId !=null and clipId != ''">
            AND t1.clip_id = #{clipId}
        </if>
        <if test="planId !=null and planId != ''">
            AND t1.plan_id = #{planId}
        </if>
        <if test="shotId !=null and shotId != ''">
            AND t1.shot_id = #{shotId}
        </if>
        <if test="companyId !=null and companyId != ''">
            AND t1.company_id = #{companyId}
        </if>
        group by signature
        ) t
    </select>


    <select id="queryVideoReportBy" resultType="com.alibaba.fastjson.JSONObject">
        SELECT * from (SELECT
        t1.signature,
        t1.photo_url as url,
        IFNULL((SELECT u.realname FROM ctop_material_ascription t3 LEFT JOIN sys_user u on t3.clip_id=u.id where
        t3.material_id=t1.signature limit 1),'') AS clip,
        IFNULL((SELECT u.realname FROM ctop_material_ascription t3 LEFT JOIN sys_user u on t3.shot_id=u.id where
        t3.material_id=t1.signature limit 1),'') AS shot,
        IFNULL((SELECT u.realname FROM ctop_material_ascription t3 LEFT JOIN sys_user u on t3.plan_id=u.id where
        t3.material_id=t1.signature limit 1),'') AS plan,
        t1.cover_url as converUrl,
        (select t2.material_name from ctop_material_info t2 where t2.code=t1.signature limit 1) AS materialName,
        (SELECT project_name FROM ctop_project t4 WHERE t4.id = t3.project_id) AS projectName,
        (select t2.create_time from ctop_material_info t2 where t2.code=t1.signature limit 1) as createTime,
        ${filed}
        FROM
        ctop_kuaishou_report_daily_material t1
        LEFT JOIN ctop_user_allocation t3 ON t3.account_id=t1.account_id
        WHERE
        t1.stat_date >= #{startDate}
        AND t1.stat_date &lt;= #{endDate}
        <if test="accountIds != null">
            AND t1.account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY t1.signature
        ) t
        WHERE (t.clip LIKE '%${clip}%')
        AND (t.shot LIKE '%${shot}%')
        AND (t.plan LIKE '%${plan}%')
        ORDER BY ${target} ${order}
    </select>

    <select id="queryVideoDetailBy" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t1.stat_date as statDate,
        t1.signature,
        t1.photo_url as url,
        t1.cover_url as converUrl,
        (select t2.material_name from ctop_material_info t2 where t2.code=t1.signature limit 1) AS materialName,
        (
        SELECT
        project_name
        FROM
        ctop_project t4
        WHERE
        t4.id = t3.project_id
        ) AS projectName,
        (
        SELECT
        advertiser_name
        FROM
        ctop_project t4
        WHERE
        t4.id = t3.project_id limit 1
        ) AS advertiserName,
        (select t2.create_time from ctop_material_info t2 where t2.code=t1.signature limit 1) as createTime,
        ${filed}
        FROM
        ctop_kuaishou_report_daily_material t1
        LEFT JOIN ctop_user_allocation t3 ON t3.account_id=t1.account_id
        WHERE t1.signature=#{signature}
        AND t1.stat_date >= #{startDate}
        AND t1.stat_date &lt;= #{endDate}
        <if test="accountIds != null">
            AND t1.account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY t1.signature,t1.stat_date
        order by t1.stat_date desc
    </select>

    <select id="getDesignList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t1.user_id AS 'userId',
	LEFT (t1.company_name, 2) AS 'companyName',
	t3.role_name AS 'ruleName',
	t1.user_name AS 'userName'
        FROM
            user_company t1
            LEFT JOIN sys_user_role t2
        ON t1.user_id = t2.user_id
            LEFT JOIN sys_role t3 ON t2.role_id = t3.id
        WHERE
            t3.role_code IN (
            'plan'
            , 'shot'
            , 'clip'
            , 'designTeamLeader'
            )
    </select>


</mapper>