<?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,
            c.id semesterId,
            c.name semesterName,
            a.name,
            a.exam_start_time AS examStartTime,
            a.exam_end_time AS examEndTime,
            a.status,
            a.category,
            (
            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.exam_id as examId,
            d.name as examName
        FROM
            exam_print_plan a
                LEFT JOIN
            sys_user b on a.create_id = b.id
                LEFT JOIN
            basic_exam d on a.exam_id = d.id
                LEFT JOIN
            basic_semester c on d.semester_id = c.id
        <where>
            and d.enable = true
            <if test="schoolId != null and schoolId != ''">
                and a.school_id = #{schoolId}
            </if>
            <if test="semesterId != null and semesterId != ''">
                and c.id = #{semesterId}
            </if>
            <if test="printPlanIdList != null and printPlanIdList != '' and 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 != ''">
                and a.create_time >= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and #{endTime} >= a.create_time
            </if>
            <if test="examId != null and examId != ''">
                and a.exam_id = #{examId}
            </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>
        ORDER BY a.create_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
                left join
            basic_exam be on a.exam_id = be.id
                left join
            basic_semester bs on be.semester_id = bs.id
        <where>
            and bs.enable = true and be.enable = true
            <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>
            </if>
            <if test="status != null and status != ''">
                AND a.status IN
                <foreach collection="status" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </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>
    </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(distinct c.course_name_code) courseNameCode,
            GROUP_CONCAT(distinct 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.paper_pages
            END) paperPagesLeft,
            SUM(CASE b.status
                WHEN 'FINISH' THEN 0
                ELSE b.card_pages
            END) cardPagesLeft,
            SUM(CASE b.status
                WHEN 'FINISH' THEN 0
                ELSE b.pages_a4
            END) pagesA4Left,
            ROUND(AVG(IFNULL(b.print_progress, 0)), 0) progress
        FROM
            (select epp.id, epp.name 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>
                <if test="printPlanId != null and printPlanId != ''">
                    and epp.id = #{printPlanId}
                </if>
            </where>) a
                JOIN
            exam_detail b ON a.id = b.print_plan_id
                JOIN
            (SELECT
                exam_detail_id,
                    GROUP_CONCAT(distinct CONCAT(a.course_name, '(', a.course_code, ')')) course_name_code,
                    GROUP_CONCAT(distinct 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
        <where>
            AND b.status not IN
            <foreach collection="examDetailStatus" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
            <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} or b.print_house_id is null)
            </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.paper_pages,0))/2 paperPages,
            SUM(ifnull(b.card_pages,0))/2 cardPages,
            SUM(ifnull(b.pages_a4,0))/2 pagesA4,
            SUM(CASE b.status
                WHEN 'FINISH' THEN 0
                ELSE ifnull(b.paper_pages, 0)
            END)/2 paperPagesLeft,
            SUM(CASE b.status
                WHEN 'FINISH' THEN 0
                ELSE ifnull(b.card_pages, 0)
            END)/2 cardPagesLeft,
            SUM(CASE b.status
                WHEN 'FINISH' THEN 0
                ELSE ifnull(b.pages_a4,0)
            END)/2 pagesA4Left
        FROM
            (select epp.id, epp.name 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>
                <if test="printPlanId != null">
                    and epp.id = #{printPlanId}
                </if>
            </where>) 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
        <where>
            AND b.status not IN
            <foreach collection="examDetailStatus" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
            <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} or b.print_house_id is null)
            </if>
        </where>
    </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} or c.print_house_id is null)
            </if>
        </where>
    </select>
</mapper>
