<?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.VideoCategoryPostMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into vod_video_category_post
        (`video_id`,`category_pid`,`category_id`,`scope`,`horizontal`,`duration`,`publish_at`,`publish_by`)
        values
        (#{videoId},#{categoryPid},#{categoryId},#{scope},#{horizontal},#{duration},#{publishAt},#{publishBy})
    </insert>
    <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
        insert into vod_video_category_post
        (`video_id`,`category_pid`,`category_id`,`scope`,`horizontal`,`duration`,`publish_at`,`publish_by`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.videoId},#{item.categoryPid},#{item.categoryId},#{item.scope},#{item.horizontal},#{item.duration},#{item.publishAt},#{item.publishBy})
        </foreach>
    </insert>

    <update id="updateVideoCategoryPost">
        update vod_video_category_post
        set update_time=now(),video_id=#{videoId},scope=#{scope},publish_at=#{publishAt}
        where category_id=#{categoryId} and publish_by=#{publishBy}
    </update>

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

    <select id="countByCriteria" resultType="java.lang.Integer">
        select count(*)
        from vod_video_category_post
        <where>
            deleted=0
            <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>
        </where>
    </select>
    <select id="findVideoCategoryPostByPage" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoCategoryPost">
        select *
        from vod_video_category_post post
        <where>
            deleted=0
            <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.horizontal != null">
                and post.horizontal=#{videoQuery.horizontal}
            </if>
        </where>
        order by post.publish_at desc
    </select>
    <select id="findByCategoryIdAndUserId" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoCategoryPost">
        select *
        from vod_video_category_post post
        where category_id=#{categoryId} and publish_by=#{userId}
    </select>
</mapper>