<?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="com.tzld.piaoquan.api.dao.mapper.contentplatform.ext.ContentPlatformDataStatMapperExt">

    <select id="getGzhAccountDatastatCount" resultType="java.lang.Integer">
        select count(1)
        from content_platform_gzh_datastat cpgd
        join content_platform_gzh_account cpgza on cpgd.account_id = cpgza.id
        where cpgza.create_account_id = #{createAccountId}
        <if test="param.accountId!= null and param.accountId!= ''">
            and cpgza.id = #{param.accountId}
        </if>
    </select>

    <select id="getGzhAccountDatastatList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformGzhDataStat">
        select cpgd.*
        from content_platform_gzh_datastat cpgd
        join content_platform_gzh_account cpgza on cpgd.account_id = cpgza.id
        where cpgza.create_account_id = #{createAccountId}
        <if test="param.accountId!= null and param.accountId!= ''">
            and cpgza.id = #{param.accountId}
        </if>
        order by cpgd.date_str desc, cpgd.account_id
        limit #{offset}, #{pageSize}
    </select>

    <select id="getGzhAccountVideoDatastatCount" resultType="java.lang.Integer">
        select count(1)
        from content_platform_gzh_video_datastat cpgvd
        join content_platform_gzh_account cpgza on cpgvd.account_id = cpgza.id
        where cpgza.create_account_id = #{createAccountId}
        <if test="param.accountId!= null and param.accountId!= ''">
            and cpgza.id = #{param.accountId}
        </if>
    </select>

    <select id="getGzhAccountVideoDatastatList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformGzhVideoDataStat">
        select cpgvd.*
        from content_platform_gzh_video_datastat cpgvd
        join content_platform_gzh_account cpgza on cpgvd.account_id = cpgza.id
        where cpgza.create_account_id = #{createAccountId}
        <if test="param.accountId!= null and param.accountId!= ''">
            and cpgza.id = #{param.accountId}
        </if>
        order by cpgvd.date_str desc
        limit #{offset}, #{pageSize}
    </select>

    <select id="getGzhTotalDatastatCount" resultType="java.lang.Integer">
        select count(1)
        from content_platform_gzh_datastat_total cpgdt
        join content_platform_account cpa on cpgdt.channel = cpa.channel
        where cpa.id = #{createAccountId}
    </select>

    <select id="getGzhTotalDatastatList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformGzhDataStatTotal">
        select date_str, fans_increase_count, first_level_count, score, unit_price, settlement_amount
        from content_platform_gzh_datastat_total cpgdt
         join content_platform_account cpa on cpgdt.channel = cpa.channel
        where cpa.id = #{createAccountId}
        order by cpgdt.date_str desc
        limit #{offset}, #{pageSize}
    </select>

    <select id="getFwhAccountDatastatCount" resultType="java.lang.Integer">
        select count(1)
        from content_platform_fwh_datastat cpgd
        join content_platform_gzh_account cpgza on cpgd.account_id = cpgza.id
        where cpgza.create_account_id = #{createAccountId}
        <if test="param.accountId!= null and param.accountId!= ''">
            and cpgza.id = #{param.accountId}
        </if>
    </select>

    <select id="getFwhAccountDatastatList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformGzhDataStat">
        select cpfd.*
        from content_platform_fwh_datastat cpfd
        join content_platform_gzh_account cpgza on cpfd.account_id = cpgza.id
        where cpgza.create_account_id = #{createAccountId}
        <if test="param.accountId!= null and param.accountId!= ''">
            and cpgza.id = #{param.accountId}
        </if>
        order by cpfd.date_str desc
        limit #{offset}, #{pageSize}
    </select>

    <select id="getFwhTotalDatastatCount" resultType="java.lang.Integer">
        select count(1)
        from content_platform_fwh_datastat_total cpfdt
         join content_platform_account cpa on cpfdt.channel = cpa.channel
        where cpa.id = #{createAccountId}
    </select>

    <select id="getFwhTotalDatastatList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformFwhDataStatTotal">
        select date_str, fans_increase_count, first_level_count, score, unit_price, settlement_amount
        from content_platform_fwh_datastat_total cpfdt
         join content_platform_account cpa on cpfdt.channel = cpa.channel
        where cpa.id = #{createAccountId}
        order by cpfdt.date_str desc
        limit #{offset}, #{pageSize}
    </select>

    <select id="getGzhPushTotalDatastatCount" resultType="java.lang.Integer">
        select count(1)
        from content_platform_gzh_push_datastat_total cpfdt
         join content_platform_account cpa on cpfdt.channel = cpa.channel
        where cpa.id = #{createAccountId}
    </select>

    <select id="getGzhPushTotalDatastatList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformGzhPushDataStatTotal">
        select date_str, fans_increase_count, first_level_count, score, unit_price, settlement_amount
        from content_platform_gzh_push_datastat_total cpfdt
         join content_platform_account cpa on cpfdt.channel = cpa.channel
        where cpa.id = #{createAccountId}
        order by cpfdt.date_str desc
        limit #{offset}, #{pageSize}
    </select>

    <select id="getGzhPushAccountDatastatCount" resultType="java.lang.Integer">
        select count(1)
        from content_platform_gzh_push_datastat cpgd
        join content_platform_gzh_account cpgza on cpgd.account_id = cpgza.id
        where cpgza.create_account_id = #{createAccountId}
        <if test="param.accountId!= null and param.accountId!= ''">
            and cpgza.id = #{param.accountId}
        </if>
    </select>

    <select id="getGzhPushAccountDatastatList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformGzhDataStat">
        select cpfd.*
        from content_platform_gzh_push_datastat cpfd
        join content_platform_gzh_account cpgza on cpfd.account_id = cpgza.id
        where cpgza.create_account_id = #{createAccountId}
        <if test="param.accountId!= null and param.accountId!= ''">
            and cpgza.id = #{param.accountId}
        </if>
        order by cpfd.date_str desc
        limit #{offset}, #{pageSize}
    </select>

    <select id="getQwRootSourceIdDatastatCount" resultType="java.lang.Integer">
        select count(1)
        from content_platform_qw_datastat cpqd
         join content_platform_qw_plan cpqp on cpqp.root_source_id = cpqd.root_source_id
        where cpqp.create_account_id = #{createAccountId}
    </select>

    <select id="getQwRootSourceIdDatastatList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformQwDataStat">
        select cpqd.*
        from content_platform_qw_datastat cpqd
         join content_platform_qw_plan cpqp on cpqp.root_source_id = cpqd.root_source_id
        where cpqp.create_account_id = #{createAccountId}
        order by cpqd.date_str desc
        limit #{offset}, #{pageSize}
    </select>

    <select id="getQwTotalDatastatCount" resultType="java.lang.Integer">
        select count(1)
        from content_platform_qw_datastat_total
        where channel = #{channel}
    </select>

    <select id="getQwTotalDatastatList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformQwDataStatTotal">
        select date_str, channel, first_level_count, score, unit_price, settlement_amount
        from content_platform_qw_datastat_total
        where channel = #{channel}
        order by date_str desc, id
        limit #{offset}, #{pageSize}
    </select>

    <select id="getQwReplyTotalDatastatCount" resultType="java.lang.Integer">
        select count(1)
        from content_platform_qw_datastat_reply_total
        where channel = #{channel}
    </select>

    <select id="getQwReplyTotalDatastatList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformQwDataStatReplyTotal">
        select date_str, channel, first_level_count, score
        from content_platform_qw_datastat_reply_total
        where channel = #{channel}
        order by date_str desc, id
        limit #{offset}, #{pageSize}
    </select>

    <select id="getQwSubChannelDatastatCount" resultType="java.lang.Integer">
        select count(1)
        from content_platform_qw_datastat_sub_channel
        where create_account_id = #{createAccountId}
    </select>

    <select id="getQwSubChannelDatastatList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformQwDataStatSubChannel">
        select date_str, sub_channel, first_level_count, score, unit_price, settlement_amount
        from content_platform_qw_datastat_sub_channel
        where create_account_id = #{createAccountId}
        order by date_str desc, id
        limit #{offset}, #{pageSize}
    </select>

    <select id="getQwSceneDatastatCount" resultType="java.lang.Integer">
        select count(1)
        from (select cpqd.date_str
        from content_platform_qw_datastat cpqd
         join content_platform_qw_plan cpqp on cpqp.root_source_id = cpqd.root_source_id
        where cpqp.create_account_id = #{createAccountId}
          and cpqp.scene = #{scene}
        group by cpqd.date_str) t
    </select>

    <select id="getQwSceneDatastatList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformQwDataStat">
        select cpqd.date_str, sum(cpqd.first_level_count) as first_level_count, avg(cpqd.score) as score
        from content_platform_qw_datastat cpqd
         join content_platform_qw_plan cpqp on cpqp.root_source_id = cpqd.root_source_id
        where cpqp.create_account_id = #{createAccountId}
          and cpqp.scene = #{scene}
        group by cpqd.date_str
        order by cpqd.date_str desc
        limit #{offset}, #{pageSize}
    </select>

    <select id="getQwReplyDatastatCount" resultType="java.lang.Integer">
        select count(1)
        from (select cpqd.date_str
        from content_platform_qw_datastat cpqd
         join content_platform_qw_plan cpqp on cpqp.root_source_id = cpqd.root_source_id
        where cpqp.create_account_id = #{createAccountId}
          and cpqp.type = 1
        group by cpqd.date_str) t
    </select>

    <select id="getReplyDatastatList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformQwDataStat">
        select cpqd.date_str, sum(cpqd.first_level_count) as first_level_count, avg(cpqd.score) as score
        from content_platform_qw_datastat cpqd
         join content_platform_qw_plan cpqp on cpqp.root_source_id = cpqd.root_source_id
        where cpqp.create_account_id = #{createAccountId}
          and cpqp.type = 1
        group by cpqd.date_str
        order by cpqd.date_str desc
        limit #{offset}, #{pageSize}
    </select>

    <insert id="batchInsertGzhDatastat">
        insert into content_platform_gzh_datastat (date_str, account_id, fans_increase_count, first_level_count, score,
        create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dateStr}, #{item.accountId}, #{item.fansIncreaseCount}, #{item.firstLevelCount}, #{item.score},
            #{item.createTimestamp})
        </foreach>
    </insert>

    <insert id="batchInsertGzhVideoDatastat">
        insert into content_platform_gzh_video_datastat (date_str, account_id, video_id, title, first_level_count,
                                                         score, create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dateStr}, #{item.accountId}, #{item.videoId}, #{item.title}, #{item.firstLevelCount}, #{item.score},
            #{item.createTimestamp})
        </foreach>
    </insert>

    <insert id="batchInsertGzhDatastatTotal">
        insert into content_platform_gzh_datastat_total (date_str, channel, fans_increase_count, first_level_count, score,
        unit_price, settlement_amount, create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dateStr}, #{item.channel}, #{item.fansIncreaseCount}, #{item.firstLevelCount}, #{item.score},
            #{item.unitPrice}, #{item.settlementAmount}, #{item.createTimestamp})
        </foreach>
    </insert>

    <insert id="batchInsertFwhDatastat">
        insert into content_platform_fwh_datastat (date_str, account_id, fans_increase_count, first_level_count, score,
        create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dateStr}, #{item.accountId}, #{item.fansIncreaseCount}, #{item.firstLevelCount}, #{item.score},
            #{item.createTimestamp})
        </foreach>
    </insert>

    <insert id="batchInsertFwhDatastatTotal">
        insert into content_platform_fwh_datastat_total (date_str, channel, fans_increase_count, first_level_count, score,
        unit_price, settlement_amount, create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dateStr}, #{item.channel}, #{item.fansIncreaseCount}, #{item.firstLevelCount}, #{item.score},
            #{item.unitPrice}, #{item.settlementAmount}, #{item.createTimestamp})
        </foreach>
    </insert>

    <insert id="batchInsertQwDatastat">
        insert into content_platform_qw_datastat (date_str, root_source_id, first_level_count, score, create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dateStr}, #{item.rootSourceId}, #{item.firstLevelCount}, #{item.score}, #{item.createTimestamp})
        </foreach>
    </insert>

    <insert id="batchInsertQwDatastatTotal">
        insert into content_platform_qw_datastat_total (date_str, channel, first_level_count, score, unit_price,
                                                        settlement_amount, create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dateStr}, #{item.channel}, #{item.firstLevelCount}, #{item.score}, #{item.unitPrice},
             #{item.settlementAmount}, #{item.createTimestamp})
        </foreach>
    </insert>

    <insert id="batchInsertQwDatastatReplyTotal">
        insert into content_platform_qw_datastat_reply_total (date_str, channel, first_level_count, score, create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dateStr}, #{item.channel}, #{item.firstLevelCount}, #{item.score}, #{item.createTimestamp})
        </foreach>
    </insert>

    <insert id="batchInsertQwDatastatSubChannel">
        insert into content_platform_qw_datastat_sub_channel (date_str, sub_channel, first_level_count, score,
                                                              create_account_id, unit_price, settlement_amount, create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dateStr}, #{item.subChannel}, #{item.firstLevelCount}, #{item.score}, #{item.createAccountId},
            #{item.unitPrice}, #{item.settlementAmount}, #{item.createTimestamp})
        </foreach>
    </insert>

    <insert id="batchInsertGzhPushDatastat">
        insert into content_platform_gzh_push_datastat (date_str, account_id, fans_increase_count, first_level_count, score,
        create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dateStr}, #{item.accountId}, #{item.fansIncreaseCount}, #{item.firstLevelCount}, #{item.score},
            #{item.createTimestamp})
        </foreach>
    </insert>

    <insert id="batchInsertGzhPushDatastatTotal">
        insert into content_platform_gzh_push_datastat_total (date_str, channel, fans_increase_count, first_level_count, score,
        unit_price, settlement_amount, create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dateStr}, #{item.channel}, #{item.fansIncreaseCount}, #{item.firstLevelCount}, #{item.score},
            #{item.unitPrice}, #{item.settlementAmount}, #{item.createTimestamp})
        </foreach>
    </insert>

    <select id="getQwTotalDatastatScoreList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformQwDataStatTotal">
        select cpa.channel, round(avg(cpqd.score), 2) as score
        from content_platform_qw_datastat cpqd
        join content_platform_qw_plan cpqp on cpqp.root_source_id = cpqd.root_source_id
        join content_platform_account cpa on cpa.id = cpqp.create_account_id
        where cpqd.date_str = #{dt}
        group by cpa.channel
    </select>

    <select id="getQwTotalDatastatReplyScoreList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformQwDataStatReplyTotal">
        select cpa.channel, round(avg(cpqd.score), 2) as score
        from content_platform_qw_datastat cpqd
        join content_platform_qw_plan cpqp on cpqp.root_source_id = cpqd.root_source_id
        join content_platform_account cpa on cpa.id = cpqp.create_account_id
        where cpqd.date_str = #{dt} and cpqp.type = 1
        group by cpa.channel
    </select>

    <delete id="deleteQwDatastat">
        delete from content_platform_qw_datastat where date_str = #{dt}
    </delete>

    <delete id="deleteQwDatastatTotal">
        delete from content_platform_qw_datastat_total where date_str = #{dt}
    </delete>

    <delete id="deleteQwDatastatReplyTotal">
        delete from content_platform_qw_datastat_reply_total where date_str = #{dt}
    </delete>

    <delete id="deleteQwDatastatSubChannel">
        delete from content_platform_qw_datastat_sub_channel where date_str = #{dt}
    </delete>

    <delete id="deleteGzhDatastat">
        delete from content_platform_gzh_datastat where date_str = #{dt}
    </delete>

    <delete id="deleteGzhDatastatTotal">
        delete from content_platform_gzh_datastat_total where date_str = #{dt}
    </delete>

    <delete id="deleteFwhDatastat">
        delete from content_platform_fwh_datastat where date_str = #{dt}
    </delete>

    <delete id="deleteFwhDatastatTotal">
        delete from content_platform_fwh_datastat_total where date_str = #{dt}
    </delete>

    <delete id="deleteGzhVideoDatastat">
        delete from content_platform_gzh_video_datastat where date_str = #{dt}
    </delete>

    <delete id="deleteGzhPushDatastat">
        delete from content_platform_gzh_push_datastat where date_str = #{dt}
    </delete>

    <delete id="deleteGzhPushDatastatTotal">
        delete from content_platform_gzh_push_datastat_total where date_str = #{dt}
    </delete>

</mapper>