<?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.qmth.jkserver.dao.ExamStudentDao">

    <select id="findById" resultType="com.qmth.jkserver.model.ExamStudent">
        select * from jk_exam_student t
        <where>
            <if test="id != null and id != ''">
                and t.id = #{id}
            </if>
        </where>
    </select>

    <delete id="deleteByExamCode">
        delete t from jk_exam_student t
        <where>
            <if test="examCode != null and examCode != ''">
                and t.exam_code = #{examCode}
            </if>
        </where>
    </delete>

    <select id="findByExamCode" resultType="com.qmth.jkserver.model.ExamStudent">
        select t.exam_code as examCode,t.ticket_no as ticketNo from jk_exam_student t
        <where>
            <if test="examCode != null and examCode != ''">
                and t.exam_code = #{examCode}
            </if>
        </where>
        group by t.ticket_no
    </select>

    <select id="findByExamCodeAndTicketNo" resultType="com.qmth.jkserver.model.ExamStudent">
        select * from jk_exam_student t
        <where>
            <if test="examCode != null and examCode != ''">
                and t.exam_code = #{examCode}
            </if>
            <if test="ticketNo != null and ticketNo != ''">
                and t.ticket_no = #{ticketNo}
            </if>
        </where>
    </select>

    <select id="findByExamCodeAndTicketNoAndCourseCode" resultType="com.qmth.jkserver.model.ExamStudent">
        select * from jk_exam_student t
        <where>
            <if test="examCode != null and examCode != ''">
                and t.exam_code = #{examCode}
            </if>
            <if test="ticketNo != null and ticketNo != ''">
                and t.ticket_no = #{ticketNo}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and t.course_code = #{courseCode}
            </if>
        </where>
    </select>

    <select id="findByExamCodeByMap" resultType="java.util.Map">
        select t.batch_id as batchId,t.batch_name as batchName from jk_exam_student t
        <where>
            <if test="examCode != null and examCode != ''">
                and t.exam_code = #{examCode}
            </if>
        </where>
        group by t.exam_code,t.batch_id,t.batch_name
        order by t.batch_id
    </select>

    <select id="findByExamCodeAndBatchIdAndPlaceCodeAndRoomCodeAndPlaceName" resultType="java.util.Map">
        select t.stage_id as stageId,t.batch_id as batchId,t.place_code as placeCode,t.room_code as roomCode,t.batch_name as batchName,count(1) as fullStudentNum
        from jk_exam_student t
        left join jk_exam_room jer on jer.exam_code = t.exam_code
        <where>
            <if test="examCode != null and examCode != ''">
                and t.exam_code = #{examCode}
            </if>
            <if test="batchId != null and batchId != ''">
                and t.batch_id = #{batchId}
            </if>
            <if test="placeCodeSet != null and placeCodeSet != '' and placeCodeSet.size > 0">
                and t.place_code in
                <foreach collection="placeCodeSet" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="roomCode != null and roomCode != ''">
                and t.room_code = #{roomCode}
            </if>
            <if test="placeName != null and placeName != ''">
                and jer.place_name like concat('%', #{placeName}, '%')
            </if>
        </where>
        group by t.stage_id,t.batch_id,t.place_code,t.room_code,t.batch_name
    </select>

    <select id="findByExamCodeAndBatchIdAndPlaceCodeAndRoomCodeAndPlaceNameByIpage" resultType="java.util.Map">
        select t.stage_id as stageId,t.batch_id as batchId,t.place_code as placeCode,t.room_code as roomCode,t.batch_name as batchName,count(1) as fullStudentNum from jk_exam_student t
        left join jk_exam_room jer on jer.exam_code = t.exam_code
        <where>
            <if test="examCode != null and examCode != ''">
                and t.exam_code = #{examCode}
            </if>
            <if test="batchId != null and batchId != ''">
                and t.batch_id = #{batchId}
            </if>
            <if test="placeCodeSet != null and placeCodeSet != '' and placeCodeSet.size > 0">
                and t.place_code in
                <foreach collection="placeCodeSet" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="roomCode != null and roomCode != ''">
                and t.room_code = #{roomCode}
            </if>
            <if test="placeName != null and placeName != ''">
                and jer.place_name like concat('%', #{placeName}, '%')
            </if>
        </where>
        group by t.stage_id,t.batch_id,t.place_code,t.room_code,t.batch_name
        order by t.place_code,t.room_code
    </select>

    <select id="findByExamCodeAndBatchIdAndPlaceCodeAndRoomCodeAndPlaceNameCount" resultType="java.lang.Long">
        select
        count(1)
        from
        jk_exam_student t
        left join jk_exam_room jer on jer.exam_code = t.exam_code
        <where>
            <if test="examCode != null and examCode != ''">
                and t.exam_code = #{examCode}
            </if>
            <if test="batchId != null and batchId != ''">
                and t.batch_id = #{batchId}
            </if>
            <if test="placeCodeSet != null and placeCodeSet != '' and placeCodeSet.size > 0">
                and t.place_code in
                <foreach collection="placeCodeSet" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="roomCode != null and roomCode != ''">
                and t.room_code = #{roomCode}
            </if>
            <if test="placeName != null and placeName != ''">
                and jer.place_name like concat('%', #{placeName}, '%')
            </if>
        </where>
    </select>

    <select id="findByIpage" resultType="com.qmth.jkserver.model.ExamStudent">
        select t.* from jk_exam_student t
        join jk_exam_code tec on tec.exam_code = t.exam_code
        <where>
            <if test="schoolId != null and schoolId != ''">
                and tec.school_id = #{schoolId}
            </if>
            <if test="examCode != null and examCode != ''">
                and t.exam_code = #{examCode}
            </if>
            <if test="examCode != null and examCode != ''">
                and t.exam_code = #{examCode}
            </if>
            <if test="batchId != null and batchId != ''">
                and t.batch_id = #{batchId}
            </if>
            <if test="stageId != null and stageId != ''">
                and t.stage_id = #{stageId}
            </if>
            <if test="placeCode != null and placeCode != ''">
                and t.place_code = #{placeCode}
            </if>
            <if test="roomCode != null and roomCode != ''">
                and t.room_code = #{roomCode}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and t.course_code = #{courseCode}
            </if>
            <if test="registrationStatus != null and registrationStatus != '' or registrationStatus == 0">
                and t.registration_status = #{registrationStatus}
            </if>
            <if test="examFinishStatus != null and examFinishStatus != ''">
                <choose>
                    <when test="examFinishStatus == FINISHED">
                        and t.exam_finish_status = #{examFinishStatus}
                    </when>
                    <when test="examFinishStatus == UN_FINISHED">
                        and (t.exam_finish_status = #{examFinishStatus} or t.exam_finish_status is null)
                    </when>
                    <otherwise>
                        and 1 = 1
                    </otherwise>
                </choose>
            </if>
            <if test="pattern != null and pattern != ''">
                and (t.name like concat('%',#{pattern} ,'%') or t.ticket_no like concat('%',#{pattern} ,'%'))
            </if>
        </where>
        order by t.place_code,t.room_code,t.course_code,t.ticket_no
    </select>
</mapper>
