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

    <select id="queryAccountCostInfo" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        stat_date AS statDate,account_id AS accountId,ROUND(SUM(charge),3) AS totalCost,
        "account" AS type, sysdate() AS createTime,
        CASE WHEN SUM(bclick) != 0 THEN
        ROUND(SUM(charge)/SUM(bclick),3) ELSE 0 END AS 'actionCost',
        CASE WHEN SUM(activation) != 0 THEN
        ROUND(SUM(charge)/SUM(activation),3) ELSE 0 END AS activationCost,
        CASE WHEN SUM(event_register) != 0 THEN
        ROUND(SUM(charge)/SUM(event_register),3)  ELSE 0 END AS  registerCost,
        CASE WHEN SUM(event_pay) != 0 THEN
        ROUND(SUM(charge) / SUM(event_pay), 3) ELSE 0 END AS  payCost,
        CASE WHEN SUM(form_count) != 0 THEN
        ROUND(SUM(charge) / SUM(form_count),3) ELSE 0 END AS  formCost,
        CASE WHEN SUM(event_jin_jian_app) != 0 THEN
        ROUND(SUM(charge)/SUM(event_jin_jian_app) ,3) ELSE 0 END AS finishedCost,
        CASE WHEN SUM(event_credit_grant_app) != 0 THEN
        ROUND(SUM(charge)/SUM(event_credit_grant_app) ,3)ELSE 0 END AS approvalCost,
        CASE WHEN SUM(charge) != 0 THEN
        ROUND(SUM(event_pay_purchase_amount_first_day)/SUM(charge) ,4) ELSE 0 END AS firstDayRoi,
        CASE WHEN SUM(charge) != 0 THEN
        ROUND(SUM(event_pay_purchase_amount)/SUM(charge) ,4) ELSE 0 END AS roi
        FROM ctop_kuaishou_report_hourly_account
        WHERE stat_date = #{date}
        GROUP BY account_id
    </select>

    <select id="queryCampaignCostInfo" resultType="com.alibaba.fastjson.JSONObject">
       SELECT
        t1.stat_date AS statDate,t1.account_id AS accountId,
		t1.campaign_id AS campaignId,IFNULL(t2.status,-1) as campaignStatus,
		"campaign" AS type, sysdate() AS createTime,
		ROUND(SUM(charge),3) AS totalCost,
        CASE WHEN SUM(bclick) != 0 THEN
        ROUND(SUM(charge)/SUM(bclick),3) ELSE 0 END AS 'actionCost',
        CASE WHEN SUM(activation) != 0 THEN
        ROUND(SUM(charge)/SUM(activation),3) ELSE 0 END AS activationCost,
        CASE WHEN SUM(event_register) != 0 THEN
        ROUND(SUM(charge)/SUM(event_register),3)  ELSE 0 END AS  registerCost,
        CASE WHEN SUM(event_pay) != 0 THEN
        ROUND(SUM(charge) / SUM(event_pay), 3) ELSE 0 END AS  payCost,
        CASE WHEN SUM(form_count) != 0 THEN
        ROUND(SUM(charge) / SUM(form_count),3) ELSE 0 END AS  formCost,
        CASE WHEN SUM(event_jin_jian_app) != 0 THEN
        ROUND(SUM(charge)/SUM(event_jin_jian_app) ,3) ELSE 0 END AS finishedCost,
        CASE WHEN SUM(event_credit_grant_app) != 0 THEN
        ROUND(SUM(charge)/SUM(event_credit_grant_app) ,3)ELSE 0 END AS approvalCost,
        CASE WHEN SUM(charge) != 0 THEN
        ROUND(SUM(event_pay_purchase_amount_first_day)/SUM(charge) ,4) ELSE 0 END AS firstDayRoi,
        CASE WHEN SUM(charge) != 0 THEN
        ROUND(SUM(event_pay_purchase_amount)/SUM(charge) ,4) ELSE 0 END AS roi
        FROM ctop_kuaishou_report_hourly_campaign t1
		LEFT JOIN ctop_kuaishou_campaign t2 ON t1.campaign_id = t2.campaign_id
        WHERE stat_date = #{date}
        GROUP BY t1.campaign_id
    </select>

    <select id="queryGroupCostInfo" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        stat_date AS statDate,t1.account_id AS accountId,t1.campaign_id AS campaignId,
        t1.unit_id AS unitId,IFNULL(t2.status,-1) as unitStatus,
        "unit" AS type, sysdate() AS createTime,
		ROUND(SUM(charge),3) AS totalCost,
        CASE WHEN SUM(bclick) != 0 THEN
        ROUND(SUM(charge)/SUM(bclick),3) ELSE 0 END AS 'actionCost',
        CASE WHEN SUM(activation) != 0 THEN
        ROUND(SUM(charge)/SUM(activation),3) ELSE 0 END AS activationCost,
        CASE WHEN SUM(event_register) != 0 THEN
        ROUND(SUM(charge)/SUM(event_register),3)  ELSE 0 END AS  registerCost,
        CASE WHEN SUM(event_pay) != 0 THEN
        ROUND(SUM(charge) / SUM(event_pay), 3) ELSE 0 END AS  payCost,
        CASE WHEN SUM(form_count) != 0 THEN
        ROUND(SUM(charge) / SUM(form_count),3) ELSE 0 END AS  formCost,
        CASE WHEN SUM(event_jin_jian_app) != 0 THEN
        ROUND(SUM(charge)/SUM(event_jin_jian_app) ,3) ELSE 0 END AS finishedCost,
        CASE WHEN SUM(event_credit_grant_app) != 0 THEN
        ROUND(SUM(charge)/SUM(event_credit_grant_app) ,3)ELSE 0 END AS approvalCost,
        CASE WHEN SUM(charge) != 0 THEN
        ROUND(SUM(event_pay_purchase_amount_first_day)/SUM(charge) ,4) ELSE 0 END AS firstDayRoi,
        CASE WHEN SUM(charge) != 0 THEN
        ROUND(SUM(event_pay_purchase_amount)/SUM(charge) ,4) ELSE 0 END AS roi
        FROM ctop_kuaishou_report_hourly_group t1
		LEFT JOIN ctop_kuaishou_group t2 ON t1.unit_id = t2.unit_id
        WHERE stat_date = #{date}
        GROUP BY t1.unit_id
    </select>

    <insert id="bulkInsert" parameterType="com.alibaba.fastjson.JSONObject">
        insert into ctop_kuaishou_cost_group (
        account_id, campaign_id,unit_id, total_cost, total_balance, type,stat_date, action_cost, activation_cost,
        register_cost, pay_cost, form_cost,finished_cost,approval_cost,first_day_roi,roi,create_time
        )
        values
        <foreach collection="list" item="it" separator=",">
            (
            #{it.accountId},
            #{it.campaignId},
            #{it.unitId},
            #{it.totalCost},
            #{it.totalBalance},
            #{it.type},
            #{it.statDate},
            #{it.actionCost},
            #{it.activationCost},
            #{it.registerCost},
            #{it.payCost},
            #{it.formCost},
            #{it.finishedCost},
            #{it.approvalCost},
            #{it.firstDayRoi},
            #{it.roi},
            #{it.createTime}
            )
        </foreach>
    </insert>

</mapper>