<?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.xhkjedu.slive.mapper.recordserver.RecordserverMapper">
    <!--修改-->
    <update id="update">
        update l_recordserver set rsname=#{rs.rsname},rspath=#{rs.rspath},rsmaxnum=#{rs.rsmaxnum}
        ,handleid=#{rs.handleid},handletime=#{rs.handletime}
        where rsid=#{rs.rsid}
    </update>
    <!--列表-->
    <select id="findAll" resultType="java.util.Map">
        select r.rsid,r.rsname,r.rspath,r.rsstate,r.rsmaxnum,u.username,r.handletime
        ,count(case when cs.sectionid is not null then csp.pbid else null end) synum
        ,truncate(count(case when cs.sectionid is not null then csp.pbid else null end)*100/r.rsmaxnum,2) syrate
        from l_recordserver r left join t_user u on r.handleid=u.userid
        left join l_course_section_playback csp on r.rsid=csp.rsid and csp.endtime is null
        left join l_course_section cs on csp.sectionid=cs.sectionid and cs.sectionstate=2
        <where>
            <if test="rs.rsstate!=null and rs.rsstate!=-1">
                and r.rsstate=#{rs.rsstate}
            </if>
            <if test="rs.rsname!=null and rs.rsname!=''">
                and (r.rsname like '%${rs.rsname}%' or r.rspath like '%${rs.rsname}%')
            </if>
        </where>
        group by r.rsid order by r.rsid desc
    </select>
    <!--更改状态-->
    <update id="updateStateById">
        update l_recordserver set rsstate=#{rs.rsstate},handletime=#{rs.handletime}
        <if test="rs.handleid!=null and rs.handleid!=0">
            ,handleid=#{rs.handleid}
        </if>
        where rsid=#{rs.rsid}
    </update>
    <!--获取地址-->
    <select id="getPath" resultType="com.xhkjedu.slive.model.recordserver.LRecordserver">
        select r.rsid,r.rspath from(select r.rsid,r.rspath
        ,(r.rsmaxnum-count(distinct case when cs.sectionid is not null then cs.sectionid else null end))synum
        from l_recordserver r left join l_course_section_playback csp on r.rsid=csp.rsid
        left join l_course_section cs on csp.sectionid=cs.sectionid and cs.sectionstate=2
        where r.rsstate=1 group by r.rsid having synum>0)r order by synum desc,rand() limit 1
    </select>
    <!--根据录制服务id获取地址-->
    <select id="getPathById" resultType="java.lang.String">
        select r.rspath from l_course_section_playback csp left join l_recordserver r on csp.rsid=r.rsid
        where csp.sid=#{sid} limit 1
    </select>
    <!--根据课节id获取地址-->
    <select id="getRspathBySectionid" resultType="com.xhkjedu.slive.model.recordserver.LRecordserver">
        select r.rsid,r.rspath
        from(select distinct rsid from l_course_section_playback csp
        where sectionid=#{sectionid} and rsid is not null)csp
        left join l_recordserver r on csp.rsid=r.rsid
        where r.rsstate=1 limit 1
    </select>
    <!--根据教师id获取区域编码和学校id-->
    <select id="getCloudCodeAndSchoolIdByTid" resultType="com.xhkjedu.slive.vo.liveplay.RecordserverVo">
        select u.schoolid,(select cloudcode from t_cloud limit 1)cloudcode
        from t_user u where u.userid=#{tid}
    </select>
</mapper>
