<?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.ctop.mapper.MediaReportMapper">
    <select id="queryAdvertiserNameByCompanyId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT id,
               name
        from ctop_advertiser
        where company_id = #{companyId}
    </select>

    <select id="queryProjectNameByAdvertiserId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT id,
               project_name
        from ctop_project
        where advertiser_id = #{advertiserId}
    </select>

    <select id="queryAllBy" resultType="com.alibaba.fastjson.JSONObject">
        SELECT e.*
        from (
                 SELECT r.*, (r.todayCost - r.yesterdayCost) as diffCost, (r.todayCost + r.yesterdayCost) as sumCost

                 from (
                          SELECT t2.media_id                                                           as mediaType,
                                 t1.company_id                                                         as companyId,
                                 t1.id                                                                 as advertiserId,
                                 t1.NAME                                                               as advertiserName,
                                 t2.id                                                                 as projectId,
                                 t2.project_name                                                       as projectName,
                                 t2.responsible_name                                                   as operatorManger,
                                 t2.design_responsible_name                                            as designManger,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_user_allocation t3
                                  where t2.id = t3.project_id)                                         as operator,
                                 (SELECT realname from sys_user u WHERE u.id = t1.sale_id)             as sale,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_project_member member
                                  where t2.id = member.project_id
                                    and role_code = 'plan')                                            as plan,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_project_member member
                                  where t2.id = member.project_id
                                    and role_code = 'shot')                                            as shot,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_project_member member
                                  where t2.id = member.project_id
                                    and role_code = 'clip')                                            as clip,
                                 IFNULL((SELECT sum(cost)
                                         from ctop_bytedance_report_advertiser_daily report
                                         where report.advertiser_id in (SELECT account_id
                                                                        from ctop_user_allocation t3
                                                                        where t2.id = t3.project_id)
                                           and report.stat_datetime = '${todayDate} 00:00:00'), 0)     as todayCost,
                                 IFNULL((SELECT sum(cost)
                                         from ctop_bytedance_report_advertiser_daily report
                                         where report.advertiser_id in (SELECT account_id
                                                                        from ctop_user_allocation t3
                                                                        where t2.id = t3.project_id)
                                           and report.stat_datetime = '${yesterdayDate} 00:00:00'), 0) as yesterdayCost
                          FROM ctop_advertiser t1
                                   LEFT JOIN ctop_project t2 ON t1.id = t2.advertiser_id
                          where t2.media_id in(1,3)
                            and t2.id is not null) as r

                 UNION

                 SELECT r.*, (r.todayCost - r.yesterdayCost) as diffCost, (r.todayCost + r.yesterdayCost) as sumCost
                 from (
                          SELECT t2.media_id                                               as mediaType,
                                 t1.company_id                                             as companyId,
                                 t1.id                                                     as advertiserId,
                                 t1.NAME                                                   as advertiserName,
                                 t2.id                                                     as projectId,
                                 t2.project_name                                           as projectName,
                                 t2.responsible_name                                       as operatorManger,
                                 t2.design_responsible_name                                as designManger,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_user_allocation t3
                                  where t2.id = t3.project_id)                             as operator,
                                 (SELECT realname from sys_user u WHERE u.id = t1.sale_id) as sale,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_project_member member
                                  where t2.id = member.project_id
                                    and role_code = 'plan')                                as plan,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_project_member member
                                  where t2.id = member.project_id
                                    and role_code = 'shot')                                as shot,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_project_member member
                                  where t2.id = member.project_id
                                    and role_code = 'clip')                                as clip,
                                 IFNULL((SELECT sum(charge)
                                         from ctop_kuaishou_report_daily_account report
                                         where report.account_id in (SELECT account_id
                                                                     from ctop_user_allocation t3
                                                                     where t2.id = t3.project_id)
                                           and report.stat_date = '${todayDate}'), 0)      as todayCost,
                                 IFNULL((SELECT sum(charge)
                                         from ctop_kuaishou_report_daily_account report
                                         where report.account_id in (SELECT account_id
                                                                     from ctop_user_allocation t3
                                                                     where t2.id = t3.project_id)
                                           and report.stat_date = '${yesterdayDate}'), 0)  as yesterdayCost
                          FROM ctop_advertiser t1
                                   LEFT JOIN ctop_project t2 ON t1.id = t2.advertiser_id
                          where t2.media_id  in(2,4)
                            and t2.id is not null) as r

                 union

                 SELECT r.*, (r.todayCost - r.yesterdayCost) as diffCost, (r.todayCost + r.yesterdayCost) as sumCost
                 from (
                          SELECT '- '                                                            as mediaType,
                                 '-'                                                             as companyId,
                                 id                                                              as advertiserId,
                                 corporation_name                                                as advertiserName,
                                 '-'                                                             as projectId,
                                 account_name                                                    as projectName,
                                 '-'                                                             as operatorManger,
                                 '-'                                                             as designManger,
                                 '-'                                                             as operator,
                                 '-'                                                             as sale,
                                 '-'                                                             as plan,
                                 '-'                                                             as shot,
                                 '-'                                                             as clip,
                                 IFNULL((SELECT sum(balance)
                                         from ctop_kuaishou_agent_account report
                                         where report.id = t1.id
                                           and report.create_time like '%${todayDate}%'), 0)     as todayCost,
                                 IFNULL((SELECT sum(balance)
                                         from ctop_kuaishou_agent_account report
                                         where report.id = t1.id
                                           and report.create_time like '%${yesterdayDate}%'), 0) as yesterdayCost
                          from ctop_kuaishou_agent_account t1
                          where t1.create_time like '%${yesterdayDate}%'
                          GROUP by advertiserName, projectName) as r) as e
        ORDER BY sumCost desc
    </select>

    <select id="queryMediaReportBy" resultType="com.alibaba.fastjson.JSONObject">
        SELECT e.*
        FROM (
                 SELECT r.*, (r.todayCost - r.yesterdayCost) as diffCost, (r.todayCost + r.yesterdayCost) as sumCost

                 from (
                          SELECT t2.media_id                                                           as mediaType,
                                 t1.company_id                                                         as companyId,
                                 t1.id                                                                 as advertiserId,
                                 t1.NAME                                                               as advertiserName,
                                 t2.id                                                                 as projectId,
                                 t2.project_name                                                       as projectName,
                                 t2.responsible_name                                                   as operatorManger,
                                 t2.design_responsible_name                                            as designManger,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_user_allocation t3
                                  where t2.id = t3.project_id)                                         as operator,
                                 (SELECT realname from sys_user u WHERE u.id = t1.sale_id)             as sale,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_project_member member
                                  where t2.id = member.project_id
                                    and role_code = 'plan')                                            as plan,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_project_member member
                                  where t2.id = member.project_id
                                    and role_code = 'shot')                                            as shot,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_project_member member
                                  where t2.id = member.project_id
                                    and role_code = 'clip')                                            as clip,
                                 IFNULL((SELECT sum(cost)
                                         from ctop_bytedance_report_advertiser_daily report
                                         where report.advertiser_id in (SELECT account_id
                                                                        from ctop_user_allocation t3
                                                                        where t2.id = t3.project_id)
                                           and report.stat_datetime = '${todayDate} 00:00:00'), 0)     as todayCost,
                                 IFNULL((SELECT sum(cost)
                                         from ctop_bytedance_report_advertiser_daily report
                                         where report.advertiser_id in (SELECT account_id
                                                                        from ctop_user_allocation t3
                                                                        where t2.id = t3.project_id)
                                           and report.stat_datetime = '${yesterdayDate} 00:00:00'), 0) as yesterdayCost
                          FROM ctop_advertiser t1
                                   LEFT JOIN ctop_project t2 ON t1.id = t2.advertiser_id
                          where t2.media_id  in(1,3)
                            and t2.id is not null) as r

                 UNION

                 SELECT r.*, (r.todayCost - r.yesterdayCost) as diffCost, (r.todayCost + r.yesterdayCost) as sumCost
                 from (
                          SELECT t2.media_id                                               as mediaType,
                                 t1.company_id                                             as companyId,
                                 t1.id                                                     as advertiserId,
                                 t1.NAME                                                   as advertiserName,
                                 t2.id                                                     as projectId,
                                 t2.project_name                                           as projectName,
                                 t2.responsible_name                                       as operatorManger,
                                 t2.design_responsible_name                                as designManger,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_user_allocation t3
                                  where t2.id = t3.project_id)                             as operator,
                                 (SELECT realname from sys_user u WHERE u.id = t1.sale_id) as sale,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_project_member member
                                  where t2.id = member.project_id
                                    and role_code = 'plan')                                as plan,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_project_member member
                                  where t2.id = member.project_id
                                    and role_code = 'shot')                                as shot,
                                 (SELECT GROUP_CONCAT(DISTINCT user_name)
                                  from ctop_project_member member
                                  where t2.id = member.project_id
                                    and role_code = 'clip')                                as clip,
                                 IFNULL((SELECT sum(charge)
                                         from ctop_kuaishou_report_daily_account report
                                         where report.account_id in (SELECT account_id
                                                                     from ctop_user_allocation t3
                                                                     where t2.id = t3.project_id)
                                           and report.stat_date = '${todayDate}'), 0)      as todayCost,
                                 IFNULL((SELECT sum(charge)
                                         from ctop_kuaishou_report_daily_account report
                                         where report.account_id in (SELECT account_id
                                                                     from ctop_user_allocation t3
                                                                     where t2.id = t3.project_id)
                                           and report.stat_date = '${yesterdayDate}'), 0)  as yesterdayCost
                          FROM ctop_advertiser t1
                                   LEFT JOIN ctop_project t2 ON t1.id = t2.advertiser_id
                          where t2.media_id  in(2,4)
                            and t2.id is not null) as r) as e
        where companyId = ${condition}
        ORDER BY sumCost desc
    </select>

    <select id="queryAgentReportBy" resultType="com.alibaba.fastjson.JSONObject">
        SELECT e.*
        from (
                 SELECT r.*, (r.todayCost - r.yesterdayCost) as diffCost, (r.todayCost + r.yesterdayCost) as sumCost
                 from (
                          SELECT '-'                                                             as mediaType,
                                 '-'                                                             as companyId,
                                 id                                                              as advertiserId,
                                 corporation_name                                                as advertiserName,
                                 '-'                                                             as projectId,
                                 account_name                                                    as projectName,
                                 '-'                                                             as operatorManger,
                                 '-'                                                             as designManger,
                                 '-'                                                             as operator,
                                 '-'                                                             as sale,
                                 '-'                                                             as plan,
                                 '-'                                                             as shot,
                                 '-'                                                             as clip,
                                 IFNULL((SELECT sum(balance)
                                         from ctop_kuaishou_agent_account report
                                         where report.id = t1.id
                                           and report.create_time like '%${todayDate}%'), 0)     as todayCost,
                                 IFNULL((SELECT sum(balance)
                                         from ctop_kuaishou_agent_account report
                                         where report.id = t1.id
                                           and report.create_time like '%${yesterdayDate}%'), 0) as yesterdayCost
                          from ctop_kuaishou_agent_account t1
                          where t1.create_time like '%${yesterdayDate}%'
                          GROUP by advertiserName, projectName) as r) as e
        ORDER BY sumCost desc
    </select>
</mapper>
