<?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.reghao.tnb.content.app.vod.db.mapper.VideoPostMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into vod_video_post
        (`video_id`,`title`,`description`,`category_pid`,`category_id`,`cover_url`,`scope`,`status`,`horizontal`,`duration`,`publish_at`,`publish_by`)
        values
        (#{videoId},#{title},#{description},#{categoryPid},#{categoryId},#{coverUrl},#{scope},#{status},#{horizontal},#{duration},#{publishAt},#{publishBy})
    </insert>

    <update id="updateVideoScope">
        update vod_video_post
        set update_time=now(),scope=#{scope}
        where video_id=#{videoId}
    </update>
    <update id="updateVideoInfo">
        update vod_video_post
        set update_time=now(),title=#{title},`description`=#{description}
        where video_id=#{videoId}
    </update>
    <update id="updateVideoCover">
        update vod_video_post
        set update_time=now(),cover_url=#{coverUrl}
        where video_id=#{videoId}
    </update>
    <update id="updateVideoFile">
        update vod_video_post
        set update_time=now(),duration=#{videoInfo.duration},quality=#{videoInfo.quality}
        where video_id=#{videoId}
    </update>
    <update id="updateVideoStatus">
        update vod_video_post
        set update_time=now(),`status`=#{status}
        where video_id=#{videoId}
    </update>
    <update id="updateVideoPublish">
        update vod_video_post
        set update_time=now(),`title`=#{title},`description`=#{description},`category_pid`=#{categoryPid},`category_id`=#{categoryId},`cover_url`=#{coverUrl},`scope`=#{scope},`status`=#{status},`publish_at`=#{publishAt}
        where video_id=#{videoId}
    </update>
    <update id="updateVideoCached">
        update vod_video_post
        set update_time=now(),duration=#{videoInfo.duration},`status`=2
        where video_id=#{videoId}
    </update>
    <update id="updateVideoUser">
        update vod_video_post
        set publish_by=#{newUserId}
        where video_id in
        <foreach collection="videoIds" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <delete id="deleteByVideoId">
        delete from vod_video_post
        where video_id=#{videoId}
    </delete>

    <select id="countByCriteria" resultType="java.lang.Integer">
        select count(*)
        from vod_video_post
        <where>
            deleted=0
            <if test="status != null">
                and `status` in
                <foreach collection="status" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="scope != null">
                and scope in
                <foreach collection="scope" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="categoryPid != null">
                and category_pid=#{categoryPid}
            </if>
            <if test="categoryId != null">
                and category_id=#{categoryId}
            </if>
            <if test="userId != null">
                and publish_by=#{userId}
            </if>
            <if test="horizontal != null">
                and horizontal=#{horizontal}
            </if>
            <if test="videoId != null">
                and video_id=#{videoId}
            </if>
        </where>
    </select>
    <select id="findVideoPostByPage" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
        select *
        from vod_video_post post
        <where>
            deleted=0
            <if test="videoQuery.status != null">
                and post.status in
                <foreach collection="videoQuery.status" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="videoQuery.scope != null">
                and post.scope in
                <foreach collection="videoQuery.scope" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="videoQuery.categoryPid != null">
                and post.category_pid=#{videoQuery.categoryPid}
            </if>
            <if test="videoQuery.categoryId != null">
                and post.category_id=#{videoQuery.categoryId}
            </if>
            <if test="videoQuery.userId != null">
                and post.publish_by=#{videoQuery.userId}
            </if>
            <if test="videoQuery.horizontal != null">
                and post.horizontal=#{videoQuery.horizontal}
            </if>
        </where>
        order by post.publish_at desc
    </select>
    <select id="findVideoCardByPage" resultMap="videoPostCard">
        select post.id,post.video_id,post.title,post.cover_url,post.duration,post.horizontal,post.publish_at,post.publish_by,post.status,
        statistic.view,statistic.comment
        from vod_video_post post
        inner join vod_video_statistic statistic
        on statistic.video_id=post.video_id
        <where>
            and post.deleted=0
            <if test="videoQuery.status != null">
                and post.status in
                <foreach collection="videoQuery.status" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="videoQuery.scope != null">
                and post.scope in
                <foreach collection="videoQuery.scope" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="videoQuery.categoryPid != null">
                and post.category_pid=#{videoQuery.categoryPid}
            </if>
            <if test="videoQuery.categoryId != null">
                and post.category_id=#{videoQuery.categoryId}
            </if>
            <if test="videoQuery.userId != null">
                and post.publish_by=#{videoQuery.userId}
            </if>
            <if test="videoQuery.horizontal != null">
                and post.horizontal=#{videoQuery.horizontal}
            </if>
            <if test="videoQuery.duration != null">
                and post.duration &lt; #{videoQuery.duration}
            </if>
        </where>
        order by post.publish_at desc
    </select>

    <select id="countAdminVideo" resultType="java.lang.Integer">
        select count(*)
        from vod_video_post
        <where>
            deleted=0
            <if test="status != null">
                and `status` in
                <foreach collection="status" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="scope != null">
                and scope in
                <foreach collection="scope" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="categoryPid != null">
                and category_pid=#{categoryPid}
            </if>
            <if test="categoryId != null">
                and category_id=#{categoryId}
            </if>
            <if test="userId != null">
                and publish_by=#{userId}
            </if>
            <if test="horizontal != null">
                and horizontal=#{horizontal}
            </if>
            <if test="videoId != null">
                and video_id=#{videoId}
            </if>
            <if test="title != null">
                and match(title) against(#{title})
            </if>
        </where>
    </select>
    <select id="findAdminVideoByPage" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
        select *
        from vod_video_post
        <where>
            deleted=0
            <if test="videoQuery.status != null">
                and `status` in
                <foreach collection="videoQuery.status" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="videoQuery.scope != null">
                and scope in
                <foreach collection="videoQuery.scope" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="videoQuery.categoryPid != null">
                and category_pid=#{videoQuery.categoryPid}
            </if>
            <if test="videoQuery.categoryId != null">
                and category_id=#{videoQuery.categoryId}
            </if>
            <if test="videoQuery.userId != null">
                and publish_by=#{videoQuery.userId}
            </if>
            <if test="videoQuery.horizontal != null">
                and horizontal=#{videoQuery.horizontal}
            </if>
            <if test="videoQuery.title != null">
                and match(title) against(#{videoQuery.title})
            </if>
        </where>
        order by #{videoQuery.orderBy} #{videoQuery.orderDirection}
    </select>

    <resultMap id="videoPostCard" type="cn.reghao.tnb.content.api.dto.VideoPostCard">
        <id column="video_id" property="videoId"/>
        <result column="title" property="title"/>
        <result column="cover_url" property="coverUrl"/>
        <result column="duration" property="duration"/>
        <result column="horizontal" property="horizontal"/>
        <result column="status" property="status"/>
        <result column="publish_at" property="publishAt"/>
        <result column="publish_by" property="publishBy"/>
        <result column="view" property="view"/>
        <result column="comment" property="comment"/>
    </resultMap>
    <select id="findVideoCardByVideoIds" resultMap="videoPostCard">
        select post.id,post.video_id,post.title,post.cover_url,post.duration,post.horizontal,post.publish_at as publish_at,post.publish_by,post.status,
        statistic.view,statistic.comment
        from vod_video_post post
        inner join vod_video_statistic statistic
        on post.deleted=0 and statistic.video_id=post.video_id and post.status in (2,5,6,7) and post.video_id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
    <select id="findByUserIds" resultMap="videoPostCard">
        select post.id,post.video_id,post.title,post.cover_url,post.duration,post.horizontal,post.publish_at as publish_at,post.publish_by,post.status,
        statistic.view,statistic.comment
        from vod_video_post post
        inner join vod_video_statistic statistic
        on statistic.video_id=post.video_id and post.status in (2,5,6,7) and post.publish_by in
        <foreach collection="userIds" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        where post.deleted=0 and post.id &lt; #{nextId}
        order by post.publish_at desc
        limit #{pageSize}
    </select>

    <resultMap id="videoDetail" type="cn.reghao.tnb.content.app.vod.model.vo.VideoDetail">
        <id column="video_id" property="videoId"/>
        <result column="title" property="title"/>
        <result column="description" property="description"/>
        <result column="cover_url" property="coverUrl"/>
        <result column="duration" property="duration"/>
        <result column="publish_at" property="pubDate"/>
        <result column="scope" property="scope"/>
        <result column="status" property="status"/>
        <result column="publish_by" property="userId"/>

        <result column="view" property="view"/>
        <result column="thumb_up" property="thumbUp"/>
        <result column="favorite" property="favorite"/>
        <result column="share" property="share"/>
        <result column="comment" property="comment"/>
        <collection column="tag_name" property="tags" ofType="java.lang.String" select="findVideoTags"/>
    </resultMap>
    <select id="findVideoPostDetail" resultMap="videoDetail">
        select post.publish_by,post.video_id,post.title,post.description,post.cover_url,post.duration,post.publish_at,post.scope,post.status,
        statistic.view,statistic.danmaku,statistic.comment,statistic.thumb_up,
        statistic.favorite,statistic.share
        from vod_video_post post
        inner join vod_video_statistic statistic
        on statistic.video_id=post.video_id
        <where>
            post.deleted=0
            <if test="status != null">
                and post.status in
                <foreach collection="status" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="scope != null">
                and post.scope in
                <foreach collection="scope" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="videoId != null">
                and post.video_id=#{videoId}
            </if>
        </where>
    </select>
    <select id="findVideoTags" resultType="java.lang.String">
        select tag_name
        from video_tags
        inner join video_tag
        on video_tag.tag_id=video_tags.tag_id and video_tags.video_id=#{videoId}
    </select>
    <select id="findByNextVideos" resultMap="videoPostCard">
        select post.id,post.video_id,post.title,post.cover_url,post.duration,post.horizontal,post.publish_at,post.publish_by,post.status,
        statistic.view,statistic.comment
        from vod_video_post post
        inner join vod_video_statistic statistic
        on statistic.video_id=post.video_id
        <where>
            post.deleted=0
            <if test="videoQuery.status != null">
                and post.status in
                <foreach collection="videoQuery.status" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="videoQuery.scope != null">
                and post.scope in
                <foreach collection="videoQuery.scope" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="videoQuery.userId != null">
                and post.publish_by=#{videoQuery.userId}
            </if>
            and post.publish_at > #{publishAt}
        </where>
        order by post.publish_at asc
        limit 4
    </select>

    <select id="countBySearchCriteria" resultType="java.lang.Integer">
        select count(*)
        from vod_video_post
        <where>
            deleted=0
            <if test="status != null">
                and `status` in
                <foreach collection="status" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="scope != null">
                and scope in
                <foreach collection="scope" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="title != null">
                and match(title) against(#{title})
            </if>
        </where>
    </select>
    <select id="findSearchCriteriaByPage" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
        select *
        from vod_video_post
        <where>
            deleted=0
            <if test="searchCriteria.status != null">
                and status in
                <foreach collection="searchCriteria.status" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="searchCriteria.scope != null">
                and scope in
                <foreach collection="searchCriteria.scope" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="searchCriteria.title != null">
                and match(title) against(#{searchCriteria.title})
            </if>
        </where>
    </select>

    <select id="findByVideoId" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
        select *
        from vod_video_post
        where deleted=0 and video_id=#{videoId}
    </select>
    <select id="findAll" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
        select *
        from vod_video_post
        where deleted=0 and `status`=2 and category_pid=11
    </select>
    <select id="findAllById" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
        select *
        from vod_video_post
        where deleted=0 and `status` in (2,5) and id > #{nextId}
        order by id asc
        limit #{pageSize}
    </select>

    <select id="findBannerVideos" resultType="cn.reghao.tnb.content.app.vod.model.po.BannerVideo">
        select post.video_id,post.cover_url,post.title
        from vod_video_post post
        where deleted=0
        order by publish_at desc
        limit 10
    </select>
    <select id="findPublishByPost" resultType="cn.reghao.tnb.common.db.GroupCount">
        select publish_by as id,count(*) total
        from vod_video_post
        where deleted=0 and category_pid=11
        group by publish_by
        order by total desc
    </select>
    <select id="findPublishByImage" resultType="cn.reghao.tnb.common.db.GroupCount">
        select user_id as id,count(*) as total
        from image_post
        where user_id not in
        (
        select publish_by
        from vod_video_post
        where category_pid=11
        group by publish_by
        )
        group by user_id
        order by total desc
    </select>
    <select id="findPublishByVideo" resultType="cn.reghao.tnb.common.db.GroupCount">
        select publish_by as id,count(*) total
        from vod_video_post
        where category_pid=11
        group by publish_by
        order by total desc
    </select>
    <select id="findRandomVideoIds" resultType="java.lang.String">
        SELECT t1.video_id
        FROM vod_video_post AS t1
        JOIN (SELECT ROUND(RAND()*(SELECT MAX(id) FROM vod_video_post)) AS id) AS t2
        WHERE t1.id>=t2.id and t1.scope in
        <foreach collection="scopes" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        ORDER BY t1.id
        LIMIT #{size}
    </select>
    <select id="findShortVideo" resultType="java.lang.String">
        SELECT video_id
        FROM vod_video_post AS t1
        WHERE t1.duration&lt;=120 and t1.`status`=2 and t1.`horizontal`=0 and t1.scope in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        limit 100000
    </select>
    <select id="findGroupByUserAndCategory" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
        select *
        from vod_video_post
        where `deleted`=0 and `status`=2 and category_pid=#{categoryPid}
        group by publish_by,category_id
    </select>
    <select id="findUserAndCategory" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
        select *
        from vod_video_post
        where `deleted`=0 and `status`=2 and publish_by=#{publishBy} and category_id=#{categoryId}
        order by publish_at desc
        limit 1
    </select>
    <select id="findByTitle" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
        select *
        from vod_video_post
        where `deleted`=0 and `status`=2 and category_pid=11 and title like concat(#{title},'%')
    </select>
</mapper>