<?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.link.mapper.ETLReportBytedanceVideoMapper">
    <insert id="insertBatch">
        insert into etl_report_bytedance_video(
        material_id,
        material_name,
        md5,
        url,
        toutiao_url,
        cover_url,
        account_id,
        account_name,
        project_id,
        project_name,
        product_id,
        product_name,
        advertiser_id,
        advertiser_name,
        clip_id,
        clip_name,
        shot_id,
        shot_name,
        plan_id,
        plan_name,
        material_create_time,
        stat_datetime,
        click,
        cost,
        in_app_cart,
        in_app_order,
        in_app_pay,
        phone,
        form,
        map_search,
        view_material,
        qq,
        lottery,
        vote,
        redirect,
        shopping,
        consult,
        wechat,
        phone_confirm,
        phone_connect,
        consult_effective,
        total_play,
        valid_play,
        wifi_play,
        play25_feed_break_rate,
        play25_feed_break,
        play50_feed_break,
        play75_feed_break,
        play100_feed_break,
        advanced_creative_phone_click,
        advanced_creative_counsel_click,
        advanced_creative_form_click,
        share_material,
        comment_material,
        follow,
        home_visited,
        ies_challenge_click,
        ies_music_click,
        next_day_open,
        next_day_open_rate,
        next_day_open_cost,
        active_pay_amount,
        valid_play_cost,
        advanced_creative_coupon_addition,
        convert_material,
        active_pay_cost,
        download,
        cpc,
        location_click,
        play_over_rate,
        ctr,
        cpm,
        wifi_play_rate,
        like_material,
        active_cost,
        game_addiction_cost,
        game_addiction,
        active_rate,
        game_addiction_rate,
        active_register_rate,
        download_finish_cost,
        active_register_cost,
        convert_rate,
        download_finish_rate,
        install_finish_rate,
        coupon,
        coupon_single_page,
        play_over,
        download_start_cost,
        message,
        valid_play_rate,
        average_play_time_per_play,
        convert_cost,
        install_finish_cost,
        download_start_rate,
        show_material
        )
        values
        <foreach collection="mlist" item="image" separator=",">
            (#{image.accountId},
            #{image.projectId},
            #{image.projectName},
            #{image.advertiserId},
            #{image.advertiserName},
            #{image.signature},
            #{image.videoUrl},
            #{image.url},
            #{image.huanbi},
            #{image.materialName},
            #{image.appType},
            #{image.companyId},
            #{image.materialId},
            #{image.statDatetime},
            #{image.click},
            #{image.cost},
            #{image.active},
            #{image.downloadFinish},
            #{image.downloadStart},
            #{image.installFinish},
            #{image.register},
            #{image.payCount},
            #{image.inAppUv},
            #{image.inAppDetailUv},
            #{image.inAppCart},
            #{image.inAppOrder},
            #{image.inAppPay},
            #{image.phone},
            #{image.form},
            #{image.mapSearch},
            #{image.viewMaterial},
            #{image.qq},
            #{image.lottery},
            #{image.vote},
            #{image.redirect},
            #{image.shopping},
            #{image.consult},
            #{image.wechat},
            #{image.phoneConfirm},
            #{image.phoneConnect},
            #{image.consultEffective},
            #{image.totalPlay},
            #{image.validPlay},
            #{image.wifiPlay},
            #{image.play25FeedBreakRate},
            #{image.play25FeedBreak},
            #{image.play50FeedBreak},
            #{image.play75FeedBreak},
            #{image.play100FeedBreak},
            #{image.advancedCreativePhoneClick},
            #{image.advancedCreativeCounselClick},
            #{image.advancedCreativeFormClick},
            #{image.shareMaterial},
            #{image.commentMaterial},
            #{image.follow},
            #{image.homeVisited},
            #{image.iesChallengeClick},
            #{image.iesMusicClick},
            #{image.nextDayOpen},
            #{image.nextDayOpenRate},
            #{image.nextDayOpenCost},
            #{image.activePayAmount},
            #{image.validPlayCost},
            #{image.advancedCreativeCouponAddition},
            #{image.convertMaterial},
            #{image.activePayCost},
            #{image.download},
            #{image.cpc},
            #{image.locationClick},
            #{image.playOverRate},
            #{image.ctr},
            #{image.cpm},
            #{image.wifiPlayRate},
            #{image.likeMaterial},
            #{image.activeCost},
            #{image.gameAddictionCost},
            #{image.gameAddiction},
            #{image.activeRate},
            #{image.gameAddictionRate},
            #{image.activeRegisterRate},
            #{image.downloadFinishCost},
            #{image.activeRegisterCost},
            #{image.convertRate},
            #{image.downloadFinishRate},
            #{image.installFinishRate},
            #{image.coupon},
            #{image.couponSinglePage},
            #{image.playOver},
            #{image.downloadStartCost},
            #{image.message},
            #{image.validPlayRate},
            #{image.averagePlayTimePerPlay},
            #{image.convertCost},
            #{image.installFinishCost},
            #{image.downloadStartRate},
            #{image.showMaterial}
            )
        </foreach>
    </insert>
    <select id="queryVideoInfoByMaterialId"  resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            signature AS md5,
            video_url AS url
        FROM
            ctop_bytedance_video_info
        WHERE
            material_id = #{materialId}
        limit 1
    </select>
    <select id="queryMaterialVideoByMd5"  resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            `name`  as materialName,
            url,
            cover_url coverUrl,
            create_time as createTime
        FROM
            ctop_material_video_info
        WHERE
            md5 = #{md5}
        LIMIT 1
    </select>
    <select id="queryMaterialVideoByMd5Old"  resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            material_name as materialName,
            url,
            cover_url coverUrl,
            create_time as createTime
        FROM
            ctop_material_info
        WHERE
            code = #{md5}
        LIMIT 1
    </select>
    <select id="queryRelatePersonByMd5"  resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            clip_id clipId,
            clip_name clipaname,
            shot_id shotId,
            shot_name shotName,
            plan_id planId,
            plan_name planName
        FROM
            ctop_material_video_author
        WHERE
            video_md5 = #{md5}
        LIMIT 1
    </select>
    <select id="queryRelatePersonByMd5Old"  resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            clip_id clipId,
            (select realname from sys_user where id=clip_id) as clipName,
            shot_id shotId,
            (select realname from sys_user where id=shot_id) as shotName,
            plan_id planId,
            (select realname from sys_user where id=plan_id) as planName
        FROM
            ctop_material_ascription
        WHERE
            code = #{md5}
        LIMIT 1
    </select>
    <select id="queryAccountDetailByAccountId"  resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            t1.advertiser_id as advertiserId,
            (SELECT `name` from ctop_advertiser where id=t1.advertiser_id limit 1) as advertiserName,
            project_id as projectId,
            (SELECT `project_name` from ctop_project where id=t1.project_id limit 1) as projectName,
            auth_name as accountName
        FROM
            ctop_user_allocation t1
        WHERE
            account_id = #{accountId}
        LIMIT 1
    </select>

    <select id="queryETLData"  resultType="cn.com.ctop.toutiao.modules.link.entity.ETLReportBytedanceVideo">
        SELECT
            t1.*,
            t3.clip_id clipId,
            (select realname from sys_user where id=t3.clip_id) as clipName,
            t3.shot_id shotId,
            (select realname from sys_user where id=t3.shot_id) as shotName,
            t3.plan_id planId,
            (select realname from sys_user where id=t3.plan_id) as planName,
            t5.advertiser_id as advertiserId,
            (SELECT `name` from ctop_advertiser where id= t5.advertiser_id limit 1) as advertiserName,
            t5.project_id as projectId,
            (SELECT `project_name` from ctop_project where id= t5.project_id limit 1) as projectName,
            t5.auth_name as accountName,
            t4.material_name as materialName,
            t4.url,
            t4.cover_url coverUrl,
            t4.create_time as materialCreateTime,
            t2.signature AS md5,
            t2.video_url AS toutiaoUrl
        FROM
            (SELECT * from base_report_bytedance_video_daily where stat_datetime=#{statDate}) t1
                LEFT JOIN (select material_id,signature,video_url from ctop_bytedance_video_info where material_id in (SELECT DISTINCT material_id from base_report_bytedance_video_daily where stat_datetime=#{statDate})
            ) t2 ON t1.material_id = t2.material_id
                LEFT JOIN ctop_material_ascription t3 on t2.signature=t3.code
                LEFT JOIN ctop_material_info t4 on t4.code=t2.signature
                LEFT JOIN (SELECT account_id, advertiser_id,project_id,auth_name from ctop_user_allocation where account_id in (SELECT DISTINCT account_id from base_report_bytedance_video_daily where stat_datetime=#{statDate})) t5 on t1.account_id=t5.account_id
        GROUP BY t1.id
    </select>
</mapper>
