<?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.liveplay.CourseSectionMapper">
    <!--课程课节列表-->
    <select id="listByCourseId" resultType="com.xhkjedu.slive.vo.liveplay.CourseSectionVo">
        select cs.sectionid,cs.sectionname,cs.sectiondate,cs.planbegintime,cs.planendtime,cs.begintime,cs.endtime,cs.sectionstate
        ,cs.teacherid,cs.createid,cs.stunum,cs.sectionorder
        ,(UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planendtime))-UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planbegintime))) totaltime
        ,(select count(*) from l_course_section_playback p where p.sectionid=cs.sectionid and p.pb is not null)playnum
        ,(select u.username from t_user u where u.userid=cs.teacherid)teachername
        from l_course_section cs where cs.courseid=#{courseid}
        <if test="teacherid!=null and teacherid!=0"> and cs.teacherid=#{teacherid}</if>
        order by UNIX_TIMESTAMP(concat(sectiondate,' ',planbegintime))
    </select>
    <!--修改课节状态-->
    <update id="updateState">
        update l_course_section cs
        <if test="section.sectionstate==2">
            left join l_course c on cs.courseid=c.courseid
        </if>
        set cs.sectionstate=#{section.sectionstate}
        <if test="section.sectionstate==2">
            ,c.coursestate=2,cs.begintime=
            (case when cs.begintime is null then CURRENT_TIME() else cs.begintime end)
        </if>
        <if test="section.sectionstate==3">
            ,cs.endtime=cs.planendtime
        </if>
        where cs.sectionid=#{section.sectionid} and cs.sectionstate=0
    </update>
    <!--获取要结束课节id集合-->
    <select id="listEndSectionId" resultType="java.lang.String">
        select group_concat(sectionid) from l_course_section
        where sectionstate=2 and UNIX_TIMESTAMP(CONCAT(sectiondate,' ',planendtime))<![CDATA[ < ]]>UNIX_TIMESTAMP(now())
    </select>
    <!--结束课节-->
    <update id="updateSectionState2">
        update l_course_section cs
        set cs.sectionstate=3,cs.endtime=cs.planendtime
        where cs.sectionid in (${sectionids})
    </update>
    <!--修改已过期课节状态-->
    <update id="updateSectionState3">
        update l_course_section set sectionstate=4
        where sectionstate=0 and UNIX_TIMESTAMP(CONCAT(sectiondate,' ',planendtime))<![CDATA[ < ]]>UNIX_TIMESTAMP(now())
    </update>
    <!--获取要处理的课节-->
    <select id="listPendingSection" resultType="com.xhkjedu.slive.vo.liveplay.CourseSectionVo">
        SELECT cs.sectionid,cs.sectionstate,cs.planendtime FROM l_course_section cs WHERE cs.sectionstate BETWEEN 0 AND 2
        AND cs.sectiondate=CURRENT_DATE() AND cs.planendtime <![CDATA[ < ]]> CURRENT_TIME()
    </select>
    <!--更新课节学生在线状态为下线-->
    <update id="updateStuOnLined">
        update l_course_section_student css
        set css.outstamp=UNIX_TIMESTAMP(NOW()),css.onlined=2
        where css.onlined=1 and css.sectionid in
        <foreach collection="sectionids" item="sectionid" index="index" open="(" separator="," close=")">
            ${sectionid}
        </foreach>
    </update>
    <!--批量修改课节状态-->
    <update id="updateSectionState">
        update l_course_section
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="sectionstate = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when sectionid = #{item.sectionid} then #{item.sectionstate}
                </foreach>
            </trim>
            <trim prefix="endtime = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item" >
                    when sectionid = #{item.sectionid} then #{item.endtime}
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" item="item" index="index" separator="or">
            sectionid =#{item.sectionid}
        </foreach>
    </update>
    <!--获取课程教师id集合-->
    <select id="listTeacherId" resultType="java.lang.Integer">
        select distinct teacherid from l_course_section where courseid=#{courseid} and teacherid is not null
    </select>
    <!--更新实际参加学生数量-->
    <update id="updateStuNum">
        update l_course_section set stunum=
        (select count(distinct studentid) from l_course_section_student where sectionid=#{sectionid})
        where sectionid=#{sectionid}
    </update>
    <!--课节统计-->
    <select id="analyze" resultType="java.util.Map">
        select count(*)cqnum,(select classnum from t_class where classid=#{classid})classnum
        ,ifnull(sum(case when css.tostamp>a.begintime then 1 else 0 end),0)cdnum
        ,ifnull(sum(case when a.endtime>css.outstamp then 1 else 0 end),0)ztnum
        from (select UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planendtime))endtime
        ,UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planbegintime))begintime
        from l_course_section cs where cs.sectionid=#{sectionid})a
        ,l_course_section_student css where css.sectionid=#{sectionid}
    </select>
    <!--根据日期获取教师课节列表-->
    <select id="listByDate" resultType="com.xhkjedu.slive.vo.liveplay.CourseSectionVo">
        select c.coursename,cs.courseid,cs.sectionid,cs.sectionname,cs.sectiondate,cs.teacherid,cs.createid
        ,cs.planbegintime,cs.planendtime,cs.begintime,cs.endtime,cs.sectionstate,cs.stunum,cs.sectionorder
        ,(UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planendtime))-UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planbegintime))) totaltime
        ,(select count(*) from l_course_section_playback p where p.sectionid=cs.sectionid and p.pb is not null)playnum
        ,(select group_concat(distinct u.username separator '、')
        from t_user u left join l_course_section s on s.teacherid=u.userid where s.courseid=cs.courseid)teachername
        from l_course_section cs left join l_course c on cs.courseid = c.courseid
        <if test="section.classid!=null and section.classid!=0">
            left join l_course_class cc on cc.courseid=c.courseid
        </if>
        where cs.sectiondate=#{section.sectiondate} and c.deleted=1
        <if test="section.teacherid!=null and section.teacherid!=0">
            and cs.teacherid=#{section.teacherid}
        </if>
        <if test="section.schoolid!=null and section.schoolid!=0">
            and c.schoolid=#{section.schoolid}
        </if>
        <if test="section.gradeid!=null and section.gradeid!=0">
            and c.gradeid=#{section.gradeid}
        </if>
        <if test="section.subjectid!=null and section.subjectid!='0'.toString() and section.subjectid!=''">
            and c.subjectid=#{section.subjectid}
        </if>
        <if test="section.classid!=null and section.classid!=0">
            and cc.classid=#{section.classid}
        </if>
        order by cs.sectionstate,abs(UNIX_TIMESTAMP(concat(cs.sectiondate,' ',cs.planbegintime))-UNIX_TIMESTAMP(now())),cs.sectionid
    </select>
    <!--根据日期获取巡课课节列表-->
    <select id="listXkByDate" resultType="com.xhkjedu.slive.vo.liveplay.CourseSectionVo">
        select c.coursename,c.comm,cs.courseid,cs.sectionid,cs.sectionname,cs.sectiondate,cs.teacherid,cs.createid
        ,cs.planbegintime,cs.planendtime,cs.begintime,cs.endtime,cs.sectionstate,c.stunum,cs.sectionorder
        ,(UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planendtime))-UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planbegintime))) totaltime
        ,(select count(*) from l_course_section_playback p where p.sectionid=cs.sectionid and p.pb is not null)playnum
        ,concat('[',group_concat(distinct json_object('teachername',u.username,'headpic',u.headpic)),']')teachername
        ,c.gradeid,c.subjectid,s.subjectname,s.level,s.subjectorder
        from l_course_section cs left join l_course c on cs.courseid = c.courseid
        left join t_user u on cs.teacherid=u.userid
        left join t_subject s on s.subjectid=c.subjectid
        <if test="(section.classid!=null and section.classid!=0)
        or (section.classids!=null and section.classids!='')">
            left join l_course_class cc on cc.courseid=c.courseid
        </if>
        where c.deleted=1 and c.schoolid=#{section.schoolid} and cs.sectiondate=#{section.timestr}
        <if test="section.teachername!=null and section.teachername!=''">
            and u.username like '%${section.teachername}%'
        </if>
        <if test="section.cstate!=null and section.cstate!=-1">
            and cs.sectionstate=#{section.cstate}
        </if>
        <if test="section.gradeid!=null and section.gradeid!=0">
            and c.gradeid=#{section.gradeid}
        </if>
        <if test="section.subjectid!=null and section.subjectid!='0'.toString() and section.subjectid!=''">
            and c.subjectid=#{section.subjectid}
        </if>
        <if test="section.classid!=null and section.classid!=0">
            and cc.classid=#{section.classid}
        </if>
        <if test="section.classids!=null and section.classids!=''">
            and cc.classid in (${section.classids})
        </if>
        group by cs.sectionid
        order by cs.sectionstate,abs(UNIX_TIMESTAMP(concat(cs.sectiondate,' ',cs.planbegintime))-UNIX_TIMESTAMP(now())),cs.sectionid
    </select>
    <!--修改教师上课时长-->
    <update id="updateTime">
        update l_course_section set totaltime=ifnull(totaltime,0)+#{section.totaltime}
        where sectionid=#{section.sectionid} and teacherid=#{section.teacherid}
    </update>
    <resultMap id="reportResult" type="java.util.Map">
        <collection property="students" select="listKqStu" column="{classid=classid,sectionid=sectionid}"></collection>
        <collection property="qdStu" select="listQdStu" column="{sectionid=sectionid}"></collection>
        <collection property="dmStu" select="listDmStu" column="{sectionid=sectionid}"></collection>
    </resultMap>
    <!--课节报告-->
    <select id="getReport" resultMap="reportResult">
        select cs.sectionid,cs.sectionname,cs.sectiondate,cs.begintime,cs.endtime,cs.sectionstate,cs.stunum
        ,(UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planendtime))-
          UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',ifnull(cs.begintime,cs.planbegintime))))totaltime
        ,c.classid,c1.classname,truncate(cs.stunum*100/c1.classnum,2)cqrate
        ,(select u.username from t_user u where u.userid=cs.teacherid)teachername
        ,(select count(*) from l_course_section_chatroom csc where csc.sectionid=cs.sectionid and csc.usertype=2)msgnum
        ,sum(case when cst.at=20 then 1 else 0 end)qdnum
        ,sum(case when cst.at=21 then 1 else 0 end)dmnum
        from l_course_section cs left join l_course c on c.courseid=cs.courseid
        left join t_class c1 on c1.classid=c.classid
        left join l_course_section_askteacher cst on cs.sectionid=cst.sectionid and cst.at in(20,21)
        where cs.sectionid=#{sectionid}
    </select>
    <!--考勤学生名单-->
    <select id="listKqStu" resultType="com.xhkjedu.slive.vo.gradeclass.CStudentVo">
        select u.username studentname,css.studentid
        from t_class_student cs left join l_course_section_student css on cs.studentid=css.studentid and css.sectionid=#{sectionid}
        left join t_user u on cs.studentid=u.userid
        where cs.classid=#{classid} group by cs.studentid
    </select>
    <!--参与抢答学生名单-->
    <select id="listQdStu" resultType="java.util.Map">
        select u.username studentname,count(*)num,u.headpic
        from l_course_section_askteacher cst right join l_course_section_askstudent css on cst.satid=css.satid
        left join t_user u on css.studentid=u.userid
        where cst.sectionid=#{sectionid} and cst.at=20
        group by css.studentid
    </select>
    <!--参与点名学生名单-->
    <select id="listDmStu" resultType="java.util.Map">
        select u.username studentname,count(*)num,u.headpic
        from l_course_section_askteacher cst right join l_course_section_askstudent css on cst.satid=css.satid
        left join t_user u on css.studentid=u.userid
        where cst.sectionid=#{sectionid} and cst.at=21
        group by css.studentid
    </select>
    <!--课节观看列表-->
    <select id="listView" resultType="java.util.Map">
        select u.username studentname,u.loginname,css2.tostamp,css2.outstamp,css2.device
        ,(case when css2.totaltime>css2.totaltime2 then css2.totaltime2 else css2.totaltime end)totaltime
        ,truncate((case when css2.totaltime>css2.totaltime2 then css2.totaltime2 else css2.totaltime end)*100/css2.totaltime2,2)seerate
        ,(select count(*) from l_course_section_chatroom c where c.sectionid=css2.sectionid and c.userid=css2.studentid)msgnum
        ,(select count(*) from l_course_section_playback_see s where s.sectionid=css2.sectionid and s.userid=css2.studentid)seenum
        from(select css.studentid,css.sectionid,css.tostamp,css.outstamp,css.totaltime,css.device
        ,(UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planendtime))-
          UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',ifnull(cs.begintime,cs.planbegintime))))totaltime2
        from l_course_section_student css
        left join l_course_section cs on css.sectionid=cs.sectionid
        where css.sectionid=#{section.sectionid})css2 left join t_user u on css2.studentid=u.userid
    </select>
    <!--课程班级列表-->
    <select id="listClass" resultType="java.util.Map">
        select cc.classid,tc.gradeid,tc.classname
        from l_course_class cc left join t_class tc on cc.classid=tc.classid
        where cc.courseid=#{courseid}
    </select>
    <!--获取课节时长和课程类型-->
    <select id="getTotalTime" resultType="com.xhkjedu.slive.vo.liveplay.CourseVo">
        select UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planendtime))-UNIX_TIMESTAMP(CONCAT(cs.sectiondate,'
        ',cs.begintime))duration
        ,(select c.coursetype from l_course c where c.courseid=cs.courseid)coursetype
        from l_course_section cs where cs.sectionid=#{sectionid}
    </select>
    <!--课节观看列表2-->
    <select id="listView2" resultType="java.util.Map">
        select css2.studentid,u.username studentname,u.loginname,css2.tostamp,css2.totaltime
        ,truncate((case when css2.totaltime>#{section.totaltime} then #{section.totaltime} else css2.totaltime end)*100/#{section.totaltime},2)seerate
        ,(select count(*) from l_course_section_chatroom c where c.sectionid=css2.sectionid and c.userid=css2.studentid)msgnum
        ,(select count(*) from l_course_section_playback_see s where s.sectionid=css2.sectionid and s.userid=css2.studentid)seenum
        ,c.gradeid,c.classname,(select s.schoolname from t_school s where s.schoolid=c.schoolid)schoolname
        from(select css.courseid,css.studentid,css.sectionid,min(css.tostamp)tostamp
        <if test="coursetype==0">
            ,css.classid,sum(css.totaltime)totaltime
        </if>
        <if test="coursetype==1">
            ,max(cs.classid)classid,sum(css.totaltime)/count(cc.ccid) totaltime
        </if>
        from l_course_section_student css
        <if test="coursetype==1">
            left join l_course_class cc on css.courseid=cc.courseid
            left join t_class_student cs on cs.studentid=css.studentid and cs.classid=cc.classid
        </if>
        where css.sectionid=#{section.sectionid}
        <if test="section.classid!=null and section.classid!=0">
            and cc.classid=#{section.classid} and cs.csid is not null
        </if>
        group by css.studentid)css2 left join t_user u on css2.studentid=u.userid
        <if test="coursetype==0">
            left join t_class_student cs on cs.studentid=css2.studentid
            left join t_class c on cs.classid=c.classid where c.classtype=1
        </if>
        <if test="coursetype==1">
            left join t_class c on c.classid=css2.classid
        </if>
        group by css2.studentid order by css2.totaltime,css2.studentid,c.classtype
    </select>
    <!--学生观看详情列表-->
    <select id="listStuView" resultType="com.xhkjedu.slive.vo.liveplay.CSVStudentVo">
        select css.tostamp,css.outstamp,css.totaltime,css.logip,css.logaddress,css.device
        from l_course_section_student css
        where css.sectionid=#{section.sectionid} and css.studentid=#{section.studentid}
    </select>
    <!--课节提问列表-->
    <select id="listAsk" resultType="java.util.Map">
        select cst.satid,cst.at,cst.ra,cst.createtime
        ,ifnull(truncate(sum(case when css.sa is not null and css.sa!='' then 1 else 0 end)*100/
        (case when cst.stunum=0 then count(css.sasid) else cst.stunum end),2),0)tjrate
        ,ifnull(truncate(sum(case when css.sa=cst.ra then 1 else 0 end)*100/count(css.sasid),2),0)zqrate
        from l_course_section_askteacher cst left join l_course_section_askstudent css on cst.satid=css.satid
        where cst.sectionid=#{sectionid} and cst.at between 30 and 32
        group by cst.satid
    </select>
    <!--获取课节提问详情-->
    <select id="listAskStu" resultType="java.util.Map">
        select css.sa,count(*)num
        ,group_concat(u.username separator '、')studentname
        from l_course_section_askstudent css left join t_user u on css.studentid=u.userid
        where css.satid=#{satid}
        group by css.sa
    </select>
    <resultMap id="askResult" type="java.util.Map">
        <collection property="students" select="listStu" column="satid"></collection>
    </resultMap>
    <select id="listStu" resultType="java.util.Map">
        select css.sa,u.username studentname
        from l_course_section_askstudent css left join t_user u on css.studentid=u.userid
        where css.satid=#{satid}
        order by css.sa
    </select>
    <select id="listAskStu2" resultMap="askResult">
        select cst.satid,cst.at,cst.ra,cst.createtime
        ,ifnull(truncate(sum(case when css.sa is not null and css.sa!='' then 1 else 0 end)*100/
        (case when cst.stunum=0 then count(css.sasid) else cst.stunum end),2),0)tjrate
        ,ifnull(truncate(sum(case when css.sa=cst.ra then 1 else 0 end)*100/count(css.sasid),2),0)zqrate
        from l_course_section_askteacher cst left join l_course_section_askstudent css on cst.satid=css.satid
        where cst.satid=#{satid}
    </select>

    <!--获取直播中用户id列表-->
    <select id="listUserId" resultType="java.lang.Integer">
        select cs.studentid
        from t_class_student cs right join l_course c on cs.classid=c.classid
        right join l_course_section s on c.courseid=s.courseid
        where s.sectionstate=2 group by cs.studentid
        union select s.teacherid from l_course_section s where s.sectionstate=2 group by s.teacherid
    </select>
    <!--获取要结束的直播用户id列表-->
    <select id="listUserId2" resultType="java.lang.Integer">
        select cs.studentid
        from l_course_section_student cs right join l_course_section s on cs.sectionid=s.sectionid
        where s.sectionid in (${sectionids}) group by cs.studentid
        union select s.teacherid from l_course_section s
        where s.sectionid in (${sectionids}) group by s.teacherid
    </select>
    <!--获取直播中用户id列表-->
    <select id="listUserId3" resultType="java.lang.Integer">
        select cs.studentid
        from l_course_section_student cs right join l_course_section s on cs.sectionid=s.sectionid
        where s.sectionstate=2 and s.sectionid not in (${sectionids}) group by cs.studentid
        union select s.teacherid from l_course_section s
        where s.sectionstate=2 and s.sectionid not in (${sectionids}) group by s.teacherid
    </select>
    <!--我的月份下日期课节数量-->
    <select id="listMySectionDate" resultType="java.util.Map">
        select cs.sectiondate as timestr,count(distinct cs.sectionid)as num
        ,count(distinct case when cs.sectionstate=0 then cs.sectionid else null end)wksnum
        ,count(distinct case when cs.sectionstate=2 then cs.sectionid else null end)jxznum
        ,count(distinct case when cs.sectionstate=3 then cs.sectionid else null end)yjsnum
        ,count(distinct case when cs.sectionstate=4 then cs.sectionid else null end)ygqnum
        from l_course_section cs left join l_course c on cs.courseid = c.courseid
        where cs.teacherid=#{section.teacherid} and c.deleted=1 and DATE_FORMAT(cs.sectiondate, '%Y-%m')=#{section.timestr}
        <if test="section.cstate!=null and section.cstate!=-1">
            and cs.sectionstate=#{section.cstate}
        </if>
        <if test="section.sectionname!=null and section.sectionname!=''">
            and cs.sectionname like '%${section.sectionname}%'
        </if>
        group by cs.sectiondate order by cs.sectiondate asc
    </select>
    <!--我的课程列表-->
    <select id="listMySection" resultType="java.util.Map">
        select c.courseid,c.gradeid,c.stunum,c.comm,c.createid,c.classid
        ,(select t.typename from l_course_type t where t.ctypeid=c.ctypeid)typename
        ,cs.sectionid,cs.sectionname,cs.sectiondate,cs.teacherid,cs.stunum cynum
        ,cs.planbegintime,cs.planendtime,cs.begintime,cs.endtime,cs.sectionstate
        ,(UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planendtime))-UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planbegintime))) totaltime
        ,(select count(*) from l_course_section_playback p where p.sectionid=cs.sectionid and p.pb is not null)playnum
        from l_course_section cs left join l_course c on cs.courseid = c.courseid
        where cs.teacherid=#{section.teacherid} and c.deleted=1 and cs.sectiondate=#{section.timestr}
        <if test="section.cstate!=null and section.cstate!=-1">
            and cs.sectionstate=#{section.cstate}
        </if>
        <if test="section.sectionname!=null and section.sectionname!=''">
            and cs.sectionname like '%${section.sectionname}%'
        </if>
        order by cs.sectionstate,abs(UNIX_TIMESTAMP(concat(cs.sectiondate,' ',cs.planbegintime))-UNIX_TIMESTAMP(now())),cs.sectionid
    </select>
    <!--根据课节id获取课程状态-->
    <select id="getCourseBySectionId" resultType="com.xhkjedu.slive.vo.liveplay.CourseVo">
        select c.coursestate,c.deleted from l_course_section cs left join l_course c on cs.courseid = c.courseid
        where cs.sectionid=#{sectionid}
    </select>
    <!--巡课详情-->
    <resultMap id="xkResult" type="java.util.Map">
        <result column="teachername" property="teachername"></result>
        <result column="cynum" property="cynum"></result>
        <collection property="classes" javaType="java.util.List" ofType="java.util.Map">
            <result column="classname" property="classname"></result>
            <result column="classnum" property="classnum"></result>
            <result column="gradeid" property="gradeid"></result>
            <result column="classtype" property="classtype"></result>
        </collection>
    </resultMap>
    <select id="xkDetail" resultMap="xkResult">
        select c.classname,c.classnum,c.gradeid,c.classtype,cs.stunum cynum
        ,(select u.username from t_user u where u.userid=cs.teacherid)teachername
        from l_course_section cs left join l_course_class cc on cs.courseid = cc.courseid
        left join t_class c on cc.classid=c.classid
        where cs.sectionid=#{sectionid}
    </select>
    <!--课程信息-->
    <select id="getCourseById" resultType="com.xhkjedu.slive.vo.liveplay.CourseVo">
        select c.createid,c.courseid,c.classid,cs.sectionstate coursestate
        ,(select count(*) from l_course_section cs2 where cs2.courseid=c.courseid)sectionnum
        from l_course_section cs left join l_course c on cs.courseid=c.courseid
        where cs.sectionid=#{sectionid}
    </select>
    <!--修改课节、课程名称， 时间，日期-->
    <update id="myUpdate">
        update l_course_section cs left join l_course c on cs.courseid=c.courseid
        set cs.sectionname=#{section.sectionname},cs.planbegintime=#{section.planbegintime},cs.planendtime=#{section.planendtime}
        <if test="section.sectiondate!=null and section.sectiondate!=''">
            ,cs.sectiondate=#{section.sectiondate},c.begindate=#{section.sectiondate},c.enddate=#{section.sectiondate}
        </if>
        ,c.coursename=#{section.sectionname},c.begintime=#{section.planbegintime},c.endtime=#{section.planendtime}
        ,c.duration=(time_to_sec(#{section.planendtime})-time_to_sec(#{section.planbegintime}))/60
        where cs.sectionid=#{section.sectionid}
    </update>
    <!--获取课程学生列表-->
    <select id="listStudent" resultType="java.util.Map">
        select u.username studentname,u.loginname,u.usersex,cs.createtime
        <if test="coursetype==0">
        ,(select concat(c.gradeid,'_',c.classname,'_',ts.schoolname) from t_class_student cs2
        left join t_class c on cs2.classid=c.classid
        left join t_school ts on c.schoolid = ts.schoolid
        where cs2.studentid=cs.studentid and c.classtype=1 and c.year=ts.year)classname
        </if>
        <if test="coursetype==1">
        ,(select concat(c.gradeid,'_',c.classname,'_',ts.schoolname) from t_class_student cs2
        left join l_course_class cc on cs2.classid=cc.classid
        left join t_class c on cs2.classid=c.classid
        left join t_school ts on c.schoolid = ts.schoolid
        where cs2.studentid=cs.studentid and cc.courseid=lc.courseid)classname
        </if>
        from l_course lc left join t_class_student cs on lc.classid=cs.classid
        left join t_user u on cs.studentid = u.userid
        where lc.classid=#{classid}
        <if test="studentname!=null and studentname!=''">
            and (u.username like '%${studentname}%' or u.loginname like '%${studentname}%')
        </if>
        order by cs.csid desc
    </select>
    <!--根据班级id获取课程班级类型-->
    <select id="getCourseTypeByClassId" resultType="java.lang.Integer">
        select coursetype from l_course where classid=#{classid} limit 1
    </select>

    <!--学生课节日历-->
    <select id="listStuSectionDate" resultType="java.util.Map">
        select cs.sectiondate as timestr,count(distinct cs.sectionid)as num
        ,count(distinct case when cs.sectionstate=0 then cs.sectionid else null end)wksnum
        ,count(distinct case when cs.sectionstate=2 then cs.sectionid else null end)jxznum
        ,count(distinct case when cs.sectionstate=3 then cs.sectionid else null end)yjsnum
        ,count(distinct case when cs.sectionstate=4 then cs.sectionid else null end)ygqnum
        from l_course_section cs right join l_course c on cs.courseid = c.courseid
        right join t_class_student s on s.classid=c.classid
        where s.studentid=#{section.studentid} and c.deleted=1 and DATE_FORMAT(cs.sectiondate, '%Y-%m')=#{section.timestr}
        <if test="section.subjectid!=null and section.subjectid!='0'.toString() and section.subjectid!=''">
            and c.subjectid=#{section.subjectid}
        </if>
        <if test="section.cstate!=null and section.cstate!=-1">
            and cs.sectionstate=#{section.cstate}
        </if>
        <if test="section.sectionname!=null and section.sectionname!=''">
            and cs.sectionname like '%${section.sectionname}%'
        </if>
        group by cs.sectiondate order by cs.sectiondate asc
    </select>
    <!--学生课节列表-->
    <select id="listStuByDate" resultType="java.util.Map">
        select c.courseid,c.gradeid,c.stunum,c.comm,c.createid,c.classid
        ,cs.sectionid,cs.sectionname,cs.sectiondate,cs.teacherid,cs.stunum cynum
        ,cs.planbegintime,cs.planendtime,cs.begintime,cs.endtime,cs.sectionstate
        ,(UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planendtime))-UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planbegintime))) totaltime
        ,(select count(*) from l_course_section_playback p where p.sectionid=cs.sectionid and p.pb is not null)playnum
        ,(select s.subjectname from t_subject s where s.subjectid=c.subjectid)subjectname
        ,u.username teachername,u.headpic
        from l_course_section cs right join l_course c on cs.courseid = c.courseid
        right join t_class_student s on s.classid=c.classid
        left join t_user u on u.userid=cs.teacherid
        where s.studentid=#{section.studentid} and c.deleted=1 and cs.sectiondate=#{section.timestr}
        <if test="section.subjectid!=null and section.subjectid!='0'.toString() and section.subjectid!=''">
            and c.subjectid=#{section.subjectid}
        </if>
        <if test="section.cstate!=null and section.cstate!=-1">
            and cs.sectionstate=#{section.cstate}
        </if>
        <if test="section.sectionname!=null and section.sectionname!=''">
            and cs.sectionname like '%${section.sectionname}%'
        </if>
        order by cs.sectionstate,abs(UNIX_TIMESTAMP(concat(cs.sectiondate,' ',cs.planbegintime))-UNIX_TIMESTAMP(now())),cs.sectionid
    </select>
</mapper>
