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

    <insert id="insertGzhPlanReturnId" useGeneratedKeys="true" keyProperty="id">
        insert into content_platform_gzh_plan (`type`, publish_stage, account_id, scene, external_id,
                                               select_video_type, stage_publish_status,
                                               create_account_id, create_timestamp, update_timestamp)
        values (#{record.type}, #{record.publishStage}, #{record.accountId}, #{record.scene}, #{record.externalId},
                #{record.selectVideoType}, #{record.stagePublishStatus}, #{record.createAccountId},
                #{record.createTimestamp}, #{record.updateTimestamp})
    </insert>

    <select id="getGzhPlanCount" resultType="java.lang.Integer">
        select count(*)
        from content_platform_gzh_plan
        where create_account_id = #{createAccountId} and status = 1
        <if test="param.type != null">
            and type = #{param.type}
        </if>
        <if test="param.accountId != null">
            and account_id = #{param.accountId}
        </if>
        <if test="param.publishStage != null">
            and publish_stage = #{param.publishStage}
        </if>
        <if test="param.selectVideoType != null">
            and select_video_type = #{param.selectVideoType}
        </if>
        <if test="param.createTimestampStart != null">
            and create_timestamp > #{param.createTimestampStart}
        </if>
        <if test="param.createTimestampEnd != null">
            and create_timestamp &lt; #{param.createTimestampEnd}
        </if>
        <if test="param.title != null and param.title != ''">
            and id in (select plan_id
            from content_platform_gzh_plan_video
            where title like concat('%', #{param.title}, '%')
            or custom_title like concat('%', #{param.title}, '%'))
        </if>
    </select>

    <select id="getGzhPlanList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformGzhPlan">
        select *
        from content_platform_gzh_plan
        where create_account_id = #{createAccountId} and status = 1
        <if test="param.type != null">
            and type = #{param.type}
        </if>
        <if test="param.accountId != null">
            and account_id = #{param.accountId}
        </if>
        <if test="param.publishStage != null">
            and publish_stage = #{param.publishStage}
        </if>
        <if test="param.selectVideoType != null">
            and select_video_type = #{param.selectVideoType}
        </if>
        <if test="param.createTimestampStart != null">
            and create_timestamp > #{param.createTimestampStart}
        </if>
        <if test="param.createTimestampEnd != null">
            and create_timestamp &lt; #{param.createTimestampEnd}
        </if>
        <if test="param.title != null and param.title != ''">
            and id in (select plan_id
                       from content_platform_gzh_plan_video
                       where title like concat('%', #{param.title}, '%')
                       or custom_title like concat('%', #{param.title}, '%'))
        </if>
        order by create_timestamp desc
        limit #{offset}, #{pageSize}
    </select>

    <select id="getGzhPlanExists"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformGzhPlan">
        select *
        from content_platform_gzh_plan
        where account_id = #{accountId}
          and scene = #{scene}
          and type = #{type}
          and status = 1
        <if test="id != null">
            and id != #{id}
        </if>
        limit 1
    </select>

    <select id="getVideoMaxDt" resultType="java.lang.String">
        select max(dt)
        from content_platform_video_agg
    </select>

    <select id="getVideoDatastatMaxDt" resultType="java.lang.String">
        select max(dt)
        from content_platform_video_datastat_agg
    </select>

    <select id="getVideoCount" resultType="java.lang.Integer">
        select count(1)
        from content_platform_video_agg
        where dt = #{dt} and status = 1 and score >= #{minScore}
        <if test="param.title!= null and param.title!= ''">
            and title like concat('%', #{param.title}, '%')
        </if>
        <if test="param.category!= null and param.category!= ''">
            and category = #{param.category}
        </if>
    </select>

    <select id="getVideoList" resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformVideo">
        select video.*
        from content_platform_video_agg video
        left join content_platform_video_datastat_agg datastat
            on datastat.dt = #{datastatDt} and datastat.type = #{type} and datastat.channel = #{channel}
                   and datastat.strategy = #{strategy} and datastat.video_id = video.video_id
        where video.dt = #{dt} and video.status = 1 and video.score >= #{minScore}
        <if test="param.title!= null and param.title!= ''">
            and video.title like concat('%', #{param.title}, '%')
        </if>
        <if test="param.category!= null and param.category!= ''">
            and video.category = #{param.category}
        </if>
        order by ${sort}
        limit #{offset}, #{pageSize}
    </select>

    <select id="getVideoMinScoreList" resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformVideo">
        select *
        from content_platform_video_agg video
        where video.dt = #{dt} and video.status = 1 and video.score > #{minScore}
        and video.video_id not in (select video_id from third_part_we_com_msg where send_userid = #{roomId} and video_id is not null)
        order by ${sort}
        limit #{offset}, #{pageSize}
    </select>

    <select id="getVideoMinDatastatScoreList" resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformVideo">
        select video.*
        from content_platform_video_agg video
        left join content_platform_video_datastat_agg datastat
            on datastat.dt = #{datastatDt} and datastat.type = #{type} and datastat.channel = #{channel}
                   and datastat.strategy = #{strategy} and datastat.video_id = video.video_id
        where video.dt = #{dt} and video.status = 1 and datastat.fission_rate > #{minScore}
        <if test="excludeVideoIds != null and excludeVideoIds.size() > 0">
            and video.video_id not in
            <foreach collection="excludeVideoIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order by ${sort}
        limit #{offset}, #{pageSize}
    </select>

    <select id="getVideoCategoryList" resultType="java.lang.String">
        select distinct category
        from content_platform_video_agg
        where dt = #{dt} and status = 1
        <if test="category != null and category != ''">
            and category like concat('%', #{category}, '%')
        </if>
    </select>

    <insert id="batchInsertContentPlatformVideo">
        insert into content_platform_video (dt, video_id, category, title, cover, video, score, rovn, exposure, create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dt}, #{item.videoId}, #{item.category}, #{item.title}, #{item.cover}, #{item.video}, #{item.score},
            #{item.rovn}, #{item.exposure}, #{item.createTimestamp})
        </foreach>
    </insert>

    <insert id="batchInsertContentPlatformIllegalVideo">
        insert into content_platform_illegal_video (date_str, video_id, content, title, create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dateStr}, #{item.videoId}, #{item.content}, #{item.title}, #{item.createTimestamp})
        </foreach>
    </insert>

    <insert id="batchInsertContentPlatformVideoAgg">
        insert into content_platform_video_agg (dt, video_id, category, title, cover, video, score, create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dt}, #{item.videoId}, #{item.category}, #{item.title}, #{item.cover}, #{item.video}, #{item.score},
            #{item.createTimestamp})
        </foreach>
    </insert>

    <select id="getQwPlanCount" resultType="java.lang.Integer">
        select count(*)
        from content_platform_qw_plan
        where create_account_id = #{createAccountId}
          and type = #{param.type}
          and status = 1
        <if test="param.scene != null">
            and scene = #{param.scene}
        </if>
        <if test="param.subChannel != null and param.subChannel != ''">
            and sub_channel like concat('%', #{param.subChannel}, '%')
        </if>
        <if test="param.title != null and param.title != ''">
            and id in (select plan_id from content_platform_qw_plan_video where title like concat('%', #{param.title},
            '%'))
        </if>
        <if test="param.startDate != null and param.startDate != ''">
            and create_timestamp &gt;= UNIX_TIMESTAMP(#{param.startDate}) * 1000
        </if>
        <if test="param.endDate != null and param.endDate != ''">
            and create_timestamp &lt; UNIX_TIMESTAMP(DATE_ADD(#{param.endDate}, INTERVAL 1 DAY)) * 1000
        </if>
    </select>

    <select id="getQwPlanList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformQwPlan">
        select *
        from content_platform_qw_plan
        where create_account_id = #{createAccountId}
        and type = #{param.type}
        and status = 1
        <if test="param.scene != null">
            and scene = #{param.scene}
        </if>
        <if test="param.subChannel != null and param.subChannel != ''">
            and sub_channel like concat('%', #{param.subChannel}, '%')
        </if>
        <if test="param.title != null and param.title != ''">
            and id in (select plan_id from content_platform_qw_plan_video where title like concat('%', #{param.title},
            '%'))
        </if>
        <if test="param.startDate != null and param.startDate != ''">
            and create_timestamp &gt;= UNIX_TIMESTAMP(#{param.startDate}) * 1000
        </if>
        <if test="param.endDate != null and param.endDate != ''">
            and create_timestamp &lt; UNIX_TIMESTAMP(DATE_ADD(#{param.endDate}, INTERVAL 1 DAY)) * 1000
        </if>
        order by create_timestamp desc
        limit #{offset}, #{pageSize}
    </select>

    <insert id="batchInsertQwPlan">
        insert into content_platform_qw_plan (`type`, scene, page_url, root_source_id,
        create_account_id, create_timestamp, update_timestamp
        )
        values 
        <foreach collection="records" item="item" separator=",">
            (#{item.type}, #{item.scene}, #{item.pageUrl}, #{item.rootSourceId},
            #{item.createAccountId}, #{item.createTimestamp}, #{item.updateTimestamp}
            )
        </foreach>
    </insert>

    <insert id="insertQwPlanReturnId" useGeneratedKeys="true" keyProperty="id">
        insert into content_platform_qw_plan (`type`, sub_channel, scene, page_url, root_source_id, create_account_id,
                                              create_timestamp, update_timestamp)
        values (#{record.type}, #{record.subChannel}, #{record.scene}, #{record.pageUrl}, #{record.rootSourceId},
                #{record.createAccountId}, #{record.createTimestamp}, #{record.updateTimestamp})
    </insert>

    <select id="getGzhPlanVideoListByCooperateAccountId"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformGzhPlanVideo">
        select cpgpv.*
        from content_platform_gzh_plan_video cpgpv
         join content_platform_gzh_plan cpgp on cpgp.id = cpgpv.plan_id
         join content_platform_gzh_account cpgac on cpgac.id = cpgp.account_id
        where cpgac.gh_id = #{ghId}
          and cpgac.status = 1
          and cpgp.scene = 0
          and cpgp.status = 1
    </select>

    <select id="getVideoAggList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformVideoAgg">
        SELECT t.video_id, t.category, t.title, t.cover, t.video, round(t.weighted_score, 3) as score
        FROM (
        SELECT video_id, category, title, cover, video,
            SUM(IFNULL(rovn, 0) * (1 + LN(GREATEST(IFNULL(exposure, 0) / 5000.0, 1.0)))
                * POWER(0.5, DATEDIFF(STR_TO_DATE(#{aggDt}, '%Y%m%d'), STR_TO_DATE(dt, '%Y%m%d'))))
                OVER (PARTITION BY video_id) AS weighted_score,
            ROW_NUMBER() OVER (PARTITION BY video_id ORDER BY dt DESC) AS rn
        FROM content_platform_video
        where dt in
        <foreach collection="dtList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and status = 1
        and cover is not null
        ) AS t
        WHERE t.rn = 1;
    </select>

    <update id="updateVideoStatusWithOldStatus">
        update content_platform_video
        set status = #{status},
            update_timestamp = #{now}
        where video_id = #{videoId}
        and status = #{oldStatus}
    </update>

    <update id="updateVideoAggStatusWithOldStatus">
        update content_platform_video_agg
        set status = #{status},
            update_timestamp = #{now}
        where video_id = #{videoId}
        and status = #{oldStatus}
    </update>

    <select id="getVideoListByIds"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformVideo">
        SELECT t1.*
        FROM content_platform_video t1
        INNER JOIN (
        SELECT video_id, MAX(dt) AS max_dt
        FROM content_platform_video
        WHERE video_id IN
        <foreach collection="videoIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        GROUP BY video_id
        ) t2 ON t1.video_id = t2.video_id AND t1.dt = t2.max_dt
        ORDER BY t1.dt DESC
    </select>

    <delete id="deleteContentPlatformVideo">
        delete from content_platform_video where dt = #{dt}
    </delete>

    <delete id="deleteContentPlatformVideoAgg">
        delete from content_platform_video_agg where dt = #{dt}
    </delete>

    <delete id="deleteContentPlatformVideoDatastatAgg">
        delete
        from content_platform_video_datastat_agg
        where dt = #{dt}
        <if test="strategy != null and strategy != ''">
            and strategy = #{strategy}
        </if>
    </delete>

    <delete id="deleteContentPlatformVideoDatastat">
        delete from content_platform_video_datastat where dt = #{dt}
    </delete>

    <insert id="batchInsertContentPlatformVideoDatastat">
        insert into content_platform_video_datastat (dt, video_id, `type`, channel, first_level, fission0,
                                                         fission_rate, create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dt}, #{item.videoId}, #{item.type}, #{item.channel}, #{item.firstLevel},
            #{item.fission0}, #{item.fissionRate}, #{item.createTimestamp})
        </foreach>
    </insert>

    <insert id="batchInsertContentPlatformVideoDatastatAgg">
        insert into content_platform_video_datastat_agg (dt, video_id, `type`, channel, strategy, first_level, fission0,
                                                         fission_rate, create_timestamp)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.dt}, #{item.videoId}, #{item.type}, #{item.channel}, #{item.strategy}, #{item.firstLevel},
            #{item.fission0}, #{item.fissionRate}, #{item.createTimestamp})
        </foreach>
    </insert>

    <update id="updateOtherVideoStatus">
        update content_platform_video
        set status = 0,
            update_timestamp = #{now}
        where dt = #{dt} and video_id not in
        <foreach collection="videoIdList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>

    <select id="getGzhPlanIllegalVideoList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.PlanIllegalVideoDTO">
        select plan.create_account_id as account_id, plan.type, video.video_id, '公众号' as channel,
               coalesce(nullif(video.custom_title, ''), video.title) as title
        from content_platform_gzh_plan plan
        join content_platform_gzh_plan_video video on plan.id = video.plan_id
        where video.video_id = #{videoId} and plan.`status` = 1
    </select>

    <select id="getQwPlanIllegalVideoList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.PlanIllegalVideoDTO">
        select plan.create_account_id as account_id, plan.type, video.video_id, '企微' as channel, video.title
        from content_platform_qw_plan plan
        join content_platform_qw_plan_video video on plan.id = video.plan_id
        where video.video_id = #{videoId} and plan.`status` = 1
    </select>

    <select id="getUploadVideoCount" resultType="java.lang.Integer">
        select count(1)
        from content_platform_upload_video
        where audit_status = 2 and create_account_id = #{createAccountId} and is_delete = 0
        <if test="param.title!= null and param.title!= ''">
            and title like concat('%', #{param.title}, '%')
        </if>
    </select>

    <select id="getUploadVideoList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformVideo">
        select video.*
        from content_platform_upload_video video
        left join content_platform_video_datastat_agg datastat
        on datastat.dt = #{datastatDt} and datastat.type = #{type} and datastat.channel = #{channel}
               and datastat.strategy = #{strategy} and datastat.video_id = video.video_id
        where video.audit_status = 2 and video.create_account_id = #{createAccountId} and video.is_delete = 0
        <if test="param.title!= null and param.title!= ''">
            and video.title like concat('%', #{param.title}, '%')
        </if>
        order by ${sort}
        limit #{offset}, #{pageSize}
    </select>

    <update id="updateUploadVideoStatusWithOldStatus">
        update content_platform_upload_video
        set audit_status = #{status},
            update_timestamp = #{now}
        where video_id = #{videoId}
    </update>

    <select id="getGzhPushPlanVideoByRootSourceId"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformGzhPlanVideo">
        select cpgpv.*
        from content_platform_gzh_plan_video cpgpv
        join content_platform_gzh_plan cpgp on cpgp.id = cpgpv.plan_id
        where cpgpv.root_source_id = #{rootSourceId}
          and cpgp.type = #{planType}
          and cpgp.status = 1
    </select>

    <select id="getXcxPlanCount" resultType="java.lang.Integer">
        select count(*)
        from content_platform_xcx_plan
        where create_account_id = #{createAccountId}
          and status = 1
        <if test="param.audiencePackage != null and param.audiencePackage != ''">
            and audience_package = #{param.audiencePackage}
        </if>
        <if test="param.title != null and param.title != ''">
            and id in (select plan_id from content_platform_xcx_plan_video where title like concat('%', #{param.title},
            '%'))
        </if>
        <if test="param.remark != null and param.remark != ''">
            and remark = #{param.remark}
        </if>
        <if test="param.createTimestampStart != null">
            and create_timestamp &gt; #{param.createTimestampStart}
        </if>
        <if test="param.createTimestampEnd != null">
            and create_timestamp &lt; #{param.createTimestampEnd}
        </if>
    </select>

    <select id="getXcxPlanList"
            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformXcxPlan">
        select *
        from content_platform_xcx_plan
        where create_account_id = #{createAccountId}
        and status = 1
        <if test="param.audiencePackage != null and param.audiencePackage != ''">
            and audience_package = #{param.audiencePackage}
        </if>
        <if test="param.title != null and param.title != ''">
            and id in (select plan_id from content_platform_xcx_plan_video where title like concat('%', #{param.title},
            '%'))
        </if>
        <if test="param.remark != null and param.remark != ''">
            and remark = #{param.remark}
        </if>
        <if test="param.createTimestampStart != null">
            and create_timestamp &gt; #{param.createTimestampStart}
        </if>
        <if test="param.createTimestampEnd != null">
            and create_timestamp &lt; #{param.createTimestampEnd}
        </if>
        order by create_timestamp desc
        limit #{offset}, #{pageSize}
    </select>

    <insert id="insertXcxPlanReturnId" useGeneratedKeys="true" keyProperty="id">
        insert into content_platform_xcx_plan (audience_package, page_url, root_source_id, remark, status,
                                              create_account_id, create_timestamp, update_timestamp)
        values (#{record.audiencePackage}, #{record.pageUrl}, #{record.rootSourceId}, #{record.remark},
                #{record.status}, #{record.createAccountId}, #{record.createTimestamp}, #{record.updateTimestamp})
    </insert>

</mapper>