<?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.BasicExamStudentMapper">

    <select id="findBasicExamStudentPage"
            resultType="com.qmth.distributed.print.business.bean.result.BasicExamStudentResult">
        SELECT
            bes.id,
            bes.semester_id AS semesterId,
            bes.exam_id AS examId,
            bc.name AS courseName,
            bes.course_code AS courseCode,
            bes.student_name AS studentName,
            bes.student_code AS studentCode,
            bes.college,
            bes.major,
            tbc.clazz_name AS teachClazz,
            bes.paper_number AS paperNumber,
            teacher.real_name AS teacherName,
            teacher.code AS teacherCode,
            bes.exam_start_time AS examStartTime,
            bes.exam_end_time AS examEndTime,
            bes.exam_place AS examPlace,
            bes.exam_room AS examRoom
        FROM
            basic_exam_student bes
                LEFT JOIN
            basic_course bc ON bes.course_code = bc.code AND bes.school_id = bc.school_id
                LEFT JOIN
            basic_teach_clazz tbc ON bes.clazz_id = tbc.id
                LEFT JOIN
            sys_user teacher ON bes.teacher_id = teacher.id
                LEFT JOIN
            sys_user su ON bes.create_id = su.id
        <where>
            <if test="schoolId != null and schoolId != ''">
                AND bes.school_id = #{schoolId}
            </if>
            <if test="semesterId != null">
                AND bes.semester_id = #{semesterId}
            </if>
            <if test="examId != null">
                AND bes.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                AND bes.course_code LIKE CONCAT('%',#{courseCode},'%')
            </if>
            <if test="teacher != null and teacher != ''">
                AND (teacher.real_name LIKE CONCAT('%',#{teacher},'%') OR teacher.code LIKE CONCAT('%',#{teacher},'%'))
            </if>
            <if test="college != null and college != ''">
                AND bes.college LIKE CONCAT('%',#{college},'%')
            </if>
            <if test="major != null and major != ''">
                AND bes.major LIKE CONCAT('%',#{major},'%')
            </if>
            <if test="teachClazz != null and teachClazz != ''">
                AND tbc.clazz_name LIKE CONCAT('%',#{teachClazz},'%')
            </if>
            <if test="examStudentInfo != null and examStudentInfo != ''">
                AND (bes.student_code LIKE CONCAT('%',#{examStudentInfo},'%') OR bes.student_name LIKE CONCAT('%',#{examStudentInfo},'%'))
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND bes.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND su.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>
    <select id="findBasicExamStudentList"
            resultType="com.qmth.distributed.print.business.bean.result.BasicExamStudentResult">
        SELECT
            bes.id,
            bc.name AS courseName,
            bes.course_code AS courseCode,
            bes.student_name AS studentName,
            bes.student_code AS studentCode,
            bes.college,
            bes.major,
            tbc.clazz_name AS teachClazz,
            bes.paper_number AS paperNumber,
            teacher.real_name AS teacherName,
            teacher.code AS teacherCode,
            bes.exam_start_time AS examStartTime,
            bes.exam_end_time AS examEndTime,
            bes.exam_place AS examPlace,
            bes.exam_room AS examRoom
        FROM
            basic_exam_student bes
                LEFT JOIN
            basic_course bc ON bes.course_code = bc.code AND bes.school_id = bc.school_id
                LEFT JOIN
            basic_teach_clazz tbc ON bes.clazz_id = tbc.id
                LEFT JOIN
            sys_user teacher ON bes.teacher_id = teacher.id
                LEFT JOIN
            sys_user su ON bes.create_id = su.id
        <where>
            <if test="schoolId != null and schoolId != ''">
                AND bes.school_id = #{schoolId}
            </if>
            <if test="semesterId != null">
                AND bes.semester_id = #{semesterId}
            </if>
            <if test="examId != null">
                AND bes.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                AND bes.course_code LIKE CONCAT('%',#{courseCode},'%')
            </if>
            <if test="teacher != null and teacher != ''">
                AND (teacher.real_name LIKE CONCAT('%',#{teacher},'%') OR teacher.code LIKE CONCAT('%',#{teacher},'%'))
            </if>
            <if test="college != null and college != ''">
                AND bes.college LIKE CONCAT('%',#{college},'%')
            </if>
            <if test="major != null and major != ''">
                AND bes.major LIKE CONCAT('%',#{major},'%')
            </if>
            <if test="teachClazz != null and teachClazz != ''">
                AND tbc.clazz_name LIKE CONCAT('%',#{teachClazz},'%')
            </if>
            <if test="examStudentInfo != null and examStudentInfo != ''">
                AND (bes.student_code LIKE CONCAT('%',#{examStudentInfo},'%') OR bes.student_name LIKE CONCAT('%',#{examStudentInfo},'%'))
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND bes.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND su.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>
    <select id="findBoundBasicExamStudentNames" resultType="java.lang.String">
        SELECT
            bes.student_name AS studentName
        FROM
            basic_exam_student bes
                INNER JOIN
            teach_student ts ON bes.id = ts.student_id
        <where>
            <if test="idList != null and idList != '' and idList.size > 0">
                AND bes.id IN
                <foreach collection="idList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
