<?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.report.mapper.BytedanceAccountReportDailyDwMapper">

    <select id="getReportByParams" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        stat_datetime as statDate,
        account_id as accountId,
        account_name as authName,
        ${filedAll}
        FROM
        bytedance_advertiser_report_daily_dw
        WHERE
        stat_datetime &lt;= #{endDate}
        AND stat_datetime &gt;= #{startDate}
        AND account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="hasGroupBy == 1">
            group by ${groupBy}
            order by ${target} ${order}
        </if>
    </select>

    <select id="getYesterdayUploadNewMaterialCount" resultType="Long">
        SELECT IFNULL(sum(1), 0)
        FROM `app_bytedance_material_info`
        WHERE media_time = DATE_FORMAT(DATE_SUB(#{lastEndDate}, INTERVAL 1 DAY ), '%Y%m%d')
          AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b', 'd57fecdcf7a94d009736d9c850731582')
    </select>


    <select id="getTotalCharge" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t1.todaycost, t2.yesterdaycost, t3.thisWeekcost, t4.lastWeek1 + t5.lastWeek2 as 'lastWeekcost'
        FROM (
                 SELECT IFNULL(sum(cost), 0.00) as 'todaycost'
                 FROM bytedance_advertiser_report_hourly_dw
                 WHERE stat_datetime = #{today}
                   AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b', 'd57fecdcf7a94d009736d9c850731582')
             ) t1
                 LEFT JOIN
             (
                 SELECT IFNULL(sum(cost), 0.00) as 'yesterdaycost'
                 FROM bytedance_advertiser_report_hourly_dw
                 WHERE stat_datetime = #{yesterday}
                           AND hour &lt;= #{hour}
                   AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b', 'd57fecdcf7a94d009736d9c850731582')
             ) t2 ON 1 = 1
                 LEFT JOIN
             (
                 SELECT IFNULL(sum(cost), 0.00) as 'thisWeekcost'
                 FROM bytedance_advertiser_report_hourly_dw
                 WHERE stat_datetime >= #{monday}
                   AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b', 'd57fecdcf7a94d009736d9c850731582')
                   AND stat_datetime &lt;= #{today}
             ) t3 ON 1 = 1
                 LEFT JOIN
             (
                 SELECT IF(#{lastStartDate} - #{lastEndDate} = 0, 0.00, sum(cost)) as 'lastWeek1'
                 FROM bytedance_advertiser_report_daily_dw
                 WHERE stat_datetime >= #{lastStartDate}
                   AND stat_datetime &lt;= DATE_FORMAT(DATE_SUB(#{lastEndDate}, INTERVAL 1 DAY ), '%Y%m%d')
                   AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b', 'd57fecdcf7a94d009736d9c850731582')
             ) t4 ON 1 = 1
                 LEFT JOIN
             (
                 SELECT IFNULL(sum(cost), 0.00) as 'lastWeek2'
                 FROM bytedance_advertiser_report_hourly_dw
                 WHERE stat_datetime = #{lastEndDate}
                           AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b', 'd57fecdcf7a94d009736d9c850731582')
                           AND hour &lt;= #{hour}
             ) t5 ON 1 = 1
    </select>

    <select id="getHourCost" resultType="com.alibaba.fastjson.JSONObject">
        SELECT hour as 'time', IFNULL(SUM(cost), 0.00) as 'cost'
        FROM bytedance_advertiser_report_hourly_dw
        WHERE stat_datetime = #{startTime}
        AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b' , 'd57fecdcf7a94d009736d9c850731582')
        <if test="projectId != null and projectId != ''">
            AND project_id = #{projectId}
        </if>
        GROUP BY hour
        ORDER BY hour
    </select>

    <select id="getDataCost" resultType="com.alibaba.fastjson.JSONObject">
        SELECT DATE_FORMAT(stat_datetime, '%Y-%m-%d') as 'time', IFNULL(sum(cost), 0.00) as 'cost'
        FROM bytedance_advertiser_report_hourly_dw
        WHERE stat_datetime &lt;= #{endTime}
        AND stat_datetime >= #{startTime}
        AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b', 'd57fecdcf7a94d009736d9c850731582')
        <if test="projectId != null and projectId != ''">
            AND project_id = #{projectId}
        </if>
        GROUP BY stat_datetime
        ORDER BY stat_datetime
    </select>


    <select id="getProjectToDayReportTotal" resultType="Long">
        SELECT IFNULL(sum(1), 0)
        FROM (
        SELECT 1
        FROM bytedance_advertiser_report_hourly_dw
        WHERE stat_datetime = #{startTime}
        <if test="projectId != null and projectId != ''">
            AND project_id = #{projectId}
        </if>
        GROUP BY project_id
        ) t
    </select>


    <select id="getProjectToDayReport" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        project_name AS 'projectName',
        t2.product_name AS 'productName',
        company_name AS 'companyName',
        t3.realname AS 'saler',
        t4.realname AS 'operater',
        sum( cost ) AS 'cost',
        SUM( `show` ) AS 'aclick',
        SUM( click ) AS 'bclick',
        IFNULL( t5.num, 0 ) AS 'num',
        IFNULL(concat(round((sum(cost) - t6.costs) / t6.costs * 100, 2), '%'), '-') as 'rio'
        FROM bytedance_advertiser_report_hourly_dw t1
        LEFT JOIN `jeecg-boot`.ctop_product t2 ON t2.id = t1.product_id
        LEFT JOIN `jeecg-boot`.sys_user t3 ON t1.sale_id = t3.id
        LEFT JOIN `jeecg-boot`.sys_user t4 ON t1.user_id = t4.id
        LEFT JOIN (
        SELECT tt2.project_id, sum(1) as 'num'
        FROM app_bytedance_material_info tt1
        LEFT JOIN bytedance_project_material_relation tt2 ON tt1.material_id = tt2.signature
        WHERE media_time = DATE_FORMAT(DATE_SUB(#{startTime}, INTERVAL 1 DAY ), '%Y%m%d')
        AND tt1.company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b' , 'd57fecdcf7a94d009736d9c850731582')
        <if test="projectId != null and projectId != ''">
            AND project_id = #{projectId}
        </if>
        <if test="projectId == null">
            GROUP BY project_id
        </if>
        ) t5 ON t1.project_id = t5.project_id
        LEFT JOIN (
        SELECT project_id, SUM(cost) as 'costs'
        FROM bytedance_advertiser_report_hourly_dw
        WHERE stat_datetime = DATE_FORMAT(DATE_SUB(20220804, INTERVAL 1 DAY ), '%Y%m%d')
        AND hour &lt;= #{hour}
        AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b' , 'd57fecdcf7a94d009736d9c850731582')
        <if test="projectId != null and projectId != ''">
            AND project_id = #{projectId}
        </if>
        <if test="projectId == null">
            GROUP BY project_id
        </if>
        ) t6 ON t1.project_id = t6.project_id
        WHERE stat_datetime = #{startTime}
        AND t1.company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b' , 'd57fecdcf7a94d009736d9c850731582')
        <if test="projectId != null and projectId != ''">
            AND t1.project_id = #{projectId}
        </if>
        <if test="projectId == null">
            GROUP BY t1.project_id
            ORDER BY sum(cost) desc
        </if>
    </select>


    <select id="getProjectMoreDaysReportTotal" resultType="Long">
        SELECT IFNULL(sum(1), 0)
        FROM (
        SELECT 1
        FROM bytedance_advertiser_report_daily_dw
        WHERE stat_datetime >= #{startTime}
        AND stat_datetime &lt;= #{endTime}
        <if test="projectId != null and projectId != ''">
            AND project_id = #{projectId}
        </if>
        GROUP BY project_id
        ) t
    </select>

    <select id="getProjectMoreDaysReport" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        project_name AS 'projectName',
        t2.product_name AS 'productName',
        company_name AS 'companyName',
        t3.realname AS 'saler',
        t4.realname AS 'operater',
        sum( cost ) AS 'cost',
        SUM( `show` ) AS 'aclick',
        SUM( click ) AS 'bclick',
        IFNULL( t5.num, 0 ) AS 'num',
        '-' as 'rio'
        FROM bytedance_advertiser_report_daily_dw t1
        LEFT JOIN `jeecg-boot`.ctop_product t2 ON t2.id = t1.product_id
        LEFT JOIN `jeecg-boot`.sys_user t3 ON t1.sale_id = t3.id
        LEFT JOIN `jeecg-boot`.sys_user t4 ON t1.user_id = t4.id
        LEFT JOIN (
        SELECT tt2.project_id, sum(1) as 'num'
        FROM app_bytedance_material_info tt1
        LEFT JOIN bytedance_project_material_relation tt2 ON tt1.material_id = tt2.signature
        WHERE media_time >= #{startTime}
        AND media_time &lt;= #{endTime}
        AND tt1.company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b' , 'd57fecdcf7a94d009736d9c850731582')
        <if test="projectId != null and projectId != ''">
            AND project_id = #{projectId}
        </if>
        <if test="projectId == null">
            GROUP BY project_id
        </if>
        ) t5 ON t1.project_id = t5.project_id
        WHERE stat_datetime = #{startTime}
        AND t1.company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b' , 'd57fecdcf7a94d009736d9c850731582')
        <if test="projectId != null and projectId != ''">
            AND t1.project_id = #{projectId}
        </if>
        <if test="projectId == null">
            GROUP BY t1.project_id
            ORDER BY sum(cost) desc
        </if>
    </select>


</mapper>