<?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.fs.live.mapper.LiveVideoMapper">

    <resultMap type="LiveVideo" id="LiveVideoResult">
        <result property="videoId"    column="video_id"    />
        <result property="liveId"    column="live_id"    />
        <result property="videoUrl"    column="video_url"    />
        <result property="videoName"    column="video_name"    />
        <result property="videoType"    column="video_type"    />
        <result property="category"    column="category"    />
        <result property="sort"    column="sort"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="duration"    column="duration"    />
        <result property="remark"    column="remark"    />
        <result property="fileSize"    column="file_size"    />
        <result property="finishStatus"    column="finish_status"    />
        <result property="companyIds"    column="company_ids"    typeHandler="com.fs.live.domain.handler.CompanyIdsTypeHandler"    />
    </resultMap>

    <sql id="selectLiveVideoVo">
        select video_id, live_id, video_url, video_name, video_type, category, sort, create_time, create_by, update_by, update_time, remark,duration,file_size,finish_status,company_ids from live_video
    </sql>

    <select id="selectLiveVideoList" parameterType="LiveVideo" resultMap="LiveVideoResult">
        <include refid="selectLiveVideoVo"/>
        <where>
            <if test="liveId != null "> and live_id = #{liveId}</if>
            <if test="videoUrl != null  and videoUrl != ''"> and video_url = #{videoUrl}</if>
            <if test="videoName != null and videoName != ''"> and video_name like CONCAT('%',#{videoName},'%')</if>
            <if test="videoType != null "> and video_type = #{videoType}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="remark != null and remark != ''"> and remark like CONCAT('%',#{remark},'%')</if>
            <if test="category != null and category != ''"> and category like CONCAT('%',#{category},'%')</if>
            <if test="finishStatus != null "> and finish_status  = #{finishStatus}</if>
            <if test="companyId != null">
                and (company_ids IS NULL OR company_ids = '' OR company_ids = '[]' OR JSON_CONTAINS(company_ids, CAST(#{companyId} AS JSON), '$'))
            </if>
        </where>
        order by case when sort is null then 1 else 0 end asc, sort asc, create_time desc
    </select>

    <select id="selectLiveVideoByVideoId" parameterType="Long" resultMap="LiveVideoResult">
        <include refid="selectLiveVideoVo"/>
        where video_id = #{videoId}
    </select>
    <select id="selectLiveVideoByVideoIdAndCompanyIdAndCompanyUserId" resultMap="LiveVideoResult">
        <include refid="selectLiveVideoVo"/>
        where video_id = #{videoId} and company_id = #{companyId} and company_user_id = #{companyUserId}
    </select>

    <insert id="insertLiveVideo" parameterType="LiveVideo" useGeneratedKeys="true" keyProperty="videoId">
        insert into live_video
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="liveId != null">live_id,</if>
            <if test="videoUrl != null">video_url,</if>
            <if test="videoName != null">video_name,</if>
            <if test="videoType != null">video_type,</if>
            <if test="category != null">category,</if>
            <if test="sort != null">sort,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="duration != null">duration,</if>
            <if test="fileSize != null">file_size,</if>
            <if test="finishStatus != null">finish_status,</if>
            <if test="companyIds != null">company_ids,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="liveId != null">#{liveId},</if>
            <if test="videoUrl != null">#{videoUrl},</if>
            <if test="videoName != null">#{videoName},</if>
            <if test="videoType != null">#{videoType},</if>
            <if test="category != null">#{category},</if>
            <if test="sort != null">#{sort},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="duration != null">#{duration},</if>
            <if test="fileSize != null">#{fileSize},</if>
            <if test="finishStatus != null">#{finishStatus},</if>
            <if test="companyIds != null">#{companyIds, typeHandler=com.fs.live.domain.handler.CompanyIdsTypeHandler},</if>
         </trim>
    </insert>

    <update id="updateLiveVideo" parameterType="LiveVideo">
        update live_video
        <trim prefix="SET" suffixOverrides=",">
            <if test="liveId != null">live_id = #{liveId},</if>
            <if test="videoUrl != null">video_url = #{videoUrl},</if>
            <if test="videoName != null">video_name = #{videoName},</if>
            <if test="videoType != null">video_type = #{videoType},</if>
            <if test="category != null">category = #{category},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="fileSize != null">file_size = #{fileSize},</if>
            <if test="finishStatus != null">finish_status = #{finishStatus},</if>
            <if test="companyIds != null">company_ids = #{companyIds, typeHandler=com.fs.live.domain.handler.CompanyIdsTypeHandler},</if>
        </trim>
        where video_id = #{videoId}
    </update>

    <delete id="deleteLiveVideoByVideoId" parameterType="Long">
        delete from live_video where video_id = #{videoId}
    </delete>

    <delete id="deleteLiveVideoByVideoIds" parameterType="String">
        delete from live_video where video_id in
        <foreach item="videoId" collection="array" open="(" separator="," close=")">
            #{videoId}
        </foreach>
    </delete>

    <update id="batchUpdateCategory">
        update live_video set category = #{category}, update_time = NOW()
        where video_id in
        <foreach item="videoId" collection="videoIds" open="(" separator="," close=")">
            #{videoId}
        </foreach>
    </update>
</mapper>
