<?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.MsClassStudentMapper">
    <!--批量保存考试学生信息-->
    <insert id="batchSave">
        insert ignore into ms_class_student(examid,subjectid,classid,studentid)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.examid},#{item.subjectid},#{item.classid},#{item.studentid})
        </foreach>
    </insert>
    <!--根据班级ids获取没有考号学生数量-->
    <select id="getNoExamNoNumByClassIds" resultType="java.lang.Integer">
        select count(u.userid) from t_class_student cs left join t_user u on cs.studentid=u.userid
        where classid in
        <foreach collection="classids" item="classid" index="index" open="(" separator="," close=")">
            ${classid}
        </foreach>
        and u.userstate=1 and (u.examno is null or u.examno='')
    </select>
    <!--获取考试关联学校的准备考号长度-->
    <select id="listSchoolExamNoLength" resultType="java.lang.Integer">
        select length(u.examno) as length from t_user u
        left join t_class_student cs on u.userid=cs.studentid
        where u.usertype=2 and u.userstate=1 and cs.classid in
        <foreach collection="classids" item="classid" index="index" separator="," open="(" close=")">
            #{classid}
        </foreach>
        group by length
    </select>
    <!--获取班级学生列表-->
    <select id="listByClassIds" resultType="com.xhkjedu.smarking.model.exam.MsClassStudent">
        select classid,studentid
        from t_class_student cs left join t_user u on cs.studentid=u.userid
        where classid in
        <foreach collection="classids" item="classid" index="index" open="(" separator="," close=")">
            ${classid}
        </foreach>
        and u.usertype=2 and u.userstate=1
    </select>
    <!--根据考号搜索参加考试学生-->
    <select id="listStudentByExamIdAnExamno" resultType="java.util.Map">
        select s.studentid,u.username studentname,u.examno,c.classid,c.classname,e.gradeid
        from ms_class_student s left join ms_class c on s.examid=c.examid and c.subjectid=s.subjectid and s.classid=c.classid
        left join t_user u on s.studentid=u.userid
        left join ms_exam e on c.examid=e.examid
        where s.examid=#{examid} and s.subjectid=#{subjectid}
        and u.usertype=2 and u.examno like concat('%', #{examno}, '%')
        group by s.studentid
    </select>
    <!--考生信息列表-->
    <select id="listAll" resultType="java.util.Map">
        select es.mcsid,es.examid,es.classid,es.subjectid,s.subjectname,u.username,u.examno,u.usersex,ec.classname,ps.mpsid,ps.sstate
        from ms_class_student es left join t_user u on es.studentid = u.userid
        left join ms_subject s on es.examid=s.examid and es.subjectid=s.subjectid
        left join ms_class ec on es.classid=ec.classid and es.examid=ec.examid and es.subjectid=ec.subjectid
        left join ms_paper_student ps on es.examid=ps.examid and es.subjectid=ps.subjectid and es.studentid=ps.studentid
        where es.examid=#{cs.examid} and ec.schoolid=#{cs.schoolid}
        <if test="cs.classid!=null and cs.classid!=0">
            and es.classid=#{cs.classid}
        </if>
        <if test="cs.subjectid!=null and cs.subjectid!=''">
            and es.subjectid=#{cs.subjectid}
        </if>
        <if test="cs.studentname!=null and cs.studentname!=''">
            and (u.username like '%${cs.studentname}%' or u.examno like '%${cs.studentname}%')
        </if>
        group by es.studentid,es.subjectid
        order by es.mcsid desc
    </select>
    <!--根据学生ids获取没有考号学生数量-->
    <select id="getNoExamNoNumByStudentIds" resultType="java.lang.Integer">
        select count(u.userid) from t_user u
        where u.userid in
        <foreach collection="studentIds" item="studentid" index="index" open="(" separator="," close=")">
            ${studentid}
        </foreach>
        and (u.examno is null or u.examno='')
    </select>
    <!--根据科目和学生id获取班级id-->
    <select id="listBySubjectIdAndStuId" resultType="java.lang.Integer">
        select classid from ms_class_student where examid=#{cs.examid} and subjectid=#{cs.subjectid} and studentid=#{cs.studentid}
    </select>
    <!--考试班级列表-->
    <select id="listClass" resultType="java.util.Map">
        select c.classid,c.classname,count(distinct cs.studentid) as classnum
        from ms_class c left join ms_class_student cs on c.examid=cs.examid and c.classid=cs.classid
        where c.examid=#{cs.examid}
        group by c.classid
        order by c.mcid
    </select>
    <!--班级搜索列表-->
    <select id="searchClass" resultType="java.util.Map">
        select c.classid,c.classname,count(distinct cs.studentid) as classnum
        ,count(distinct if((u.examno is null or u.examno=''),u.userid,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
        where c.schoolid=#{cs.schoolid} and c.classtype=#{cs.classtype} and c.classstate=1
        and c.gradeid=#{cs.gradeid} and c.year=#{cs.year}
        and u.userstate=1 and u.usertype=2
        group by c.classid
        having classnum>0
    </select>
    <!--学生搜索列表-->
    <select id="searchStudent" resultType="java.util.Map">
        select cs.classid,c.classname,cs.studentid,u.examno,u.username
        from t_class c right join t_class_student cs on c.classid=cs.classid
        left join t_user u on cs.studentid=u.userid
        where c.schoolid=#{cs.schoolid} and c.classtype=#{cs.classtype} and c.classstate=1
        and c.gradeid=#{cs.gradeid} and c.year=#{cs.year}
        and u.userstate=1 and u.usertype=2
        group by cs.classid,cs.studentid
    </select>
    <!--获取考试关联学生-->
    <select id="listStudentByExamId" resultType="com.xhkjedu.smarking.model.exam.MsClassStudent">
        select examid,subjectid,classid,studentid
        from ms_class_student where examid=#{examid}
        <if test="subjectid!=null and subjectid!=''"> and subjectid=#{subjectid} </if>
    </select>
    <!--移除单个科目学生-->
    <delete id="deleteByStu">
        delete ps.*,cs.*
        from ms_class_student cs left join ms_paper_student ps on cs.examid=ps.examid and cs.subjectid=ps.subjectid and cs.studentid=ps.studentid
        where cs.examid=#{cs.examid} and cs.studentid=#{cs.studentid} and cs.subjectid=#{cs.subjectid}
    </delete>
    <!--移除班级学生-->
    <delete id="deleteByClass">
        delete ps.*,cs.*,c.*
        from ms_class c left join ms_class_student cs on c.examid=cs.examid and c.classid=cs.classid
                        left join ms_paper_student ps on cs.examid=ps.examid and cs.studentid=ps.studentid
        where c.examid=#{cs.examid} and c.classid=#{cs.classid}
    </delete>


</mapper>
