<?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.VideoTagMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into vod_video_tag
            (`tag_id`,`tag_name`)
        values
        (#{tagId},#{tagName})
    </insert>
    <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
        insert ignore into vod_video_tag
            (`tag_id`,`tag_name`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.tagId},#{item.tagName})
        </foreach>
    </insert>

    <delete id="deleteByTagId">
        delete from vod_video_tag
        where tag_id=#{tagId}
    </delete>

    <select id="findAll" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoTag">
        select *
        from vod_video_tag
    </select>
    <select id="findByName" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoTag">
        select * from vod_video_tag where tag_name=#{name}
    </select>
    <select id="findByTagId" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoTag">
        select *
        from vod_video_tag
        where tag_id=#{tagId}
    </select>
    <select id="findVideoTags" resultType="java.lang.String">
        select tag.tag_name
        from vod_video_tag tag
        inner join vod_video_tags tags
        on tag.tag_id=tags.tag_id and tags.video_id=#{videoId}
    </select>
    <select id="findVideoIds" resultType="java.lang.String">
        select video_id
        from vod_video_tags
        where tag_id in (
            select tag_id
            from vod_video_tag
            where tag_name=#{tagName}
        )
        limit 100
    </select>
    <select id="findRandomTags" resultType="java.lang.String">
        SELECT t1.tag_id FROM vod_video_tag AS t1
        JOIN (SELECT ROUND(RAND()*(SELECT MAX(id) FROM vod_video_tag)) AS id) AS t2
        WHERE t1.id>=t2.id
        ORDER BY t1.id
        LIMIT #{size};
    </select>
</mapper>