<?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.ClientMapper">
    <select id="pageTryTask" resultType="com.qmth.distributed.print.business.bean.dto.ClientExamTaskDto">
        SELECT
            distinct a.school_id schoolId,
            a.exam_id examId,
            a.id printPlanId,
            a.name printPlanName,
            c.course_code courseCode,
            c.course_name courseName,
            c.paper_number paperNumber,
            d.id examTaskId,
            d.specialty,
            d.user_id userId,
            f.real_name userName,
            ifnull(e.is_try, false) isTry,
            ifnull(e.is_pass, false) isPass,
            e.try_time tryTime
        FROM
            (select epp.id, epp.school_id, epp.exam_id, epp.name, epp.status from exam_print_plan  epp
        LEFT JOIN
        basic_exam be ON epp.exam_id = be.id
        <where>
            and epp.school_id = #{schoolId}
            and be.enable = true
            and be.semester_id = #{semesterId}
            <if test="examId != null">
                and be.id = #{examId}
            </if>
            and epp.status = #{printPlanStatus}
            and LOCATE('PAPER', epp.print_content) > 0
        </where>) 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
            exam_task d ON d.school_id = c.school_id
            AND d.exam_id = b.exam_id
            AND d.course_code = c.course_code
            AND d.paper_number = c.paper_number
        LEFT JOIN
            client_status e ON e.school_id = d.school_id
            AND b.id = e.exam_detail_id
            AND a.id = e.print_plan_id
            AND e.course_code = d.course_code
            AND e.paper_number = d.paper_number
            and e.machine_code = #{machineCode}
        LEFT JOIN
            sys_user f ON d.user_id = f.id
        <where>
            and b.status not in
            <foreach item="item" collection="examDetailStatus" separator="," open="(" close=")" index="">
                #{item}
            </foreach>
            <if test="printPlanId != null and printPlanId != ''">
                and a.id = #{printPlanId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and c.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and c.paper_number = #{paperNumber}
            </if>
            <if test="isTry != null">
                and IFNULL(e.is_try, FALSE) = #{isTry}
            </if>
            <if test="isPass != null">
                and IFNULL(e.is_pass, FALSE) = #{isPass}
            </if>
            <if test="orgId != null">
                AND (b.print_house_id = #{orgId} or b.print_house_id is null)
            </if>
        </where>
    </select>
    <select id="listClientPrintTask" resultType="com.qmth.distributed.print.business.bean.dto.ClientPrintTaskDto">
        SELECT
        b.id examDetailId,
        b.package_code packageCode,
        a.id printPlanId,
        a.NAME printPlanName,
        a.print_content printContent,
        a.variable_content variableContent,
        a.ordinary_content ordinaryContent,
        b.exam_start_time examStartTime,
        b.exam_end_time examEndTime,
        c.courseNameCode,
        c.paperNumber,
        c.classId,
        c.className,
        b.exam_place examPlace,
        b.exam_room examRoom,
        b.total_subjects totalSubjects,
        IFNULL(b.paper_pages, 0) paperPages,
        IFNULL(b.card_pages, 0) cardPages,
        IFNULL(b.pages_a4, 0) pagesA4,
        CASE b.STATUS
        WHEN 'FINISH' THEN IFNULL(b.paper_pages, 0) + IFNULL(b.card_pages, 0) + IFNULL(b.pages_a4, 0)
        ELSE 0
        END totalPrint,
        c.singlePagesA3,
        b.STATUS,
        IFNULL(b.validate, FALSE) validate,
        CASE c.isDownload
        WHEN 0 THEN TRUE
        ELSE FALSE
        END download,
        b.print_user printUser,
        b.print_start_time printStartTime,
        b.print_end_time printEndTime,
        CASE c.isTry
        WHEN 0 THEN TRUE
        ELSE FALSE
        END isTry,
        CASE c.isPass
        WHEN 0 THEN TRUE
        ELSE FALSE
        END isPass
        FROM
        (SELECT
        epp.school_id,
        epp.id,
        epp.exam_id,
        epp.NAME,
        epp.print_content,
        epp.variable_content,
        epp.ordinary_content,
        epp.STATUS
        FROM
        exam_print_plan epp
        JOIN basic_exam be ON epp.exam_id = be.id AND be.ENABLE = TRUE
        <where>
            and epp.school_id = #{schoolId}
            and be.semester_id = #{semesterId}
            <if test="examId != null">
                and epp.exam_id = #{examId}
            </if>
            <if test="printPlanId != null">
                and epp.id = #{printPlanId}
            </if>
        </where>) a
        JOIN
        (SELECT *
        from exam_detail t
        <where>
            and t.normal = 1
            <choose>
                <when test="status != null and status != ''">
                    and t.status = #{status}
                </when>
                <otherwise>
                    AND t.status not IN
                    <foreach collection="examDetailStatus" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </otherwise>
            </choose>
            <if test="examPlace != null and examPlace != ''">
                and t.exam_place = #{examPlace}
            </if>
            <if test="examRoom != null and examRoom != ''">
                and t.exam_room = #{examRoom}
            </if>
            <if test="examStartTime != null and examStartTime != ''">
                and t.exam_start_time &gt; #{examStartTime}
            </if>
            <if test="examEndTime != null and examEndTime != ''">
                and t.exam_end_time &lt; #{examEndTime}
            </if>
            <if test="validate != null">
                and ifnull(t.validate, false) = #{validate}
            </if>
            <if test="orgId != null">
                AND (t.print_house_id = #{orgId} or t.print_house_id is null)
            </if>
        </where>
        ) b ON a.id = b.print_plan_id
        JOIN
        (SELECT
        a.exam_detail_id,
        GROUP_CONCAT(CONCAT(a.course_name, '(', a.course_code, ')')) courseNameCode,
        GROUP_CONCAT(a.paper_number) paperNumber,
        GROUP_CONCAT(a.clazz_id) classId,
        GROUP_CONCAT(a.clazz_name) className,
        GROUP_CONCAT(IFNULL(a.paper_pages_a3, 0) + IFNULL(a.card_pages_a3, 0)) singlePagesA3,
        SUM(CASE b.is_download
        WHEN TRUE THEN 0
        ELSE 1
        END) isDownload,
        SUM(CASE b.is_try
        WHEN TRUE THEN 0
        ELSE 1
        END) isTry,
        SUM(CASE b.is_pass
        WHEN TRUE THEN 0
        ELSE 1
        END) isPass
        FROM
        exam_detail_course a
        LEFT JOIN client_status b ON a.school_id = b.school_id
        AND a.exam_detail_id = b.exam_detail_id
        AND a.course_code = b.course_code
        AND a.paper_number = b.paper_number
        AND b.machine_code = #{machineCode}
        GROUP BY a.exam_detail_id) c ON b.id = c.exam_detail_id
        <where>
            <if test="courseCode != null and courseCode != ''">
                and c.courseNameCode like concat('%',#{courseCode},'%')
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and c.paperNumber like concat('%',#{paperNumber},'%')
            </if>
            <if test="isDownload != null">
                <if test="isDownload == true">
                    and c.isDownload = 0
                </if>
                <if test="isDownload == false">
                    and c.isDownload > 0
                </if>
            </if>
        </where>
        ORDER BY a.id DESC , b.id DESC
    </select>
    <select id="clientTaskTotalData" resultType="com.qmth.distributed.print.business.bean.dto.ClientPrintTaskTotalDto">
        SELECT
        ifnull(count(b.package_code), 0) packageCount,
        ifnull(sum(b.total_subjects), 0) totalSubjects,
        ifnull(sum(ifnull(b.paper_pages, 0)), 0)/2 paperPages,
        ifnull(sum(ifnull(b.card_pages, 0)), 0)/2 cardPages,
        ifnull(sum(ifnull(b.pages_a4, 0)), 0) /2 pagesA4,
        ifnull(sum(case b.status when 'FINISH' then 0 else ifnull(b.paper_pages, 0) end),0)/2 paperPagesLeft,
        ifnull(sum(case b.status when 'FINISH' then 0 else ifnull(b.card_pages, 0) end),0)/2 cardPagesLeft,
        ifnull(sum(case b.status when 'FINISH' then 0 else ifnull(b.pages_a4, 0) end),0)/2 pagesA4Left,
        GROUP_CONCAT(c.paperType) paperNumberAndType
        FROM
        (select epp.id from exam_print_plan epp
        JOIN
        basic_exam be ON epp.exam_id = be.id AND be.ENABLE = TRUE
        <where>
            and epp.school_id = #{schoolId}
            and be.semester_id = #{semesterId}
            <if test="examId != null">
                and epp.exam_id = #{examId}
            </if>
            <if test="printPlanId != null">
                and epp.id = #{printPlanId}
            </if>
        </where>) a
        JOIN
        (SELECT *
        from exam_detail t
        <where>
            <choose>
                <when test="status != null and status != ''">
                    and t.status = #{status}
                </when>
                <otherwise>
                    AND t.status not IN
                    <foreach collection="examDetailStatus" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </otherwise>
            </choose>
            <if test="examPlace != null and examPlace != ''">
                and t.exam_place = #{examPlace}
            </if>
            <if test="examRoom != null and examRoom != ''">
                and t.exam_room = #{examRoom}
            </if>
            <if test="examStartTime != null and examStartTime != ''">
                and t.exam_start_time &gt; #{examStartTime}
            </if>
            <if test="examEndTime != null and examEndTime != ''">
                and t.exam_end_time &lt; #{examEndTime}
            </if>
            <if test="validate != null">
                and ifnull(t.validate, false) = #{validate}
            </if>
            <if test="orgId != null">
                AND (t.print_house_id = #{orgId} or t.print_house_id is null)
            </if>
        </where>
        ) b ON a.id = b.print_plan_id
        JOIN
        (SELECT
        a.exam_detail_id,
        GROUP_CONCAT(CONCAT(a.course_name, '(', a.course_code, ')')) courseNameCode,
        GROUP_CONCAT(CONCAT(a.paper_number, ':', a.paper_type)) paperType,
        GROUP_CONCAT(a.paper_number) paperNumber,
        GROUP_CONCAT(IFNULL(a.paper_pages_a3, 0) + IFNULL(a.card_pages_a3, 0)) singlePagesA3,
        SUM(CASE b.is_download
        WHEN true THEN 0
        ELSE 1
        END) isDownload,
        SUM(CASE b.is_try
        WHEN true THEN 0
        ELSE 1
        END) isTry,
        SUM(CASE b.is_pass
        WHEN true THEN 0
        ELSE 1
        END) isPass
        FROM
        exam_detail_course a
        LEFT JOIN client_status b ON a.school_id = b.school_id
        AND a.exam_detail_id = b.exam_detail_id
        AND a.course_code = b.course_code
        AND a.paper_number = b.paper_number
        AND b.machine_code = #{machineCode}
        GROUP BY a.exam_detail_id) c ON b.id = c.exam_detail_id
        <where>
            <if test="courseCode != null and courseCode != ''">
                and c.courseNameCode like concat('%',#{courseCode},'%')
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and c.paperNumber like concat('%',#{paperNumber},'%')
            </if>
            <if test="isDownload != null">
                <if test="isDownload == true">
                    and c.isDownload = 0
                </if>
                <if test="isDownload == false">
                    and c.isDownload > 0
                </if>
            </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="studentCode != null and studentCode != ''">
                and c.student_code like concat('%', #{studentCode}, '%')
            </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>
        order by c.student_code
    </select>
    <select id="pagePrintSingleData" resultType="com.qmth.distributed.print.business.bean.dto.client.PrintSingleDto">
        SELECT
            b.exam_id examId,
            be.name examName,
            epp.id printPlanId,
            epp.name printPlanName,
            c.course_code courseCode,
            c.course_name courseName,
            c.paper_number paperNumber,
            group_concat(c.paper_type) paperType,
            group_concat(b.id) examDetailIds
        FROM
            exam_detail b
                JOIN
            exam_detail_course c ON b.id = c.exam_detail_id
                LEFT JOIN
            exam_print_plan epp ON b.print_plan_id = epp.id
                LEFT JOIN
            basic_exam be on b.exam_id = be.id
        <where>
            <if test="semesterId != null">
                and be.semester_id = #{semesterId}
            </if>
            <if test="examId != null">
                and be.id = #{examId}
            </if>
            <if test="printPlanId != null">
                and epp.id = #{printPlanId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and c.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and c.paper_number = #{paperNumber}
            </if>
            and b.status not in
            <foreach item="item" collection="examDetailStatus" separator="," open="(" close=")" index="">
                #{item}
            </foreach>
        </where>
        group by b.exam_id,
            be.name,
            epp.id,
            epp.name,
            c.course_code,
            c.course_name,
            c.paper_number
    </select>
</mapper>