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

    <select id="getMaxHourByProjectIdAndStatDate" resultType="java.lang.Integer">
    SELECT
	max(t2.stat_hour)
    FROM
	ctop_user_allocation t1
    LEFT JOIN ctop_kuaishou_report_hourly_account t2 ON t1.account_id = t2.account_id
    WHERE
	t1.project_id = #{projectId}
    AND t2.stat_date = #{statDate}
    </select>

    <select id="selectReportByMap" parameterType="java.util.Map" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        (case when t1.system_type = 'android'
         then '安卓'
         when t1.system_type = 'ios'
         then 'ios'
         else '-'
         end
        ) as systemType,
        t1.auth_name authName,
        ROUND(sum(t2.charge) / #{disCount},3) charge,
        sum(t2.activation) activation,
        (
        case
        when sum(t2.activation) != 0
        then
        ROUND((sum(t2.charge) / sum(t2.activation))/ #{disCount},3)
        else
        0
        end
        ) activationCost,

        (
        case
        when t1.system_type = 'android'
        then 16
        when t1.system_type = 'ios'
        then 23
        else 0
        end
        ) estimatedCost

        FROM
        ctop_user_allocation t1
        LEFT JOIN ctop_kuaishou_report_hourly_account t2 ON t1.account_id = t2.account_id
        where 1 = 1
        <if test="projectId != null">
            and t1.project_id = #{projectId}
        </if>

        <if test="statDate != null">
            and t2.stat_date = #{statDate}
        </if>

        <if test="statHour != null">
            and t2.stat_hour &lt;= #{statHour}
        </if>
        <if test="systemType != null">
            and t1.system_type = #{systemType}
        </if>
        group by t1.account_id

    </select>


    <select id="selectSumReportByMap" parameterType="java.util.Map" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        (case when t1.system_type = 'android'
        then '安卓'
        when t1.system_type = 'ios'
        then 'ios'
        else '-'
        end
        ) as systemType,
        #{dateDesc} authName,
        ROUND(sum(t2.charge) / #{disCount},3) charge,
        sum(t2.activation) activation,
        (
        case
        when sum(t2.activation) != 0
        then
        ROUND((sum(t2.charge) / sum(t2.activation))/ #{disCount},3)
        else
        0
        end
        ) activationCost,

        (
        case
        when t1.system_type = 'android'
        then 16
        when t1.system_type = 'ios'
        then 23
        else 0
        end
        ) estimatedCost
        FROM
        ctop_user_allocation t1
        LEFT JOIN ctop_kuaishou_report_hourly_account t2 ON t1.account_id = t2.account_id
        where 1 = 1
        <if test="projectId != null">
            and t1.project_id = #{projectId}
        </if>

        <if test="statDate != null">
            and t2.stat_date = #{statDate}
        </if>

        <if test="statHour != null">
            and t2.stat_hour &lt;= #{statHour}
        </if>
        <if test="systemType != null">
            and t1.system_type = #{systemType}
        </if>
    </select>


    <select id="getDailyReportByMap" parameterType="java.util.Map" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        '汇创' agent,
        (
        CASE
        WHEN t1.media_id = 1 THEN
        '头条'
        WHEN t1.media_id = 2 THEN
        '快手'
        ELSE
        '未知'
        END
        ) media,
        (case when t1.system_type = 'android'
        then '安卓'
        when t1.system_type = 'ios'
        then 'ios'
        else '-'
        end
        ) as systemType,
        DATE_FORMAT(t2.stat_date,'%Y/%c/%d')  statDate,
        sum(t2.charge) charge,
        ROUND(sum(t2.charge) / #{disCount}, 3) cashCost,
        sum(t2.aclick + t2.photo_show) photoShow,
        sum(t2.photo_click + t2.bclick) photoClick,
        (
        CASE
        WHEN sum(t2.photo_show) != 0 THEN
        CONCAT(
        CAST(ROUND(
        (
        sum(t2.photo_click + t2.bclick) / sum(t2.photo_show + t2.aclick)
        ) * 100,
        3
        ) AS CHAR)
        ,
        '%'
        )
        ELSE
        '0%'
        END
        ) clickRate,
        (
        CASE
        WHEN sum(t2.photo_show) != 0 THEN
        ROUND(
        (
        sum(t2.charge) / sum(t2.photo_show)
        ) * 1000,
        3
        )
        ELSE
        0
        END
        ) impression1kCost,
        sum(t2.bclick) bclick,
        sum(t2.download_completed) eventJinJianApp,
        sum(t2.activation) activation,
        0 youMengactivation,
        sum(t2.activation) tdActivation,
        (
        CASE
        WHEN sum(t2.activation) != 0 THEN
        ROUND(
        (sum(t2.charge) / #{disCount}) / sum(t2.activation),
        3
        )
        ELSE
        0
        END
        ) activationCost,
        0 youMengactivationCost,
        (
        CASE
        WHEN sum(t2.activation) != 0 THEN
        ROUND(
        (sum(t2.charge) / #{disCount}) / sum(t2.activation),
        3
        )
        ELSE
        0
        END
        ) tdActivationCost,
        (
        CASE
        WHEN sum(t2.bclick) != 0 THEN
        ROUND(
        sum(t2.charge) / sum(t2.bclick),
        3
        )
        ELSE
        0
        END
        ) actionCost,
        (
        CASE
        WHEN sum(t2.photo_click) != 0 THEN
        ROUND(
        sum(t2.charge) / sum(t2.photo_click),
        3
        )
        ELSE
        0
        END
        ) clickCost,
        (
        CASE
        WHEN sum(t2.photo_click) != 0 THEN
        CONCAT(
        CAST(ROUND(
        sum(t2.bclick) / sum(t2.photo_click),
        3
        ) * 100 AS CHAR),
        '%'
        )
        ELSE
        '0%'
        END
        ) actionRatio,
        (
        CASE
        WHEN sum(t2.bclick) != 0 THEN
        CONCAT(
        CAST( ROUND(
        sum(t2.download_completed) / sum(t2.bclick),
        3
        ) * 100 AS CHAR)
        ,
        '%'
        )
        ELSE
        '0%'
        END
        ) eventJinJianAppRatio,
        (
        CASE
        WHEN sum(t2.download_completed) != 0 THEN
        CONCAT(
        CAST(ROUND(
        sum(t2.activation) / sum(t2.download_completed),
        3
        ) * 100 AS CHAR) ,
        '%'
        )
        ELSE
        '0%'
        END
        ) activationRatio
        FROM
        ctop_user_allocation t1
        LEFT JOIN ctop_kuaishou_report_daily_account t2 ON t1.account_id = t2.account_id
        where 1 = 1
        <if test="projectId != null">
            and t1.project_id = #{projectId}
        </if>
        <if test="systemType != null">
            and t2.stat_date = #{statDate}
        </if>
        <if test="systemType != null">
            and t1.system_type = #{systemType}
        </if>

    </select>


    <select id="getDirectApplicationReportByMap" parameterType="java.util.Map"
            resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        '汇创' agent,
        (
        CASE
        WHEN t2.media_id = 1 THEN
        '头条'
        WHEN t2.media_id = 2 THEN
        '快手'
        ELSE
        '未知'
        END
        ) media,
        (case when t2.system_type = 'android'
        then '安卓'
        when t2.system_type = 'ios'
        then 'ios'
        else '-'
        end
        ) as systemType,
        DATE_FORMAT(t2.stat_date,'%Y/%c/%d') statDate,
        sum(t2.charge) charge,
        ROUND(sum(t2.charge) / #{disCount}, 3) cashCost,
        sum(t2.aclick + t2.photo_show) photoShow,
        sum(t2.photo_click + t2.bclick) photoClick,
        (
        CASE
        WHEN sum(t2.photo_show) != 0 THEN
        CONCAT(
        CAST( ROUND(
        (
        sum(t2.photo_click + t2.bclick) / sum(t2.photo_show + t2.aclick)
        ) * 100,
        3
        ) AS CHAR)
        ,
        '%'
        )
        ELSE
        '0%'
        END
        ) clickRate,
        (
        CASE
        WHEN sum(t2.photo_show) != 0 THEN
        ROUND(
        (
        sum(t2.charge) / sum(t2.photo_show)
        ) * 1000,
        3
        )
        ELSE
        0
        END
        ) impression1kCost,
        sum(t2.bclick) bclick,
        sum(t2.download_completed) eventJinJianApp,
        sum(t2.activation) activation,
        0 youMengactivation,
        sum(t2.activation) tdActivation,
        (
        CASE
        WHEN sum(t2.activation) != 0 THEN
        ROUND(
        (sum(t2.charge) / #{disCount}) / sum(t2.activation),
        3
        )
        ELSE
        0
        END
        ) activationCost,
        0 youMengactivationCost,
        (
        CASE
        WHEN sum(t2.activation) != 0 THEN
        ROUND(
        (sum(t2.charge) / #{disCount}) / sum(t2.activation),
        3
        )
        ELSE
        0
        END
        ) tdActivationCost,
        (
        CASE
        WHEN sum(t2.bclick) != 0 THEN
        ROUND(
        sum(t2.charge) / sum(t2.bclick),
        3
        )
        ELSE
        0
        END
        ) actionCost,
        (
        CASE
        WHEN sum(t2.photo_click) != 0 THEN
        ROUND(
        sum(t2.charge) / sum(t2.photo_click),
        3
        )
        ELSE
        0
        END
        ) clickCost,
        (
        CASE
        WHEN sum(t2.photo_click) != 0 THEN
        CONCAT(
        CAST( ROUND(
        sum(t2.bclick) / sum(t2.photo_click),
        3
        ) * 100 AS CHAR)
        ,
        '%'
        )
        ELSE
        '0%'
        END
        ) actionRatio,
        (
        CASE
        WHEN sum(t2.bclick) != 0 THEN
        CONCAT(
        CAST( ROUND(
        sum(t2.download_completed) / sum(t2.bclick),
        3
        ) * 100 AS CHAR)
        ,
        '%'
        )
        ELSE
        '0%'
        END
        ) eventJinJianAppRatio,
        (
        CASE
        WHEN sum(t2.download_completed) != 0 THEN
        CONCAT(
        CAST( ROUND(
        sum(t2.activation) / sum(t2.download_completed),
        3
        ) * 100 AS CHAR)
        ,
        '%'
        )
        ELSE
        '0%'
        END
        ) activationRatio
        FROM
        (
        SELECT
        t.system_type,
        t1.*,
        t.media_id,
        (
        CASE
        WHEN t.system_type = 'android' THEN
        REPLACE (
        SUBSTRING_INDEX(t1.campaign_name, '-', 1),
        'c',
        ''
        )
        WHEN t.system_type = 'ios' THEN
        REPLACE (
        SUBSTRING_INDEX(t1.campaign_name, '-', 1),
        'ks',
        ''
        )

        ELSE
        ''
        END
        ) campaignName
        FROM
        ctop_user_allocation t

        LEFT JOIN ctop_kuaishou_report_daily_campaign t1 ON t.account_id = t1.account_id
        WHERE
        1 = 1
        <if test="projectId != null">
            and t.project_id = #{projectId}
        </if>
        <if test="statDate != null">
            and t1.stat_date = #{statDate}
        </if>
        ) t2
        WHERE t2.campaignName
        in(68,441,469,470,481,581,582,583,584,552,553,554,680,681,845,846,847,848,849,850,1070,1071,1072,1066,1069,1063,1064)

    </select>


    <select id="getChannelSubcontractingReportByMap" parameterType="java.util.Map"
            resultType="com.alibaba.fastjson.JSONObject">

        SELECT
        '汇创' agent,
        (
        CASE
        WHEN t2.media_id = 1 THEN
        '头条'
        WHEN t2.media_id = 2 THEN
        '快手'
        ELSE
        '未知'
        END
        ) media,
        (case when t2.system_type = 'android'
        then '安卓'
        when t2.system_type = 'ios'
        then 'ios'
        else '-'
        end
        ) as systemType,
        DATE_FORMAT(t2.stat_date,'%Y/%c/%d') statDate,
        t2.campaignName campaignName ,
        sum(t2.charge) charge,
        ROUND(sum(t2.charge) / #{disCount}, 3) cashCost,
        sum(t2.aclick + t2.photo_show) photoShow,
        sum(t2.photo_click + t2.bclick) photoClick,
        (
        CASE
        WHEN sum(t2.photo_show) != 0 THEN
        CONCAT(
        CAST( ROUND(
        (
        sum(t2.photo_click + t2.bclick) / sum(t2.photo_show + t2.aclick)
        ) * 100,
        3
        ) AS CHAR)
        ,
        '%'
        )
        ELSE
        '0%'
        END
        ) clickRate,
        (
        CASE
        WHEN sum(t2.photo_show) != 0 THEN
        ROUND(
        (
        sum(t2.charge) / sum(t2.photo_show)
        ) * 1000,
        3
        )
        ELSE
        0
        END
        ) impression1kCost,
        sum(t2.bclick) bclick,
        sum(t2.download_completed) eventJinJianApp,
        sum(t2.activation) activation,
        0 youMengactivation,
        sum(t2.activation) tdActivation,
        (
        CASE
        WHEN sum(t2.activation) != 0 THEN
        ROUND(
        (sum(t2.charge) / #{disCount}) / sum(t2.activation),
        3
        )
        ELSE
        0
        END
        ) activationCost,
        0 youMengactivationCost,
        (
        CASE
        WHEN sum(t2.activation) != 0 THEN
        ROUND(
        (sum(t2.charge) / #{disCount}) / sum(t2.activation),
        3
        )
        ELSE
        0
        END
        ) tdActivationCost,
        (
        CASE
        WHEN sum(t2.bclick) != 0 THEN
        ROUND(
        sum(t2.charge) / sum(t2.bclick),
        3
        )
        ELSE
        0
        END
        ) actionCost,
        (
        CASE
        WHEN sum(t2.photo_click) != 0 THEN
        ROUND(
        sum(t2.charge) / sum(t2.photo_click),
        3
        )
        ELSE
        0
        END
        ) clickCost,
        (
        CASE
        WHEN sum(t2.photo_click) != 0 THEN
        CONCAT(
        CAST( ROUND(
        sum(t2.bclick) / sum(t2.photo_click),
        3
        ) * 100 AS CHAR),
        '%'
        )
        ELSE
        '0%'
        END
        ) actionRatio,
        (
        CASE
        WHEN sum(t2.bclick) != 0 THEN
        CONCAT(
        CAST(ROUND(
        sum(t2.download_completed) / sum(t2.bclick),
        3
        ) * 100 AS CHAR)
        ,
        '%'
        )
        ELSE
        '0%'
        END
        ) eventJinJianAppRatio,
        sum(t2.key_action) as 'keyAction',
        (
        CASE
        WHEN sum(t2.download_completed) != 0 THEN
        CONCAT(
        CAST( ROUND(
        sum(t2.activation) / sum(t2.download_completed),
        3
        ) * 100 AS CHAR)
        ,
        '%'
        )
        ELSE
        '0%'
        END
        ) activationRatio
        FROM
        (
        SELECT
        t.system_type,
        t1.*,
        t.media_id,
        (
        CASE
        WHEN t.system_type = 'android' THEN
        SUBSTRING_INDEX(t1.campaign_name, '-', 1)
        WHEN t.system_type = 'ios' THEN
        SUBSTRING_INDEX(t1.campaign_name, '-', 1)
        ELSE
        ''
        END
        ) campaignName
        FROM
        ctop_user_allocation t
        LEFT JOIN ctop_kuaishou_report_daily_campaign t1 ON t.account_id = t1.account_id
        WHERE
        1 = 1
        <if test="projectId != null">
            and t.project_id = #{projectId}
        </if>
        <if test="statDate != null">
            and t1.stat_date = #{statDate}
        </if>
        ) t2

        GROUP BY t2.campaignName
    </select>


</mapper>
