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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.distributed.print.business.entity.ExamDetail">
    <result column="id" property="id" />
        <result column="school_id" property="schoolId" />
        <result column="print_plan_id" property="printPlanId" />
        <result column="print_plan_name" property="printPlanName" />
        <result column="exam_start_time" property="examStartTime" />
        <result column="exam_end_time" property="examEndTime" />
        <result column="exam_place" property="examPlace" />
        <result column="total_subjects" property="totalSubjects" />
        <result column="exam_room" property="examRoom" />
        <result column="print_progress" property="printProgress" />
        <result column="print_user" property="printUser" />
        <result column="package_code" property="packageCode" />
        <result column="create_id" property="createId" />
        <result column="create_time" property="createTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        school_id, print_plan_id, print_plan_name, exam_name, exam_start_time, exam_end_time, exam_place, total_subjects, exam_room, print_progress, print_user, package_code, create_id, create_time
    </sql>
    <select id="listPrintTask" resultType="com.qmth.distributed.print.business.bean.dto.PrintTaskDto">
        SELECT
            ed.id examDetailId,
            ed.package_code packageCode,
            epp.id printPlanId,
            epp.name printPlanName,
            ed.exam_start_time examStartTime,
            ed.exam_end_time examEndTime,
            c.courseNameCode,
            c.paperNumber,
            ed.exam_place examPlace,
            ed.exam_room examRoom,
            ed.total_subjects totalSubjects,
            ed.status,
            ed.validate,
            ed.print_start_time printStartTime,
            ed.print_end_time printEndTime,
            ed.normal,
            so.name AS printHouseName,
            ed.exam_id AS examId
        FROM
            (SELECT
                 a.*
             FROM
                 exam_detail a
                     LEFT JOIN exam_print_plan epp ON a.print_plan_id = epp.id
                     LEFT JOIN basic_exam b ON epp.exam_id = b.id
                <where>
                    <if test="schoolId != null">
                        and a.school_id = #{schoolId}
                    </if>
                    <if test="semesterId != null">
                        and b.semester_id = #{semesterId}
                    </if>
                    <if test="examId != null">
                        and epp.exam_id = #{examId}
                    </if>
                    <if test="printPlanId != null">
                        and a.print_plan_id = #{printPlanId}
                    </if>
                    <if test="status != null and status != ''">
                        and a.status = #{status}
                    </if>
                    <if test="examPlace != null and examPlace != ''">
                        and a.exam_place = #{examPlace}
                    </if>
                    <if test="examRoom != null and examRoom != ''">
                        and a.exam_room = #{examRoom}
                    </if>
                    <if test="examStartTime != null and examStartTime != ''">
                        and a.exam_start_time &gt; #{examStartTime}
                    </if>
                    <if test="examEndTime != null and examEndTime != ''">
                        and a.exam_end_time &lt; #{examEndTime}
                    </if>
                    <if test="printStartTime != null and printStartTime != ''">
                        and a.print_start_time &gt; #{printStartTime}
                    </if>
                    <if test="printEndTime != null and printEndTime != ''">
                        and a.print_start_time &lt; #{printEndTime}
                    </if>
                    <if test="printHouseId != null and printHouseId != ''">
                        and a.print_house_id = #{printHouseId}
                    </if>
                </where>
             ) ed
                JOIN
            (SELECT
                 edc.exam_detail_id,
                 GROUP_CONCAT(CONCAT(edc.course_name, '（', edc.course_code, '）')) courseNameCode,
                 GROUP_CONCAT(edc.paper_number) paperNumber
             FROM
                 exam_detail_course edc
             WHERE
                 EXISTS( SELECT
                             1
                         FROM
                             exam_detail ed
                                LEFT JOIN exam_print_plan epp ON ed.print_plan_id = epp.id
                         WHERE
                             edc.exam_detail_id = ed.id
                           AND epp.exam_id = #{examId})
             GROUP BY exam_detail_id) c ON ed.id = c.exam_detail_id
                LEFT JOIN
            exam_print_plan epp ON ed.print_plan_id = epp.id
                LEFT JOIN
            sys_org so ON ed.print_house_id = so.id
        WHERE
            EXISTS( SELECT
                        1
                    FROM
                        (SELECT
                             *
                         FROM
                             exam_detail_course edc
                         WHERE
                             EXISTS( SELECT
                                         1
                                     FROM
                                         exam_detail ed
                                            LEFT JOIN exam_print_plan epp ON ed.print_plan_id = epp.id
                                     WHERE
                                         edc.exam_detail_id = ed.id
                                       AND epp.exam_id = #{examId})) edc
                           LEFT JOIN
                        (SELECT
                             *
                         FROM
                             exam_task
                         WHERE
                             exam_id = #{examId}) et ON edc.course_code = et.course_code
                            AND edc.paper_number = et.paper_number
                            LEFT JOIN
                                sys_user su ON et.user_id = su.id
                    WHERE
                        ed.id = edc.exam_detail_id
                        <if test="courseCode != null and courseCode != ''">
                            and edc.course_code = #{courseCode}
                        </if>
                        <if test="paperNumber != null and paperNumber != ''">
                            and edc.paper_number = #{paperNumber}
                        </if>
                        <if test="dpr != null">
                            <if test="dpr.requestUserId != null">
                                AND et.user_id = #{dpr.requestUserId}
                            </if>
                            <if test="dpr.courseUserId != null">
                                AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = et.course_code)
                            </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>
                        )
        ORDER BY ed.id DESC
    </select>

    <select id="findBriefPage" resultType="com.qmth.distributed.print.business.bean.result.ExaminationResult">
        SELECT
        det.id AS id,
        MAX(bs.id) AS semesterId,
        MAX(bs.name) AS semesterName,
        MAX(be.id) AS examId,
        MAX(be.name) AS examName,
        MAX(det.school_id) AS schoolId,
        MAX(det.print_plan_id) AS printPlanId,
        MAX(det.print_plan_name) AS printPlanName,
        MAX(det.exam_start_time) AS examStartTime,
        MAX(det.exam_end_time) AS examEndTime,
        MAX(det.exam_place) AS examPlace,
        MAX(det.total_subjects) AS totalSubjects,
        MAX(det.exam_room) AS examRoom,
        MAX(det.package_code) AS packageCode,
        MAX(det.create_id) AS createId,
        MAX(det.create_time) AS createTime,
        GROUP_CONCAT(cou.id) AS examDetailCourseIds,
        GROUP_CONCAT(CONCAT(cou.course_name, '（', cou.course_code, '）')) AS courseNameCode,
        GROUP_CONCAT(cou.course_code) AS courseCodes,
        GROUP_CONCAT(cou.paper_number) AS paperNumber
        FROM
        exam_detail det
        INNER JOIN
        exam_detail_course cou ON det.id = cou.exam_detail_id
        LEFT JOIN
        basic_exam be on be.id = det.exam_id
        LEFT JOIN
        basic_semester bs on bs.id = be.semester_id
        <where>
            and be.enable = true
            <if test="schoolId != null and schoolId > 0">
                AND det.school_id = #{schoolId}
            </if>
            <if test="printPlanIdList != null and printPlanIdList != '' and printPlanIdList.size() > 0">
                AND det.print_plan_id IN
                <foreach collection="printPlanIdList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="examPlace != null and examPlace != ''">
                AND det.exam_place = #{examPlace}
            </if>
            <if test="examRoom != null and examRoom != ''">
                AND det.exam_room = #{examRoom}
            </if>
            <if test="packageCode != null and packageCode != ''">
                AND det.package_code = #{packageCode}
            </if>
            <if test="startDate != null and startDate > 0">
                AND det.exam_start_time >= #{startDate}
            </if>
            <if test="endDate != null and endDate > 0">
                AND #{endDate} >= det.exam_end_time
            </if>
            <if test="semesterId != null and semesterId != ''">
                AND bs.id = #{semesterId}
            </if>
            <if test="examId != null and examId != ''">
                AND be.id = #{examId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND det.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND det.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        GROUP BY det.id
        <trim prefix="having" suffixOverrides="and">
            <if test="courseCode != null and courseCode != ''">
                find_in_set(#{courseCode},courseCodes) and
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                find_in_set(#{paperNumber},paperNumber) and
            </if>
        </trim>
         order by det.id desc
    </select>
    <select id="findDetailPage"
            resultType="com.qmth.distributed.print.business.bean.result.ExaminationDetailResult">
        SELECT
            stu.id AS id,
            stu.exam_detail_course_id,
            stu.student_name AS studentName,
            stu.student_code AS studentCode,
            stu.ticket_number AS ticketNumber,
            stu.site_number AS siteNumber,
            stu.create_id AS createId,
            stu.create_time AS createTime,
            a.exam_id AS examId,
            a.exam_name AS examName,
            CONCAT(a.course_name,
                   '(',
                   a.course_code,
                   ')') AS courseNameCode,
            a.paper_number AS paperNumber,
            a.print_plan_id AS printPlanId,
            a.print_plan_name AS printPlanName,
            a.exam_start_time AS examStartTime,
            a.exam_end_time AS examEndTime,
            a.exam_place AS examPlace,
            a.exam_room AS examRoom
        FROM
            (select be.id exam_id,
                    be.name exam_name,
                    edc.id exam_detail_course_id,
                    edc.course_code,
                    edc.course_name,
                    edc.paper_number,
                    ed.print_plan_id,
                    ed.print_plan_name,
                    ed.exam_start_time,
                    ed.exam_end_time,
                    ed.exam_place,
                    ed.exam_room
             from exam_detail ed
                JOIN
            exam_detail_course edc on ed.id = edc.exam_detail_id
                LEFT JOIN
            sys_user b on ed.create_id = b.id
                LEFT JOIN
            basic_exam be ON be.id = ed.exam_id
                LEFT JOIN
            basic_semester bs ON bs.id = be.semester_id
            <where>
                and be.enable = true
                <if test="schoolId != null">
                    AND ed.school_id = #{schoolId}
                </if>
                <if test="semesterId != null">
                    and bs.id = #{semesterId}
                </if>
                <if test="examId != null">
                    and be.id = #{examId}
                </if>
                <if test="printPlanIdList != null and printPlanIdList != '' and printPlanIdList.size() > 0">
                    AND ed.print_plan_id IN
                    <foreach collection="printPlanIdList" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="courseCode != null and courseCode != ''">
                    AND edc.course_code = #{courseCode}
                </if>
                <if test="paperNumber != null and paperNumber != ''">
                    AND edc.paper_number = #{paperNumber}
                </if>
                <if test="examPlace != null and examPlace != ''">
                    AND ed.exam_place = #{examPlace}
                </if>
                <if test="examRoom != null and examRoom != ''">
                    AND ed.exam_room = #{examRoom}
                </if>
                <if test="startDate != null and startDate > 0">
                    AND ed.exam_start_time &gt;= #{startDate}
                </if>
                <if test="endDate != null and endDate > 0">
                    AND ed.exam_end_time &lt;= #{endDate}
                </if>
                <if test="dpr != null">
                    <if test="dpr.requestUserId != null">
                        AND b.id = #{dpr.requestUserId}
                    </if>
                    <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                        AND b.org_id IN
                        <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </if>
                </if>
            </where>) a
                join
            exam_student stu ON a.exam_detail_course_id = stu.exam_detail_course_id
        <where>

            <if test="studentParams != null and studentParams != ''">
                AND (stu.ticket_number LIKE CONCAT('%',#{studentParams},'%') OR stu.student_code LIKE CONCAT('%',#{studentParams},'%') OR stu.student_name LIKE CONCAT('%',#{studentParams},'%'))
            </if>
        </where>
        order by stu.exam_detail_course_id desc, stu.id desc
    </select>
    <select id="findDetailPageById"
            resultType="com.qmth.distributed.print.business.bean.result.ExaminationDetailResult">
        SELECT
            stu.id AS id,
            stu.exam_detail_course_id,
            stu.student_name AS studentName,
            stu.student_code AS studentCode,
            stu.ticket_number AS ticketNumber,
            stu.site_number AS siteNumber,
            stu.create_id AS createId,
            stu.create_time AS createTime,
            CONCAT(cou.course_name,
                   '(',
                   cou.course_code,
                   ')') AS courseNameCode,
            cou.paper_number AS paperNumber,
            det.print_plan_id AS printPlanId,
            det.print_plan_name AS printPlanName,
            det.exam_place AS examPlace,
            det.exam_room AS examRoom,
            bs.id AS semesterId,
            bs.name AS semesterName,
            be.id AS examId,
            be.name AS examName,
            stu.can_delete canDelete
        FROM
            exam_student stu
                INNER JOIN
            exam_detail_course cou ON stu.exam_detail_course_id = cou.id
                AND stu.school_id = cou.school_id
                INNER JOIN
            exam_detail det ON cou.exam_detail_id = det.id
                AND stu.school_id = det.school_id
                LEFT JOIN
            exam_print_plan epp ON det.print_plan_id = epp.id
                LEFT JOIN
            basic_exam be ON be.id = epp.exam_id
                LEFT JOIN
            basic_semester bs ON bs.id = be.semester_id
        <where>
            <if test="examDetailId != null">
                AND det.id = #{examDetailId}
            </if>
            <if test="param != null and param != ''">
                AND (stu.student_name like concat('%', #{param}) or stu.student_code like concat('%', #{param}))
            </if>
        </where>
        order by stu.can_delete desc, stu.student_code
    </select>
    <select id="taskTotalData" resultType="com.qmth.distributed.print.business.bean.dto.PrintTaskTotalDto">
        SELECT
            IFNULL(COUNT(ed.package_code), 0) packageCount,
            IFNULL(SUM(ed.total_subjects), 0) totalSubjects,
            IFNULL(SUM(IFNULL(ed.paper_pages, 0)), 0) / 2 paperPages,
            IFNULL(SUM(IFNULL(ed.card_pages, 0)), 0) / 2 cardPages,
            IFNULL(SUM(IFNULL(ed.pages_a4, 0)), 0) / 2 pagesA4,
            IFNULL(SUM(CASE ed.status
                           WHEN 'FINISH' THEN 0
                           ELSE IFNULL(ed.paper_pages, 0)
                END),
                   0) / 2 paperPagesLeft,
            IFNULL(SUM(CASE ed.status
                           WHEN 'FINISH' THEN 0
                           ELSE IFNULL(ed.card_pages, 0)
                END),
                   0) / 2 cardPagesLeft,
            IFNULL(SUM(CASE ed.status
                           WHEN 'FINISH' THEN 0
                           ELSE IFNULL(ed.pages_a4, 0)
                END),
                   0) / 2 pagesA4Left
        FROM
            (SELECT
                 a.*
             FROM
                 exam_detail a
                    LEFT JOIN exam_print_plan epp ON a.print_plan_id = epp.id
                     LEFT JOIN basic_exam b ON epp.exam_id = b.id
                    <where>
                        <if test="schoolId != null">
                            and a.school_id = #{schoolId}
                        </if>
                        <if test="semesterId != null">
                            and b.semester_id = #{semesterId}
                        </if>
                        <if test="examId != null">
                            and epp.exam_id = #{examId}
                        </if>
                        <if test="printPlanId != null">
                            and a.print_plan_id = #{printPlanId}
                        </if>
                        <if test="status != null and status != ''">
                            and a.status = #{status}
                        </if>
                        <if test="examPlace != null and examPlace != ''">
                            and a.exam_place = #{examPlace}
                        </if>
                        <if test="examRoom != null and examRoom != ''">
                            and a.exam_room = #{examRoom}
                        </if>
                        <if test="examStartTime != null and examStartTime != ''">
                            and a.exam_start_time &gt; #{examStartTime}
                        </if>
                        <if test="examEndTime != null and examEndTime != ''">
                            and a.exam_end_time &lt; #{examEndTime}
                        </if>
                        <if test="printStartTime != null and printStartTime != ''">
                            and a.print_start_time &gt; #{printStartTime}
                        </if>
                        <if test="printEndTime != null and printEndTime != ''">
                            and a.print_start_time &lt; #{printEndTime}
                        </if>
                        <if test="printHouseId != null and printHouseId != ''">
                            and a.print_house_id = #{printHouseId}
                        </if>
                    </where>
             ) ed
        WHERE
            EXISTS( SELECT
                        1
                    FROM
                        (SELECT
                             *
                         FROM
                             exam_detail_course edc
                         WHERE
                             EXISTS( SELECT
                                         1
                                     FROM
                                         exam_detail ed
                                            LEFT JOIN exam_print_plan epp ON ed.print_plan_id = epp.id
                                     WHERE
                                         edc.exam_detail_id = ed.id
                                       AND epp.exam_id = #{examId})) edc
                            JOIN
                        (SELECT
                             *
                         FROM
                             exam_task
                         WHERE
                             exam_id = #{examId}) et ON edc.course_code = et.course_code
                            AND edc.paper_number = et.paper_number
                            left join sys_user su ON et.user_id = su.id
                    WHERE
                        ed.id = edc.exam_detail_id
                        <if test="courseCode != null and courseCode != ''">
                            and edc.course_code = #{courseCode}
                        </if>
                        <if test="paperNumber != null and paperNumber != ''">
                            and edc.paper_number = #{paperNumber}
                        </if>
                        <if test="dpr != null">
                            <if test="dpr.requestUserId != null">
                                AND et.user_id = #{dpr.requestUserId}
                            </if>
                            <if test="dpr.courseUserId != null">
                                AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = et.course_code)
                            </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>
                )
    </select>
    <select id="listStudentByExamDetailCourseId" resultType="java.util.Map">
        SELECT
            b.course_code courseCode,
            b.course_name courseName,
            b.paper_number paperNumber,
            c.student_name studentName,
            c.student_code studentCode,
            c.attachment_id attachmentId,
            c.paper_type paperType
        FROM
            exam_detail a
                JOIN
            exam_detail_course b ON a.id = b.exam_detail_id
                JOIN
            exam_student c ON b.id = c.exam_detail_course_id
        WHERE
            b.id = #{examDetailCourseId}
        <if test="type == 'ONE'">
            and c.student_code = #{studentCode}
        </if>
        <if test="type == 'MORE'">
            and c.student_code >= #{studentCode}
        </if>
    </select>
    <select id="selectPaperCount" resultType="com.qmth.distributed.print.business.entity.ExamDetailCourse">
        SELECT
            edc.*
        FROM
            (SELECT
                 edc.exam_detail_id,
                 edc.course_code,
                 edc.paper_number,
                 edc.paper_type
             FROM
                 exam_detail_course edc
             WHERE
                 EXISTS( SELECT
                             1
                         FROM
                             exam_detail ed
                                LEFT JOIN
                             basic_exam be ON ed.exam_id = be.id
                         WHERE
                             edc.exam_detail_id = ed.id
                            <if test="schoolId != null">
                                and ed.school_id = #{schoolId}
                            </if>
                            <if test="semesterId != null">
                                and be.semester_id = #{semesterId}
                            </if>
                            <if test="examId != null">
                                and ed.exam_id = #{examId}
                            </if>
                            <if test="printPlanId != null">
                                and ed.print_plan_id = #{printPlanId}
                            </if>
                            <if test="status != null and status != ''">
                                and ed.status = #{status}
                            </if>
                            <if test="examPlace != null and examPlace != ''">
                                and ed.exam_place = #{examPlace}
                            </if>
                            <if test="examRoom != null and examRoom != ''">
                                and ed.exam_room = #{examRoom}
                            </if>
                            <if test="examStartTime != null and examStartTime != ''">
                                and ed.exam_start_time &gt; #{examStartTime}
                            </if>
                            <if test="examEndTime != null and examEndTime != ''">
                                and ed.exam_end_time &lt; #{examEndTime}
                            </if>
                            <if test="printStartTime != null and printStartTime != ''">
                                and ed.print_start_time &gt; #{printStartTime}
                            </if>
                            <if test="printEndTime != null and printEndTime != ''">
                                and ed.print_start_time &lt; #{printEndTime}
                            </if>
                            <if test="printHouseId != null and printHouseId != ''">
                                and ed.print_house_id = #{printHouseId}
                            </if>
                    )
               AND edc.paper_type IS NOT NULL) edc
                JOIN
            (SELECT
                 *
             FROM
                 exam_task
             WHERE
                 exam_id = #{examId}) et ON edc.course_code = et.course_code
                AND edc.paper_number = et.paper_number
                LEFT JOIN
             sys_user su ON et.user_id = su.id
            <where>
                <if test="courseCode != null and courseCode != ''">
                    and edc.course_code = #{courseCode}
                </if>
                <if test="paperNumber != null and paperNumber != ''">
                    and edc.paper_number = #{paperNumber}
                </if>
                <if test="dpr != null">
                    <if test="dpr.requestUserId != null">
                        AND et.user_id = #{dpr.requestUserId}
                    </if>
                    <if test="dpr.courseUserId != null">
                        AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = et.course_code)
                    </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="findPdfDownload" resultType="com.qmth.distributed.print.business.bean.dto.ExamDetailPdfDownloadDto">
        select
            ed.*,
            bs.name as semesterName,
            be.name as examName,
            GROUP_CONCAT(CONCAT(edc.course_name, '(', edc.course_code, ')')) as courseNameCode,
            GROUP_CONCAT(edc.paper_number) paperNumber
        from
            exam_detail ed
                join exam_print_plan epp on
                epp.id = ed.print_plan_id
                join basic_exam be on
                be.id = epp.exam_id
                join basic_semester bs on
                bs.id = be.semester_id
                join exam_detail_course edc on
                edc.exam_detail_id = ed.id
        <where>
            <if test="ids != null and ids != '' and ids.size > 0">
                AND ed.id IN
                <foreach collection="ids" item="id" index="index" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        GROUP BY
            edc.exam_detail_id
        order by bs.name
    </select>
    <select id="findSummarizedData"
            resultType="com.qmth.distributed.print.business.bean.result.SummarizedDataResult">
        SELECT
        det.school_id schoolId,
        sum(ifnull(det.total_subjects, 0)) AS totalSubjects,
        count(det.package_code) AS packageCount,
        GROUP_CONCAT(cou.course_code) AS courseCodes,
        GROUP_CONCAT(cou.paper_number) AS paperNumbers
        FROM
        exam_detail det
        INNER JOIN
        exam_detail_course cou ON det.id = cou.exam_detail_id
        LEFT JOIN
        exam_print_plan epp on epp.id = det.print_plan_id
        LEFT JOIN
        basic_exam be on be.id = epp.exam_id
        LEFT JOIN
        basic_semester bs on bs.id = be.semester_id
        <where>
            <if test="schoolId != null">
                AND det.school_id = #{schoolId}
            </if>
                AND bs.id = #{semesterId}
                AND be.id = #{examId}
            <if test="printPlanIdList != null and printPlanIdList != '' and printPlanIdList.size() > 0">
                AND det.print_plan_id IN
                <foreach collection="printPlanIdList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="courseCode != null and courseCode != ''">
                AND cou.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                AND cou.paper_number = #{paperNumber}
            </if>
            <if test="examPlace != null and examPlace != ''">
                AND det.exam_place = #{examPlace}
            </if>
            <if test="examRoom != null and examRoom != ''">
                AND det.exam_room = #{examRoom}
            </if>
            <if test="packageCode != null and packageCode != ''">
                AND det.package_code = #{packageCode}
            </if>
            <if test="startDate != null and startDate > 0">
                AND det.exam_start_time >= #{startDate}
            </if>
            <if test="endDate != null and endDate > 0">
                AND #{endDate} >= det.exam_end_time
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND det.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND det.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        GROUP BY det.school_id
        <trim prefix="having" suffixOverrides="and">
            <if test="courseCode != null and courseCode != ''">
                find_in_set(#{courseCode},courseCodes) and
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                find_in_set(#{paperNumber},paperNumbers) and
            </if>
        </trim>
    </select>
    <select id="listByExamIdAndCourseCode"
            resultType="com.qmth.distributed.print.business.bean.dto.ExamDetailCourseDto">
        SELECT
            edc.clazz_id clazzId,
            su.login_name loginName,
            su.real_name realName
        FROM
            exam_detail_course edc
                JOIN
            exam_detail ed ON edc.exam_detail_id = ed.id
                left join
            sys_user su ON ed.create_id = su.id
        WHERE
            edc.school_id = #{schoolId} AND ed.exam_id = #{examId}
          AND edc.course_code = #{courseCode} AND ed.status != 'CANCEL'
    </select>
    <select id="listPrintFinishExamDetailCourse"
            resultType="com.qmth.distributed.print.business.bean.dto.initMarkData.ExamDetailCourseInitMarkDto">
        SELECT
            ed.school_id schoolId,
            be.semester_id semesterId,
            ed.exam_id examId,
            edc.course_code courseCode,
            edc.course_name courseName,
            edc.paper_number paperNumber,
            et.id coursePaperId,
            edc.paper_type paperType,
            et.user_id createId,
            ed.exam_start_time examStartTime,
            ed.exam_end_time examEndTime,
            group_concat(ed.package_code) packageCodes,
            group_concat(edc.id) examDetailCourseIds
        FROM
            exam_detail ed
                LEFT JOIN
            exam_detail_course edc ON ed.id = edc.exam_detail_id
                LEFT JOIN
            exam_task et ON ed.exam_id = et.exam_id AND edc.paper_number = et.paper_number
                JOIN
            (select * from basic_exam where enable = true)be ON be.id = ed.exam_id
        WHERE
            ed.status = #{printStatus}
          AND ed.print_end_time > #{startTime}
          AND NOT EXISTS( SELECT
                              1
                          FROM
                              mark_paper mp
                          WHERE
                              ed.exam_id = mp.exam_id
                            AND edc.paper_number = mp.paper_number
                            AND mp.status = #{markStatus})
        GROUP BY ed.school_id , be.semester_id, ed.exam_id , edc.course_code , edc.course_name , edc.paper_number , et.id , edc.paper_type , et.user_id, ed.exam_start_time, ed.exam_end_time
    </select>
    <select id="countMakeupUsedByExamIdAndPaperNumber" resultType="java.lang.Integer">
        SELECT
            COUNT(1)
        FROM
            exam_detail ed
                LEFT JOIN
            exam_detail_course edc ON ed.id = edc.exam_detail_id
        WHERE
            ed.exam_id = #{examId}
          AND edc.paper_number = #{paperNumber}
          AND EXISTS( SELECT
                          1
                      FROM
                          exam_print_plan epp
                      WHERE
                          ed.print_plan_id = epp.id
                        AND epp.category = 'MAKEUP')
    </select>
    <select id="listByExamIdAndCourseCodeAndPaperNumber"
            resultType="com.qmth.distributed.print.business.entity.ExamDetail">
        SELECT
            b.*
        FROM
            exam_detail_course a
                JOIN
            exam_detail b ON a.exam_detail_id = b.id
        WHERE
            a.school_id = #{schoolId}
          AND b.exam_id = #{examId}
          AND a.course_code = #{courseCode}
          AND a.paper_number = #{paperNumber}
    </select>
    <select id="listPrintFinishPackageCode"
            resultType="com.qmth.distributed.print.business.bean.dto.initMarkData.ExamDetailCourseInitMarkDto">
        SELECT
            ed.exam_id examId,
            edc.course_code courseCode,
            edc.course_name courseName,
            edc.paper_number paperNumber,
            edc.course_paper_id coursePaperId,
            ed.package_code
        FROM
            exam_detail ed
                LEFT JOIN
            exam_detail_course edc ON ed.id = edc.exam_detail_id
        WHERE
            ed.exam_id = #{examId}
          AND edc.paper_number = #{paperNumber}
          AND edc.paper_type LIKE CONCAT('%', #{paperType}, '%')
          AND NOT EXISTS( SELECT
                              1
                          FROM
                              mark_paper_package mpp
                          WHERE
                              ed.exam_id = mpp.exam_id
                            AND edc.paper_number = mpp.paper_number
                            AND mpp.paper_type = #{paperType}
                            AND ed.package_code = mpp.package_code)

    </select>
    <select id="listPrintFinishExamDetailCourseIds" resultType="com.qmth.distributed.print.business.bean.dto.initMarkData.InitExamDetailDto">
        SELECT
            edc.id examDetailCourseId,
            ed.exam_place examPlace,
            ed.exam_room examRoom,
            ed.package_code packageCode
        FROM
            exam_detail ed
                LEFT JOIN
            exam_detail_course edc ON ed.id = edc.exam_detail_id
        WHERE
            ed.exam_id = #{examId}
          AND edc.paper_number = #{paperNumber}
          AND edc.paper_type LIKE CONCAT('%', #{paperType}, '%')
    </select>
</mapper>
