<?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, match_method, match_video_filter, match_sort,
        video_id, config_code, score, sim, rov, match_text, title, cover, video, experiment_id, scene_sum_rov,
        channel_level3, status, create_timestamp, update_timestamp,
        match_exposure_pv, match_head_single_return_rate, match_head_distribution_single_return_rate,
        match_generalized_element, match_generalized_point_type)
        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.matchMethod}, #{item.matchVideoFilter}, #{item.matchSort},
            #{item.videoId}, #{item.configCode}, #{item.score}, #{item.sim}, #{item.rov},
            #{item.matchText}, #{item.title}, #{item.cover}, #{item.video}, #{item.experimentId}, #{item.sceneSumRov},
            #{item.channelLevel3}, #{item.status}, #{item.createTimestamp}, #{item.updateTimestamp},
            #{item.matchExposurePv}, #{item.matchHeadSingleReturnRate}, #{item.matchHeadDistributionSingleReturnRate},
            #{item.matchGeneralizedElement}, #{item.matchGeneralizedPointType})
        </foreach>
    </insert>

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

    <delete id="deleteByDtLessThan">
        DELETE FROM content_platform_demand_video WHERE dt &lt; #{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,
               match_method, match_video_filter, match_sort,
               video_id, config_code, score, sim, rov,
               match_text, title, cover, video, experiment_id, scene_sum_rov, channel_level3, status, create_timestamp, update_timestamp,
               match_exposure_pv, match_head_single_return_rate, match_head_distribution_single_return_rate,
               match_generalized_element, match_generalized_point_type
        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>
            <if test="channelName != null and channelName != ''">
                channel_name = #{channelName}
            </if>
        </where>
    </select>

    <select id="selectChannelNameByGh" resultType="java.lang.String">
        SELECT channel_name
        FROM content_platform_demand_video
        WHERE channel_level3 = #{ghName}
          AND status = 1
        ORDER BY dt DESC
        LIMIT 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,
               match_method, match_video_filter, match_sort,
               video_id, config_code, score, sim, rov,
               match_text, title, cover, video, experiment_id, scene_sum_rov, status, create_timestamp, update_timestamp,
               match_exposure_pv, match_head_single_return_rate, match_head_distribution_single_return_rate,
               match_generalized_element, match_generalized_point_type
        FROM content_platform_demand_video
        WHERE dt = #{dt} AND status = 1
        <if test="excludeCategories != null and excludeCategories.size() > 0">
            AND (category IS NULL OR category NOT IN
            <foreach collection="excludeCategories" item="c" open="(" close=")" separator=",">
                #{c}
            </foreach>
            )
        </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="matchMethod != null and matchMethod != ''">
            AND match_method = #{matchMethod}
        </if>
        <if test="demandType != null and demandType != ''">
            AND demand_type = #{demandType}
        </if>
        <if test="crowdPackage != null and crowdPackage != ''">
            <choose>
                <when test='crowdPackage == "泛人群"'>
                    AND (crowd_segment IS NULL OR crowd_segment = '' OR crowd_segment = '-' OR crowd_segment = 'null')
                </when>
                <otherwise>
                    AND crowd_segment = #{crowdPackage}
                </otherwise>
            </choose>
        </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="selectSearchWhitelist" 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,
               match_method, match_video_filter, match_sort,
               video_id, config_code, score, sim, rov,
               match_text, title, cover, video, experiment_id, scene_sum_rov, status, create_timestamp, update_timestamp,
               match_exposure_pv, match_head_single_return_rate, match_head_distribution_single_return_rate,
               match_generalized_element, match_generalized_point_type
        FROM content_platform_demand_video
        WHERE dt = #{dt} AND status = 1
          AND channel_name = #{channelName}
          AND demand_strategy IN ('人群需求', '优质相似')
          AND video_id IS NOT NULL
          AND rov IS NOT NULL
          AND (
              (match_method = '场景已看视频' AND rov &gt; 0 AND scene_sum_rov IS NOT NULL AND scene_sum_rov &gt;= #{priorSceneMinSumRov})
              OR
              ((match_method IS NULL OR match_method &lt;&gt; '场景已看视频') AND rov &gt;= #{demandMinRov})
          )
        <if test="crowdSegment != null and crowdSegment != ''">
            <choose>
                <when test='crowdSegment == "泛人群"'>
                    AND (crowd_segment IS NULL OR crowd_segment = '' OR crowd_segment = '-' OR crowd_segment = 'null')
                </when>
                <otherwise>
                    AND crowd_segment = #{crowdSegment}
                </otherwise>
            </choose>
        </if>
        <if test="channelLevel3 != null and channelLevel3 != ''">
            AND channel_level3 = #{channelLevel3}
        </if>
        ORDER BY rov DESC, id ASC
    </select>

    <select id="selectDistinctCrowdPackages" resultType="java.lang.String">
        SELECT DISTINCT
            CASE
                WHEN crowd_segment IS NULL
                  OR crowd_segment = ''
                  OR crowd_segment = '-'
                  OR crowd_segment = 'null'
                THEN '泛人群'
                ELSE crowd_segment
            END AS cp
        FROM content_platform_demand_video
        WHERE dt = #{dt} AND status = 1
        <if test="channelName != null and channelName != ''">
            AND channel_name = #{channelName}
        </if>
        ORDER BY cp
    </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 AND video_id IS NOT NULL
    </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>
