<?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.KuaishouReportHourlyAccountMapper">
    <insert id="replaceBatch">
        replace into ctop_kuaishou_report_hourly_account(
        id,
        account_id,
        charge,
        photo_show,
        aclick,
        bclick,
        share,
        comment,
        photo_like,
        follow,
        cancel_follow,
        report,
        block,
        negative,
        download_started,
        download_completed,
        activation,
        submit,
        stat_date,
        stat_hour,
        photo_click,
        photo_click_ratio,
        action_ratio,
        impression_1k_cost,
        photo_click_cost,
        action_cost,
        event_pay_first_day,
        event_pay_purchase_amount_first_day,
        event_pay_first_day_roi,
        event_pay,
        event_pay_purchase_amount,
        event_pay_roi,
        event_register,
        event_register_cost,
        event_jin_jian_app,
        event_jin_jian_app_cost,
        event_credit_grant_app,
        event_credit_grant_app_cost,
        event_credit_grant_app_ratio,
        event_order_paid,
        event_order_paid_purchase_amount,
        event_jin_jian_landing_page,
        event_jin_jian_landing_page_cost,
        event_credit_grant_landing_page,
        event_credit_grant_landing_page_cost,
        form_action_ratio,
        event_order_paid_cost,
        event_next_day_stay,
        form_cost,
        form_count,
        play_3s_ratio,
        play_5s_ratio,
        play_end_ratio,
        event_app_invoked,
        event_new_user_pay,
        event_new_user_pay_cost,
        event_new_user_pay_ratio
        )
        values
        <foreach collection="addList" item="add" separator=",">
            (
            #{add.id},
            #{add.accountId},
            #{add.charge},
            #{add.photoShow},
            #{add.aclick},
            #{add.bclick},
            #{add.share},
            #{add.comment},
            #{add.photoLike},
            #{add.follow},
            #{add.cancelFollow},
            #{add.report},
            #{add.block},
            #{add.negative},
            #{add.downloadStarted},
            #{add.downloadCompleted},
            #{add.activation},
            #{add.submit},
            #{add.statDate},
            #{add.statHour},
            #{add.photoClick},
            #{add.photoClickRatio},
            #{add.actionRatio},
            #{add.impression1kCost},
            #{add.photoClickCost},
            #{add.actionCost},
            #{add.eventPayFirstDay},
            #{add.eventPayPurchaseAmountFirstDay},
            #{add.eventPayFirstDayRoi},
            #{add.eventPay},
            #{add.eventPayPurchaseAmount},
            #{add.eventPayRoi},
            #{add.eventRegister},
            #{add.eventRegisterCost},
            #{add.eventJinJianApp},
            #{add.eventJinJianAppCost},
            #{add.eventCreditGrantApp},
            #{add.eventCreditGrantAppCost},
            #{add.eventCreditGrantAppRatio},
            #{add.eventOrderPaid},
            #{add.eventOrderPaidPurchaseAmount},
            #{add.eventJinJianLandingPage},
            #{add.eventJinJianLandingPageCost},
            #{add.eventCreditGrantLandingPage},
            #{add.eventCreditGrantLandingPageCost},
            #{add.formActionRatio},
            #{add.eventOrderPaidCost},
            #{add.eventNextDayStay},
            #{add.formCost},
            #{add.formCount},
            #{add.play3sRatio},
            #{add.play5sRatio},
            #{add.playEndRatio},
            #{add.eventAppInvoked},
            #{add.eventNewUserPay},
            #{add.eventNewUserPayCost},
            #{add.eventNewUserPayRatio}
            )
        </foreach>

    </insert>

    <select id="selectMaxHour" resultType="java.lang.Integer">
        select max(stat_hour)
        from ctop_kuaishou_report_hourly_account
        where account_id = #{accountId}
          and stat_date = #{statDate}

    </select>

    <select id="getCost" resultType="java.math.BigDecimal">
        select charge
        from ctop_kuaishou_report_hourly_account
        where account_id = #{accountId}
          and stat_date = #{statDate}
          and stat_hour = #{statHour}

    </select>

    <select id="queryEveryhourlyDataForAccount"
            resultType="cn.com.ctop.kuaishou.modules.report.entity.EtlKuaishouReportAccountHourly">
        SELECT stat_date,
               account_id,
               DATE_FORMAT(now(), '%H') as 'statHour',
               SUM(charge) as 'charge',
               SUM(photo_show) as 'photoShow',
               SUM(photo_click) as 'photoClick',
               SUM(aclick) as 'aclick',
               SUM(bclick) as 'bclick',
               SUM(share) as 'photoShare',
               SUM(comment) as 'photoComment',
               SUM(photo_like) as 'photoLike',
               SUM(follow) as 'follow',
               SUM(cancel_follow) as 'cancelFollow',
               SUM(report) as 'report',
               SUM(block) as 'block',
               SUM(negative) as 'negative',
               SUM(download_started) as 'downloadStarted',
               SUM(download_completed) as 'downloadCompleted',
               SUM(activation) as 'activation',
               SUM(submit) as 'submit',
               SUM(event_pay_first_day) as 'eventPayFirstDay',
               SUM(event_pay_purchase_amount_first_day) as 'eventPayPurchaseAmountFirstDay',
               SUM(event_pay) as 'eventPay',
               SUM(event_pay_purchase_amount) as 'eventPayPurchaseAmount',
               SUM(event_register) as 'eventRegister',
               SUM(event_jin_jian_app) as 'eventJinJianApp',
               SUM(event_credit_grant_app) as 'eventCreditGrantApp',
               SUM(event_order_paid) as 'eventOrderPaid',
               SUM(event_order_paid_purchase_amount) as 'eventOrderPaidPurchaseAmount',
               SUM(event_jin_jian_landing_page) as 'eventJinJianLandingPage',
               SUM(event_credit_grant_landing_page) as 'eventCreditGrantLandingPage',
               SUM(event_next_day_stay) as 'eventNextDayStay',
               SUM(form_count) as 'formCount',
               SUM(play_3s_ratio * aclick) as 'play3sCount'
        FROM ctop_kuaishou_report_hourly_account
        WHERE stat_date = DATE_FORMAT(now(), '%Y-%m-%d')
          AND stat_hour &lt;= DATE_FORMAT(now(), '%H')
        GROUP BY account_id
    </select>


    <select id="queryPersonalAchievements" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        IFNULL((SELECT realname from sys_user WHERE id = #{userId}),'') as 'realName',
        IFNULL(SUM(charge),0) 'charge',
        IFNULL(ROUND((SUM(charge)-t3.zcharge)/t3.zcharge*100,2),0) 'grew',
        IFNULL(t4.weekCharge,0) 'weekCharge',
        IFNULL(t5.lweekCharge,0) 'lweekCharge',
        IFNULL(ROUND((t4.weekCharge-t5.lweekCharge)/t5.lweekCharge*100,2),0) 'weekGrew',
        IFNULL((SELECT count(1) FROM ctop_user_allocation where user_id = #{userId} AND account_status = 0),0) 'accouuntCount'
        FROM ctop_kuaishou_report_hourly_account t1
        LEFT JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
        LEFT JOIN(
        SELECT SUM(charge) 'zcharge' FROM ctop_kuaishou_report_hourly_account i1
        LEFT JOIN ctop_user_allocation i2 ON i1.account_id = i2.account_id
        WHERE i2.user_id = #{userId}
        AND i1.stat_date = DATE_SUB(#{today},INTERVAL 1 DAY)
        AND i1.stat_hour  &lt;= DATE_FORMAT(now(), '%H')
        ) t3 ON 1=1
        LEFT JOIN(
        SELECT SUM(charge) 'weekCharge' FROM ctop_etl_kuaishou_report_account_daily i1
        LEFT JOIN ctop_user_allocation i2 ON i1.account_id = i2.account_id
        WHERE i2.user_id = #{userId}
        AND i1.stat_date &gt;= #{startDate}
        AND i1.stat_date  &lt;= #{today}
        ) t4 ON 1=1
        LEFT JOIN(
        SELECT SUM(charge) 'lweekCharge' FROM ctop_etl_kuaishou_report_account_daily i1
        LEFT JOIN ctop_user_allocation i2 ON i1.account_id = i2.account_id
        WHERE i2.user_id = #{userId}
        AND i1.stat_date &gt;= #{lastStartDate}
        AND i1.stat_date  &lt;= #{lastEndDate}
        ) t5 ON 1=1
        WHERE t2.user_id = #{userId}
        AND t1.stat_date = #{today}
        AND t1.stat_hour &lt;= DATE_FORMAT(now(), '%H')
    </select>

</mapper>