<?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="org.jeecg.modules.bytedance.report.mapper.BytedanceAdvertiserHourlyReportMapper">



    <!-- 时段 查询  账户时报数据信息 -->
    <select id="getReportCostInfo" resultType="org.jeecg.modules.bytedance.report.entity.vo.ReportCostVo">
        SELECT
            id,
            cost,
            show_num,
            click,
            convert_num,
            stat_hour,
            stat_datetime,
            stat_hour AS time,
            create_time
        FROM
            ctop_bytedance_report_advertiser_hourly
        WHERE
            advertiser_id = #{accountId}
            AND stat_datetime &gt;= #{startTime}
            AND stat_datetime &lt;= #{endTime}
        ORDER BY
            stat_datetime,
            stat_hour




    </select>

    <!-- 查询  账户时报 数据信息 -->
    <select id="getReportCostInfoStage" resultType="org.jeecg.modules.bytedance.report.entity.vo.ReportCostVo">
        SELECT
            id,
            SUM(cost) AS cost,
            SUM(show_num) AS showNum,
            SUM(click) AS click,
            SUM(convert_num) AS convertNum,
            stat_datetime,
            stat_hour AS time,
            create_time
        FROM
            ctop_bytedance_report_advertiser_hourly
        WHERE 1 = 1
        <if test="accountId !=null and accountId != ''">
            AND advertiser_id = #{accountId}
        </if>
        <if test="startTime !=null and startTime != ''">
            AND stat_datetime &gt;= #{startTime}
        </if>
        <if test="endTime !=null and endTime != ''">
            AND stat_datetime &lt;= #{endTime}
        </if>
        GROUP BY
            stat_hour
        ORDER BY
            stat_datetime,
            stat_hour
    </select>

    <!-- 查询  账户时报  时间点截至 数据信息 -->
    <select id="getReportCostInfoStageLast" resultType="org.jeecg.modules.bytedance.report.entity.vo.ReportCostVo">
        SELECT
            id,
            SUM(cost) AS cost,
            SUM(show_num) AS showNum,
            SUM(click) AS click,
            SUM(convert_num) AS convertNum,
            stat_datetime,
            stat_hour AS time,
            create_time
        FROM
            ctop_bytedance_report_advertiser_hourly
        WHERE
        1 = 1
        <if test="accountId !=null and accountId != ''">
            AND advertiser_id = #{accountId}
        </if>
        <if test="startTime !=null and startTime != ''">
            AND stat_datetime &gt;= #{startTime}
        </if>
        <if test="endTime !=null and endTime != ''">
            AND stat_datetime &lt;= #{endTime}
        </if>
        <if test="statHour !=null and statHour != ''">
            AND stat_hour &lt;= #{statHour}
        </if>
        GROUP BY
            stat_hour
        ORDER BY
            stat_datetime,
            stat_hour
    </select>

<!-- 查询用户下 花费最高的 账户-->
    <select id="selAcountIdCostMax" resultType="java.util.HashMap">
        SELECT
            cah.advertiser_id AS accountId,
            SUM( cah.cost ) cost,
            cua.auth_name AS authName,
            cah.create_time
        FROM
            ctop_bytedance_report_advertiser_hourly cah
        LEFT JOIN ctop_user_allocation cua ON cah.advertiser_id = cua.account_id
        WHERE
            1 = 1
        <if test="type != null and type !=''  and type !='bytedance'.toString()">
              AND ( cua.media_id = '1' OR cua.media_id = '3' )
        </if>
        <if test="userId !=null and userId != ''">
              AND cua.user_id = #{userId}
        </if>
        GROUP BY
            cah.advertiser_id
        ORDER BY
            cost DESC
        LIMIT 1
    </select>

<!-- 查询账户花费-->
    <select id="selectAccountCost" resultType="java.util.HashMap">
        SELECT
            cua.account_status AS accountStatus,
            cua.auth_name AS authName,
            rda.cost AS cost,
            rda.valid_balance AS validBalance,
            cua.account_id AS accountId
        FROM
            ctop_user_allocation cua
                LEFT JOIN ctop_rule_data_account rda ON cua.account_id = rda.account_id
        WHERE
            1 = 1
          AND cua.account_id = #{accountId}
    </select>


