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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.distributed.print.business.entity.ExamCard">
        <result column="id" property="id"/>
        <result column="school_id" property="schoolId"/>
        <result column="org_id" property="orgId"/>
        <result column="course_code" property="courseCode"/>
        <result column="course_name" property="courseName"/>
        <result column="title" property="title"/>
        <result column="make_method" property="makeMethod"/>
        <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"/>
        <result column="type" property="type"/>
        <result column="create_method" property="createMethod"/>
        <result column="attachment_id" property="attachmentId"/>
        <result column="content" property="content"/>
        <result column="html_content" property="htmlContent"/>
        <result column="backup_attachment" property="backupAttachment"/>
        <result column="jpg_attachment" property="jpgAttachment"/>
        <result column="enable" property="enable"/>
        <result column="used" property="used"/>
        <result column="remark" property="remark"/>
        <result column="card_rule_id" property="cardRuleId"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
       select id, school_id,org_id, course_code, course_name, title, make_method, status, type, create_method, content,html_content, backup_attachment, jpg_attachment,attachment_id, enable, used, remark, card_rule_id, create_id, create_time, update_id, update_time from exam_card
    </sql>
    <select id="listGenericCard" resultType="com.qmth.distributed.print.business.entity.ExamCard">
        SELECT
            distinct a.id, a.title, a.type, a.make_method makeMethod, a.create_method createMethod
        FROM
            exam_card a
        WHERE
            a.school_id = #{schoolId}
            AND a.type = #{type}
            AND a.enable = TRUE
              <if test="cardRuleId != null and cardRuleId != ''">
                  <if test="cardRuleId == '-1'">
                      AND a.create_method != 'STANDARD'
                  </if>
                  <if test="cardRuleId != '-1'">
                      AND ((a.create_method = 'STANDARD' AND a.card_rule_id = #{cardRuleId}) or a.create_method != 'STANDARD')
                  </if>
              </if>
        order by a.id desc
    </select>
    <select id="listCustom" resultType="com.qmth.distributed.print.business.entity.ExamCard">
        SELECT
            a.id, a.title, a.type, a.create_id createId, a.make_method makeMethod, a.create_method createMethod, a.used
        FROM
            exam_card a
        WHERE
            a.school_id = #{schoolId}
            AND a.course_code = #{courseCode}
            AND a.type = #{type}
            AND a.card_rule_id = #{cardRuleId}
            AND a.create_time >= 1680624000000
            AND a.enable = true
        order by a.create_time desc
    </select>
    <select id="listPage" resultMap="BaseResultMap">
        <include refid="Base_Column_List"></include>
        <where>
            school_id = #{schoolId}
            <if test="semesterId != null || examId != null">
               AND EXISTS( SELECT
                    1
                FROM
                (SELECT
                REPLACE(substring_index(substring_index(CONVERT((etd.paper_attachment_ids ->>'$[*].cardId')
                USING utf8), ']', 1), '[',-1),'"','') as cardId
                FROM
                exam_task et
                LEFT JOIN exam_task_detail etd ON et.id = etd.exam_task_id
                LEFT JOIN basic_exam be ON et.exam_id = be.id
                <where>
                    <if test="semesterId != null">
                       AND be.semester_id = #{semesterId}
                    </if>
                    <if test="examId != null">
                        AND et.exam_id = #{examId}
                    </if>
                    <if test="courseCode != null">
                        AND et.course_code = #{courseCode}
                    </if>
                    <if test="paperNumber != null">
                        AND et.paper_number = #{paperNumber}
                    </if>
                </where>
                ) t
                WHERE LOCATE(id,t.cardId) > 0)
            </if>
            <if test="cardType != null and cardType != ''">
                and type = #{cardType}
            </if>
            <if test="title != null and title != ''">
                and title like concat('%', #{title}, '%')
            </if>
            <if test="createMethod != null and createMethod != ''">
                and create_method = #{createMethod}
            </if>
            <if test="enable != null and enable != ''">
                and enable = #{enable}
            </if>
            <if test="createStartTime != null and createStartTime != ''">
                and create_time &gt;= #{createStartTime}
            </if>
            <if test="createEndTime != null and createEndTime != ''">
                and create_time &lt;= #{createEndTime}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        order by create_time desc
    </select>
    <select id="listCustomByExamStage" resultType="com.qmth.distributed.print.business.entity.ExamCard">
        SELECT
            a.id, a.title, a.type, a.create_id createId, a.make_method makeMethod, a.create_method createMethod
        FROM
            exam_card a
        WHERE
            a.school_id = #{schoolId}
          AND a.type = #{type}
          AND a.course_code = #{courseCode}
          AND EXISTS ( SELECT
                              1
                          FROM
                              exam_task_detail b
                                  LEFT JOIN exam_task et on b.exam_task_id = et.id
                          WHERE
                                et.exam_id = #{examId}
                              and et.paper_number = #{paperNumber}
                            and FIND_IN_SET(a.id, REPLACE(REPLACE(substring_index(substring_index(CONVERT((b.paper_attachment_ids ->>'$[*].cardId') USING utf8), ']', 1), '[',-1),' "','"'),'"','')))
        order by a.create_time desc
    </select>
</mapper>
