<?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.sapi.mapper.analyze.SchoolRoomMapper">
    <!--课堂统计-->
    <resultMap id="schoolroomsbuject" type="com.xhkjedu.sapi.vo.analyze.SchoolRoomSubjectVo">
        <result property="subjectid" column="subjectid"/>
        <result property="subjectname" column="subjectname"/>
        <collection property="roomlist" ofType="com.xhkjedu.sapi.vo.analyze.SchoolRoomGradeRoomVo">
            <result property="roomid" column="roomid"/>
            <result property="roomtime" column="roomtime"/>
        </collection>
    </resultMap>
    <select id="listSchoolRoomSubject" resultMap="schoolroomsbuject">
       SELECT s.subjectid,s.subjectname,r.roomid,
       (CASE WHEN r.endtime IS NULL THEN 0 WHEN r.endtime IS NOT NULL THEN (r.endtime-r.createtime) END)AS roomtime
         FROM t_classroom r LEFT JOIN t_subject_book sb ON r.lsbid=sb.lsbid
         LEFT JOIN t_class c ON r.classid=c.classid
         LEFT JOIN t_subject s ON sb.subjectid = s.subjectid
         LEFT JOIN t_user u ON r.createid=u.userid
        left join t_director d on r.directorid=d.directorid
        where d.disabled=0 and r.roomstate=2 AND r.schoolid=#{sparams.schoolid} and c.gradeid=#{sparams.gradeid} and r.year=#{sparams.year} AND u.userstate=1
        <if test="sparams.starttime!=null and sparams.starttime!=0">
            AND r.createtime >= #{sparams.starttime}
        </if>
        <if test="sparams.stoptime!=null and sparams.stoptime!=0">
            AND r.createtime <![CDATA[ <= ]]>#{sparams.stoptime}
        </if>
          GROUP BY r.roomid ORDER BY s.subjectorder
    </select>


    <!--课堂统计详情-->
    <resultMap id="schoolroomdetail" type="com.xhkjedu.sapi.vo.analyze.SchoolRoomGradeUserVo">
        <result property="userid" column="userid"/>
        <result property="username" column="username"/>
        <result property="headpic" column="headpic"/>
        <collection property="udlist" ofType="com.xhkjedu.sapi.vo.analyze.SchoolRoomGradeUserDetailVo">
            <result property="roomid" column="roomid"/>
            <result property="questionnum" column="questionnum"/>
            <result property="examnum" column="examnum"/>
            <result property="asknum" column="asknum"/>
            <result property="callnum" column="callnum"/>
            <result property="picnum" column="picnum"/>
            <result property="totaltime" column="totaltime"/>
            <result property="classnum" column="classnum"/>
            <result property="realstunum" column="realstunum"/>
        </collection>
    </resultMap>
    <select id="listSchoolRoomDetailBySubject" resultMap="schoolroomdetail">
        SELECT r.teacherid AS userid,u.username,u.headpic,r.roomid,r.classnum,r.realstunum,
        (CASE WHEN r.endtime IS NULL THEN 0 WHEN r.endtime IS NOT NULL THEN (r.endtime-r.createtime) END)AS totaltime,
        (SELECT COUNT(*) FROM t_classroom_askteacher ca WHERE ca.roomid=r.roomid AND asktype BETWEEN 30 AND 33) questionnum,
        (SELECT COUNT(*) FROM t_classroom_askteacher ca WHERE ca.roomid=r.roomid AND asktype=40) examnum,
        (SELECT COUNT(*) FROM t_classroom_askteacher ca WHERE ca.roomid=r.roomid AND asktype=20) asknum,
        (SELECT COUNT(*) FROM t_classroom_askteacher ca WHERE ca.roomid=r.roomid AND asktype=21) callnum,
        (SELECT COUNT(*) FROM t_classroom_file cf WHERE cf.roomid=r.roomid) picnum
         FROM t_classroom r
          LEFT JOIN t_subject_book sb ON r.lsbid=sb.lsbid
          LEFT JOIN t_subject s ON sb.subjectid=s.subjectid
         LEFT JOIN t_class c ON r.classid=c.classid
          LEFT JOIN t_user u ON r.createid=u.userid
        left join t_director d on r.directorid=d.directorid
        where d.disabled=0 and r.roomstate=2 AND r.schoolid=#{sparams.schoolid} AND c.gradeid=#{sparams.gradeid} and r.year=#{sparams.year} AND u.userstate=1
        <if test="sparams.subjectid!=null and sparams.subjectid!='0'.toString() and sparams.subjectid!=''">
            AND s.subjectid=#{sparams.subjectid}
        </if>
        <if test="sparams.username!=null and sparams.username!=''">
            AND u.username like '%${sparams.username}%'
        </if>
        <if test="sparams.starttime!=null and sparams.starttime!=0">
            AND r.createtime >= #{sparams.starttime}
        </if>
        <if test="sparams.stoptime!=null and sparams.stoptime!=0">
            AND r.createtime <![CDATA[ <= ]]>#{sparams.stoptime}
        </if>
         GROUP BY r.roomid
    </select>

</mapper>
