<?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.sclass.mapper.classroom.ClassroomMapper">
    <!--保存课堂-->
    <insert id="save">
        insert into t_classroom(roomid,roomname,classid,roomstate,lsbid,teacherid,directorid
        ,directortype,classnum,endtime,createid,createtime,schoolid)
        values
        (#{r.roomid},#{r.roomname},#{r.classid},#{r.roomstate},#{r.lsbid},#{r.teacherid},#{r.directorid}
        ,#{r.directortype},#{r.classnum},#{r.endtime},#{r.teacherid},#{r.createtime},#{r.schoolid})
    </insert>
    <!--获取班级学生id集合-->
    <select id="listStudentId" resultType="java.lang.Integer">
        select cs.studentid from t_class_student cs
        left join t_classroom r on cs.classid=r.classid
        where r.roomid=#{roomid};
    </select>
    <!--获取批阅信息-->
    <resultMap id="correctResult" type="com.xhkjedu.sclass.vo.classroom.RoomCorrectVo">
        <result property="roomid" column="roomid"></result>
        <result property="roomname" column="roomname"></result>
        <result property="createtime" column="createtime"></result>
        <result property="endtime" column="endtime"></result>
        <result property="classname" column="classname"></result>
        <result property="gradeid" column="gradeid"></result>
        <result property="teachername" column="teachername"></result>
        <collection property="calls" ofType="com.xhkjedu.sclass.vo.classroom.RoomCorrectCallVo"
                    select="listCall" column="{roomid=roomid,realstunum=realstunum}">
        </collection>
        <collection property="exams" ofType="com.xhkjedu.sclass.vo.classroom.RoomCorrectExamVo"
                    select="listExam" column="{roomid=roomid,realstunum=realstunum}">
        </collection>
    </resultMap>
    <select id="listCall" resultType="com.xhkjedu.sclass.vo.classroom.RoomCorrectCallVo">
        select s.asktid,count(*)commitnum,count(*) stunum
        ,sum(case when s.radiocorrect!=0 then 1 else 0 end)correctnum
        from t_classroom_askstudent s left join t_classroom_askteacher rt on s.asktid=rt.asktid
        where rt.roomid=#{roomid} and rt.asktype between 32 and 33
        group by s.asktid order by rt.createtime
    </select>
    <select id="listExam" resultType="com.xhkjedu.sclass.vo.classroom.RoomCorrectExamVo">
        select s.paperid,p.papername,count(*)commitnum,count(*) stunum
        ,sum(case when s.pschecked!=0 then 1 else 0 end)correctnum
        from t_classroom_paper_student s left join t_classroom_paper p on s.paperid=p.paperid
        left join t_classroom_askteacher rt on s.paperid=rt.paperid
        where rt.roomid=#{roomid} and rt.asktype=40
        group by s.paperid order by p.createtime
    </select>
    <select id="listCorrect" resultMap="correctResult">
        select r.roomid,r.roomname,r.createtime,r.endtime,r.realstunum
        ,c.classname,c.gradeid
        ,(select u.username  from t_user u where u.userid=r.createid)teachername
        from t_classroom r left join t_class c on r.classid=c.classid
        where r.roomid=#{roomid}
    </select>

    <!--获取提问批改学生列表-->
    <resultMap id="checkResult" type="com.xhkjedu.sclass.vo.paper.CheckClassVo">
        <result property="classid" column="classid"></result>
        <result property="classnum" column="classnum"></result>
        <result property="ytjnum" column="ytjnum"></result>
        <result property="ypynum" column="ypynum"></result>
        <result property="teacherpic" column="teacherpic"></result>
        <collection property="students" ofType="com.xhkjedu.sclass.vo.paperstudent.StudentVo">
            <result property="psid" column="psid"></result>
            <result property="studentid" column="studentid"></result>
            <result property="studentname" column="studentname"></result>
            <result property="pschecked" column="pschecked"></result>
            <result property="studentpic" column="studentpic"></result>
        </collection>
    </resultMap>
    <select id="listStudentsByAskTid" resultMap="checkResult">
        select rs.classid,rs.asksid psid,rs.studentid,u.username studentname,rs.radiocorrect pschecked,rs.studentpic,rt.teacherpic
        ,count(rs2.asksid)ytjnum,count(rs2.asksid)classnum
        ,sum(case when rs2.radiocorrect!=0 then 1 else 0 end)ypynum
        from t_classroom_askstudent rs left join t_user u on rs.studentid=u.userid
        left join t_classroom_askteacher rt on rs.asktid=rt.asktid
        left join t_classroom_askstudent rs2 on rs2.asktid=rs.asktid
        where rs.asktid=#{asktid}
        group by rs.asksid order by rs.studentid
    </select>
    <!--获取考试批改学生列表-->
    <select id="listStudentsByPaperId" resultMap="checkResult">
        select ps.classid,ps.paperid,ps.psid,ps.studentid,u.username studentname,ps.pschecked
        ,count(ps2.psid)ytjnum,count(ps2.psid)classnum
        ,sum(case when ps2.pschecked=1 then 1 else 0 end)ypynum
        from t_classroom_paper_student ps left join t_user u on ps.studentid=u.userid
        left join t_classroom_paper_student ps2 on ps2.paperid=ps.paperid
        where ps.paperid=#{paperid}
        group by ps.psid order by ps.studentid
    </select>

    <resultMap id="objectiveResult" type="com.xhkjedu.sclass.vo.classroom.ObjectiveVo">
        <result property="typename" column="typename"></result>
        <result property="answer" column="answer"></result>
        <result property="correctrate" column="correctrate"></result>
        <result property="psorder" column="psorder"></result>
        <result property="psscore" column="psscore"></result>
        <result property="avgscore" column="avgscore"></result>
        <result property="maxscore" column="maxscore"></result>
        <collection property="answers" ofType="com.xhkjedu.sclass.vo.classroom.ObjectiveAnswerVo">
            <result property="section" column="section"></result>
            <result property="stunum" column="stunum"></result>
            <result property="stuscore" column="stuscore"></result>
            <result property="stuname" column="stuname"></result>
        </collection>
    </resultMap>
    <!--客观题详情-->
    <select id="callObjective" resultMap="objectiveResult">
        select (case rt.asktype when 30 then '判断题' when 31 then '选择题' when 32 then '简答题' else '画图题' end )typename
        ,rt.radioanswer answer,rs.studentradio section,count(*)stunum
        ,group_concat(u.username order by convert(u.username using gbk) separator '  ')stuname
        ,truncate((select sum(case when rs2.radiocorrect=1 then 1 else 0 end)*100/
        count(*) from t_classroom_askstudent rs2 where rs2.asktid=rt.asktid),2)correctrate
        from t_classroom_askstudent rs right join t_classroom_askteacher rt on rs.asktid=rt.asktid
        left join t_user u on rs.studentid=u.userid
        where rt.asktid=#{asktid}
        group by rs.studentradio
    </select>
    <!--2020.11.16修改，125行添加left-->
    <select id="examObjective" resultMap="objectiveResult">
        select ps.ctypename typename,ps.psanswer answer,ps.psorder,ps.psscore
        ,pss.useranswer section,count(*)stunum,pss.stuscore,a.avgscore,a.maxscore
        ,group_concat(u.username order by convert(u.username using gbk) separator '  ')stuname
        from t_classroom_paper_scantron ps left join t_classroom_paper_student_scantron pss on ps.pscaid=pss.pscaid
        left join (select truncate(sum(pss2.stuscore)/(select count(ps2.psid)
        from t_classroom_paper_student ps2 where ps2.paperid=pss2.paperid),2)avgscore
        ,max(pss2.stuscore)maxscore,pss2.pscaid
        FROM t_classroom_paper_student_scantron pss2 WHERE pss2.pscaid=#{pscaid})a on ps.pscaid=a.pscaid
        left join t_user u on pss.studentid=u.userid
        where ps.pscaid=#{pscaid}
        group by pss.useranswer
    </select>

    <resultMap id="subjectiveResult" type="com.xhkjedu.sclass.vo.classroom.SubjectiveVo">
        <result property="typename" column="typename"></result>
        <result property="correctrate" column="correctrate"></result>
        <result property="psorder" column="psorder"></result>
        <result property="psscore" column="psscore"></result>
        <result property="avgscore" column="avgscore"></result>
        <result property="maxscore" column="maxscore"></result>
        <collection property="answers" ofType="java.util.Map">
            <result property="useranswer" column="useranswer"></result>
            <result property="checked" column="checked"></result>
            <result property="stuscore" column="stuscore"></result>
            <result property="stuname" column="stuname"></result>
        </collection>
    </resultMap>
    <!--主观题详情-->
    <select id="callSubjective" resultMap="subjectiveResult">
        select (case rt.asktype when 30 then '判断题' when 31 then '选择题' when 32 then '简答题' else '画图题' end )typename
        ,rs.studentpic useranswer,rs.radiocorrect checked,u.username stuname
        ,(select ifnull(truncate(sum(case when s.radiocorrect=1 then 1 else 0 end)*100/count(*),2),0)
        from t_classroom_askstudent s where s.asktid=rt.asktid)correctrate
        from t_classroom_askstudent rs right join t_classroom_askteacher rt on rs.asktid=rt.asktid
        left join t_user u on rs.studentid=u.userid
        where rt.asktid=#{asktid}
    </select>
    <select id="examSubjective" resultMap="subjectiveResult">
        select ps.ctypename typename,ps.psorder,ps.psscore,a.avgscore,a.maxscore
        ,pss.useranswer,pss.checked,pss.stuscore,u.username stuname
        from t_classroom_paper_scantron ps left join t_classroom_paper_student_scantron pss on ps.pscaid=pss.pscaid
        left join t_user u on pss.studentid=u.userid
        left join(select truncate(sum(pss1.stuscore)/(select count(ps2.psid)
        from t_classroom_paper_student ps2 where ps2.paperid=pss1.paperid),2)avgscore
        ,max(pss1.stuscore)maxscore,pss1.pscaid
        from t_classroom_paper_student_scantron pss1 where pss1.pscaid=#{pscaid})a on pss.pscaid=a.pscaid
        where ps.pscaid=#{pscaid}
    </select>

    <!--课堂提问批阅-->
    <update id="correctCall">
        update t_classroom_askstudent set radiocorrect=#{radiocorrect} where asksid=#{asksid}
    </update>

    <!--学生课堂回顾列表-->
    <select id="listStuClass" resultMap="classRooms">
        select r.roomid,r.roomname,r.classid,r.roomstate,r.directorid,r.createtime,r.endtime,r.realstunum,u.username createname
        ,(select d.directorname from t_director d where d.directorid=r.directorid)directorname
        ,g.gradeid,g.classname,(select count(*) from t_classroom_sign s where s.userid=#{room.studentid}
        and s.roomid=r.roomid) signstate
        from t_classroom r left join t_user u on r.teacherid=u.userid
        left join t_subject_book sb on r.lsbid=sb.lsbid
        left join t_class_student cs on r.classid=cs.classid
        LEFT JOIN t_class g ON r.classid=g.classid
        where cs.studentid=#{room.studentid}
        <if test="room.subjectid!=null and room.subjectid!='0'.toString() and room.subjectid!=''">
            and sb.subjectid=#{room.subjectid}
        </if>
        <if test="room.lsbid!=null and room.lsbid!='0'.toString() and room.lsbid!=''">
          and r.lsbid=#{room.lsbid}
        </if>
        <if test="room.directorid!=null and room.directorid!='0'.toString() and room.directorid!=''">
          and r.directorid=#{room.directorid}
        </if>
        <if test="room.createtime!=null and room.createtime!=0">
            AND UNIX_TIMESTAMP(FROM_UNIXTIME(r.createtime,'%Y%m%d'))>=#{room.createtime}
        </if>
        <if test="room.endtime!=null and room.endtime!=0">
            AND UNIX_TIMESTAMP(FROM_UNIXTIME(r.createtime,'%Y%m%d'))&lt;=#{room.endtime}
        </if>
        group by r.roomid order by r.createtime desc
    </select>
    <!--学生课堂回顾详情-->
    <resultMap id="stuRoomResult" type="java.util.Map">
        <result property="roomid" column="roomid"></result>
        <result property="roomname" column="roomname"></result>
        <result property="createtime" column="createtime"></result>
        <result property="endtime" column="endtime"></result>
        <result property="classnum" column="classnum"></result>
        <result property="realstunum" column="realstunum"></result>
        <result property="teachername" column="teachername"></result>
        <result property="directorname" column="directorname"></result>
        <collection property="questions" ofType="java.util.Map"
                    select="listStuCall" column="{roomid=roomid,studentid=studentid}">
        </collection>
        <collection property="exams" ofType="java.util.Map"
                    select="listStuExam" column="{roomid=roomid,studentid=studentid,realstunum=realstunum}">
        </collection>
        <collection property="asks" ofType="java.util.Map"
                    select="listStuAsk" column="{roomid=roomid,studentid=studentid}">
        </collection>
        <collection property="files" ofType="com.xhkjedu.sclass.model.classroom.TClassroomFile"
                    select="listFile" column="{roomid=roomid}">
        </collection>
    </resultMap>
    <select id="listFile" resultType="com.xhkjedu.sclass.model.classroom.TClassroomFile">
        select * from t_classroom_file where roomid=#{roomid}
    </select>
    <select id="getReportStu" resultMap="stuRoomResult">
        select r.roomid,r.roomname,r.endtime,r.createtime,r.classnum,r.realstunum,#{studentid} studentid,
        u.username as teachername,d.directorname,c.gradeid,c.classname
        from t_classroom r left join t_user u on r.teacherid=u.userid
        left join t_director d on r.directorid=d.directorid
        left join t_class c on r.classid=c.classid
        where r.roomid=#{roomid} limit 1
    </select>
    <select id="listStuCall" resultType="java.util.Map">
        select rt.asktype,rt.teacherpic,rt.radioanswer,ifnull(s.createtime,0)createtime,s.radiocorrect
        ,(case when rt.asktype between 32 and 33 then s.studentpic else s.studentradio end)useranswer
        ,(select ifnull(truncate(sum(case when s1.radiocorrect=1 then 1 else 0 end)*100/count(*),2),0)
        from t_classroom_askstudent s1 where s1.asktid=rt.asktid)correctrate
        from t_classroom_askstudent s right join t_classroom_askteacher rt on s.asktid=rt.asktid and s.studentid=#{studentid}
        where rt.roomid=#{roomid} and rt.asktype between 30 and 33
        group by rt.asktid order by rt.createtime
    </select>
    <select id="listStuAsk" resultType="java.util.Map">
        select a.asktype,a.fqnum,a.cynum,truncate(a.cynum*100/a.fqnum,2)cyrate
        from(select rt.asktype,count(distinct rt.asktid)fqnum,(select count(*) from t_classroom_askstudent s1
        left join t_classroom_askteacher rt1 on s1.asktid=rt1.asktid
        where rt1.roomid=rt.roomid and rt1.asktype=rt.asktype and s1.studentid=#{studentid})cynum
        from t_classroom_askstudent s right join t_classroom_askteacher rt on s.asktid=rt.asktid
        where rt.roomid=#{roomid} and rt.asktype between 20 and 21
        group by rt.asktype)a
    </select>
    <resultMap id="stuExamResult" type="java.util.Map">
        <result property="paperid" column="paperid"></result>
        <result property="papername" column="papername"></result>
        <result property="papernum" column="papernum"></result>
        <result property="paperscore" column="paperscore"></result>
        <result property="avgscore" column="avgscore"></result>
        <result property="maxscore" column="maxscore"></result>
        <result property="scorerate" column="scorerate"></result>
        <result property="stuscore" column="stuscore"></result>
        <collection property="files" ofType="java.lang.String"
                    select="listStuEFile" column="{paperid=paperid}">
        </collection>
        <collection property="questions" ofType="java.util.Map"
                    select="listStuEQuestion" column="{paperid=paperid,studentid=studentid,realstunum=realstunum}">
        </collection>
    </resultMap>
    <select id="listStuEFile" resultType="java.lang.String">
        select filepath from t_classroom_paper_file where paperid=#{paperid} order by fileorder
    </select>
    <select id="listStuExam" resultMap="stuExamResult">
        select p.paperid,p.papername,p.papernum,p.paperscore
        ,(select ifnull(truncate(sum(s.stuscore)/(count(s.psid)*s.paperscore),2),0))avgscore
        ,(select ifnull(max(s.stuscore),0))maxscore
        ,ifnull(truncate(s1.stuscore*100/p.paperscore,2),0)scorerate,ifnull(s1.stuscore,0)stuscore,s1.psid
        ,#{studentid} studentid,count(s.psid) realstunum
        from t_classroom_paper p right join t_classroom_askteacher rt on p.paperid=rt.paperid
        left join t_classroom_paper_student s on s.paperid=p.paperid
        left join t_classroom_paper_student s1 on s1.paperid=rt.paperid and s1.studentid=#{studentid}
        where rt.roomid=#{roomid} and rt.asktype=40 and p.paperid is not null
        group by p.paperid order by p.createtime
    </select>
    <select id="listStuEQuestion" resultType="java.util.Map">
        select
        ps.pscaid,ps.psorder,ps.ctypename,ps.ctype,ps.psscore,ps.psanswer,ifnull(pss.stuscore,0)stuscore,pss.useranswer
        ,ifnull(truncate(pss.stuscore*100/pss.qscore,2),0)scorerate
        ,ifnull(truncate(sum(pss1.stuscore)/#{realstunum},2),0)avgscore,ifnull(max(pss1.stuscore),0)maxscore
        from t_classroom_paper_scantron ps left join t_classroom_paper_student_scantron pss1 on ps.pscaid=pss1.pscaid
        left join t_classroom_paper_student_scantron pss on ps.pscaid=pss.pscaid and pss.studentid=#{studentid}
        where ps.paperid=#{paperid} group by ps.pscaid
    </select>
    <select id="getRoomStudentSigin" resultType="java.lang.Integer">
        select count(*) from t_classroom_sign where roomid=#{roomid} and userid=#{studentid}
    </select>
    <!--结束课堂-->
    <update id="endClass">
        UPDATE t_classroom SET roomstate=2,endtime=#{room.endtime} where roomid=#{room.roomid};
        UPDATE t_classroom_askteacher t LEFT JOIN (SELECT s.asktid,count(*)asknum
        FROM t_classroom_askstudent s left join t_classroom_askteacher ct on s.asktid=ct.asktid
        LEFT JOIN t_classroom c ON ct.roomid=c.roomid
        WHERE ct.roomid=#{room.roomid} AND s.classid=c.classid GROUP BY ct.asktid)t2
        ON t.asktid=t2.asktid
        SET t.asknum=t2.asknum WHERE t.roomid=#{room.roomid}
    </update>
    <!--根据班级id获取学生数量-->
    <select id="getStudentNumById" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM t_class_student s WHERE s.classid=#{classid}
    </select>

    <!--课堂集合-->
    <resultMap id="classRooms" type="java.util.Map">
        <result property="roomid" column="roomid"></result>
        <result property="roomname" column="roomname"></result>
        <result property="directorname" column="directorname"></result>
        <result property="classid" column="classid"></result>
        <result property="classname" column="classname"></result>
        <result property="gradeid" column="gradeid"></result>
        <result property="createtime" column="createtime"></result>
        <result property="endtime" column="endtime"></result>
        <result property="realstunum" column="realstunum"></result>
        <result property="createname" column="createname"></result>
        <result property="wpynum" column="wpynum"></result>
        <result property="classnum" column="classnum"></result>
        <result property="signstate" column="signstate"></result>
        <collection property="hdlist" ofType="com.xhkjedu.sclass.vo.classroom.RoomAskTeacherVo"
                    select="listAskTeachersForRoom" column="{roomid=roomid}">
            <result property="asktid" column="asktid"></result>
            <result property="asktype" column="asktype"></result>
        </collection>
        <collection property="filelist" ofType="java.util.Map" select="listFilesForRoom" column="{roomid=roomid}">
            <result property="rfid" column="rfid"></result>
            <result property="rfpath" column="rfpath"></result>
            <result property="rftype" column="rftype"></result>
            <result property="createtime" column="createtime"></result>
        </collection>
    </resultMap>

    <!--获取教师指定章节下上过的课堂历史-->
    <select id="listClassByDirectorId" resultMap="classRooms">
        SELECT c.roomid,c.roomname,c.classid,g.classname,g.gradeid,c.roomstate,c.createtime,c.endtime,
        c.realstunum,u.username createname,c.classnum,d.directorname,
        (select count(rs.asksid)+count(ps.psid) from t_classroom_askteacher rt
        left join t_classroom_askstudent rs on rt.asktid=rs.asktid and rs.radiocorrect=0
        left join t_classroom_paper_student ps on rt.paperid=ps.paperid and ps.pschecked!=1
        where rt.roomid=c.roomid and rt.asktype between 32 and 40)wpynum
        FROM t_classroom c LEFT JOIN t_class g ON c.classid=g.classid
        left join t_user u on c.teacherid=u.userid
        left join t_director d on c.directorid=d.directorid
        WHERE c.teacherid=#{room.teacherid} and c.lsbid=#{room.lsbid}
        <if test="room.directorid!=null and room.directorid!='0'.toString() and room.directorid!=''">AND
            c.directorid=#{room.directorid}
        </if>
        <if test="room.classid!=null and room.classid!=0">AND c.classid=#{room.classid}</if>
        <if test="room.createtime!=null and room.createtime!=0">
            AND UNIX_TIMESTAMP(FROM_UNIXTIME(c.createtime,'%Y%m%d'))>=#{room.createtime}
        </if>
        <if test="room.endtime!=null and room.endtime!=0">
            AND UNIX_TIMESTAMP(FROM_UNIXTIME(c.createtime,'%Y%m%d'))&lt;=#{room.endtime}
        </if>
        ORDER BY c.createtime DESC
    </select>
    <select id="listAskTeachersForRoom" resultType="com.xhkjedu.sclass.vo.classroom.RoomAskTeacherVo">
        select asktid,asktype from t_classroom_askteacher where roomid=#{roomid}
    </select>
    <select id="listFilesForRoom" resultType="java.util.Map">
        select rfid,rfpath,rftype,createtime from t_classroom_file where roomid=#{roomid} order by createtime
    </select>

    <!--更新班级实际签到学生数量-->
    <update id="updateBatchRoomRealStunum" parameterType="java.util.List">
        UPDATE t_classroom c SET c.realstunum=(SELECT COUNT(DISTINCT s.userid) FROM t_classroom_sign s WHERE
        s.roomid=c.roomid )
        WHERE
        <foreach collection="list" item="roomid" index="index" separator="or">
            c.roomid=#{roomid}
        </foreach>
    </update>
    <!--批量结束班级之前的课堂-->
    <update id="updateBatchRoomState" parameterType="java.util.List">
        UPDATE t_classroom set roomstate=2,endtime=createtime+60 * 45 WHERE
        <foreach collection="list" item="classid" index="index" separator="or">
            (classid=#{classid} and roomstate=1)
        </foreach>
    </update>
    <!--课堂回顾详情-->
    <select id="getReport" resultType="com.xhkjedu.sclass.vo.classroom.ReportRoomVo">
        select r.roomname,r.classnum,r.realstunum,r.createtime,r.endtime,c.classname,c.gradeid,u.username createname
        ,(select count(*) from t_classroom_askteacher ct where ct.roomid=r.roomid and ct.asktype=20)asknum
        ,(select count(*) from t_classroom_askteacher ct where ct.roomid=r.roomid and ct.asktype=21)callnum
        from t_classroom r left join t_class c on r.classid=c.classid
        left join t_user u on r.createid=u.userid
        where r.roomid=#{roomid}
    </select>
    <!--课堂提问列表-->
    <select id="listQuestionReport" resultType="java.util.Map">
        select ct.asktype,ct.teacherpic,ct.asktid,ct.radioanswer
        ,(case when count(cs.asksid)=0 then 0 else 100 end)commitrate
        ,ifnull(truncate(sum(case when cs.radiocorrect=1 and cs.classid=c.classid then 1 else 0
        end)*100/count(cs.asksid),2),0)correctrate
        from t_classroom_askteacher ct left join t_classroom_askstudent cs on ct.asktid = cs.asktid
        left join t_classroom c on ct.roomid=c.roomid and cs.classid=c.classid
        where ct.roomid=#{roomid} and ct.asktype between 30 and 33
        group by ct.asktid order by ct.createtime
    </select>
    <!--课堂点名列表-->
    <select id="listCallReport" resultType="java.util.Map">
        select cs.studentid,u.username studentname,u.headpic,u.usersex,count(*)num
        from t_classroom_askteacher ct left join t_classroom_askstudent cs on ct.asktid = cs.asktid
        left join t_user u on cs.studentid=u.userid
        left join t_classroom c on ct.roomid=c.roomid and cs.classid=c.classid
        where ct.roomid=#{roomid} and ct.asktype=21
        group by cs.studentid
        order by convert(u.username using gbk)
    </select>
    <!--课堂抢答列表-->
    <select id="listAskReport" resultType="java.util.Map">
        select cs.studentid,u.username studentname,u.headpic,u.usersex,count(*)num
        from t_classroom_askteacher ct left join t_classroom_askstudent cs on ct.asktid = cs.asktid
        left join t_user u on cs.studentid=u.userid
        left join t_classroom c on ct.roomid=c.roomid
        where ct.roomid=#{roomid} and ct.asktype=20 and cs.classid=c.classid
        group by cs.studentid
        order by convert(u.username using gbk)
    </select>
    <!--课堂截图列表-->
    <select id="listFileReport" resultType="com.xhkjedu.sclass.model.classroom.TClassroomFile">
        select * from t_classroom_file f where f.roomid=#{roomid} order by f.createtime
    </select>
    <!--课堂考试列表-->
    <select id="listExamReport" resultType="com.xhkjedu.sclass.vo.classroom.ReportEQuestionVo">
        select ps.paperid,ps.psorder,ps.psanswer,ps.psscore,ps.ctypename,ps.ctype,ps.pscaid
        ,sum(case when q.answered=1 then 1 else 0 end)commitrate
        ,sum(case when q.stuscore=ps.psscore then 1 else 0 end)correctrate
        from t_classroom_paper_scantron ps left join t_classroom_paper_student_scantron q
        on q.pscaid=ps.pscaid and q.classid=#{classid}
        where ps.paperid in
        <foreach collection="paperids" item="paperid" index="index" open="(" separator="," close=")">
            ${paperid}
        </foreach>
        group by ps.pscaid order by ps.psorder
    </select>
    <!--获取课堂考试列表-->
    <select id="listPaper" resultType="com.xhkjedu.sclass.vo.classroom.ReportExamVo">
        SELECT p.paperid,p.papername,c.classid,ifnull(truncate(SUM(ps.stuscore)*100/(p.paperscore*COUNT(ps.psid)),2),0)scorerate
        ,ifnull(truncate(SUM(ps.stuscore)/COUNT(ps.psid),2),0)avgscore,ifnull(MAX(ps.stuscore),0)maxscore,COUNT(ps.psid)ytjnum
        FROM t_classroom_paper p
        LEFT JOIN t_classroom_paper_student ps ON p.paperid=ps.paperid
        LEFT JOIN t_classroom_askteacher rt ON p.paperid=rt.paperid
        LEFT JOIN t_classroom c ON rt.roomid=c.roomid
        WHERE c.roomid=#{roomid} GROUP BY p.paperid order by p.createtime
    </select>

    <!--班级学生列表-->
    <select id="listStudent" resultType="java.util.Map">
        select cs.studentid,u.username studentname,count(rs.asksid) num
        from t_class_student cs left join t_classroom r on cs.classid=r.classid
        and r.createtime>UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 1 YEAR)) and r.lsbid=#{lsbid}
        left join t_user u on cs.studentid=u.userid
        left join t_classroom_askteacher ct on r.roomid=ct.roomid and ct.asktype=21
        left join t_classroom_askstudent rs on ct.asktid=rs.asktid and cs.studentid=rs.studentid
        where cs.classid=#{classid}
        group by cs.studentid order by convert(u.username using gbk)
    </select>

    <!--批量保存课堂-->
    <insert id="batchSave">
        insert ignore into t_classroom(roomid,roomname,classid,roomstate,lsbid,teacherid,directorid
        ,directortype,classnum,endtime,createid,createtime,schoolid)
        values
        <foreach collection="rooms" item="r" separator=",">
            (#{r.roomid},#{r.roomname},#{r.classid},1,#{r.lsbid},#{r.teacherid},#{r.directorid}
            ,#{r.directortype},#{r.classnum},#{r.endtime},#{r.teacherid},#{r.createtime},#{r.schoolid})
        </foreach>
    </insert>
    <!--批量结束课堂-->
    <update id="batchEndClass">
        <foreach collection="rooms" item="r" separator=";">
            UPDATE t_classroom SET roomstate=2,endtime=${r.endtime} where roomid=${r.roomid}
        </foreach>
    </update>
    <!--根据班级ids获取学生数量-->
    <select id="listStudentNumByClassIds" resultType="com.xhkjedu.sclass.model.classroom.TClassroom">
        SELECT classid,classnum FROM t_class WHERE classid in
        <foreach collection="classids" item="classid" open="(" close=")" separator=",">
            ${classid}
        </foreach>
    </select>
    <!--根据课堂id获取课堂名称-->
    <select id="getNameById" resultType="java.lang.String">
        select roomname from t_classroom where roomid=#{roomid}
    </select>
</mapper>
