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

    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO content_platform_demand_video
        (dt, channel_name, crowd_segment, dimension, point_type, standard_element, element_dimension,
         category_name, category, demand_id, crowd_package, conversion_target, partner, account, scene_value,
         demand_strategy, drive_dimension_time, demand_filter_sort_strategy, demand_type,
         demand_content_id, demand_content_title, demand_content_topic,
         crowd_count, video_count, visit_uv, uv_ratio, total_rov, online_action, match_experiment_id,
         video_id, config_code, score, sim, rov,
         match_text, title, cover, video, experiment_id, channel_level3, status, create_timestamp, update_timestamp)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.dt}, #{item.channelName}, #{item.crowdSegment}, #{item.dimension}, #{item.pointType}, #{item.standardElement}, #{item.elementDimension},
             #{item.categoryName}, #{item.category}, #{item.demandId}, #{item.crowdPackage}, #{item.conversionTarget}, #{item.partner}, #{item.account}, #{item.sceneValue},
             #{item.demandStrategy}, #{item.driveDimensionTime}, #{item.demandFilterSortStrategy}, #{item.demandType},
             #{item.demandContentId}, #{item.demandContentTitle}, #{item.demandContentTopic},
             #{item.crowdCount}, #{item.videoCount}, #{item.visitUv}, #{item.uvRatio}, #{item.totalRov}, #{item.onlineAction}, #{item.matchExperimentId},
             #{item.videoId}, #{item.configCode}, #{item.score}, #{item.sim}, #{item.rov},
             #{item.matchText}, #{item.title}, #{item.cover}, #{item.video}, #{item.experimentId}, #{item.channelLevel3}, #{item.status}, #{item.createTimestamp}, #{item.updateTimestamp})
        </foreach>
    </insert>

    <delete id="deleteByDt">
        DELETE FROM content_platform_demand_video WHERE dt = #{dt}
    </delete>

    <delete id="deleteByDtAndChannelName">
        DELETE FROM content_platform_demand_video WHERE dt = #{dt} AND channel_name = #{channelName}
    </delete>

    <select id="selectByCondition" resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformDemandVideo">
        SELECT id, dt, channel_name, channel_level3, crowd_segment, dimension, point_type, standard_element, element_dimension,
               category_name, category, demand_id, crowd_package, conversion_target, partner, account, scene_value,
               demand_strategy, drive_dimension_time, demand_filter_sort_strategy, demand_type,
               demand_content_id, demand_content_title, demand_content_topic,
               crowd_count, video_count, visit_uv, uv_ratio, total_rov, online_action, match_experiment_id,
               video_id, config_code, score, sim, rov,
               match_text, title, cover, video, experiment_id, channel_level3, status, create_timestamp, update_timestamp
        FROM content_platform_demand_video
        WHERE dt = #{dt} AND status = 1
        <if test="channelName != null and channelName != ''">
            AND channel_name = #{channelName}
        </if>
        <if test="pointType != null and pointType != ''">
            AND point_type = #{pointType}
        </if>
        <if test="title != null and title != ''">
            AND title LIKE CONCAT('%', #{title}, '%')
        </if>
        <if test="crowdSegment != null and crowdSegment != ''">
            AND crowd_segment = #{crowdSegment}
        </if>
        ORDER BY score DESC
        LIMIT #{offset}, #{pageSize}
    </select>

    <select id="countByCondition" resultType="int">
        SELECT COUNT(*) FROM content_platform_demand_video
        WHERE dt = #{dt} AND status = 1
        <if test="channelName != null and channelName != ''">
            AND channel_name = #{channelName}
        </if>
        <if test="pointType != null and pointType != ''">
            AND point_type = #{pointType}
        </if>
        <if test="title != null and title != ''">
            AND title LIKE CONCAT('%', #{title}, '%')
        </if>
        <if test="crowdSegment != null and crowdSegment != ''">
            AND crowd_segment = #{crowdSegment}
        </if>
    </select>

    <select id="getMaxDt" resultType="java.lang.String">
        SELECT MAX(dt) FROM content_platform_demand_video WHERE status = 1
    </select>

    <select id="selectForRecommend" resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformDemandVideo">
        SELECT id, dt, channel_name, channel_level3, crowd_segment, dimension, point_type, standard_element,
               category_name, category, demand_id, crowd_package, conversion_target, partner, account, scene_value,
               demand_strategy, drive_dimension_time, demand_filter_sort_strategy, demand_type,
               demand_content_id, demand_content_title, demand_content_topic,
               crowd_count, video_count, visit_uv, uv_ratio, total_rov, online_action, match_experiment_id,
               video_id, config_code, score, sim, rov,
               match_text, title, cover, video, experiment_id, status, create_timestamp, update_timestamp
        FROM content_platform_demand_video
        WHERE dt = #{dt} AND status = 1
        <if test="channelName == null || channelName != '公众号合作-Daily-自选'">
        AND (category IS NULL OR category NOT IN ('早中晚好','节日祝福'))
        </if>
        <if test="channelName != null and channelName != ''">
            AND channel_name = #{channelName}
        </if>
        <if test="crowdSegment != null and crowdSegment != ''">
            AND crowd_segment = #{crowdSegment}
        </if>
        <if test="demandStrategy != null and demandStrategy != ''">
            AND demand_strategy = #{demandStrategy}
        </if>
        <if test="dimension != null and dimension != ''">
            AND dimension = #{dimension}
        </if>
        <if test="dimensionExclude != null and dimensionExclude != ''">
            AND (dimension IS NULL OR dimension &lt;&gt; #{dimensionExclude})
        </if>
        <if test="demandFilterSortStrategyLike != null and demandFilterSortStrategyLike != ''">
            AND demand_filter_sort_strategy LIKE #{demandFilterSortStrategyLike}
        </if>
        <if test="channelLevel3 != null and channelLevel3 != ''">
            AND channel_level3 = #{channelLevel3}
        </if>
        <if test="driveDimensionTime != null and driveDimensionTime != ''">
            AND drive_dimension_time = #{driveDimensionTime}
        </if>
        <if test="category != null and category != ''">
            AND category = #{category}
        </if>
        <if test="excludeSelfTitle">
            AND (title IS NULL OR demand_content_title IS NULL OR title &lt;&gt; demand_content_title)
        </if>
        ORDER BY total_rov DESC, score DESC
        LIMIT #{limit}
    </select>

    <select id="selectActiveVideos" resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformDemandVideo">
        SELECT DISTINCT video_id
        FROM content_platform_demand_video
        WHERE dt = #{dt} AND status = 1
    </select>

    <update id="updateStatusByVideoId">
        UPDATE content_platform_demand_video
        SET status = #{status}, update_timestamp = #{updateTimestamp}
        WHERE video_id = #{videoId} AND dt = #{dt} AND status = 1
    </update>

</mapper>