<!-- 查询 计划时报 数据-->
    <select id="selectReportPlanCostInfo" resultType="org.jeecg.modules.bytedance.report.entity.vo.ReportCostVo">
        SELECT
        stat_datetime,
        sum(show_num) AS showNum,
        sum(click) AS click,
        sum(convert_num) AS convertNum,
        sum(cost) AS cost,
        ad_id,
        ad_name,
        create_time,
        update_time,
        MAX(stat_hour) as statHour
        FROM
        ctop_bytedance_report_plan_hourly
        where 1 =1
        <if test="accountId !=null and accountId != ''">
            and advertiser_id = #{accountId}
        </if>
        <if test="planId !=null and planId != ''">
            and ad_id = #{planId}
        </if>
        <if test="startTime !=null and startTime != ''">
            and DATE_FORMAT(stat_datetime,'%Y-%m-%d') &gt;= #{startTime}
        </if>
        <if test="endTime !=null and endTime != ''">
            and DATE_FORMAT(stat_datetime,'%Y-%m-%d') &lt;= #{endTime}
        </if>
         GROUP BY ad_id
        ORDER BY cost DESC

    </select>
<!-- 查询  上阶段 计划时报 数据-->
    <select id="selectReportPlanCostHourLast" resultType="org.jeecg.modules.bytedance.report.entity.vo.ReportCostVo">
        SELECT
        stat_datetime,
        sum(cost) AS cost,
        ad_id,
        ad_name,
        create_time,
        update_time
        FROM
        ctop_bytedance_report_plan_hourly
        where 1 =1
        <if test="accountId !=null and accountId != ''">
            and advertiser_id = #{accountId}
        </if>
        <if test="planId !=null and planId != ''">
            and ad_id = #{planId}
        </if>
        <if test="startTime !=null and startTime != ''">
            and DATE_FORMAT(stat_datetime,'%Y-%m-%d') &gt;= #{startTime}
        </if>
        <if test="endTime !=null and endTime != ''">
            and DATE_FORMAT(stat_datetime,'%Y-%m-%d') &lt;= #{endTime}
        </if>
        <if test="statHour !=null and statHour != ''">
            and stat_hour &lt;= #{statHour}
        </if>
         GROUP BY ad_id
        ORDER BY cost DESC

    </select>

<!-- 查询 计划日报 数据-->
    <select id="selectReportPlanCostInfoDay" resultType="org.jeecg.modules.bytedance.report.entity.vo.ReportCostVo">
        SELECT
        stat_datetime,
        sum(show_num) AS showNum,
        sum(click) AS click,
        sum(convert_num) AS convertNum,
        sum(cost) AS cost,
        ad_id,
        ad_name,
        create_time,
        update_time
        FROM
        ctop_bytedance_report_plan_daily
        where 1 =1
        <if test="accountId !=null and accountId != ''">
            and advertiser_id = #{accountId}
        </if>
        <if test="planId !=null and planId != ''">
            and ad_id = #{planId}
        </if>
        <if test="startTime !=null and startTime != ''">
            and DATE_FORMAT(stat_datetime,'%Y-%m-%d') &gt;= #{startTime}
        </if>
        <if test="endTime !=null and endTime != ''">
            and DATE_FORMAT(stat_datetime,'%Y-%m-%d') &lt;= #{endTime}
        </if>
        group BY ad_id
        ORDER BY cost DESC

    </select>



    <!-- 查询 账户日报 数据 数据-->
    <select id="selectPlanCostInfoCount" resultType="org.jeecg.modules.bytedance.report.entity.vo.ReportCostVo">
        SELECT
        stat_datetime,
        sum(show_num) AS showNum,
        sum(click) AS click,
        sum(convert_num) AS convertNum,
        sum(cost) AS cost,
        DATE_FORMAT(stat_datetime,'%Y-%m-%d') AS time,
        create_time,
        update_time
        FROM
        ctop_bytedance_report_advertiser_daily
        where 1 =1
        <if test="accountId !=null and accountId != ''">
            and advertiser_id = #{accountId}
        </if>
        <if test="planId !=null and planId != ''">
            and ad_id = #{planId}
        </if>
        <if test="startTime !=null and startTime != ''">
            and DATE_FORMAT(stat_datetime,'%Y-%m-%d') &gt;= #{startTime}
        </if>
        <if test="endTime !=null and endTime != ''">
            and DATE_FORMAT(stat_datetime,'%Y-%m-%d') &lt;= #{endTime}
        </if>
        GROUP BY stat_datetime
        ORDER BY cost DESC

    </select>








</mapper>
