<?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.KuaishouAccountReportDailyDwMapper">

    <select id="getTotalCharge" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t1.todayCharge, t2.yesterdayCharge, t3.thisWeekCharge, t4.lastWeek1 + t5.lastWeek2 as 'lastWeekCharge'
        FROM (
                 SELECT IFNULL(sum(charge), 0.00) as 'todayCharge'
                 FROM application.kuaishou_account_report_hourly_dw
                 WHERE stat_date = #{today}
                   AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b', 'd57fecdcf7a94d009736d9c850731582')
             ) t1
                 LEFT JOIN
             (
                 SELECT IFNULL(sum(charge), 0.00) as 'yesterdayCharge'
                 FROM application.kuaishou_account_report_hourly_dw
                 WHERE stat_date = #{yesterday}
                   AND `hour` &lt;= #{hour}
                   AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b', 'd57fecdcf7a94d009736d9c850731582')
             ) t2
             ON 1 = 1
                 LEFT JOIN
             (
                 SELECT IFNULL(sum(charge), 0.00) as 'thisWeekCharge'
                 FROM application.kuaishou_account_report_hourly_dw
                 WHERE stat_date >= #{monday}
                   AND stat_date &lt;= #{today}
                   AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b', 'd57fecdcf7a94d009736d9c850731582')
             ) t3
             ON 1 = 1
                 LEFT JOIN
             (
                 SELECT IF(#{lastStartDate} - #{lastEndDate} = 0, 0.00, sum(charge)) as 'lastWeek1'
                 FROM application.kuaishou_account_report_daily_dw
                 WHERE stat_date >= #{lastStartDate}
                   AND stat_date &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(charge), 0.00) as 'lastWeek2'
                 FROM application.kuaishou_account_report_hourly_dw
                 WHERE stat_date = #{lastEndDate}
                   AND `hour` &lt;= #{hour}
                   AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b', 'd57fecdcf7a94d009736d9c850731582')
             ) t5 ON 1 = 1
    </select>

    <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 = 2
          and project_id in (select id from `jeecg-boot`.ctop_project where agent_code in (1, 2))
    </select>

    <select id="getHourcharge" resultType="com.alibaba.fastjson.JSONObject">
        SELECT hour as 'time', IFNULL(SUM(charge), 0.00) as 'charge'
        FROM application.kuaishou_account_report_hourly_dw
        WHERE stat_date = #{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="getDatacharge" resultType="com.alibaba.fastjson.JSONObject">
        SELECT DATE_FORMAT(stat_date, '%Y-%m-%d') as 'time', IFNULL(sum(charge), 0.00) as 'charge'
        FROM application.kuaishou_account_report_hourly_dw
        WHERE stat_date &lt;= #{endTime}
        AND stat_date >= #{startTime}
        AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b', 'd57fecdcf7a94d009736d9c850731582')
        <if test="projectId != null and projectId != ''">
            AND project_id = #{projectId}
        </if>
        GROUP BY stat_date
        ORDER BY stat_date
    </select>


    <select id="getProjectToDayReportTotal" resultType="Long">
        SELECT IFNULL(sum(1), 0)
        FROM (
        SELECT 1
        FROM application.kuaishou_account_report_hourly_dw
        WHERE stat_date = #{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(charge) as 'charge',
        SUM(aclick) as 'aclick',
        SUM(bclick) as 'bclick',
        IFNULL(t6.num,0) as 'num',
        IFNULL(concat(round((sum(charge) - t7.charges) / t7.charges * 100, 2), '%'), '-') as 'rio',
        '-' AS 'ssAd',
        '-' AS 'sxhAd',
        '-' AS 'lmAd',
        '-' AS 'xxlAd',
        '-' AS 'jlAd',
        '-' AS 'kpAd',
        IFNULL(t9.accountNum,0) as 'accountNum',
        IFNULL(t9.operaterNum,0) as 'operaterNum',
        IFNULL(t11.new_unit_num,0) as 'newUnitNum',
        IFNULL(t11.new_trush_unit_num,0) as 'newTrushUnitNum',
        IFNULL(t12.new_unit_num,0) as 'yesterdayUnitNum',
        IFNULL(t12.new_trush_unit_num,0) as 'yesterdayTrushUnitNum',
        IFNULL(t13.material_upload_media_num,0) as 'materialUploadMediaNum'
        FROM application.kuaishou_account_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 =2
        <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(charge) as 'charges'
        FROM application.kuaishou_account_report_hourly_dw
        WHERE stat_date = 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,count(account_id)as 'accountNum',count(DISTINCT user_id)as 'operaterNum' FROM (
        SELECT project_id,account_id,user_id FROM application.kuaishou_account_report_daily_dw
        WHERE stat_date = #{startTime}
        GROUP BY project_id,account_id
        HAVING sum(charge) > 0
        )t
        GROUP BY project_id
        ) t9 ON t1.project_id = t9.project_id
        LEFT JOIN (
        SELECT project_id, SUM(new_unit_num) as 'new_unit_num',SUM(new_trush_unit_num) as 'new_trush_unit_num'
        FROM application.app_kuaishou_account_today_base_info_d
        WHERE stat_date = #{startTime}
        GROUP BY project_id
        )t11 ON t1.project_id = t11.project_id
        LEFT JOIN (
        SELECT project_id,SUM(new_unit_num) as 'new_unit_num', SUM(new_trush_unit_num) as 'new_trush_unit_num'
        FROM application.app_kuaishou_account_today_base_info_d WHERE
        stat_date = DATE_FORMAT(DATE_SUB(#{startTime}, INTERVAL 1 DAY ), '%Y%m%d')
        GROUP BY project_id
        )t12 ON t1.project_id = t12.project_id
        LEFT JOIN
        (SELECT dimension , SUM(material_upload_media_num) as 'material_upload_media_num' FROM
        application.today_material_upload_num
        WHERE type_id =1
        and stat_date = #{startTime}
        GROUP BY dimension
        ) t13 ON t1.project_id = t13.dimension
        WHERE stat_date = #{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(charge) > 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.kuaishou_account_report_daily_dw
        WHERE stat_date >= #{startTime}
        AND stat_date &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 tt.*,
        IFNULL(GROUP_CONCAT(DISTINCT CASE WHEN t9.ad_scene ='搜索广告' THEN t9.sceneCharge END),'-') AS 'ssAd',
        IFNULL(GROUP_CONCAT(DISTINCT CASE WHEN t9.ad_scene ='上下滑大屏广告' THEN t9.sceneCharge END),'-') AS 'sxhAd',
        IFNULL(GROUP_CONCAT(DISTINCT CASE WHEN t9.ad_scene ='联盟' THEN t9.sceneCharge END),'-') AS 'lmAd',
        IFNULL(GROUP_CONCAT(DISTINCT CASE WHEN t9.ad_scene ='信息流广告' THEN t9.sceneCharge END),'-') AS 'xxlAd',
        IFNULL(GROUP_CONCAT(DISTINCT CASE WHEN t9.ad_scene ='激励视频广告' THEN t9.sceneCharge END),'-') AS 'jlAd',
        IFNULL(GROUP_CONCAT(DISTINCT CASE WHEN t9.ad_scene ='开屏广告' THEN t9.sceneCharge END),'-') AS 'kpAd' 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(charge) as 'charge',
        SUM(aclick) as 'aclick',
        SUM(bclick) as 'bclick',
        IFNULL(t6.num,0) as 'num',
        '-' as 'rio',
        IFNULL(t9.accountNum,0) as 'accountNum',
        IFNULL(t9.operaterNum,0) as 'operaterNum',
        IFNULL(t11.new_unit_num,0) as 'unitNum',
        IFNULL(t11.new_trush_unit_num,0) as 'trushUnitNum',
        IFNULL(t13.material_upload_media_num,0) as 'materialUploadMediaNum'
        FROM application.kuaishou_account_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 =2
        <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,count(account_id)as 'accountNum',count(DISTINCT user_id)as 'operaterNum' FROM (
        SELECT project_id,account_id,user_id FROM application.kuaishou_account_report_daily_dw
        WHERE stat_date >= #{startTime}
        AND stat_date &lt;= #{endTime}
        GROUP BY project_id,account_id
        HAVING sum(charge) > 0
        )t
        GROUP BY project_id
        ) t9 ON t1.project_id = t9.project_id
        LEFT JOIN (
        SELECT project_id, SUM(new_unit_num) as 'new_unit_num',SUM(new_trush_unit_num) as 'new_trush_unit_num'
        FROM application.app_kuaishou_account_today_base_info_d
        WHERE stat_date >= #{startTime}
        AND stat_date &lt;= #{endTime}
        GROUP BY project_id
        )t11 ON t1.project_id = t11.project_id
        LEFT JOIN
        (SELECT dimension , SUM(material_upload_media_num) as 'material_upload_media_num' FROM
        application.today_material_upload_num
        WHERE type_id =1
        AND stat_date >= #{startTime}
        AND stat_date &lt;= #{endTime}
        GROUP BY dimension
        ) t13 ON t1.project_id = t13.dimension
        WHERE stat_date >= #{startTime}
        AND stat_date &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(charge) > 0
        ) tt
        LEFT JOIN (
        SELECT project_id ,ad_scene, SUM(charge) as 'sceneCharge' FROM
        application.app_kuaishou_account_ad_scene_report_d
        WHERE stat_date >= #{startTime}
        AND stat_date &lt;= #{endTime}
        AND company_id in ('5e46b5dea3d740eeb8ff1a2895015a4b' , 'd57fecdcf7a94d009736d9c850731582')
        GROUP BY project_id,ad_scene
        ORDER BY project_id
        ) t9 ON tt.projectId = t9.project_id
        GROUP BY tt.projectId
        ORDER BY ${sortCode} ${order}
    </select>

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

    <select id="getAdvertiserNoByDays" resultType="com.alibaba.fastjson.JSONObject">
        SELECT stat_date as 'time',COUNT(1) as 'count'
        FROM (SELECT stat_date, advertiser_id
              FROM application.kuaishou_account_report_hourly_dw
              WHERE stat_date >= #{startTime}
                AND stat_date &lt;= #{endTime}
              GROUP BY stat_date, advertiser_id
              HAVING sum(charge) >= 10
             ) t
        GROUP BY stat_date
        ORDER BY stat_date
    </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 = 2
        )
        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 = 2
        GROUP BY stat_date
        ORDER BY stat_date
    </select>

</mapper>