<?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.EClassStudentMapper">
    <!--获取班级学生列表-->
    <select id="listByClassIds" resultType="com.xhkjedu.sexam.model.exam.EClassStudent">
        select classid,studentid from t_class_student
        where classid in
        <foreach collection="classids" item="classid" index="index" open="(" separator="," close=")">
            ${classid}
        </foreach>
    </select>
    <!--考试学生-->
    <select id="listByExamId" resultType="java.util.Map">
        select es.ecsid,es.classid,u.username,u.studentno,u.usersex
        from e_class_student es left join t_user u on es.studentid = u.userid
        left join e_class ec on es.classid=ec.classid and es.examid=ec.examid
        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.studentname!=null and cs.studentname!=''">
            and (u.username like '%${cs.studentname}%' or u.studentno like '%${cs.studentname}%')
        </if>
    </select>

    <!--根据考号搜索参加考试学生-->
    <select id="listStudentByExamidAnExamno" resultType="java.util.Map">
        select s.studentid,u.username studentname,u.examno,c.classid,c.classname,e.gradeid
        from e_class_student s left join e_class c on s.examid=c.examid and c.classid=s.classid
        LEFT JOIN t_user u on s.studentid=u.userid
        left join e_base e on c.examid=e.examid
        where s.examid=#{examid} and u.usertype=2 and u.examno like concat('%', #{examno}, '%') group by s.studentid
    </select>

    <!--获取考试参与的学生集合-->
    <select id="listStudentsByEpid" resultType="java.util.Map">
        select s.studentid,u.username studentname, u.examno from e_class_student s
        left join t_user u on s.studentid=u.userid
        left join e_paper p on s.examid=p.examid
        where p.epid=#{epid} group by s.studentid order by s.studentid
    </select>
    <!--根据班级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.examno='' or u.examno is null)
    </select>

    <!--更加考试id和学生id获取班级id-->
    <select id="getClassidByStudentidAndExamid" resultType="java.lang.Integer">
        select classid from e_class_student where examid=#{examid} and studentid=#{studentid} limit 1
    </select>

    <!--批量生成学生pdf报告-->
    <update id="updateBatchReportstupath">
        update e_class_student
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="reportstu = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when studentid=#{item.studentid} and examid=#{item.examid} then reportstu=#{item.reportstu}
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" item="item" index="index" separator="or">
            studentid=#{item.studentid} and examid=#{item.examid}
        </foreach>
    </update>
    <!--考试学生列表-->
    <select id="listStuByExamId" resultType="java.util.Map">
        select es.studentid,u.username,u.loginname,es.classid
        from e_class_student es left join t_user u on es.studentid = u.userid
        where es.examid=#{examid}
    </select>

    <!--获取考试关联学校的准备考号长度-->
    <select id="listSchoolExamNoLength" resultType="java.lang.Integer">
        select LENGTH(u.examno) 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(u.examno)
    </select>
</mapper>
