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


    <select id="selectDayReport" resultType="com.alibaba.fastjson.JSONObject">
        select
         sum(charge) cost,
         sum(photo_show) photoShow,
         sum(photo_click) photoClick,
         sum(aclick) aclick,
         max(stat_hour) maxHour
       from
        ctop_kuaishou_report_hourly_account
        where stat_date = #{date}
      </select>


    <select id="selectByDate" resultType="com.alibaba.fastjson.JSONObject">
      select
       sum(charge) cost,
       sum(photo_show) photoShow,
       sum(photo_click) photoClick,
       sum(aclick) aclick
     from
      ctop_kuaishou_report_daily_account
      where stat_date &lt;= #{startDate}
      and  stat_date &gt;= #{endDate}
    </select>

    <select id="selectAllAccount" resultType="com.alibaba.fastjson.JSONObject">
      select
       sum(charge) cost,
       sum(photo_show) photoShow,
       sum(photo_click) photoClick,
       sum(aclick) aclick
     from
      ctop_kuaishou_report_daily_account
    </select>


    <select id="selectReportDetail" resultType="com.alibaba.fastjson.JSONObject">
      select
       sum(charge) cost,
       sum(photo_show) photoShow,
       sum(photo_click) photoClick,
       sum(aclick) aclick,
       stat_hour statHour,
       stat_date statDate
     from
      ctop_kuaishou_report_hourly_account
      where stat_date = #{date}
      group by stat_hour
      order by stat_hour asc
    </select>


    <select id="selectDayHourlyByHourAndDate" resultType="com.alibaba.fastjson.JSONObject">
      select
       sum(charge) cost,
       sum(photo_show) photoShow,
       sum(photo_click) photoClick,
       sum(aclick) aclick
     from
      ctop_kuaishou_report_hourly_account
      where stat_date = #{date}
      and stat_hour = #{statHour}
    </select>

    <select id="selectSumDayHourlyByHourAndDate" resultType="com.alibaba.fastjson.JSONObject">
      select
       sum(charge) cost,
       sum(photo_show) photoShow,
       sum(photo_click) photoClick,
       sum(aclick) aclick
     from
      ctop_kuaishou_report_hourly_account
      where stat_date = #{date}
      and stat_hour &lt;= #{statHour}
    </select>


    <select id="selectDayDetailByDate" resultType="com.alibaba.fastjson.JSONObject">
      select
       sum(charge) cost,
       sum(photo_show) photoShow,
       sum(photo_click) photoClick,
       sum(aclick) aclick,
       stat_date statDate
     from
      ctop_kuaishou_report_daily_account
      where stat_date &lt;= #{startDate}
      and  stat_date &gt;= #{endDate}
      group by stat_date
      order by stat_date asc
    </select>


    <select id="selectDayDetailByMonth" resultType="com.alibaba.fastjson.JSONObject">
      select
       sum(charge) cost,
       sum(photo_show) photoShow,
       sum(photo_click) photoClick,
       sum(aclick) aclick,
       DATE_FORMAT(stat_date,'%Y-%m') statDate
     from
      ctop_kuaishou_report_daily_account
      where stat_date &lt;= #{startDate}
      and  stat_date &gt;= #{endDate}
      group by DATE_FORMAT(stat_date,'%Y-%m')
      order by stat_date asc
    </select>

    <select id="selectAllDayDetailByMonth" resultType="com.alibaba.fastjson.JSONObject">
      select
       sum(charge) cost,
       sum(photo_show) photoShow,
       sum(photo_click) photoClick,
       sum(aclick) aclick,
       DATE_FORMAT(stat_date,'%Y-%m') statDate
     from
      ctop_kuaishou_report_daily_account
      group by DATE_FORMAT(stat_date,'%Y-%m')
      order by stat_date asc
    </select>


    <select id="selectMonthReportByDate" resultType="com.alibaba.fastjson.JSONObject">
      select
       sum(charge) cost,
       sum(photo_show) photoShow,
       sum(photo_click) photoClick,
       sum(aclick) aclick,
       DATE_FORMAT(stat_date,'%Y-%m') statDate
     from
      ctop_kuaishou_report_daily_account
      where DATE_FORMAT(stat_date,'%Y-%m') = #{date}
    </select>


    <select id="selectDayDetailsByMonth" resultType="com.alibaba.fastjson.JSONObject">
      select
       sum(charge) cost,
       sum(photo_show) photoShow,
       sum(photo_click) photoClick,
       sum(aclick) aclick,
       stat_date statDate
     from
      ctop_kuaishou_report_daily_account
      where DATE_FORMAT(stat_date,'%Y-%m') = #{date}
      group by stat_date
      order by stat_date asc
    </select>


    <select id="selectDayDailyByDate" resultType="com.alibaba.fastjson.JSONObject">
      select
       sum(charge) cost,
       sum(photo_show) photoShow,
       sum(photo_click) photoClick,
       sum(aclick) aclick,
       stat_date statDate
     from
      ctop_kuaishou_report_daily_account
      where stat_date = #{statDate}
      group by stat_date
    </select>


    <select id="selectDayReportByDate" resultType="com.alibaba.fastjson.JSONObject">
      select
       sum(charge) cost,
       sum(photo_show) photoShow,
       sum(photo_click) photoClick,
       sum(aclick) aclick,
       stat_date statDate
     from
      ctop_kuaishou_report_daily_account
      where stat_date = #{date}
      group by stat_date
    </select>


    <select id="selectHourlyDetailsByDate" resultType="com.alibaba.fastjson.JSONObject">
      select
       sum(charge) cost,
       sum(photo_show) photoShow,
       sum(photo_click) photoClick,
       sum(aclick) aclick,
       stat_hour statHour,
       stat_date statDate
     from
      ctop_kuaishou_report_hourly_account
      where stat_date = #{date}
      group by stat_hour
      order by stat_hour asc
    </select>





    <select id="videoInfoListWeek" resultType="cn.com.ctop.toutiao.modules.report.entity.ByteDanceVideoReportDaily">
        select
        a.project_id                             as projectId,
        a.project_name                           as projectName,
        a.advertiser_id                          as advertiserId,
        a.advertiser_name                        as advertiserName,
        a.stat_datetime                          as statDatetime,
        sum(a.cost)                              as cost,
        a.signature                              as signature,
        a.video_url                              as videoUrl,
        '-'                                      as materialDesc,
        (select info.url from ctop_material_info info where info.code = a.signature limit 1)                                    as url,
        1                                        as appType,
        sum(a.click)                             as clickMaterial,

        case
        when sum(a.total_play) != 0
        then sum(a.play25_feed_break) / sum(a.total_play)
        else 0 end                           as play25FeedBreakRate,

        case
        when sum(a.total_play) != 0
        then sum(a.play_over) / sum(a.total_play)
        else 0 end                           as playOverRate,

        case
        when sum(a.click) != 0
        then sum(a.convert_material) / sum(a.click)
        else 0 end                           as convertRate,
        sum(a.active)                            as active,
        sum(a.download_finish)                   as downloadFinish,
        sum(a.download_start)                    as downloadStart,
        sum(a.install_finish)                    as installFinish,
        sum(a.register)                          as register,
        sum(a.pay_count)                         as payCount,
        sum(a.form)                              as form,
        sum(a.total_play)                        as totalPlay,
        sum(a.valid_play)                        as validPlay,
        sum(a.wifi_play)                         as wifiPlay,
        sum(a.play25_feed_break)                 as play25FeedBreak,
        sum(a.play50_feed_break)                 as play50FeedBreak,
        sum(a.play75_feed_break)                 as play75FeedBreak,
        sum(a.play100_feed_break)                as play100FeedBreak,
        sum(a.share_material)                    as shareMaterial,
        sum(a.follow)                            as follow,
        sum(a.next_day_open)                     as nextDayOpen,
        case
        when sum(a.active) != 0
        then sum(a.next_day_open) / sum(a.active)
        else 0 end                           as nextDayOpenRate,

        case
        when sum(a.next_day_open) != 0
        then sum(a.cost) / sum(a.next_day_open)
        else 0 end                           as nextDayOpenCost,

        sum(a.active_pay_amount)                 as activePayAmount,

        case
        when sum(a.valid_play) != 0
        then sum(a.cost) / sum(a.valid_play)
        else 0 end                              validPlayCost,
        sum(a.convert_material)                  as convertMaterial,

        case
        when sum(a.active_pay_amount) != 0
        then sum(a.cost) / sum(a.active_pay_amount)
        else 0 end                           as activePayCost,

        case
        when sum(a.click) != 0
        then sum(a.cost) / sum(a.click)
        else 0 end                           as cpc,
        sum(a.play_over_rate)                    as playOverRate,

        case
        when sum(a.show_material) != 0
        then sum(a.click) / sum(a.show_material)
        else 0 end                           as ctr,

        case
        when sum(a.show_material) != 0
        then sum(a.cost) / sum(a.show_material) * 1000
        else 0 end                              cpm,
        sum(a.wifi_play_rate)                    as wifiPlayRate,
        sum(a.like_material)                     as likeMaterial,


        case
        when sum(a.active) != 0
        then sum(a.cost) / sum(a.active)
        else 0 end                              activeCost,

        case
        when sum(a.game_addiction) != 0
        then sum(a.cost) / sum(a.game_addiction)
        else 0 end                           as gameAddictionCost,
        sum(a.game_addiction)                    as gameAddiction,

        case
        when sum(a.click) != 0
        then sum(a.active) / sum(a.click)
        else 0 end                           as activeRate,


        case
        when sum(a.active) != 0
        then sum(a.game_addiction) / sum(a.active)
        else 0 end                            as  gameAddictionRate,

        case
        when sum(a.active) != 0
        then sum(a.register) / sum(a.active)
        else 0 end                           as activeRegisterRate,


        case
        when sum(a.download_finish) != 0
        then sum(a.cost) / sum(a.download_finish)
        else 0 end                           as downloadFinishCost,


        case
        when sum(a.register) != 0
        then sum(a.cost) / sum(a.register)
        else 0 end                           as activeRegisterCost,
        sum(a.show_material)                     as showMaterial,


        case
        when sum(a.download_start) != 0
        then sum(a.download_finish) / sum(a.download_start)
        else 0 end                           as downloadFinishRate,

        case
        when sum(a.download_finish) != 0
        then sum(a.install_finish) / sum(a.download_finish)
        else 0 end                           as installFinishRate,
        sum(a.play_over)                         as playOver,

        case
        when sum(a.download_start) != 0
        then sum(a.cost) / sum(a.download_start)
        else 0 end                           as downloadStartCost,

        case
        when sum(a.show_material) != 0
        then sum(a.valid_play) / sum(a.show_material)
        else 0 end                           as validPlayRate,
        case
        when sum(a.valid_play) != 0
        then sum(a.average_play_time_per_play*a.valid_play)/sum(a.valid_play)
        else 0 end as averagePlayTimePerPlay,
        case
        when sum(a.convert_material) != 0
        then sum(a.cost) / sum(a.convert_material)
        else 0 end                           as convertCost,

        case
        when sum(a.install_finish) != 0
        then sum(a.cost) / sum(a.install_finish)
        else 0 end                           as installFinishCost,

        case
        when sum(a.click) != 0
        then sum(a.download_start) / sum(a.click)
        else 0 end                           as downloadStartRate

        from ctop_bytedance_video_report_daily a
        left join (select material_id,clip_id,shot_id,plan_id,plane_id from ctop_material_ascription group by material_id) f on a.signature = f.material_id
        where stat_datetime &gt;= #{startDate}
        and stat_datetime &lt;= #{endDate}

        <if test=" role != 'admin' ">
            and a.company_id = #{companyId}
        </if>

        and f.material_id is not null

        <choose>
            <when test="role == 'plan'">
                and f.plan = #{userId}
            </when>
            <when test="role == 'clip'">
                and f.clip = #{userId}
            </when>
            <when test="role == 'shot' ">
                and f.shot = #{userId}
            </when>
            <when test="role == 'plane'  or role == 'planeLeader' ">
                and f.plan = #{userId}
            </when>
            <when test="role == 'admin' or role == 'designTeamLeader'">
                and 1=1
            </when>
            <otherwise>F
                and a.id = 0
            </otherwise>
        </choose>

        <if test="signature != null and signature != '' ">
            and a.signature = #{signature}
        </if>

        group by a.signature
        <if test="cost != null">
            having sum(a.cost) &gt;= #{cost}
        </if>
        order by sum(a.cost)
    </select>


</mapper>
