<?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.bytedance.mapper.DailyReportMapper">

    <select id="getDailyReport" parameterType="Map" resultType="cn.com.ctop.bytedance.vo.DailyAccountReportVO">
        SELECT
        t1.*,
        truncate((t1.conversions / t1.bclick),4) conversionRate, -- 转化率

        truncate(t1.conversionPrice / #{discount},2) discountConversionPrice, -- 折后转化成本

        (SELECT sum(contract_rebate_real_charged)
        from ctop_kuaishou_daily_flows
        <where>
            <if test="date != null">
                and date = #{date}
            </if>

            <if test="accountIds != null">
                and account_id in
                <foreach collection="accountIds" item="item" separator=","
                         open="(" close=")">
                    #{item}
                </foreach>

            </if>
        </where>
        ) contractRebateRealCharged -- 激励花费

        FROM (
        SELECT
        sum(charge) cost, -- 消耗
        sum(photo_show) photoShow, -- 展现数
        sum(aclick) aclick, -- 素材曝光数
        sum(photo_click) photoClick, -- 点击数
        (
        CASE
        WHEN sum(photo_show) != 0 THEN
        (
        truncate( sum(photo_click) / sum(photo_show),4)
        )
        ELSE
        0
        END
        ) photoClickRatio, -- 点击率
        (
        CASE
        WHEN sum(charge) != 0 THEN
        (
        truncate(sum(photo_click) / sum(charge),4)
        )
        ELSE
        0
        END
        ) photoClickCost, -- 平均点击花费

        sum(bclick) bclick, -- 行为数

        (
        CASE
        WHEN sum(aclick) != 0 THEN
        truncate(sum(bclick) / sum(aclick),4)
        ELSE
        0
        END
        ) actionRatio, -- 行为率
        (
        CASE
        WHEN sum(photo_show) != 0 THEN
        truncate( (sum(charge) / sum(photo_show)
        ) * 1000,2)
        ELSE
        0
        END
        ) impression1kCost, -- 平均千次展现花费
        (
        CASE
        WHEN sum(form_count) != 0 THEN
        sum(form_count)
        WHEN sum(activation) != 0 THEN
        sum(activation)
        ELSE
        0
        END
        ) conversions, -- 转化数
        (
        CASE
        WHEN sum(form_count) != 0 THEN
        (
        truncate(sum(charge) / sum(form_count),2)
        )
        WHEN sum(activation) != 0 THEN
        (
        truncate(sum(charge) / sum(activation),2)
        )
        ELSE
        0
        END
        ) conversionPrice -- 转化单价
        FROM
        ctop_kuaishou_report_daily_account
        <where>
            <if test="date != null">
                and stat_date = #{date}
            </if>

            <if test="accountIds != null">
                and account_id in
                <foreach collection="accountIds" item="item" separator=","
                         open="(" close=")">
                    #{item}
                </foreach>

            </if>

        </where>
        ) t1

    </select>


    <select id="getDailyGroupReport" parameterType="Map" resultType="cn.com.ctop.bytedance.vo.DailyGroupReportVO">
        SELECT
        t1.*,
        truncate(t1.conversions / t1.photoClick,4) clickConversionRate, -- 点击转化率
        truncate(t1.conversionPrice / #{discount},2) discountConversionPrice-- 折后转化单价
        FROM
        (
        SELECT
        account_id accountId,
        (
        SELECT
        auth_name
        FROM
        ctop_user_allocation
        WHERE
        account_id = accountId
        ) authName, -- 账户名称
        stat_date statDate, -- 报表日期
        campaign_name campaignName, -- 计划名称
        unit_name unitName, -- 广告组名称
        sum(charge) cost, -- 花费
        truncate((sum(charge) / #{discount}),2) disCountCost, -- 折后消费
        sum(photo_show) photoShow, -- 展现数
        sum(photo_click) photoClick, -- 点击数
        sum(bclick) bclick, -- 行为数
        (
        CASE
        WHEN sum(aclick) != 0 THEN
        truncate(sum(bclick) / sum(aclick),4)
        ELSE
        0
        END
        ) actionRatio, -- 行为率


        (
        CASE
        WHEN sum(aclick) != 0 THEN
        truncate(sum(photo_click) / sum(aclick),4)
        ELSE
        0
        END
        ) photoClickRatio, -- 点击率


        (
        CASE
        WHEN sum(photo_show) != 0
        THEN
        truncate((sum(charge) / sum(photo_show)
        ) * 1000,2)
        ELSE
        0
        END
        ) impression1kCost, -- 平均千次展现花费
        (
        CASE
        WHEN sum(charge) != 0 THEN
        (
        truncate(sum(photo_click) / sum(charge),2)
        )
        ELSE
        0
        END
        ) photoClickCost, -- 平均点击花费
        (
        CASE
        WHEN sum(form_count) != 0 THEN
        sum(form_count)
        WHEN sum(activation) != 0 THEN
        sum(activation)
        ELSE
        0
        END
        ) conversions, -- 转化数
        (
        CASE
        WHEN sum(form_count) != 0 THEN
        (
        truncate(sum(charge) / sum(form_count),2)
        )
        WHEN sum(activation) != 0 THEN
        (
        truncate(sum(charge) / sum(activation),2)
        )
        ELSE
        0
        END
        ) conversionPrice -- 转化单价

        FROM
        ctop_kuaishou_report_daily_group
        <where>
            <if test="date != null">
                and stat_date = #{date}
            </if>

            <if test="accountIds != null">
                and account_id in
                <foreach collection="accountIds" item="item" separator=","
                         open="(" close=")">
                    #{item}
                </foreach>

            </if>
        </where>
        GROUP BY unit_id
        order by account_id

        ) t1
    </select>


</mapper>