<?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.BytedanceCreativeDailyReportMapper">
    <select id="bytedanceCreativeDailyReport" parameterType="Map"
            resultType="cn.com.ctop.toutiao.modules.report.entity.BytedanceCreativeDailyReport">
    select
        t2.creative_id as creativeId,  <!-- 素材id -->
        t4.video_url as videoUrl,  <!-- 视频url -->
        sum(t2.show_num) as showNum,  <!-- 展示量 -->
        sum(t2.click) as click,  <!-- 点击量 -->
        sum(t2.convert_num) as convertNum,  <!-- 转化量 -->
        sum(t2.cost) as cost,  <!-- 总花费 -->
        sum(t2.active) as active, <!-- 应用下载-激活 -->
        sum(t2.download_finish) as downloadFinish,  <!-- 应用下载-安卓下载完成 -->
        sum(t2.click_install) as clickInstall,  <!-- 应用下载-点击安装数 -->
        sum(t2.in_app_pay) as inAppPay, <!-- 应用下载-付费 -->
        sum(t2.total_play) as totalPlay, <!-- 视频-总播放 -->
        sum(t2.valid_play) as validPlay, <!-- 视频-有效播放 -->
        sum(t2.wifi_play) as wifiPlay, <!-- 视频-wifi播放 -->
        sum(t2.play100_feed_break) as play100FeedBreak,  <!-- 视频-播放100%进度总数 -->
        sum(t2.play75_feed_break) as play75FeedBreak,  <!-- 视频-播放75%进度总数 -->
        sum(t2.comment) as comment,  <!-- 互动数据-评论数 -->
        AVG(t2.next_day_open) as nextDayOpen,  <!-- 次留数 -->
        AVG(t2.next_day_open_rate) as nextDayOpenRate,  <!-- 次留率 -->
        AVG(t2.next_day_open_cost) as nextDayOpenCost  <!-- 次留成本 -->
        from
        ctop_user_allocation t1
        left join ctop_bytedance_report_creative_daily t2 on t1.account_id = t2.advertiser_id
        left join ctop_bytedance_creative t3 on t2.creative_id = t3.id
        left join ctop_bytedance_video_info t4 on t3.video_id = t4.id
        where
        t3.id is not NULL and t4.id is not null
        and t1.media_id = '1'
        and t1.account_id != ''
        <if test="userId != null">
            and t1.user_id = #{userId,jdbcType=VARCHAR}
        </if>

        <if test="startDate != null">
            and date_format(t2.stat_datetime,'%Y-%m-%d') &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            and date_format(t2.stat_datetime,'%Y-%m-%d') &lt;= #{endDate}
        </if>
        group by t4.signature
        order by sum(t2.cost) desc
    </select>

    <select id="bytedanceCreativeAccountReport" parameterType="Map"
            resultType="cn.com.ctop.toutiao.modules.report.entity.BytedanceCreativeDailyReport">
        select
        t2.creative_id as creativeId,  <!-- 素材id -->
        t4.video_url as videoUrl,  <!-- 视频url -->
        sum(t2.show_num) as showNum,  <!-- 展示量 -->
        sum(t2.click) as click,  <!-- 点击量 -->
        sum(t2.convert_num) as convertNum,  <!-- 转化量 -->
        sum(t2.cost) as cost,  <!-- 总花费 -->
        sum(t2.active) as active, <!-- 应用下载-激活 -->
        sum(t2.download_finish) as downloadFinish,  <!-- 应用下载-安卓下载完成 -->
        sum(t2.click_install) as clickInstall,  <!-- 应用下载-点击安装数 -->
        sum(t2.in_app_pay) as inAppPay, <!-- 应用下载-付费 -->
        sum(t2.total_play) as totalPlay, <!-- 视频-总播放 -->
        sum(t2.valid_play) as validPlay, <!-- 视频-有效播放 -->
        sum(t2.wifi_play) as wifiPlay, <!-- 视频-wifi播放 -->
        sum(t2.play100_feed_break) as play100FeedBreak,  <!-- 视频-播放100%进度总数 -->
        sum(t2.play75_feed_break) as play75FeedBreak,  <!-- 视频-播放75%进度总数 -->
        sum(t2.comment) as comment,  <!-- 互动数据-评论数 -->
        AVG(t2.next_day_open) as nextDayOpen,  <!-- 次留数 -->
        AVG(t2.next_day_open_rate) as nextDayOpenRate,  <!-- 次留率 -->
        AVG(t2.next_day_open_cost) as nextDayOpenCost  <!-- 次留成本 -->
        from
        ctop_user_allocation t1
        left join ctop_bytedance_report_creative_daily t2 on t1.account_id = t2.advertiser_id
        left join ctop_bytedance_creative t3 on t2.creative_id = t3.id
        left join ctop_bytedance_video_info t4 on t3.video_id = t4.id
        where t1.account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        and t3.id is not NULL and t4.id is not null
        and t1.media_id = '1'
        <if test="startDate != null">
            and date_format(t2.stat_datetime,'%Y-%m-%d') &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            and date_format(t2.stat_datetime,'%Y-%m-%d') &lt;= #{endDate}
        </if>
        and t3.id is not NULL
        group by t4.signature
        order by sum(t2.cost) desc
    </select>

    <insert id="replaceBatch">
        replace into ctop_bytedance_report_creative_daily(
        id,
        advertiser_id ,
        stat_datetime,
        show_num,
        click,
        convert_num,
        cost,
        active,
        download_finish,
        download_start,
        click_install,
        install_finish,
        register,
        pay_count,
        in_app_uv,
        in_app_detail_uv,
        in_app_cart,
        in_app_order,
        in_app_pay,
        phone,
        form,
        map_search,
        button,
        view,
        qq,
        lottery,
        vote,
        redirect,
        shopping,
        consult,
        wechat,
        phone_confirm,
        phone_connect,
        consult_effective,
        total_play,
        valid_play,
        wifi_play,
        play_duration_sum,
        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,
        comment,
        like_num,
        follow,
        home_visited,
        ies_challenge_click,
        ies_music_click,
        interact_per_cost,
        next_day_open,
        next_day_open_rate,
        next_day_open_cost,
        ad_id,
        ad_name,
        campaign_id,
        create_time,
        update_time,
        creative_id,
        signature)
        values
        <foreach collection="reports" item="report" separator=",">
            (
            #{report.id},
            #{report.advertiserId},
            #{report.statDatetime},
            #{report.showNum},
            #{report.click},
            #{report.convertNum},
            #{report.cost},
            #{report.active},
            #{report.downloadFinish},
            #{report.downloadStart},
            #{report.clickInstall},
            #{report.installFinish},
            #{report.register},
            #{report.payCount},
            #{report.inAppUv},
            #{report.inAppDetailUv},
            #{report.inAppCart},
            #{report.inAppOrder},
            #{report.inAppPay},
            #{report.phone},
            #{report.form},
            #{report.mapSearch},
            #{report.button},
            #{report.view},
            #{report.qq},
            #{report.lottery},
            #{report.vote},
            #{report.redirect},
            #{report.shopping},
            #{report.consult},
            #{report.wechat},
            #{report.phoneConfirm},
            #{report.phoneConnect},
            #{report.consultEffective},
            #{report.totalPlay},
            #{report.validPlay},
            #{report.wifiPlay},
            #{report.playDurationSum},
            #{report.play25FeedBreak},
            #{report.play50FeedBreak},
            #{report.play75FeedBreak},
            #{report.play100FeedBreak},
            #{report.advancedCreativePhoneClick},
            #{report.advancedCreativeCounselClick},
            #{report.advancedCreativeFormClick},
            #{report.share},
            #{report.comment},
            #{report.likeNum},
            #{report.follow},
            #{report.homeVisited},
            #{report.iesChallengeClick},
            #{report.iesMusicClick},
            #{report.interactPerCost},
            #{report.nextDayOpen},
            #{report.nextDayOpenRate},
            #{report.nextDayOpenCost},
            #{report.adId},
            #{report.adName},
            #{report.campaignId},
            #{report.createTime},
            #{report.updateTime},
            #{report.creativeId},
            #{report.signature}
            )
        </foreach>
    </insert>

    <select id="getSignatureList" resultType="java.lang.String">
    SELECT
      DISTINCT(signature)
      from ctop_bytedance_report_creative_daily
      WHERE signature !=''
    </select>
    <select id="calculationMaterialReport"
            resultType="cn.com.ctop.toutiao.modules.report.entity.ByteDanceCleanMaterialReport">
        select
        signature id,
        sum(cost) cost,
        sum(show_num) showNum,
        sum(click) click,
        sum(convert_num) convertNum,
        sum(active) active,
        sum(register) register,
        sum(form) form,
        sum(total_play) totalPlay,
        sum(valid_play) validPlay
        from ctop_bytedance_report_creative_daily
        where
        signature = #{signature}
    </select>

</mapper>
