<?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
            b.id examDetailId,
            b.package_code packageCode,
            a.id printPlanId,
            a.name printPlanName,
            b.exam_start_time examStartTime,
            b.exam_end_time examEndTime,
            c.courseNameCode,
            c.paperNumber,
            b.exam_place examPlace,
            b.exam_room examRoom,
            b.total_subjects totalSubjects,
            ifnull(b.pages_a3, 0) pagesA3,
            ifnull(b.pages_a4, 0) pagesA4,
            c.singlePagesA3,
            b.status,
            b.validate,
            b.print_start_time printStartTime,
            b.print_end_time printEndTime,
            b.normal
        FROM
            exam_print_plan a
                 JOIN
            exam_detail b ON a.id = b.print_plan_id
                 JOIN
            (SELECT
                exam_detail_id,
                    GROUP_CONCAT(CONCAT(course_name, '(', course_code, ')')) courseNameCode,
                    GROUP_CONCAT(paper_number) paperNumber,
                    GROUP_CONCAT(ifnull(paper_pages_a3, 0) + ifnull(card_pages_a3, 0)) singlePagesA3
            FROM
                exam_detail_course
                <where>
                    <if test="courseCode != null and courseCode != ''">
                        and course_code = #{courseCode}
                    </if>
                    <if test="paperNumber != null and paperNumber != ''">
                        and paper_number = #{paperNumber}
                    </if>
                </where>
            GROUP BY exam_detail_id) c ON b.id = c.exam_detail_id
                LEFT JOIN
            sys_user d on a.create_id = d.id
            <where>
                <if test="schoolId != null and schoolId != ''">
                    and a.school_id = #{schoolId}
                </if>
                <if test="printPlanId != null and printPlanId != ''">
                    and a.id = #{printPlanId}
                </if>
                <if test="status != null and status != ''">
                    and b.status = #{status}
                </if>
                <if test="examPlace != null and examPlace != ''">
                    and b.exam_place = #{examPlace}
                </if>
                <if test="examRoom != null and examRoom != ''">
                    and b.exam_room = #{examRoom}
                </if>
                <if test="examStartTime != null and examStartTime != ''">
                    and b.exam_start_time &gt; #{examStartTime}
                </if>
                <if test="examEndTime != null and examEndTime != ''">
                    and b.exam_end_time &lt; #{examEndTime}
                </if>
                <if test="printStartTime != null and printStartTime != ''">
                    and b.print_start_time &gt; #{printStartTime}
                </if>
                <if test="printEndTime != null and printEndTime != ''">
                    and b.print_start_time &lt; #{printEndTime}
                </if>
                <if test="orgIds != null">
                    AND d.org_id IN
                    <foreach collection="orgIds" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </where>
        ORDER BY a.create_time desc
    </select>

    <select id="findBriefPage" resultType="com.qmth.distributed.print.business.bean.result.ExaminationResult">
        SELECT
        det.id AS id,
        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(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
        AND det.school_id = cou.school_id
        LEFT JOIN
        sys_user b on det.create_id = b.id
        <where>
            <if test="schoolId != null and schoolId > 0">
                AND det.school_id = #{schoolId}
            </if>
            <if test="printPlanIdList != null 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="orgIds != null and orgIds.size() > 0">
                AND b.org_id IN
                <foreach collection="orgIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </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},paperNumbers) and
            </if>
        </trim>
    </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,
            CONCAT(cou.course_name,
                   '(',
                   cou.course_code,
                   ')') AS courseNameCode,
            cou.paper_number AS paperNumber,
            et.specialty,
            det.print_plan_id AS printPlanId,
            det.print_plan_name AS printPlanName,
            det.exam_start_time AS examStartTime,
            det.exam_end_time AS examEndTime,
            det.exam_place AS examPlace,
            det.exam_room AS examRoom
        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_task et on cou.school_id = et.school_id
                AND cou.course_code = et.course_code
                AND cou.paper_number = et.paper_number
                LEFT JOIN
            sys_user b on det.create_id = b.id
        <where>
            <if test="schoolId != null and schoolId > 0">
                AND det.school_id = #{schoolId}
            </if>
            <if test="printPlanIdList != null 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="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>
            <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="orgIds != null">
                AND b.org_id IN
                <foreach collection="orgIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </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
        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
        <where>
            <if test="examDetailId != null and examDetailId > 0">
                AND det.id = #{examDetailId}
            </if>
        </where>
    </select>
    <select id="taskTotalData" resultType="com.qmth.distributed.print.business.bean.dto.PrintTaskTotalDto">
        SELECT
            ifnull(count(b.package_code), 0) packageCount,
            ifnull(sum(b.total_subjects), 0) totalSubjects,
            ifnull(sum(ifnull(b.pages_a3, 0)), 0)/2 pagesA3,
            ifnull(sum(ifnull(b.pages_a4, 0)), 0) /2 pagesA4,
            ifnull(sum(case b.status when 'FINISH' then 0 else ifnull(b.pages_a3, 0) end),0)/2 pagesA3Left,
            ifnull(sum(case b.status when 'FINISH' then 0 else ifnull(b.pages_a4, 0) end),0)/2 pagesA4Left
        FROM
            exam_print_plan a
         JOIN
            exam_detail b ON a.id = b.print_plan_id
        JOIN
            sys_user d on a.create_id = d.id
        <where>
            <if test="schoolId != null and schoolId != ''">
                and a.school_id = #{schoolId}
            </if>
            <if test="printPlanId != null and printPlanId != ''">
                and a.id = #{printPlanId}
            </if>
            <if test="status != null and status != ''">
                and b.status = #{status}
            </if>
            <if test="examPlace != null and examPlace != ''">
                and b.exam_place = #{examPlace}
            </if>
            <if test="examRoom != null and examRoom != ''">
                and b.exam_room = #{examRoom}
            </if>
            <if test="examStartTime != null and examStartTime != ''">
                and b.exam_start_time &gt; #{examStartTime}
            </if>
            <if test="examEndTime != null and examEndTime != ''">
                and b.exam_end_time &lt; #{examEndTime}
            </if>
            <if test="printStartTime != null and printStartTime != ''">
                and b.print_start_time &gt; #{printStartTime}
            </if>
            <if test="printEndTime != null and printEndTime != ''">
                and b.print_start_time &lt; #{printEndTime}
            </if>
            <if test="orgIds != null">
                AND d.org_id IN
                <foreach collection="orgIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="listClientExamStudentPage"
            resultType="com.qmth.distributed.print.business.bean.dto.ClientExamStudentDto">
        SELECT
            a.id examDetailId,
            b.course_code courseCode,
            b.course_name courseName,
            b.paper_number paperNumber,
            c.student_code studentCode,
            c.ticket_number ticketNumber,
            c.student_name studentName,
            c.site_number siteNumber
        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>
            and a.school_id = #{schoolId}
            and a.id = #{examDetailId}
            <if test="ticketNumber != null and ticketNumber != ''">
                and c.ticket_number like concat('%', #{ticketNumber}, '%')
            </if>
            <if test="studentName != null and studentName != ''">
                and c.student_name like concat('%', #{studentName}, '%')
            </if>
            <if test="courseCode != null and courseCode != ''">
                and b.course_code = #{courseCode}
            </if>
        </where>
    </select>
    <select id="listStudentByExamDetailId" 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
            a.id = #{examDetailId}
        <if test="type == 'ONE'">
            and c.ticket_number = #{ticketNumber}
        </if>
        <if test="type == 'MORE'">
            and c.ticket_number >= #{ticketNumber}
        </if>
    </select>
    <select id="selectPaperCount" resultType="java.lang.Integer">
        SELECT
            COUNT(distinct c.paper_number, c.paper_type)
        FROM
            exam_print_plan a
            JOIN
        exam_detail b ON a.id = b.print_plan_id
            JOIN
        exam_detail_course c ON b.id = c.exam_detail_id
            JOIN
        sys_user d ON a.create_id = d.id
        <where>
            and c.paper_type is not null
            <if test="schoolId != null and schoolId != ''">
                and a.school_id = #{schoolId}
            </if>
            <if test="printPlanId != null and printPlanId != ''">
                and a.id = #{printPlanId}
            </if>
            <if test="status != null and status != ''">
                and b.status = #{status}
            </if>
            <if test="examPlace != null and examPlace != ''">
                and b.exam_place = #{examPlace}
            </if>
            <if test="examRoom != null and examRoom != ''">
                and b.exam_room = #{examRoom}
            </if>
            <if test="examStartTime != null and examStartTime != ''">
                and b.exam_start_time &gt; #{examStartTime}
            </if>
            <if test="examEndTime != null and examEndTime != ''">
                and b.exam_end_time &lt; #{examEndTime}
            </if>
            <if test="orgIds != null">
                AND d.org_id IN
                <foreach collection="orgIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="listSyncPaperNumberByPrintPlanId"
            resultType="com.qmth.distributed.print.business.entity.ExamDetailCourse">
        SELECT DISTINCT
            b.school_id schoolId,
            b.course_code courseCode,
            b.paper_number paperNumber
        FROM
            exam_detail a
                LEFT JOIN
            exam_detail_course b ON a.id = b.exam_detail_id
        WHERE
            a.print_plan_id = #{printPlanId}
    </select>

</mapper>
