<?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.toutiao.modules.report.mapper.BytedanceAccountReportMapper">

    <select id="queryTodaySumReportBy" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ROUND(sum(cost),2) cost,
        sum(`show`) showNum,
        sum(click) click,
        sum(`convert`) as convertNum,
        max(`hour`) maxHour
        FROM
        application.bytedance_advertiser_report_hourly_dw
        WHERE stat_datetime = DATE_FORMAT(#{statDate},'%Y%m%d')
        <if test="accountIds !=null">
        AND account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        </if>
    </select>

    <select id="queryTodayReportByAccount" resultType="com.alibaba.fastjson.JSONObject">
        SELECT ROUND(sum(cost), 2) cost,
               sum(show_num)       showNum,
               sum(click)          click,
               sum(convert_num) as convertNum
        FROM ctop_bytedance_report_advertiser_daily
        WHERE stat_datetime = #{statDate} '00:00:00'
        AND account_id= #{accountId}
    </select>

    <select id="queryTodaySumByHourGroupAccount" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t1.account_id as accountId,
        t1.account_name as authName,
        ${filed}
        FROM application.bytedance_advertiser_report_hourly_dw t1
        WHERE t1.stat_datetime = DATE_FORMAT(#{statDate}, '%Y%m%d' )
        and t1.`hour` &lt;= #{hour}
        <if test="accountIds !=null">
        AND t1.account_id IN
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        </if>
        group by t1.account_id
        order by ${target} ${order}
    </select>

    <select id="queryTodaySumByHour" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ROUND(sum(cost),2) cost,
        sum(`show`) showNum,
        sum(click) click,
        sum(`convert`) as convertNum
        FROM
        application.bytedance_advertiser_report_hourly_dw
        WHERE
        stat_datetime = DATE_FORMAT(#{statDate},'%Y%m%d')
        and `hour` &lt;= #{hour}
        <if test="accountIds !=null">
        AND account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        </if>
    </select>

    <select id="querySumByHour" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ${filed}
        FROM
        application.bytedance_advertiser_report_hourly_dw t1
        WHERE
        t1.stat_datetime = DATE_FORMAT(#{statDate},'%Y%m%d')
        and t1.`hour` &lt;= #{hour}
        <if test="accountIds !=null">
        AND t1.account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        </if>
    </select>

    <select id="queryTodayDetailReportBy" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ROUND(sum(cost),2) cost,
        sum(`show`) showNum,
        sum(click) click,
        sum(`convert`) as convertNum,
        `hour` as statHour
        FROM
        application.bytedance_advertiser_report_hourly_dw
        WHERE
        stat_datetime = DATE_FORMAT(#{statDate},'%Y%m%d')
        <if test="accountIds !=null">
        AND account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        </if>
        GROUP BY
        `hour`
        ORDER BY
        `hour` ASC
    </select>

    <select id="queryAllSumByStartEndDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ${filed}
        FROM application.bytedance_advertiser_report_hourly_dw  t1
        WHERE t1.stat_datetime &lt;=DATE_FORMAT(#{endDate},'%Y%m%d')
        AND t1.stat_datetime &gt;= DATE_FORMAT(#{startDate},'%Y%m%d')
        <if test="accountIds !=null">
        AND t1.account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        </if>
    </select>

    <select id="querySumByStartEndDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT ROUND(sum(cost), 2) cost,
        sum(`show`) showNum,
        sum(click) click,
        sum(`convert`) as convertNum
        FROM
        application.bytedance_advertiser_report_hourly_dw
        WHERE stat_datetime &lt;= DATE_FORMAT(#{endDate},'%Y%m%d')
        AND stat_datetime &gt;= DATE_FORMAT(#{startDate},'%Y%m%d')
        <if test="accountIds !=null">
          AND account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        </if>
    </select>

    <select id="querySumByDateGroupAccount" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        DATE_FORMAT(t1.stat_datetime,'%Y-%m-%d') as statDate,
        t1.account_id as accountId,
        t1.account_name as authName,
        ${filed}
        FROM application.bytedance_advertiser_report_hourly_dw  t1
        WHERE
        t1.stat_datetime &lt;= DATE_FORMAT(#{endDate},'%Y%m%d')
        AND t1.stat_datetime &gt;= DATE_FORMAT(#{startDate},'%Y%m%d')
        <if test="accountIds !=null">
          AND t1.account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        </if>
        group by t1.account_id
        order by ${target} ${order}
    </select>

    <select id="querySumByDateGroupAccountAndDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        DATE_FORMAT(t1.stat_datetime,'%Y-%m-%d') as statDate,
        t1.account_id as accountId,
        t1.account_name as authName,
        ${filed}
        FROM
        FROM application.bytedance_advertiser_report_hourly_dw  t1
        WHERE
        t1.stat_datetime &lt;= DATE_FORMAT(#{endDate},'%Y%m%d')
        AND t1.stat_datetime &gt;= DATE_FORMAT(#{startDate},'%Y%m%d')
        <if test="accountIds !=null">
        AND t1.account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        </if>
        group by t1.stat_datetime
        order by ${target} ${order}
    </select>

    <select id="queryDetailByStartEndDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ROUND(sum(cost),2) cost,
        sum(`show`) showNum,
        sum(click) click,
        sum(`convert`) convertNum,
        DATE_FORMAT(stat_datetime, '%Y-%m-%d') statDate
        FROM
        application.bytedance_advertiser_report_daily_dw
        WHERE stat_datetime &lt;= DATE_FORMAT(#{endDate},'%Y%m%d')
        AND stat_datetime &gt;= DATE_FORMAT(#{startDate},'%Y%m%d')
        <if test="accountIds !=null">
        AND account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        </if>
        group by stat_datetime
        order by stat_datetime
    </select>

    <select id="queryDetailGroupMonthByDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ROUND(sum(cost),2) cost,
        sum(`show`) showNum,
        sum(click) click,
        sum(`convert`) convertNum,
        DATE_FORMAT(t1.stat_datetime, '%Y-%m') statDate
        FROM application.bytedance_advertiser_report_daily_dw t1
        WHERE t1.stat_datetime &lt;= DATE_FORMAT(#{endDate},'%Y%m%d')
        AND t1.stat_datetime &gt;= DATE_FORMAT(#{startDate},'%Y%m%d')
        <if test="accountIds !=null">
        AND t1.account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        </if>
        group by DATE_FORMAT(t1.stat_datetime, '%Y-%m')
        order by t1.stat_datetime
    </select>

    <select id="queryDetailGroupDayByMonth" resultType="com.alibaba.fastjson.JSONObject">
        SELECT DATE_FORMAT(stat_datetime, '%Y-%m-%d') statDate,
               SUBSTR(DATE_FORMAT(stat_datetime, '%Y-%m-%d') FROM 9 FOR 2) day
        FROM ctop_bytedance_report_advertiser_daily
        WHERE stat_datetime like '${month}%'
        group by DATE_FORMAT(stat_datetime, '%Y-%m-%d')
        order by stat_datetime asc
    </select>

    <sql id="selectSql">
        ifnull
        (sum(a.show_num),0)                          as showMaterial,
        ifnull(sum(a.click),0)                             as clickMaterial,
        ifnull(sum(a.convert_num),0)                       as convertMaterial,
        ifnull(sum(a.cost),0)                              as cost,
        ifnull(sum(a.active),0)                            as active,
        ifnull(sum(a.download_finish),0)                   as downloadFinish,
        ifnull(sum(a.download_start),0)                    as downloadStart,
		ifnull(sum(a.click_install),0)                     as clickInstall,
		ifnull(sum(a.install_finish),0)                    as installFinish,
		ifnull(sum(a.register),0)                          as register,
        ifnull(sum(a.pay_count),0)                         as payCount,
		ifnull(sum(a.form),0)                              as form,
		ifnull(sum(a.total_play),0)                        as totalPlay,
        ifnull(sum(a.valid_play),0)                        as validPlay,
        ifnull(sum(a.wifi_play),0)                         as wifiPlay,
        ifnull(sum(a.play25_feed_break),0)                 as play25FeedBreak,
        ifnull(sum(a.play50_feed_break),0)                 as play50FeedBreak,
        ifnull(sum(a.play75_feed_break),0)                 as play75FeedBreak,
        ifnull(sum(a.play100_feed_break),0)                as play100FeedBreak,
		ifnull(sum(a.share),0)                             as shareMaterial,
		ifnull(sum(a.like_num),0)                          as likeMaterial,
		case
        when sum(a.total_play) != 0 and sum(a.total_play) is not null and sum(a.play25_feed_break) is not null
        then round(sum(a.play25_feed_break) / sum(a.total_play),2)
        else 0
        end                               as play25FeedBreakRate,
		case
        when sum(a.click) != 0 and sum(a.click) is not null and sum(a.convert_num) is not null
        then round(sum(a.convert_num) / sum(a.click),2)
        else 0
        end                               as convertRate,
        case
        when sum(a.convert_num) != 0 and sum(a.convert_num) is not null and sum(a.cost) is not null
        then round(sum(a.cost)/sum(a.convert_num),2)
        else 0
        end as convertCost,
		ifnull(sum(a.follow),0)                            as follow,
        ifnull(sum(a.next_day_open),0)                     as nextDayOpen,
        case
        when sum(a.active) != 0 and sum(a.next_day_open) is not null and sum(a.active) is not null
        then round(sum(a.next_day_open) / sum(a.active),2)
        else 0
        end                               as nextDayOpenRate,
        case
        when sum(a.next_day_open) != 0 and sum(a.next_day_open) is not null and sum(a.cost) is not null
        then round(sum(a.cost) / sum(a.next_day_open),2)
        else 0
        end                               as nextDayOpenCost,
		case
        when sum(a.click) != 0 and sum(a.click) is not null and sum(a.cost) is not null
        then round(sum(a.cost) / sum(a.click),2)
        else 0
        end                               as cpc,
        case
        when sum(a.show_num) != 0 and sum(a.show_num) is not null and sum(a.click) is not null
        then round(sum(a.click) / sum(a.show_num),2)
        else 0
        end                               as ctr,
        case
        when sum(a.show_num) != 0 and sum(a.cost) is not null and sum(a.show_num) is not null
        then round(sum(a.cost) / sum(a.show_num) * 1000,2)
        else 0
        end                               as cpm,
		case
        when sum(a.active) != 0 and sum(a.cost) is not null and sum(a.active) is not null
        then round(sum(a.cost) / sum(a.active),2)
        else 0
        end                               as activeCost,
		case
        when sum(a.click) != 0 and sum(a.active) is not null and sum(a.click) is not null
        then round(sum(a.active) / sum(a.click),2)
        else 0
        end                               as activeRate,
        case
        when sum(a.active) != 0 and sum(a.register) is not null and sum(a.active) is not null
        then round(sum(a.register) / sum(a.active),2)
        else 0
        end                               as activeRegisterRate,
        case
        when sum(a.register) != 0 and sum(a.cost) is not null and sum(a.register) is not null
        then round(sum(a.cost) / sum(a.register),2)
        else 0
        end                               as activeRegisterCost
    </sql>

    <select id="getBytedanceAccountInfoByProjectId"
            resultType="cn.com.ctop.toutiao.modules.report.DTO.ByteDanceReportAccountDailyDTO">
        select * from (
        select
        concat(#{startDate},'~',#{endDate}) as statDate,
        a.advertiser_id as 'accountId',
        c.advertiser_id as 'advertiserId',
        (select name from ctop_advertiser where id = c.advertiser_id) as 'advertiserName',
        (
        select realname from sys_user where id = (
        select responsible_id from ctop_project where id = b.project_id
        )
        ) as 'responsibleName',

        (
        select realname from sys_user where id = (
        select design_responsible_id from ctop_project where id = b.project_id
        )
        ) as 'designResponsibleName',


        b.auth_name as 'authName',
        b.project_id as 'projectId',
        (select project_name from ctop_project where id = b.project_id) as 'projectName',
        c.media_id as 'mediaId',
        <include refid="selectSql"></include>
        from
        ctop_bytedance_report_advertiser_daily a
        left join ctop_user_allocation b on a.advertiser_id = b.account_id
        left join ctop_project c on b.project_id = c.id
        where
        date_format(a.stat_datetime,'%Y-%m-%d') &gt;= #{startDate}
        and
        date_format(a.stat_datetime,'%Y-%m-%d') &lt;= #{endDate}
        <if test="projectId!=null and projectId!=''">
            and
            b.project_id = #{projectId}
        </if>
        <if test="accountIds !=null">
            and a.advertiser_id in
            <foreach collection="accountIds" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>
        group by a.advertiser_id
        order by sum(a.cost) desc
        )m
        <if test="yn =1">
            where
            m.cost != 0
        </if>
    </select>

    <select id="getBytedanceProjectInfo"
            resultType="cn.com.ctop.toutiao.modules.report.DTO.ByteDanceReportAccountDailyDTO">

        select
        concat(#{startDate},'~',#{endDate}) as statDate,
        a.advertiser_id as 'accountId',
        c.advertiser_id as 'advertiserId',
        (select name from ctop_advertiser where id = c.advertiser_id) as 'advertiserName',

        (
        select realname from sys_user where id = (
        select responsible_id from ctop_project where id = b.project_id
        )
        ) as 'responsibleName',
        (
        select realname from sys_user where id = (
        select design_responsible_id from ctop_project where id = b.project_id
        )
        ) as 'designResponsibleName',
        b.auth_name as 'authName',
        b.project_id as 'projectId',
        (
        select realname from sys_user where id = (
        select sale_id from ctop_project where id = b.project_id
        )
        ) as 'saleName',
        (select project_name from ctop_project where id = b.project_id) as 'projectName',
        c.media_id as 'mediaId',
        <include refid="selectSql"></include>
        from
        ctop_bytedance_report_advertiser_daily a
        left join ctop_user_allocation b on a.advertiser_id = b.account_id
        left join ctop_project c on b.project_id = c.id
        where
        date_format(a.stat_datetime, '%Y-%m-%d') &gt;= #{startDate}
        and
        date_format(a.stat_datetime, '%Y-%m-%d') &lt;= #{endDate}
        and
        b.project_id in
        <foreach collection="projectList" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
        group by b.project_id
    </select>

    <select id="getRoleCodeByUserId" resultType="string">
        select role_code
        from sys_user_role a
                 left join sys_role b on a.role_id = b.id
        where a.user_id = #{userId}
    </select>

    <select id="queryProjectInfoByAdmin" resultType="long">
        select
        id as projectId
        from
        ctop_project
        where
        media_id in
        <foreach collection="mediaIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getSaleProjects" resultType="long">
        select
        distinct a.id as projectId
        from
        ctop_project a
        left join sys_user b on a.sale_id = b.id
        where a.sale_id = #{userId}
        and a.media_id in
        <foreach collection="mediaIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="queryProjectMemberByIds"
            resultType="cn.com.ctop.toutiao.modules.report.DTO.ByteDanceReportAccountDailyDTO">
        select
        t1.id as projectId,
        t1.project_name as projectName,
        t1.advertiser_id as advertiserId,
        t1.advertiser_name as advertiserName,
        t1.media_id as mediaId,
        (
        select realname from sys_user where id = t1.responsible_id
        ) as 'responsibleName',
        (
        select realname from sys_user where id = t1.design_responsible_id
        ) as 'designResponsibleName'
        from ctop_project t1
        where
        t1.id in
        <foreach collection="projectIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
        order by create_time desc
    </select>

    <select id="getUserProjectAccountIds" resultType="long">
        select a.account_id
        from (select project_id, account_id, media_id, create_time from ctop_user_allocation group by account_id) a
                 left join ctop_project_member b on a.project_id = b.project_id
        where a.user_id = #{userId}
          and a.media_id in (1, 3)
        order by a.create_time desc limit 50
    </select>


    <select id="getSaleProjectAccountIds" resultType="long">
        select a.account_id
        from (select project_id, account_id, media_id, create_time from ctop_user_allocation group by account_id) a
                 left join ctop_project b on a.project_id = b.id
        where b.sale_id = #{userId}
          and b.media_id in (1, 3)
        order by a.create_time desc

    </select>

    <select id="getUserProjectAccountIdsByAdmin" resultType="long">
        select a.account_id
        from ctop_user_allocation a
                 left join ctop_project b on a.project_id = b.id
        where b.media_id in (1, 3)

    </select>
    <select id="getBytedanceProjectInfoEntity"
            resultType="cn.com.ctop.common.module.vo.ByteDanceReportAccountDailyDTOEntity">
        select * from (
        select
        concat(#{startDate},'~',#{endDate}) as statDate,
        a.advertiser_id as 'accountId',
        c.advertiser_id as 'advertiserId',
        (select name from ctop_advertiser where id = c.advertiser_id) as 'advertiserName',

        (
        select realname from sys_user where id = (
        select responsible_id from ctop_project where id = b.project_id
        )
        ) as 'responsibleName',
        (
        select realname from sys_user where id = (
        select design_responsible_id from ctop_project where id = b.project_id
        )
        ) as 'designResponsibleName',

        (
        select realname from sys_user where id = (
        select sale_id from ctop_project where id = b.project_id
        )
        ) as 'saleName',
        b.auth_name as 'authName',
        b.project_id as 'projectId',
        (select project_name from ctop_project where id = b.project_id) as 'projectName',
        c.media_id as 'mediaId',
        <include refid="selectSql"></include>
        from
        ctop_bytedance_report_advertiser_daily a
        left join ctop_user_allocation b on a.advertiser_id = b.account_id
        left join ctop_project c on b.project_id = c.id
        where
        date_format(a.stat_datetime, '%Y-%m-%d') &gt;= #{startDate}
        and
        date_format(a.stat_datetime, '%Y-%m-%d') &lt;= #{endDate}
        and
        b.project_id in
        <foreach collection="projectList" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
        group by b.project_id
        )m
        <if test="yn = 1">
            where
            m.cost != 0
        </if>
    </select>

</mapper>
