<?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.sexam.mapper.exam.EClassMapper">
    <!--获取班级基本信息列表-->
    <select id="listByClassIds" resultType="com.xhkjedu.sexam.model.exam.EClass">
        select classid,classname,classnum,schoolid from t_class
        where classid in
        <foreach collection="classids" item="classid" index="index" open="(" separator="," close=")">
            ${classid}
        </foreach>
        and classnum!=0
    </select>
    <!--考试班级-->
    <select id="listByExamId" resultType="java.util.Map">
        select ec.classid,ec.classname,ec.classnum
        from e_class ec left join t_class c on ec.classid = c.classid
        where examid=#{examid}
        order by c.classtype,c.classorder,ec.ecid
    </select>
    <!--更新考试班级人数-->
    <update id="updateClassNumByCsId">
        update e_class c set c.classnum=(select count(*) from e_class_student s where s.examid=c.examid and s.classid=c.classid)
        where c.examid=#{examid} and c.classid=#{classid}
    </update>

    <!--获取班级教师-->
    <select id="listTeacherByClassIds" resultType="com.xhkjedu.sexam.vo.exam.CTeacherVo">
        select ct.classid,ct.teacherid from t_class_teacher ct where ct.classid in(${classids})
    </select>
    <!--获取教师科目-->
    <select id="listSubjectByTeacherIds" resultType="com.xhkjedu.sexam.vo.exam.CTeacherVo">
        select tp.teacherid,sb.subjectid
        from t_teacher_powersb tp left join t_subject_book sb on tp.lsbid = sb.lsbid
        where tp.teacherid in(${teacherids})
        group by tp.teacherid
    </select>
    <!--获取科目列表-->
    <select id="listSubjectBySubjectIds" resultType="com.xhkjedu.sexam.vo.exam.CTeacherVo">
        select subjectid,subjectname from t_subject where subjectid in('${subjectids}')
    </select>
    <!--获取学校所有教师-->
    <select id="listTeacherBySchoolId" resultType="com.xhkjedu.sexam.vo.exam.CTeacherVo">
        select userid teacherid,username,loginname,usersex from t_user where schoolid=#{schoolid} and usertype=1 and userstate=1
    </select>
    <!--学校教师查找-->
    <select id="listSearchTeacher" resultType="java.util.Map">
        select u.userid teacherid,u.username,u.loginname,u.usersex
        from t_user u left join e_class s on u.schoolid=s.schoolid
        where s.examid=#{examid} and u.usertype=1 and u.userstate=1 and u.username in
        <foreach collection="usernames" item="username" open="(" separator="," close=")">
            #{username}
        </foreach>
        group by u.userid
    </select>

    <!--按班设置教师列表-->
    <select id="listClassTeacher" resultType="java.util.Map">
        select ct.classid,ct.teacherid,u.username,u.loginname,u.usersex
        from t_class_teacher ct left join t_user u on ct.teacherid = u.userid
        left join t_teacher_powersb tp on tp.teacherid=ct.teacherid
        left join t_subject_book sb on tp.lsbid = sb.lsbid
        where ct.classid in(${classids}) and sb.subjectid=#{subjectid} and u.userstate=1
        group by ct.classid,ct.teacherid
    </select>

    <!--参加考试的班级和学生-->
    <resultMap id="listExamClassStus" type="java.util.Map">
        <result property="classid" column="classid" />
        <result property="classname" column="classname" />
        <result property="classnum" column="classnum" />
       <!-- <result property="reportclass" column="reportclass" />
        <result property="reportstu" column="reportstu" />-->
        <collection property="stus" ofType="java.util.Map" javaType="java.util.List">
            <result property="studentid" column="studentid" />
            <result property="studentname" column="studentname" />
            <result property="sstate" column="sstate" />
           <!-- <result property="reportstu" column="reportstu2" />-->
        </collection>
    </resultMap>
    <select id="listExamClassAndStu" resultMap="listExamClassStus">
        select c.classid,c.classname,c.classnum,ps.studentid,u.username studentname,ps.sstate,c.reportclass,c.reportstu
        ,(select cs.reportstu from e_class_student cs where cs.examid=c.examid and cs.studentid=ps.studentid)reportstu2
        from e_class c left join e_paper_student ps on c.examid=ps.examid and c.classid=ps.classid
        left join t_user u on ps.studentid=u.userid
        left join t_class tc on c.classid = tc.classid
        where c.examid=#{examid}
        group by ps.studentid order by tc.classtype,tc.classorder,c.ecid, ps.studentid
    </select>
    <!--考试关联教师权限下班级学生-->
    <select id="listExamClassAndStuForTeacher" resultMap="listExamClassStus">
        select c.classid,c.classname,c.classnum,ps.studentid,u.username studentname,ps.sstate
        /*,c.reportclass,c.reportstu
        ,(select cs.reportstu from e_class_student cs where cs.examid=c.examid and cs.studentid=ps.studentid)reportstu2*/
        from e_class c left join e_paper_student ps on c.examid=ps.examid and c.classid=ps.classid
        left join t_user u on ps.studentid=u.userid
        left join t_class_teacher ct on c.classid=ct.classid
        left join t_class tc on c.classid = tc.classid
        where c.examid=#{examid} and ct.teacherid=#{teacherid} and c.schoolid=#{schoolid}
        group by ps.studentid order by tc.classtype,tc.classorder,c.ecid, ps.studentid
    </select>
    <!--班级选择列表-->
    <select id="listBj" resultType="java.util.Map">
        select c.gradeid,c.classid,c.classname,c.classtype
        ,(select count(u.userid) from t_class_student cs left join t_user u on cs.studentid = u.userid
        where cs.classid=c.classid and (u.examno='' or u.examno is null))num
        <if test="teacherid==null">
            from t_class c left join t_school ts on c.schoolid = ts.schoolid
        </if>
        <if test="teacherid!=null">
            from t_class_teacher ct left join t_class c on c.classid = ct.classid
            left join t_school ts on c.schoolid = ts.schoolid
        </if>
        where c.schoolid=#{schoolid} and c.year=ts.year and c.classstate=1
        and c.classtype in(1,2)
        <if test="teacherid!=null">
            and ct.teacherid=#{teacherid}
        </if>
        and c.classnum>0
        order by c.gradeid,c.classtype,c.classorder,c.classid desc
    </select>

    <!--批量更新班级pdf报告地址-->
    <update id="updateBatchReportpath">
        update e_class
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="reportclass = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when classid=#{item.classid} and examid=#{item.examid} then reportclass=#{item.reportclass}
                </foreach>
            </trim>
            <trim prefix="reportstu = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when classid=#{item.classid} and examid=#{item.examid} then reportstu=#{item.reportstu}
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" item="item" index="index" separator="or">
            classid=#{item.classid} and examid=#{item.examid}
        </foreach>
    </update>

    <!--获取考试管理的学校信息-->
    <select id="listSchoolsByExamid" resultType="java.util.Map">
        select c.schoolid,s.schoolname from e_class c
        left join t_school s on c.schoolid=s.schoolid
        where c.examid=#{examid} group by s.schoolid order by CASE WHEN c.schoolid=#{schoolid} THEN 0 ELSE 1 END,c.schoolid
    </select>

    <resultMap id="schoolClass" type="java.util.Map">
        <result property="schoolid" column="schoolid" />
        <result property="schoolname" column="schoolname" />
        <collection property="classes" ofType="java.util.Map" javaType="java.util.List" >
            <result property="classid" column="classid" />
            <result property="classname" column="classname" />
            <result property="classnum" column="classnum" />
        </collection>
    </resultMap>
    <!--考试关联学校班级-->
    <select id="listSchoolClassByExamid" resultMap="schoolClass">
        select c.schoolid,s.schoolname,ts.classid,ts.classname,c.classnum from e_class c
        left join t_school s on c.schoolid=s.schoolid
        left join t_class ts on c.classid=ts.classid
        where c.examid=#{examid} order by CASE WHEN c.schoolid=#{schoolid} THEN 0 ELSE 1 END,c.schoolid,ts.classorder
    </select>

    <!--获取考试选择班级关联学校id-->
    <select id="listSchoolidByClassids" resultType="java.lang.Integer">
        select schoolid from t_class where classid in (${classids}) group by schoolid
    </select>
</mapper>
