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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.distributed.print.business.entity.ExamPrintPlan">
    <result column="id" property="id" />
        <result column="school_id" property="schoolId" />
        <result column="name" property="name" />
        <result column="exam_start_time" property="examStartTime" />
        <result column="exam_end_time" property="examEndTime" />
        <result column="print_content" property="printContent" />
        <result column="backup_method" property="backupMethod" />
        <result column="backup_count" property="backupCount" />
        <result column="draw_rule" property="drawRule" />
        <result column="variable_content" property="variableContent" />
        <result column="ordinary_content" property="ordinaryContent" />
        <result column="status" property="status" />
        <result column="create_id" property="createId" />
        <result column="create_time" property="createTime" />
        <result column="update_id" property="updateId" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        school_id, name, exam_start_time, exam_end_time, print_content, backup_method, backup_count, draw_rule, variable_content, ordinary_content, status, create_id, create_time, update_id, update_time
    </sql>

    <select id="findPrintPlanPage"
            resultType="com.qmth.distributed.print.business.bean.result.PrintPlanResult">
        SELECT
            a.id,
            a.name,
            a.exam_start_time AS examStartTime,
            a.exam_end_time AS examEndTime,
            a.status,
            (
            SELECT
                COUNT(
                DISTINCT ( course_code ))
                FROM
                exam_detail_course
            WHERE
            exam_detail_id IN ( SELECT id FROM exam_detail WHERE print_plan_id = a.id )) AS totalGates,
            a.print_content AS printContent,
            a.backup_method AS backupMethod,
            a.backup_count AS backupCount,
            a.draw_rule AS drawRule,
            a.variable_content AS variableContentTemp,
            a.ordinary_content AS ordinaryContentTemp,
            a.create_id AS createId,
            a.create_time AS createTime,
            b.real_name AS createName,
            a.third_relate_id thirdRelateId,
            a.third_relate_name thirdRelateName,
            a.sync_status syncStatus
        FROM
            exam_print_plan a
                LEFT JOIN
            sys_user b on a.create_id = b.id
        <where>
            <if test="schoolId != null and schoolId > 0">
                and a.school_id = #{schoolId}
            </if>
            <if test="printPlanIdList.size() > 0">
                and a.id IN
                <foreach collection="printPlanIdList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="status != null">
                and a.status = #{status}
            </if>
            <if test="startTime != null and startTime > 0">
                and a.create_time >= #{startTime}
            </if>
            <if test="endTime != null and endTime > 0">
                and #{endTime} >= a.create_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>
        ORDER BY a.update_time DESC
    </select>
    <select id="findPrintPlanSyncPage"
            resultType="com.qmth.distributed.print.business.bean.result.PrintPlanResult">
        SELECT
        a.id,
        a.name,
        a.exam_start_time AS examStartTime,
        a.exam_end_time AS examEndTime,
        a.status,
        (
        SELECT
        COUNT(
        DISTINCT ( course_code ))
        FROM
        exam_detail_course
        WHERE
        exam_detail_id IN ( SELECT id FROM exam_detail WHERE print_plan_id = a.id )) AS totalGates,
        a.print_content AS printContent,
        a.backup_method AS backupMethod,
        a.backup_count AS backupCount,
        a.draw_rule AS drawRule,
        a.variable_content AS variableContentTemp,
        a.ordinary_content AS ordinaryContentTemp,
        a.create_id AS createId,
        a.create_time AS createTime,
        b.real_name AS createName,
        a.third_relate_id thirdRelateId,
        a.third_relate_name thirdRelateName,
        a.sync_status syncStatus
        FROM
        exam_print_plan a
        LEFT JOIN
        sys_user b on a.create_id = b.id
        <where>
            AND a.status IN
            <foreach collection="status" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
            <if test="schoolId != null and schoolId > 0">
                and a.school_id = #{schoolId}
            </if>
            <if test="printPlanId!= null and printPlanId != ''">
                and a.id = #{printPlanId}
            </if>
            <if test="startTime != null and startTime > 0">
                and a.create_time >= #{startTime}
            </if>
            <if test="endTime != null and endTime > 0">
                and #{endTime} >= a.create_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>
        ORDER BY a.update_time DESC
    </select>
    <select id="list" resultType="com.qmth.distributed.print.business.bean.result.PrintPlanBrief">
        select a.id, a.name, a.status from exam_print_plan a left join sys_user b on a.create_id = b.id
        <where>
            <if test="schoolId != null and schoolId != ''">
                and a.school_id = #{schoolId}
            </if>
            <if test="source == 'client'">
                <if test="module == 'try'">
                    AND LOCATE('PAPER', print_content) > 0
                </if>
                AND a.status IN
                <foreach collection="status" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </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="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,
            b.exam_place examPlace,
            b.exam_room examRoom,
            b.total_subjects totalSubjects,
            IFNULL(b.pages_a3, 0) pagesA3,
            IFNULL(b.pages_a4, 0) pagesA4,
            case b.status when 'FINISH' then IFNULL(b.pages_a3, 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,
            e.real_name printUserName,
            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
            exam_print_plan a
                JOIN
            exam_detail b ON a.id = b.print_plan_id
                JOIN
            (SELECT
                    a.exam_detail_id,
                    c.print_plan_id,
                    GROUP_CONCAT(CONCAT(a.course_name, '(', a.course_code, ')')) courseNameCode,
                    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 exam_detail c ON a.exam_detail_id = c.id
            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.print_plan_id) c ON a.id = c.print_plan_id and b.id = c.exam_detail_id
                LEFT JOIN
            sys_user e ON a.school_id = e.school_id and b.print_user = e.login_name
                LEFT JOIN
            sys_user d ON a.create_id = d.id
        <where>
            and a.school_id = #{schoolId}
            AND a.status != 'END'
            AND b.status not IN
            <foreach collection="examDetailStatus" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
            <if test="printPlanId != null and printPlanId != ''">
                and a.id = #{printPlanId}
            </if>
            <if test="status != null and status != ''">
                and b.status = #{status}
            </if>
            <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="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="validate != null">
                and ifnull(b.validate, false) = #{validate}
            </if>
            <if test="isDownload != null">
                <if test="isDownload == true">
                    and c.isDownload = 0
                </if>
                <if test="isDownload == false">
                    and c.isDownload > 0
                </if>
            </if>
            <if test="orgId != null">
                AND b.print_house_id = #{orgId}
            </if>
        </where>
    </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.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
            (SELECT
                a.exam_detail_id,
                c.print_plan_id,
                GROUP_CONCAT(CONCAT(a.course_name, '(', a.course_code, ')')) courseNameCode,
                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 exam_detail c ON a.exam_detail_id = c.id
                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.print_plan_id) c ON a.id = c.print_plan_id and b.id = c.exam_detail_id
            LEFT JOIN
        sys_user e ON a.school_id = e.school_id and b.print_user = e.login_name
            LEFT JOIN
        sys_user d ON a.create_id = d.id
        <where>
            and a.school_id = #{schoolId}
            AND a.status != 'END'
            AND b.status not IN
            <foreach collection="examDetailStatus" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
            <if test="printPlanId != null and printPlanId != ''">
                and a.id = #{printPlanId}
            </if>
            <if test="status != null and status != ''">
                and b.status = #{status}
            </if>
            <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="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="validate != null">
                and ifnull(b.validate, false) = #{validate}
            </if>
            <if test="isDownload != null">
                <if test="isDownload == true">
                    and c.isDownload = 0
                </if>
                <if test="isDownload == false">
                    and c.isDownload > 0
                </if>
            </if>
            <if test="orgId != null">
                AND b.print_house_id = #{orgId}
            </if>
        </where>
    </select>
    <select id="listClientPrintStatistics"
            resultType="com.qmth.distributed.print.business.bean.dto.ClientPrintStatisticsDto">
        SELECT
            a.id printPlanId,
            a.name printPlanName,
            b.exam_start_time examStartTime,
            b.exam_end_time examEndTime,
            b.exam_place examPlace,
            COUNT(b.package_code) packageCount,
            GROUP_CONCAT(c.course_name_code) courseNameCode,
            GROUP_CONCAT(c.paper_number) paperNumber,
            SUM(b.total_subjects) totalSubjects,
            SUM(CASE b.status
                WHEN 'FINISH' THEN 0
                ELSE b.total_subjects
            END) totalSubjectsLeft,
            SUM(CASE b.status
                WHEN 'FINISH' THEN 0
                ELSE b.pages_a3
            END) pagesA3Left,
            SUM(CASE b.status
                WHEN 'FINISH' THEN 0
                ELSE b.pages_a4
            END) pagesA4Left,
            ROUND(AVG(IFNULL(b.print_progress, 0)), 0) progress
        FROM
            exam_print_plan a
                JOIN
            exam_detail b ON a.id = b.print_plan_id
                JOIN
            (SELECT
                exam_detail_id,
                    GROUP_CONCAT(CONCAT(a.course_name, '(', a.course_code, ')')) course_name_code,
                    GROUP_CONCAT(a.paper_number) paper_number
                FROM
                    exam_detail_course a
                    <where>
                        <if test="courseCode != null and courseCode != ''">
                            and a.course_code = #{courseCode}
                        </if>
                        <if test="paperNumber != null and paperNumber != ''">
                            and a.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>
            and a.school_id = #{schoolId}
            AND a.status != 'END'
            AND b.status not IN
            <foreach collection="examDetailStatus" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
            <if test="printPlanId != null and printPlanId != ''">
                and a.id = #{printPlanId}
            </if>
            <if test="examPlace != null and examPlace != ''">
                and b.exam_place = #{examPlace}
            </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="orgId != null">
                AND b.print_house_id = #{orgId}
            </if>
            </where>
        GROUP BY a.id , a.name , b.exam_start_time , b.exam_end_time , b.exam_place
    </select>
    <select id="clientStatisticsTotalData"
            resultType="com.qmth.distributed.print.business.bean.dto.ClientPrintStatisticsTotalDto">
        SELECT
            COUNT(b.package_code) packageCount,
            GROUP_CONCAT(c.paper_type) paperNumberAndType,
            SUM(b.total_subjects) totalSubjects,
            SUM(ifnull(b.pages_a3,0))/2 pagesA3,
            SUM(ifnull(b.pages_a4,0))/2 pagesA4,
            SUM(CASE b.status
                WHEN 'FINISH' THEN 0
                ELSE ifnull(b.pages_a3, 0)
            END)/2 pagesA3Left,
            SUM(CASE b.status
                WHEN 'FINISH' THEN 0
                ELSE ifnull(b.pages_a4,0)
            END)/2 pagesA4Left
        FROM
            exam_print_plan a
        JOIN
            exam_detail b ON a.id = b.print_plan_id
        JOIN
        (SELECT
            exam_detail_id,
                GROUP_CONCAT(CONCAT(a.paper_number, ':', a.paper_type)) paper_type,
                GROUP_CONCAT(CONCAT(a.course_name, '(', a.course_code, ')')) course_name_code,
                GROUP_CONCAT(a.paper_number) paper_number
        FROM
            exam_detail_course a
        <where>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and a.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>
            and a.school_id = #{schoolId}
            AND a.status != 'END'
            AND b.status not IN
            <foreach collection="examDetailStatus" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
            <if test="printPlanId != null and printPlanId != ''">
                and a.id = #{printPlanId}
            </if>
            <if test="examPlace != null and examPlace != ''">
                and b.exam_place = #{examPlace}
            </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="orgId != null">
                AND b.print_house_id = #{orgId}
            </if>
        </where>
    </select>
    <select id="listThirdObject" resultType="com.qmth.distributed.print.business.bean.dto.ThirdObjectDto">
        SELECT
            distinct
            third_relate_id thirdRelateId,
            third_relate_name thirdRelateName
        FROM
            exam_print_plan
        WHERE
            school_id = #{schoolId}
            and third_relate_id is not null
        ORDER BY third_relate_id DESC
    </select>
    <select id="listToClient" resultType="com.qmth.distributed.print.business.bean.result.PrintPlanBrief">
        SELECT DISTINCT
            a.id, a.name, a.status
        FROM
            exam_print_plan a
                LEFT JOIN
        exam_detail c ON a.id = c.print_plan_id
                LEFT JOIN
            sys_user b ON a.create_id = b.id
        <where>
            <if test="schoolId != null and schoolId != ''">
                and a.school_id = #{schoolId}
            </if>
            <if test="module == 'try'">
                AND LOCATE('PAPER', print_content) > 0
            </if>
            AND a.status IN
            <foreach collection="status" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
            <if test="orgId != null">
                AND c.print_house_id = #{orgId}
            </if>
        </where>
    </select>
</mapper>
