<?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.LiveMapper">

    <resultMap type="Live" id="LiveResult">
        <result property="liveId"    column="live_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="talentId"    column="talent_id"    />
        <result property="liveName"    column="live_name"    />
        <result property="liveDesc"    column="live_desc"    />
        <result property="showType"    column="show_type"    />
        <result property="status"    column="status"    />
        <result property="anchorId"    column="anchor_id"    />
        <result property="liveType"    column="live_type"    />
        <result property="startTime"    column="start_time"    />
        <result property="finishTime"    column="finish_time"    />
        <result property="liveImgUrl"    column="live_img_url"    />
        <result property="liveConfig"    column="live_config"    />
        <result property="isShow"    column="is_show"    />
        <result property="isDel"    column="is_del"    />
        <result property="qwQrCode"    column="qw_qr_code"    />
        <result property="rtmpUrl"    column="rtmp_url"    />
        <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="remark"    column="remark"    />
        <result property="configJson"    column="config_json"    />
        <result property="isAudit"    column="is_audit"    />
        <result property="idCardUrl"    column="id_card_url"    />
        <result property="liveCodeUrl"    column="live_code_url"    />
    </resultMap>

    <sql id="selectLiveVo">
        select live_id, company_id, company_user_id,talent_id, live_name, is_audit, live_desc, show_type, status, anchor_id, live_type, start_time, finish_time,
               live_img_url, live_config, id_card_url, is_show, is_del, qw_qr_code, rtmp_url, flv_hls_url,
               create_time, create_by, update_by, update_time, remark,config_json from live
    </sql>

    <select id="liveList" parameterType="Live" resultMap="LiveResult">
        select a.live_id, a.company_id, a.company_user_id,talent_id, a.live_name, a.is_audit, a.live_desc, a.show_type, a.status, a.anchor_id,
        a.live_type, a.start_time, a.finish_time, a.live_img_url, a.live_config, a.id_card_url, a.is_show, a.is_del, a.qw_qr_code, a.rtmp_url,
        a.flv_hls_url, a.create_time, a.create_by, a.update_by, a.update_time, a.remark,config_json, b.video_url,c.company_name,b.file_size
        from live
        a
        left join live_video b on a.live_id = b.live_id
        left join company c on a.company_id = c.company_id
        where 1=1 and a.is_del = 0 and a.is_audit = 1 and a.is_show = 1 and a.status in (2,4) and b.video_type in (1,2)

        order by create_time desc
    </select>
    <select id="selectReadyStartLiveList" parameterType="Live" resultMap="LiveResult">
        select a.live_id, a.company_id, a.company_user_id,talent_id, a.live_name, a.is_audit, a.live_desc, a.show_type, a.status, a.anchor_id,
        a.live_type, a.start_time, a.finish_time, a.live_img_url, a.live_config, a.id_card_url, a.is_show, a.is_del, a.qw_qr_code, a.rtmp_url,
        a.flv_hls_url, a.create_time, a.create_by, a.update_by, a.update_time, a.remark,config_json, b.video_url,c.company_name,b.file_size
        from live
        a
        left join live_video b on a.live_id = b.live_id
        left join company c on a.company_id = c.company_id
        where 1=1 and a.is_del = 0 and a.is_audit = 1 and a.is_show = 1 and a.status in (1) and b.video_type in (1,2)
          and a.start_time &lt;= NOW() + INTERVAL 5 MINUTE
          and a.start_time >= NOW()
        order by create_time desc
    </select>

    <select id="selectLiveList" parameterType="com.fs.live.domain.Live" resultMap="LiveResult">
        select a.live_id, a.company_id, a.company_user_id,talent_id, a.live_name, a.is_audit, a.live_desc, a.show_type, a.status, a.anchor_id,
               a.live_type, a.start_time, a.finish_time, a.live_img_url, a.live_config, a.id_card_url, a.is_show, a.is_del, a.qw_qr_code, a.rtmp_url,
               a.flv_hls_url, a.create_time, a.create_by, a.update_by, a.update_time, a.remark,config_json, b.video_url
                ,c.live_code_url,IFNULL(d.company_name, '总台') AS company_name,b.file_size

        from live a
        left join live_video b on a.live_id = b.live_id
            left join live_company_code c on a.live_id = c.live_id
            and c.company_id = #{companyId}
            and c.company_user_id = #{companyUserId}
        left join company d on a.company_id = d.company_id
        where 1=1 and a.is_del = 0 and  b.video_type in (1,2)
        <if test="companyId != null "> and (a.company_id = #{companyId} or a.company_id is null )</if>
        <if test="companyName != null and companyName != ''">
            and (
            (d.company_name like concat('%', #{companyName}, '%'))
            or
            (#{companyName} = '总台' and a.company_id is null)
            )
        </if>
        <if test="talentId != null "> and talent_id = #{talentId}</if>
        <if test="liveName != null  and liveName != ''"> and live_name like concat('%', #{liveName}, '%')</if>
        <if test="liveDesc != null  and liveDesc != ''"> and live_desc = #{liveDesc}</if>
        <if test="showType != null "> and show_type = #{showType}</if>
        <if test="status != null "> and a.status = #{status}</if>
        <if test="anchorId != null "> and anchor_id = #{anchorId}</if>
        <if test="liveType != null "> and live_type = #{liveType}</if>
        <if test="startTimeS != null "> and a.start_time &gt; STR_TO_DATE(#{startTimeS}, '%Y-%m-%d %H:%i:%s')</if>
        <if test="endTimeE != null "> and a.finish_time &lt; STR_TO_DATE(#{endTimeE}, '%Y-%m-%d %H:%i:%s')</if>
        <if test="finishTime != null "> and finish_time = #{finishTime}</if>
        <if test="liveImgUrl != null  and liveImgUrl != ''"> and live_img_url = #{liveImgUrl}</if>
        <if test="liveConfig != null  and liveConfig != ''"> and live_config = #{liveConfig}</if>
        <if test="isShow != null "> and a.is_show = #{isShow}</if>
        <if test="qwQrCode != null  and qwQrCode != ''"> and qw_qr_code = #{qwQrCode}</if>
        <if test="rtmpUrl != null  and rtmpUrl != ''"> and rtmp_url = #{rtmpUrl}</if>
        <if test="flvHlsUrl != null  and flvHlsUrl != ''"> and flv_hls_url = #{flvHlsUrl}</if>
        <if test="isAudit != null "> and is_audit = #{isAudit}</if>
        order by create_time desc
    </select>

    <select id="selectLiveByLiveId" parameterType="Long" resultMap="LiveResult">
        <include refid="selectLiveVo"/>
        where live_id = #{liveId}
    </select>

    <select id="selectLiveByLiveIdAndCompanyIdAndCompanyUserId" resultMap="LiveResult">
        <include refid="selectLiveVo"/>
        where live_id = #{liveId} and company_id = #{companyId} and company_user_id = #{companyUserId}
    </select>
    <select id="selectLiveToStudent" resultType="com.fs.live.domain.Live">
        <include refid="selectLiveVo"/>
        where 1=1
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        <if test="liveName != null and liveName != null">
            AND live_name like concat('%', #{liveName}, '%')
        </if>
        <if test="startTime != null and startTime != '' and finishTime != null  and finishTime != ''">
            AND start_time BETWEEN #{startTime} AND #{finishTime}
        </if>
    </select>

    <insert id="insertLive" parameterType="Live" useGeneratedKeys="true" keyProperty="liveId">
        insert into live
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="talentId != null">talent_id,</if>
            <if test="liveName != null">live_name,</if>
            <if test="liveDesc != null">live_desc,</if>
            <if test="showType != null">show_type,</if>
            <if test="status != null">status,</if>
            <if test="anchorId != null">anchor_id,</if>
            <if test="liveType != null">live_type,</if>
            <if test="startTime != null">start_time,</if>
            <if test="finishTime != null">finish_time,</if>
            <if test="liveImgUrl != null">live_img_url,</if>
            <if test="liveConfig != null">live_config,</if>
            <if test="isShow != null">is_show,</if>
            <if test="isDel != null">is_del,</if>
            <if test="qwQrCode != null">qw_qr_code,</if>
            <if test="rtmpUrl != null">rtmp_url,</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="configJson != null">config_json,</if>
            <if test="flvHlsUrl != null">flv_hls_url,</if>
            <if test="isAudit != null">is_audit,</if>
            <if test="idCardUrl != null">id_card_url,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="talentId != null">#{talentId},</if>
            <if test="liveName != null">#{liveName},</if>
            <if test="liveDesc != null">#{liveDesc},</if>
            <if test="showType != null">#{showType},</if>
            <if test="status != null">#{status},</if>
            <if test="anchorId != null">#{anchorId},</if>
            <if test="liveType != null">#{liveType},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="finishTime != null">#{finishTime},</if>
            <if test="liveImgUrl != null">#{liveImgUrl},</if>
            <if test="liveConfig != null">#{liveConfig},</if>
            <if test="isShow != null">#{isShow},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="qwQrCode != null">#{qwQrCode},</if>
            <if test="rtmpUrl != null">#{rtmpUrl},</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="configJson != null">#{configJson},</if>
            <if test="flvHlsUrl != null">#{flvHlsUrl},</if>
            <if test="isAudit != null">#{isAudit},</if>
            <if test="idCardUrl != null">#{idCardUrl},</if>
         </trim>
    </insert>

    <update id="updateLive" parameterType="Live">
        update live
        <trim prefix="SET" suffixOverrides=",">
            <if test="talentId != null">talent_id = #{talentId},</if>
            <if test="liveName != null">live_name = #{liveName},</if>
            <if test="liveDesc != null">live_desc = #{liveDesc},</if>
            <if test="showType != null">show_type = #{showType},</if>
            <if test="status != null">status = #{status},</if>
            <if test="anchorId != null">anchor_id = #{anchorId},</if>
            <if test="liveType != null">live_type = #{liveType},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
            <if test="finishTime == null">finish_time = NULL,</if>
            <if test="liveImgUrl != null">live_img_url = #{liveImgUrl},</if>
            <if test="liveConfig != null">live_config = #{liveConfig},</if>
            <if test="isShow != null">is_show = #{isShow},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="qwQrCode != null">qw_qr_code = #{qwQrCode},</if>
            <if test="rtmpUrl != null">rtmp_url = #{rtmpUrl},</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="configJson != null">config_json = #{configJson},</if>
            <if test="flvHlsUrl != null">flv_hls_url = #{flvHlsUrl},</if>
            <if test="isAudit != null">is_audit = #{isAudit},</if>
            <if test="idCardUrl != null">id_card_url = #{idCardUrl},</if>
        </trim>
        where live_id = #{liveId}
    </update>

    <delete id="deleteLiveByLiveId" parameterType="Long">
        delete from live where live_id = #{liveId}
    </delete>

    <delete id="deleteLiveByLiveIds" parameterType="String">
        delete from live where live_id in
        <foreach item="liveId" collection="array" open="(" separator="," close=")">
            #{liveId}
        </foreach>
    </delete>

    <update id="updateBatchLiveList" parameterType="com.fs.live.vo.LiveListVo">
        update live set is_show = #{liveVo.isShow},status = 3,finish_time = NOW() where live_id in
        <foreach item="liveId" collection="liveVo.liveIds" open="(" separator="," close=")">
            #{liveId}
        </foreach>
    </update>

    <update id="deleteBatchLiveList" parameterType="com.fs.live.vo.LiveListVo">
        update live set is_del = 1
        WHERE company_id = #{liveVo.companyId} and company_user_id = #{liveVo.companyUserId} and live_id IN
        <foreach item="liveId" collection="liveVo.liveIds" open="(" separator="," close=")">
            #{liveId}
        </foreach>
    </update>

    <update id="handleShelfOrUnAdmin" parameterType="com.fs.live.vo.LiveListVo">
        update live set is_show = #{liveVo.isShow},status = 3,finish_time = NOW() where  live_id in
        <foreach item="liveId" collection="liveVo.liveIds" open="(" separator="," close=")">
            #{liveId}
        </foreach>
    </update>

    <update id="handleDeleteSelectedAdmin" parameterType="com.fs.live.vo.LiveListVo">
        update live set is_del = 1
        WHERE  live_id IN
        <foreach item="liveId" collection="liveVo.liveIds" open="(" separator="," close=")">
            #{liveId}
        </foreach>
    </update>

    <update id="updateLiveList">
        <foreach collection="liveList" item="live" separator=";">
            UPDATE live
            <set>
                <if test="live.liveName != null and live.liveName != ''">live_name = #{live.liveName},</if>
                <if test="live.liveType != null">live_type = #{live.liveType},</if>
                <if test="live.status != null">status = #{live.status},</if>
                <if test="live.startTime != null">start_time = #{live.startTime},</if>
                <if test="live.finishTime != null">finish_time = #{live.finishTime},</if>
                <if test="live.rtmpUrl != null and live.rtmpUrl != ''">rtmp_url = #{live.rtmpUrl},</if>
                <if test="live.flvHlsUrl != null and live.flvHlsUrl != ''">flv_hls_url = #{live.flvHlsUrl},</if>
                <if test="live.videoUrl != null and live.videoUrl != ''">video_url = #{live.videoUrl},</if>
                <if test="live.duration != null and live.duration != ''">duration = #{live.duration},</if>
                <if test="live.companyId != null">company_id = #{live.companyId},</if>
                <if test="live.companyUserId != null">company_user_id = #{live.companyUserId},</if>
                <if test="live.isAudit != null">is_audit = #{live.isAudit},</if>
                <if test="live.isShow != null">is_show = #{live.isShow},</if>
                <if test="live.isDel != null">is_del = #{live.isDel},</if>
                <if test="live.liveConfig != null and live.liveConfig != ''">live_config = #{live.liveConfig},</if>
                <if test="live.remark != null and live.remark != ''">remark = #{live.remark},</if>
                update_time = NOW()
            </set>
            WHERE live_id = #{live.liveId}
        </foreach>
    </update>
    <update id="updateStatusAndTimeBatchById">
        <foreach collection="liveList" item="live" separator=";">
            UPDATE live
            <set>
                <if test="live.status != null">status = #{live.status},</if>
                update_time = NOW()
            </set>
            WHERE live_id = #{live.liveId}
        </foreach>
    </update>


</mapper>
