<?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.smarking.mapper.exam.MsClassMapper">
    <insert id="batchSave">
        insert ignore into ms_class(examid,subjectid,classid,classname,schoolid,classtype)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.examid},#{item.subjectid},#{item.classid},#{item.classname},#{item.schoolid},#{item.classtype})
        </foreach>
    </insert>
    <!--获取班级基本信息列表-->
    <select id="listByClassIds" resultType="com.xhkjedu.smarking.model.exam.MsClass">
        select classid,classname,schoolid,classtype
        from t_class
        where classid in
        <foreach collection="classids" item="classid" index="index" open="(" separator="," close=")">
            ${classid}
        </foreach>
        and classnum!=0
        order by classtype,classorder,classid
    </select>
    <!--考试班级-->
    <select id="listByExamId" resultType="java.util.Map">
        select mc.classid,mc.classname
        from ms_class mc left join t_class c on mc.classid = c.classid
        where examid=#{examid}
        <if test="subjectid!=null and subjectid!=''">
            and mc.subjectid=#{subjectid}
        </if>
        group by mc.classid
        order by c.classtype,c.classorder,mc.mcid
    </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
        ,(select count(*) from ms_class_student cs where cs.examid=c.examid and cs.classid=c.classid) as classnum
        from ms_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 if(c.schoolid =#{schoolid}, 0, 1),c.schoolid,ts.classorder
    </select>
    <!--获取学校所有教师-->
    <select id="listTeacherBySchoolId" resultType="com.xhkjedu.smarking.vo.exam.CTeacherVo">
        select userid teacherid,username,loginname,usersex from t_user where schoolid=#{schoolid} and usertype=1 and userstate=1
    </select>
    <!--获取教师科目-->
    <select id="listSubjectByTeacherIds" resultType="com.xhkjedu.smarking.vo.exam.CTeacherVo">
        select tp.teacherid,tp.subjectid
        from t_teacher_powersb tp
        where tp.teacherid in(${teacherids})
        group by tp.teacherid
    </select>
    <!--获取科目列表-->
    <select id="listSubjectBySubjectIds" resultType="com.xhkjedu.smarking.vo.exam.CTeacherVo">
        select subjectid,subjectname from t_subject where subjectid in('${subjectids}')
    </select>
    <!--学校教师查找-->
    <select id="listSearchTeacher" resultType="java.util.Map">
        select u.userid teacherid,u.username,u.loginname,u.usersex
        from t_user u left join ms_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
        where ct.classid in(${classids}) and tp.subjectid=#{subjectid} and u.userstate=1
        group by ct.classid,ct.teacherid
    </select>
    <!--班级选择列表（创建考试用）-->
    <select id="listSchoolClass" resultType="java.util.Map">
        select c.classid,c.classname,c.gradeid,c.classnum,c.classtype,count(if(u.examno is null or u.examno='',1,null)) as noexamnum
        from t_class c left join t_class_student cs on c.classid=cs.classid
        left join t_user u on cs.studentid=u.userid
        <if test="teacherid!=null and teacherid!=0">
            inner join t_class_teacher ct on c.classid=ct.classid and ct.teacherid=#{teacherid}
        </if>
        where c.schoolid=#{schoolid} and c.year=#{year} and c.classtype in(1,2) and c.classstate=1 and c.classnum!=0
        group by c.classid
        order by c.classtype,c.classorder,c.classid
    </select>
    <!--考试班级名称和人数列表-->
    <select id="listNameAndNumByExamId" resultType="java.util.Map">
        select c.classid,c.classname,count(distinct cs.studentid) as num
        from ms_class c inner join ms_class_student cs on c.examid=cs.examid and c.classid=cs.classid
        where c.examid=#{examid}
        group by c.classid order by c.mcid
    </select>
    <!--获取考试关联学校-->
    <select id="listSchoolByExamId" resultType="java.util.Map">
        select c.schoolid, s.schoolname
        from ms_class c inner join t_school s on c.schoolid = s.schoolid
        where c.examid = #{examid}
        group by s.schoolid
        order by if(c.schoolid =#{schoolid}, 0, 1), c.schoolid
    </select>
    <!--获取考试关联班级-->
    <select id="listClassByExamId" resultType="com.xhkjedu.smarking.model.exam.MsClass">
        select examid,subjectid,classid
        from ms_class where examid=#{examid}
        order by mcid
    </select>

    <!--获取考试科目关联班级-->
    <select id="listClassByExamidAndSubjectid" resultType="com.xhkjedu.smarking.model.exam.MsClass">
        select classid,classname,classtype from ms_class where examid=#{examid} and subjectid=#{subjectid} order by mcid
    </select>
    <!--获取考试关联班级数量-->
    <select id="getClassNumByExamId" resultType="java.lang.Integer">
        select count(distinct classid) from ms_class where examid=#{examid}
    </select>
</mapper>
