<?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.toutiao.modules.report.mapper.BytedanceAccountReportMapper">

    <select id="queryTodaySumReportBy" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
          ROUND(sum(cost),2) cost,
          sum(show_num) showNum,
          sum(click) click,
          sum(convert_num) as convertNum,
          max(stat_hour) maxHour
        FROM
          ctop_bytedance_report_advertiser_hourly
        WHERE
          stat_datetime = #{statDate}
        AND advertiser_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="queryTodaySumByHourGroupAccount" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
          t2.account_id as accountId,
          t2.auth_name as authName,
          ${filed}
        FROM ctop_bytedance_report_advertiser_hourly t1
        left join ctop_user_allocation t2 on t1.advertiser_id=t2.account_id
        WHERE t1.stat_datetime = #{statDate}
          and t1.stat_hour &lt;= #{hour}
        AND t1.advertiser_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        group by t1.advertiser_id
        order by ${target} ${order}
    </select>

    <select id="queryTodaySumByHour" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
          ROUND(sum(cost),2) cost,
          sum(show_num) showNum,
          sum(click) click,
          sum(convert_num) as convertNum
        FROM
          ctop_bytedance_report_advertiser_hourly
        WHERE
          stat_datetime = #{statDate}
        and stat_hour &lt;= #{hour}
        AND advertiser_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="querySumByHour" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
          ${filed}
        FROM
          ctop_bytedance_report_advertiser_hourly t1
        WHERE
          t1.stat_datetime = #{statDate}
        and t1.stat_hour &lt;= #{hour}
        AND t1.advertiser_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="queryTodayDetailReportBy" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
          ROUND(sum(cost),2) cost,
          sum(show_num) showNum,
          sum(click) click,
          sum(convert_num) as convertNum,
          stat_hour as statHour
        FROM
            ctop_bytedance_report_advertiser_hourly
        WHERE
            stat_datetime = #{statDate}
        AND advertiser_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        GROUP BY
            stat_hour
        ORDER BY
            stat_hour ASC
    </select>

    <select id="queryAllSumByStartEndDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
          ${filed}
        FROM ctop_bytedance_report_advertiser_daily t1
        WHERE t1.stat_datetime &lt;= #{endDate} '00:00:00'
        AND t1.stat_datetime &gt;= #{startDate}
        AND t1.advertiser_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="querySumByStartEndDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT ROUND(sum(cost), 2) cost,
        sum(show_num)       showNum,
        sum(click)          click,
        sum(convert_num) as convertNum
        FROM ctop_bytedance_report_advertiser_daily t1
        WHERE t1.stat_datetime &lt;= #{endDate} '00:00:00'
        AND t1.stat_datetime &gt;= #{startDate}
        AND advertiser_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="querySumByDateGroupAccount" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
          t2.account_id as accountId,
          t2.auth_name as authName,
          ${filed}
        FROM
        ctop_bytedance_report_advertiser_daily t1
        left join ctop_user_allocation t2 on t1.advertiser_id=t2.account_id
        WHERE
            t1.stat_datetime &lt;= #{endDate} '00:00:00'
        AND t1.stat_datetime &gt;= #{startDate}
        AND t1.advertiser_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        group by t1.advertiser_id
        order by ${target} ${order}
    </select>

    <select id="queryDetailByStartEndDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
          ROUND(sum(cost),2) cost,
          sum(show_num) showNum,
          sum(click) click,
          sum(convert_num) convertNum,
          DATE_FORMAT(stat_datetime, '%Y-%m-%d') statDate
        FROM
           ctop_bytedance_report_advertiser_daily
        WHERE stat_datetime &lt;= #{endDate} '00:00:00'
          AND stat_datetime &gt;= #{startDate}
          AND advertiser_id in
          <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
          </foreach>
        group by stat_datetime
        order by stat_datetime asc
    </select>

    <select id="queryDetailGroupMonthByDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
          ROUND(sum(cost),2) cost,
          sum(show_num) showNum,
          sum(click) click,
          sum(convert_num) convertNum,
          DATE_FORMAT(t1.stat_datetime, '%Y-%m') statDate
        FROM ctop_bytedance_report_advertiser_daily t1
        WHERE t1.stat_datetime &lt;= #{endDate} '00:00:00'
          AND t1.stat_datetime &gt;= #{startDate}
          AND t1.advertiser_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        group by DATE_FORMAT(t1.stat_datetime, '%Y-%m')
        order by t1.stat_datetime asc
    </select>

    <select id="queryDetailGroupDayByMonth" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
               DATE_FORMAT(stat_datetime, '%Y-%m-%d')                      statDate,
               SUBSTR(DATE_FORMAT(stat_datetime, '%Y-%m-%d') FROM 9 FOR 2) day
        FROM ctop_bytedance_report_advertiser_daily
        WHERE stat_datetime like '${month}%'
        group by DATE_FORMAT(stat_datetime, '%Y-%m-%d')
        order by stat_datetime asc
    </select>

</mapper>
