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

    <select id="getCtopMaterialAscriptionInfo" resultType="cn.com.ctop.toutiao.modules.link.entity.ETLReportBytedanceVideo">
        SELECT DISTINCT
            md5,
            cma.clip_id,
            cma.clip_name,
            cma.shot_id,
            cma.shot_name,
            cma.plan_id,
            cma.plan_name
        FROM
            etl_report_bytedance_video video
        LEFT JOIN ctop_material_ascription cma ON cma. CODE = video.md5
        where
		cma.clip_id is not null or
		cma.clip_name is not null or
		cma.shot_id is not null or
		cma.shot_name is not null or
		cma.plan_id is not null or
		cma.plan_name is not null;
    </select>

    <update id="updateOneBytedanceVideoInfo"  parameterType="cn.com.ctop.toutiao.modules.link.entity.ETLReportBytedanceVideo">
        UPDATE etl_report_bytedance_video
        <trim prefix="set" suffixOverrides=",">
            <if test="etlReportBytedanceVideo.clipId!=null">clip_id=#{etlReportBytedanceVideo.clipId},</if>
            <if test="etlReportBytedanceVideo.clipName!=null">clip_name=#{etlReportBytedanceVideo.clipName},</if>
            <if test="etlReportBytedanceVideo.shotId!=null">shot_id=#{etlReportBytedanceVideo.shotId},</if>
            <if test="etlReportBytedanceVideo.shotName!=null">shot_name=#{etlReportBytedanceVideo.shotName},</if>
            <if test="etlReportBytedanceVideo.planId!=null">plan_id=#{etlReportBytedanceVideo.planId},</if>
            <if test="etlReportBytedanceVideo.planName!=null">plan_name=#{etlReportBytedanceVideo.planName},</if>
        </trim>
        WHERE md5=#{etlReportBytedanceVideo.md5}
    </update>


    <select id="getBytedanceVideoInfo" resultType="java.util.Map">
        SELECT
            IF
                ( poster_url = '素材所属主体与开发者主体不一致无法获取URL', '', poster_url ) AS posterUrl,
            poster_url,
            video_url AS videoUrl,
            a.md5,
            count( 1 ) AS total
        FROM
            etl_report_bytedance_video a
                LEFT JOIN ( SELECT DISTINCT signature, poster_url, video_url FROM ctop_bytedance_video_info ) b ON b.signature = a.md5
                LEFT JOIN ( SELECT DISTINCT CODE, url FROM ctop_material_info ) c ON c.CODE = a.md5
        WHERE
            a.url = ''
          AND a.toutiao_url = ''
          AND a.md5 != ''
        GROUP BY
            a.md5
        ORDER BY
            total DESC;
    </select>
    <select id="etlBytedanceVideoInfo"
            resultType="cn.com.ctop.toutiao.modules.link.entity.ETLReportBytedanceVideo">
             select
                      (case when cmi.url is not null then cmi.url else '' end) 'url',
                          (CASE when cmi.cover_url is null then ''
                                ELSE cmi.cover_url end) 'cover_url',
                          (case when cmi.material_name is null then '' else cmi.material_name end)  'material_name',
                          (case when cbvi.signature is null then '' else cbvi.signature end)'md5',
                          report.material_id 'material_id',
                          cp.project_name 'project_name',
                          case when etlVideo.clip_name is null then '' else etlVideo.clip_name end 'clip_name',
                          case when etlVideo.plan_name is null then '' else etlVideo.plan_name end 'plan_name',
                          case when etlVideo.shot_name is null then '' else etlVideo.shot_name end 'shot_name',
                          case when cmi.create_time is not null then cmi.create_time else '' end 'material_create_time',
                          sum(report.cost) 'cost',
                          sum(report.click) 'click_material',
                          (case when sum(report.show_material)>0 and sum(report.click)>0 then round(sum(report.click)/sum(report.show_material)*100) else 0 end) 'ctr',
                          sum(report.show_material) 'show_material',
                          sum(report.convert_material) 'convert_material',
                          (case when sum(report.convert_material)>0 and sum(report.click)>0 then round(sum(report.convert_material)/sum(report.click)*100) else 0 end) 'convert_rate',
                          (case when sum(report.cost) >0 and sum(report.show_material)>0 then round(sum(report.cost)/sum(report.show_material)*1000,2) else 0 end) 'cpm',
                          (case when sum(report.cost) >0 and sum(report.click)>0 then round(sum(report.cost)/sum(report.click),2) else 0 end) 'cpc',
                          (case when sum(report.cost) >0 and sum(report.convert_material)>0 then round(sum(report.cost)/sum(report.convert_material),2) else 0 end) 'convert_cost',
                          sum(report.download_start) 'download_start',
                          sum(report.download_finish) 'download_finish',
                          sum(report.active) 'active',
                          sum(report.register) 'register',
                          sum(report.next_day_open) 'next_day_open',
                          sum(report.pay_count) 'active_pay_amount',
                          (case when sum(report.cost) >0 and sum(report.pay_count)>0 then round(sum(report.cost)/sum(report.pay_count),2) else 0 end)  'active_pay_cost',
                          (case when sum(report.cost) >0 and sum(report.next_day_open)>0 then round(sum(report.cost)/sum(report.next_day_open),2) else 0 end) 'next_day_open_cost',
                          (case when sum(report.cost) >0 and sum(report.register)>0 then round(sum(report.cost)/sum(report.register),2) else 0 end) 'active_register_cost',
                          (case when sum(report.cost) >0 and sum(report.active)>0 then round(sum(report.cost)/sum(report.active),2) else 0 end) 'active_cost',
                          (case when sum(report.cost) >0 and sum(report.download_finish)>0 then round(sum(report.cost)/sum(report.download_finish),2) else 0 end) 'download_finish_cost',
                          (case when sum(report.cost) >0 and sum(report.download_start)>0 then round(sum(report.cost)/sum(report.download_start),2) else 0 end) 'download_start_cost',
                          (case when sum(report.download_start) >0 and sum(report.click)>0 then round(sum(report.download_start)/sum(report.click)*100,2) else 0 end) 'download_start_rate',
                          (case when sum(report.download_finish) >0 and sum(report.download_start)>0 then round(sum(report.download_finish)/sum(report.download_start)*100,2) else 0 end) 'download_finish_rate',
                          (case when sum(report.active) >0 and sum(report.click)>0 then round(sum(report.active)/sum(report.click)*100,2) else 0 end) 'active_rate',
                          (case when sum(report.register) >0 and sum(report.active)>0 then round(sum(report.register)/sum(report.active)*100,2) else 0 end) 'active_register_rate',
                          (case when sum(report.active) >0 and sum(report.next_day_open)>0 then round(sum(report.next_day_open)/sum(report.active)*100,2) else 0 end) 'next_day_open_rate',
                          sum(report.form) 'form',
                          (case when sum(report.cost) >0 and sum(report.valid_play)>0 then round(sum(report.cost)/sum(report.valid_play),2) else 0 end) 'valid_play_cost',
                          sum(report.play75_feed_break) 'play75_feed_break',
                          sum(report.play100_feed_break) 'play100_feed_break',
                          sum(report.play25_feed_break) 'play25_feed_break',
                          sum(report.play50_feed_break) 'play50_feed_break',
                          sum(report.share_material) 'share_material',
                          sum(report.follow) 'follow',
                          sum(report.like_material) 'like_material',
                          report.stat_datetime 'stat_datetime',
                      report.account_id 'account_id'
  from ctop_bytedance_report_material_daily report
           left join ctop_user_allocation cua on cua.account_id = report.account_id
           left join (select distinct account_id,material_id,signature from ctop_bytedance_video_info ) cbvi
                     on report.account_id = cbvi.account_id and report.material_id = cbvi.material_id
           left join ctop_bytedance_video_etl_info etlVideo on etlVideo.video_code = cbvi.signature
           left join ctop_material_info cmi on cmi.code = cbvi.signature and cmi.project_id = cua.project_id
           left join ctop_project cp on cp.id = cua.project_id
  where 1 = 1
    and report.stat_datetime = #{date}
  group by report.material_id,report.account_id;
    </select>
    <select id="replaceEtlVideoInfo">
        REPLACE INTO etl_report_bytedance_video(
            url,
            cover_url,
            material_name,
            md5,
            material_id,
            project_name ,
            clip_name,
            plan_name,
            shot_name,
            material_create_time,
            cost,
            click,
            ctr,
            material_show,
            convert_material,
            convert_rate,
            cpm,
            cpc,
            convert_cost,
            download_start,
            download_finish,
            active,
            register,
            next_day_open,
            pay_count,
            active_pay_cost,
            next_day_open_cost,
            active_register_cost,
            active_cost,
            download_finish_cost,
            download_start_cost,
            download_start_rate,
            download_finish_rate,
            active_rate,
            active_register_rate,
            next_day_open_rate,
            form,
            valid_play_cost,
            play75_feed_break,
            play100_feed_break,
            play25_feed_break,
            play50_feed_break,
            share_material,
            follow,
            like_material,
            stat_datetime,
            account_id)
    values (
            #{videoInfo.url},
            #{videoInfo.coverUrl},
            #{videoInfo.materialName},
            #{videoInfo.md5},
            #{videoInfo.materialId},
            #{videoInfo.projectName },
            #{videoInfo.clipName},
            #{videoInfo.planName},
            #{videoInfo.shotName},
            #{videoInfo.materialCreateTime},
            #{videoInfo.cost},
            #{videoInfo.click},
            #{videoInfo.ctr},
            #{videoInfo.materialShow},
            #{videoInfo.convertMaterial},
            #{videoInfo.convertRate},
            #{videoInfo.cpm},
            #{videoInfo.cpc},
            #{videoInfo.convertCost},
            #{videoInfo.downloadStart},
            #{videoInfo.downloadFinish},
            #{videoInfo.active},
            #{videoInfo.register},
            #{videoInfo.nextDayOpen},
            #{videoInfo.payCount},
            #{videoInfo.activePayCost},
            #{videoInfo.nextDayOpenCost},
            #{videoInfo.activeRegisterCost},
            #{videoInfo.activeCost},
            #{videoInfo.downloadFinishCost},
            #{videoInfo.downloadStartCost},
            #{videoInfo.downloadStartRate},
            #{videoInfo.downloadFinishRate},
            #{videoInfo.activeRate},
            #{videoInfo.activeRegisterRate},
            #{videoInfo.nextDayOpenRate},
            #{videoInfo.form},
            #{videoInfo.validPlayCost},
            #{videoInfo.play75FeedBreak},
            #{videoInfo.play100FeedBreak},
            #{videoInfo.play25FeedBreak},
            #{videoInfo.play50FeedBreak},
            #{videoInfo.shareMaterial},
            #{videoInfo.follow},
            #{videoInfo.likeMaterial},
            #{videoInfo.statDatetime},
            #{videoInfo.accountId})
    </select>


    <update id="updBytedanceVideoByMd5">
        UPDATE etl_report_bytedance_video
        SET cover_url = #{coverUrl},
            url = #{url}
        WHERE
            md5 = #{md5};
    </update>



</mapper>
