<?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="required_fields" property="requiredFields"/>
        <result column="extend_fields" property="extendFields"/>
        <result column="paper_type" property="paperType"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        select id,school_id, exam_detail_course_id, student_name, student_code, ticket_number, site_number, print_paper, print_card, required_fields, extend_fields, paper_type from exam_student
    </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,
               bs.id             semesterId,
               bs.name           semesterName,
               be.id             examId,
               be.name           examName
        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
                 LEFT JOIN
             exam_print_plan epp on epp.id = c.print_plan_id
                 LEFT JOIN
             basic_exam be on be.id = epp.exam_id
                 LEFT JOIN
             basic_semester bs on be.semester_id = bs.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,
        es.clazz_id clazzId,
        es.clazz_name clazzName,
        es.teach_clazz_name teachClazzName,
        es.college_name collegeName,
        es.major_name majorName,
        es.teacher_id teacherId,
        su.real_name teacherName
        from
        exam_student es
        left join exam_detail_course edc on
        edc.id = es.exam_detail_course_id
            left join sys_user su on es.teacher_id = su.id
        <where>
            <if test="schoolId != null and schoolId != ''">
                and es.school_id = #{schoolId}
            </if>
            <if test="examDetailCourseIds != null and examDetailCourseIds != '' and examDetailCourseIds.size > 0">
                AND es.exam_detail_course_id IN
                <foreach collection="examDetailCourseIds" item="examDetailCourseId" index="index" open="(" separator=","
                         close=")">
                    #{examDetailCourseId}
                </foreach>
            </if>
        </where>
        order by 0 + cast(es.site_number as char)
    </select>
    <select id="listExamStudentBySchoolIdAndClazzId"
            resultType="com.qmth.distributed.print.business.entity.ExamStudent">
        SELECT
        *
        FROM
        (SELECT
        bs.school_id AS schoolId,
        bs.student_code AS studentCode,
        bs.student_name AS studentName,
        bs.clazz_id AS clazzId,
        bc.clazz_name AS clazzName,
        FROM
        basic_student bs
        INNER JOIN basic_clazz bc ON bs.clazz_id = bc.id
        <where>
            <if test="schoolId != null and schoolId != ''">
                AND bs.school_id = #{schoolId}
            </if>
            <if test="classId != null and classId != ''">
                AND bc.id = #{classId}
            </if>
        </where>
        UNION
        SELECT
        ts.school_id AS schoolId,
        bs.student_code AS studentCode,
        bs.student_name AS studentName,
        ts.teach_clazz_id AS clazzId,
        tc.teach_clazz_name AS clazzName,
        FROM
        teach_student ts
        join basic_student bs on ts.student_id = bs.id
        INNER JOIN teach_clazz tc ON ts.teach_clazz_id = tc.id
        <where>
            <if test="schoolId != null and schoolId != ''">
                AND ts.school_id = #{schoolId}
            </if>
            <if test="classId != null and classId != ''">
                AND tc.id = #{classId}
            </if>
        </where>
        ) t0;
    </select>
    <select id="getByStudentId"
            resultType="com.qmth.distributed.print.business.bean.dto.ExamStudentPdfInfoDto">
        SELECT a.student_code  studentCode,
               a.student_name  studentName,
               a.ticket_number ticketNumber,
               a.site_number   siteNumber,
               a.extend_fields extendFields,
               a.paper_type    paperType,
               a.college_name  collegeName,
               a.major_name    majorName,
               a.clazz_id      clazzId,
               a.clazz_name    clazzName,
               a.teach_clazz_name teachClazzName,
               a.campus_name   campusName,
               a.attachment_id attachmentId,
               b.course_code   courseCode,
               b.course_name   courseName,
               b.paper_number  paperNumber,
               c.exam_place    examPlace,
               c.exam_room     examRoom,
               su.real_name teacherName
        FROM exam_student a
                 JOIN
             exam_detail_course b ON a.exam_detail_course_id = b.id
                 JOIN
             exam_detail c ON b.exam_detail_id = c.id
                left join
             sys_user su on a.teacher_id = su.id
        WHERE a.id = #{id}
    </select>
    <select id="listExamStudentByPaperNumberAndPaperType"
            resultType="com.qmth.distributed.print.business.bean.dto.ExamStudentCourseClassDto">
        SELECT
            a.course_code courseCode,
            a.course_name courseName,
            a.paper_number paperNumber,
            a.paper_type paperType,
            a.clazz_name clazzName,
            b.teacher_name teacherName,
            b.teacher_number teacherNumber
        FROM
            (SELECT DISTINCT
                 ms.course_code,
                 ms.course_name,
                 ms.paper_number,
                 ms.paper_type,
                 ms.class_name as clazz_name
             FROM
                 mark_student ms
             WHERE
                 ms.exam_id = #{examId}
               AND ms.paper_number = #{paperNumber}
               AND ms.paper_type = #{paperType}) a
                LEFT JOIN
            (SELECT
                 *
             FROM
                 grade_batch_student_clazz
             WHERE
                 batch_id = #{batchId}) b ON a.course_code = b.course_code
                AND a.paper_number = b.paper_number
                AND a.paper_type = b.paper_type
                AND a.clazz_name = b.clazz_name
    </select>
    <select id="listUserClass" resultType="java.lang.String">
        SELECT DISTINCT
            clazz_name
        FROM
            exam_student
        WHERE
            school_id = #{schoolId} AND exam_id = #{examId}
          AND paper_number = #{paperNumber}
          AND clazz_name IS NOT NULL
    </select>
    <select id="listByExamDetailCourseIdLimit1" resultMap="BaseResultMap">
        <include refid="Base_Column_List"></include>
        <where>
             and school_id = #{schoolId}
            and exam_detail_course_id = #{examDetailCourseId}
        </where>
        limit 1
    </select>

    <select id="listByExamDetailCourseIdNotIntMarkStudent" resultType="com.qmth.distributed.print.business.entity.ExamStudent">
        SELECT
            es.*,
            su.real_name teacherName
        FROM
            exam_student es left join sys_user su on es.teacher_id = su.id
        WHERE
            es.exam_detail_course_id = #{examDetailCourseId}
          AND es.paper_type = #{paperType}
          AND NOT EXISTS( SELECT
                              1
                          FROM
                              mark_student ms
                          WHERE
                              es.id = ms.id)
    </select>

    <select id="queryExamStudent" resultType="com.qmth.distributed.print.business.bean.dto.ExamStudentDto">
        SELECT
            GROUP_CONCAT(DISTINCT es.teach_clazz_name) as clazzNames,
            GROUP_CONCAT(DISTINCT su.real_name) as teacherNames
        FROM
            exam_student es
            left join sys_user su on su.id = es.teacher_id
            left join exam_detail_course edc on edc.id = exam_detail_course_id
        <where>
            <if test="examId != null and examId != ''">
                AND es.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                AND edc.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                AND es.paper_number = #{paperNumber}
            </if>
        </where>
    </select>
</mapper>
