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

    <select id="getYesterdayUploadNewMaterialCount" resultType="Long">
        SELECT IFNULL(sum(material_num), 0)
        FROM application.`new_material_num_in_project`
        WHERE stat_date = #{yesterday}
          AND media_id = 1
          and project_id in (select id from `jeecg-boot`.ctop_project where agent_code in (1, 2))
    </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 application.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 application.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 application.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 application.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 application.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 application.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 application.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 application.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 project_id
        ) t
    </select>


    <select id="getProjectToDayReport" resultType="com.alibaba.fastjson.JSONObject">
        SELECT * FROM (
        SELECT
        t1.project_id as 'projectId',
        t3.project_name AS 'projectName',
        t2.product_name AS 'productName',
        t1.advertiser_name AS 'companyName',
        t5.realname AS 'saler',
        t4.realname AS 'operater',
        sum( cost ) AS 'cost',
        SUM( `show` ) AS 'aclick',
        SUM( click ) AS 'bclick',
        IFNULL( t6.num, 0 ) AS 'num',
        IFNULL(concat(round((sum(cost) - t7.costs) / t7.costs * 100, 2), '%'), '-') as 'rio',
        IFNULL(t8.unitNum,0) as 'unitNum',
        '-' AS 'ssAd',
        '-' AS 'sxhAd',
        '-' AS 'lmAd',
        '-' AS 'xxlAd',
        '-' AS 'jlAd',
        '-' AS 'kpAd',
        IFNULL(t9.accountNum,0) as 'accountNum',
        IFNULL(t9.operaterNum,0) as 'operaterNum'
        FROM application.bytedance_advertiser_report_hourly_dw t1
        LEFT JOIN `jeecg-boot`.ctop_product t2 ON t2.id = t1.product_id
        LEFT JOIN `jeecg-boot`.ctop_project t3 ON t1.project_id = t3.id
        LEFT JOIN `jeecg-boot`.sys_user t4 ON t3.responsible_id = t4.id
        LEFT JOIN `jeecg-boot`.sys_user t5 ON t3.sale_id = t5.id
        LEFT JOIN (
        SELECT project_id,sum(material_num) as 'num' FROM application.new_material_num_in_project
        WHERE stat_date = DATE_FORMAT(DATE_SUB(#{startTime}, INTERVAL 1 DAY ), '%Y%m%d')
        AND media_id = 1
        <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
        LEFT JOIN (
        SELECT project_id, SUM(cost) as 'costs'
        FROM application.bytedance_advertiser_report_hourly_dw
        WHERE stat_datetime = DATE_FORMAT(DATE_SUB(#{startTime}, 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>
        ) t7 ON t1.project_id = t7.project_id
        LEFT JOIN (
        SELECT project_id , SUM(new_ad_num) as 'unitNum' FROM application.app_bytedance_account_base_info_d
        WHERE stat_date =DATE_FORMAT(DATE_SUB(#{startTime}, INTERVAL 1 DAY ), '%Y%m%d')
        AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b' , 'd57fecdcf7a94d009736d9c850731582')
        GROUP BY project_id
        ) t8 ON t1.project_id = t8.project_id
        LEFT JOIN(
        SELECT project_id,count(account_id)as 'accountNum',count(DISTINCT user_id)as 'operaterNum' FROM (
        SELECT project_id,account_id,user_id FROM application.bytedance_advertiser_report_daily_dw
        WHERE stat_datetime =DATE_FORMAT(DATE_SUB(#{startTime}, INTERVAL 1 DAY ), '%Y%m%d')
        GROUP BY project_id,account_id
        HAVING sum(cost) > 0
        )t
        GROUP BY project_id
        ) t9 ON t1.project_id = t9.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>
            GROUP BY t1.project_id
            HAVING sum(cost) > 0
            ORDER BY ${sortCode} ${order}
        ) t
        <if test="category == 1">
            WHERE rio &lt;= -0.1 AND rio !='-'
        </if>
    </select>


    <select id="getProjectMoreDaysReportTotal" resultType="Long">
        SELECT IFNULL(sum(1), 0)
        FROM (
        SELECT 1
        FROM application.bytedance_advertiser_report_daily_dw
        WHERE stat_datetime >= #{startTime}
        AND stat_datetime &lt;= #{endTime}
        AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b' , 'd57fecdcf7a94d009736d9c850731582')
        <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
        t1.project_id as 'projectId',
        t3.project_name AS 'projectName',
        t2.product_name AS 'productName',
        t1.advertiser_name AS 'companyName',
        t5.realname AS 'saler',
        t4.realname AS 'operater',
        sum( cost ) AS 'cost',
        SUM( `show` ) AS 'aclick',
        SUM( click ) AS 'bclick',
        IFNULL(t6.num, 0 ) AS 'num',
        '-' as 'rio',
        IFNULL(t8.unitNum,0) as 'unitNum',
        '-' AS 'ssAd',
        '-' AS 'sxhAd',
        '-' AS 'lmAd',
        '-' AS 'xxlAd',
        '-' AS 'jlAd',
        '-' AS 'kpAd',
        IFNULL(t9.accountNum,0) as 'accountNum',
        IFNULL(t9.operaterNum,0) as 'operaterNum'
        FROM application.bytedance_advertiser_report_daily_dw t1
        LEFT JOIN `jeecg-boot`.ctop_product t2 ON t2.id = t1.product_id
        LEFT JOIN `jeecg-boot`.ctop_project t3 ON t1.project_id = t3.id
        LEFT JOIN `jeecg-boot`.sys_user t4 ON t3.responsible_id = t4.id
        LEFT JOIN `jeecg-boot`.sys_user t5 ON t3.sale_id = t5.id
        LEFT JOIN (
        SELECT project_id,sum(material_num) as 'num' FROM application.new_material_num_in_project
        WHERE stat_date >= #{startTime}
        AND stat_date &lt;= #{endTime}
        AND media_id = 1
        <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
        LEFT JOIN (
        SELECT project_id , SUM(new_ad_num) as 'unitNum' FROM application.app_bytedance_account_base_info_d
        WHERE stat_date >= #{startTime}
        AND stat_date &lt;= #{endTime}
        AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b' , 'd57fecdcf7a94d009736d9c850731582')
        GROUP BY project_id
        ) t8 ON t1.project_id = t8.project_id
        LEFT JOIN(
            SELECT project_id,count(account_id)as 'accountNum',count(DISTINCT user_id)as 'operaterNum' FROM (
        SELECT project_id,account_id,user_id FROM application.bytedance_advertiser_report_daily_dw
        WHERE stat_datetime >= #{startTime}
        AND stat_datetime &lt;= #{endTime}
        GROUP BY project_id,account_id
        HAVING sum(cost) > 0
        )t
        GROUP BY project_id
        ) t9 ON t1.project_id = t9.project_id
        WHERE stat_datetime >= #{startTime}
        AND stat_datetime &lt;= #{endTime}
        AND t1.company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b' , 'd57fecdcf7a94d009736d9c850731582')
        <if test="projectId != null and projectId != ''">
            AND t1.project_id = #{projectId}
        </if>
            GROUP BY t1.project_id
            HAVING sum(cost) > 0
            ORDER BY ${sortCode} ${order}
    </select>


    <select id="getProjectNoByDays" resultType="com.alibaba.fastjson.JSONObject">
        SELECT stat_datetime as 'time',COUNT(1) as 'count'
        FROM (SELECT stat_datetime, project_id
              FROM application.bytedance_advertiser_report_daily_dw
              WHERE stat_datetime >= #{startTime}
                AND stat_datetime &lt;= #{endTime}
              GROUP BY stat_datetime, project_id
              HAVING sum(cost) >= 10
             ) t
        GROUP BY stat_datetime
        ORDER BY stat_datetime
    </select>

    <select id="getAdvertiserNoByDays" resultType="com.alibaba.fastjson.JSONObject">
        SELECT stat_datetime as 'time',COUNT(1) as 'count'
        FROM (SELECT stat_datetime, advertiser_id
              FROM application.bytedance_advertiser_report_daily_dw
              WHERE stat_datetime >= #{startTime}
                AND stat_datetime &lt;= #{endTime}
              GROUP BY stat_datetime, advertiser_id
              HAVING sum(cost) >= 10
             ) t
        GROUP BY stat_datetime
        ORDER BY stat_datetime
    </select>

    <select id="getUnitNoByHours" resultType="com.alibaba.fastjson.JSONObject">
        SELECT `hour`as 'time'  , SUM(material_upload_media_num) as 'count'
        FROM application.today_material_upload_num
        WHERE type_id = 1
          and stat_date = #{startTime}
          AND dimension in (
            SELECT id
            FROM `jeecg-boot`.ctop_project
            WHERE media_id = 1
        )
        GROUP BY hour
        ORDER BY hour
    </select>

    <select id="getUnitNoByDays" resultType="com.alibaba.fastjson.JSONObject">
        SELECT stat_date as 'time',sum(material_num) as 'count'
        FROM application.new_material_num_in_project
        WHERE stat_date >= #{startTime}
          AND stat_date &lt;= #{endTime}
          AND media_id = 1
        GROUP BY stat_date
        ORDER BY stat_date
    </select>

</mapper>