<?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.distributed.print.business.mapper.ExamStudentMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.distributed.print.business.entity.ExamStudent">
    <result column="id" property="id" />
        <result column="school_id" property="schoolId" />
        <result column="exam_detail_course_id" property="examDetailCourseId" />
        <result column="student_name" property="studentName" />
        <result column="student_code" property="studentCode" />
        <result column="ticket_number" property="ticketNumber" />
        <result column="site_number" property="siteNumber" />
        <result column="print_paper" property="printPaper" />
        <result column="print_card" property="printCard" />
        <result column="extend_fields" property="extendFields" />
        <result column="paper_type" property="paperType" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        school_id, exam_detail_course_id, student_name, student_code, ticket_number, site_number, print_paper, print_card, extend_fields, paper_type
    </sql>
    <select id="getStudentDetail" resultType="java.util.Map">
        SELECT
            a.student_name studentName,
            a.student_code studentCode,
            a.ticket_number ticketNumber,
            a.site_number siteNumber,
            a.extend_fields extendFields,
            b.course_code courseCode,
            b.course_name courseName,
            b.paper_number paperNumber,
            c.exam_start_time examStartTime,
            c.exam_end_time examEndTime,
            c.exam_place examPlace,
            c.exam_room examRoom
        FROM
            exam_student a
                LEFT JOIN
            exam_detail_course b ON a.exam_detail_course_id = b.id
                LEFT JOIN
            exam_detail c ON b.exam_detail_id = c.id
        WHERE
            a.id = #{id}
    </select>

    <select id="queryBySchoolIdAndExamDetailCourseIds" resultType="com.qmth.distributed.print.business.bean.dto.ExamStudentCourseDto">
        select
            es.id,
            es.school_id as schoolId,
            es.exam_detail_course_id as examDetailCourseId,
            es.student_name as studentName,
            es.student_code as studentCode,
            es.ticket_number as ticketNumber,
            es.site_number as siteNumber,
            es.print_paper as printPaper,
            es.print_card as printCard,
            es.extend_fields as extendFields,
            es.paper_type as paperType,
            es.create_id as createId,
            es.create_time as createTime,
            es.update_id as updateId,
            es.update_time as updateTime,
            es.attachment_id as attachmentId,
            edc.course_code as courseCode,
            edc.course_name as courseName,
            edc.paper_pages_a3 as paperPageA3,
            edc.paper_number as paperNumber
        from
            exam_student es
        left join exam_detail_course edc on
            edc.id = es.exam_detail_course_id
            <where>
                <if test="schoolId != null and schoolId != ''">
                    and es.school_id = #{schoolId}
                </if>
                <if test="examDetailCourseIds != null">
                    AND edc.id IN
                    <foreach collection="examDetailCourseIds" item="examDetailCourseId" index="index" open="(" separator="," close=")">
                        #{examDetailCourseId}
                    </foreach>
                </if>
            </where>
            order by edc.course_code
    </select>
    <select id="listStudentByPrintPlanIdAndSyncStatus"
            resultType="com.qmth.distributed.print.business.bean.dto.SyncExamStudentDto">
        SELECT
            a.id,
            b.course_code courseCode,
            b.course_name courseName,
            b.paper_number paperNumber,
            a.student_name studentName,
            a.student_code studentCode,
            a.ticket_number ticketNumber,
            a.extend_fields extendFields,
            a.paper_type paperType
        FROM
            exam_student a
                LEFT JOIN
            exam_detail_course b ON a.exam_detail_course_id = b.id
                LEFT JOIN
            exam_detail c ON b.exam_detail_id = c.id
        WHERE
            c.print_plan_id = #{printPlanId}
                AND (a.sync_status IS NULL
                OR a.sync_status = FALSE)
    </select>

</mapper>
