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

    <insert id="replaceBatch">
        replace into ctop_etl_kuaishou_group_daily_report(
        account_id,
        campaign_id,
        campaign_name,
        unit_id,
        unit_name,
        unit_status,
        bid_type,
        bid,
        charge,
        photo_show,
        aclick,
        bclick,
        download_started,
        download_completed,
        activation,
        event_pay_first_day,
        event_pay,
        event_register,
        event_jin_jian_app,
        event_order_paid,
        event_next_day_stay,
        play_3s_count,
        event_app_invoked,
        stat_date
        )
        values
        <foreach collection="addList" item="addList" separator=",">
            (
            #{addList.accountId},
            #{addList.campaignId},
            #{addList.campaignName},
            #{addList.unitId},
            #{addList.unitName},
            #{addList.unitStatus},
            #{addList.bidType},
            #{addList.bid},
            #{addList.charge},
            #{addList.photoShow},
            #{addList.aclick},
            #{addList.bclick},
            #{addList.downloadStarted},
            #{addList.downloadCompleted},
            #{addList.activation},
            #{addList.eventPayFirstDay},
            #{addList.eventPay},
            #{addList.eventRegister},
            #{addList.eventJinJianApp},
            #{addList.eventOrderPaid},
            #{addList.eventNextDayStay},
            #{addList.play3sCount},
            #{addList.eventAppInvoked},
            #{addList.statDate}
            )
        </foreach>
    </insert>

    <select id="getTotal" resultType="java.lang.Long">
        SELECT count(1)
        FROM ctop_etl_kuaishou_group_daily_report t1
        WHERE account_id = #{accountId}
          AND t1.charge > 0
          AND stat_date = DATE_FORMAT(now(), '%Y-%m-%d')
    </select>

    <select id="queryPageList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t1.unit_id as 'unitId',
        account_id as 'accountId',
        unit_name as 'unitName',
        t1.charge,
        IFNULL(ROUND(t1.charge/activation,2),0) as 'activationCost',
        bid,
        bid_type as 'bidType',
        unit_status as 'unitStatus',
        activation,
        IFNULL(ROUND((t1.charge-t2.charge)/t2.charge*100,2),0)'grew',
        t1.update_time as 'updateTime'
        FROM ctop_etl_kuaishou_group_daily_report t1
        LEFT JOIN (
        SELECT charge,unit_id FROM ctop_etl_kuaishou_group_daily_report
        WHERE account_id = #{accountId}
        AND stat_date = DATE_FORMAT(date_sub(now(), interval 1 day), '%Y-%m-%d')
        ) t2 ON t1.unit_id = t2.unit_id
        WHERE account_id = #{accountId}
        AND t1.charge > 0
        AND stat_date = DATE_FORMAT(now(), '%Y-%m-%d')
        <if test="sortCode == 'charge' ">
            ORDER BY t1.charge
        </if>
        <if test="sortCode == 'activationCost' ">

            ORDER BY t1.charge/activation
        </if>
        <if test="sortCode == 'bid' ">
            ORDER BY bid
        </if>
        <if test="sortCode == 'activation' ">
            ORDER BY activation
        </if>
        ${sortType}
    </select>

    <update id="updateStatus">
        update
            ctop_etl_kuaishou_group_daily_report
        set unit_status = #{putStatus}
        where account_id = #{accountId}
          and unit_id = #{unitId}
          and stat_date = DATE_FORMAT(now(), '%Y-%m-%d')
    </update>

</mapper>