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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.distributed.print.business.entity.ExamTaskDetail">
    <result column="id" property="id" />
        <result column="exam_task_id" property="examTaskId" />
        <result column="paper_type" property="paperType" />
        <result column="paper_attachment_ids" property="paperAttachmentIds" />
        <result column="paper_confirm_attachment_ids" property="paperConfirmAttachmentIds" />
        <result column="remark" property="remark" />
        <result column="exposed_paper_type" property="exposedPaperType" />
        <result column="relate_paper_type" property="relatePaperType" />
        <result column="unexposed_paper_type" property="unexposedPaperType" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
       select id,exam_task_id, paper_type, paper_attachment_ids, paper_confirm_attachment_ids, remark, exposed_paper_type, relate_paper_type, unexposed_paper_type from exam_task_detail
    </sql>
    <select id="listPage" resultType="com.qmth.distributed.print.business.bean.dto.ExamTaskDetailDto">
        SELECT
            c.id,
            a.school_id schoolId,
            a.print_plan_id printPlanId,
            c.course_code courseCode,
            c.course_name courseName,
            c.paper_number paperNumber,
            c.card_rule_id cardRuleId,
            c.specialty,
            CASE c.card_rule_id
                WHEN - 1 THEN '全部通卡'
                ELSE e.name
            END cardRuleName,
            c.user_id userId,
            f.real_name userName,
            d.exposed_paper_type exposedPaperType,
            d.unexposed_paper_type unexposedPaperType,
            a.paper_type relatePaperType,
            a.total_subjects totalSubjects,
            a.semester_id AS semesterId,
            a.semester_name AS semesterName,
            a.exam_id AS examId,
            a.exam_name AS examName
        FROM
        (SELECT
            a.school_id,
            a.exam_id,
            a.exam_name,
            a.semester_id,
            a.semester_name,
            a.print_plan_id,
            b.paper_number,
            b.course_code,
            b.course_name,
            b.paper_type,
            SUM(b.total_subjects) total_subjects
        FROM
            (select ed.id, ed.school_id, ed.exam_id, be.name exam_name, ed.print_plan_id, bs.id semester_id, bs.name semester_name  from exam_detail ed
                LEFT JOIN
                basic_exam be ON be.id = ed.exam_id
                LEFT JOIN
                basic_semester bs ON be.semester_id = bs.id
            <where>
                <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>
            </where>) a
            LEFT JOIN exam_detail_course b ON a.id = b.exam_detail_id
        <where>
            <if test="relateType != null and relateType != ''">
                <if test="relateType == 'RELATE'">
                    and b.paper_type is not null
                </if>
                <if test="relateType == 'NOT_RELATE'">
                    and b.paper_type is null
                </if>
            </if>
        </where>
        GROUP BY a.school_id , a.exam_id, a.exam_name, a.print_plan_id, a.semester_id, a.semester_name, b.paper_number , b.course_code , b.course_name , b.paper_type) a
            LEFT JOIN
        exam_task c ON a.school_id = c.school_id
            AND a.exam_id = c.exam_id
            AND a.paper_number = c.paper_number
            LEFT JOIN
        exam_task_detail d ON c.id = d.exam_task_id
            LEFT JOIN
        basic_card_rule e ON c.card_rule_id = e.id
            LEFT JOIN
        sys_user f ON c.user_id = f.id
            LEFT JOIN
        sys_user g ON c.create_id = g.id
        <where>
            <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="cardRuleId != null and cardRuleId != ''">
                and c.card_rule_id = #{cardRuleId}
            </if>
            <if test="userName != null and userName != ''">
                and f.real_name like concat('%',#{userName},'%')
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND c.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND c.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        order by c.id desc
    </select>

    <select id="listExamDetailCourse" resultType="java.lang.String">
        SELECT
            b.id
        FROM
            exam_detail a
            LEFT JOIN exam_detail_course b ON a.id = b.exam_detail_id
            LEFT JOIN
            exam_task c ON a.school_id = c.school_id
                AND a.exam_id = c.exam_id
                AND b.course_code = c.course_code
                AND b.paper_number = c.paper_number
            LEFT JOIN
            sys_user g ON c.create_id = g.id
        <where>
            <if test="schoolId != null and schoolId != ''">
                and a.school_id = #{schoolId}
            </if>
            <if test="paperType != null">
                and b.paper_type = #{paperType}
            </if>
            <if test="paperType == null">
                and b.paper_type is null
            </if>
            <if test="printPlanId != null and printPlanId != ''">
                and a.print_plan_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>
        </where>
    </select>

    <select id="getExamTaskDetailByCardIdJson" resultType="com.qmth.distributed.print.business.entity.ExamTaskDetail">
        select
            *
        from
            (
                select
                 REPLACE(substring_index(substring_index(CONVERT((etd.paper_attachment_ids ->>'$[*].cardId')
                    USING utf8), ']', 1), '[',-1),'"','') as cardId,
                    etd.*
                from
                    exam_task_detail etd) t
        <where> 1 = 1
            <if test="cardId != null and cardId != ''">
                and FIND_IN_SET(#{cardId}, t.cardId)
            </if>
        </where>
    </select>

    <select id="findPdfDownload" resultType="com.qmth.distributed.print.business.bean.dto.ExamTaskDetailPdfDownloadDto">
        select
            etd.*,
            bs.name as semesterName,
            be.name as examName,
            GROUP_CONCAT(CONCAT(et.course_name, '(', et.course_code, ')')) as courseNameCode,
            GROUP_CONCAT(et.paper_number) paperNumber
        from
            exam_task_detail etd
                join exam_task et on
                et.id = etd.exam_task_id
                join basic_exam be on
                be.id = et.exam_id
                join basic_semester bs on
                bs.id = be.semester_id
        <where>
            <if test="examTaskId != null and examTaskId != ''">
                etd.exam_task_id = #{examTaskId}
            </if>
        </where>
        GROUP BY
            etd.id
    </select>
</mapper>
