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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.distributed.print.business.entity.ExamDetailCourse">
    <result column="id" property="id" />
        <result column="school_id" property="schoolId" />
        <result column="exam_detail_id" property="examDetailId" />
        <result column="course_code" property="courseCode" />
        <result column="course_name" property="courseName" />
        <result column="paper_number" property="paperNumber" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        school_id, exam_detail_id, course_code, course_name, paper_number
    </sql>
    <select id="listClientByExamDetailIdAndStatus" resultType="java.util.Map">
        SELECT
            ed.id examDetailId,
            ed.school_id schoolId,
            b.exam_id examId,
            a.id examDetailCourseId,
            a.course_code courseCode,
            a.course_name courseName,
            a.paper_number paperNumber,
            c.paper_attachment_ids paperAttachmentIds,
            a.attachment_id cardAttachmentIds,
            b.id examTaskId,
            c.exposed_paper_type exposedPaperType,
            c.unexposed_paper_type unexposedPaperType,
            a.paper_type paperType,
            ifnull(ed.total_subjects,0) printCount,
            a.common_attachment_id commonAttachmentId
        FROM
            exam_detail ed
                LEFT JOIN
            exam_detail_course a ON ed.id = a.exam_detail_id
                LEFT JOIN
            exam_task b ON a.school_id = b.school_id
                AND ed.exam_id = b.exam_id
                AND a.course_code = b.course_code
                AND a.paper_number = b.paper_number
                LEFT JOIN
            exam_task_detail c ON b.id = c.exam_task_id
                LEFT JOIN
            t_f_flow_approve d ON b.flow_id = d.flow_id
        WHERE
            a.exam_detail_id = #{examDetailId}
            AND (b.status = 'SUBMIT' and (d.status is null or d.status = #{status}))
    </select>
    <select id="listByExamDetailIdAndStatus" resultMap="BaseResultMap">
        SELECT
            a.*
        FROM
            exam_detail_course a
                LEFT JOIN
            exam_detail ed ON a.exam_detail_id = ed.id
                LEFT JOIN
            exam_task b ON a.school_id = b.school_id
                AND ed.exam_id = b.exam_id
                AND a.course_code = b.course_code
                AND a.paper_number = b.paper_number
                LEFT JOIN
            t_f_flow_approve c ON b.flow_id = c.flow_id
        WHERE
            a.exam_detail_id = #{examDetailId} AND b.status = #{status}
            AND (c.status is not null and c.status != 'FINISH')
    </select>
    <select id="listCoursesByPrintPlanId" resultType="com.qmth.teachcloud.common.entity.BasicCourse">
        SELECT
            distinct a.course_code code, a.course_name name
        FROM
            exam_detail_course a
                LEFT JOIN
            exam_detail b ON a.exam_detail_id = b.id
        <where>
            <if test="printPlanIdList != null and printPlanIdList != '' and printPlanIdList.size() > 0">
                and b.print_plan_id IN
                <foreach collection="printPlanIdList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param != null and param != ''">
                and (a.course_code like concat('%', #{param},'%')
                or a.course_name like concat('%', #{param},'%'))
            </if>
        </where>

    </select>
    <select id="listByPrintPlanIdAndCourseCodeAndPaperNumber" resultMap="BaseResultMap">
        SELECT
            a.*
        FROM
            exam_detail_course a
                LEFT JOIN
            exam_detail b ON a.exam_detail_id = b.id
                LEFT JOIN
            exam_print_plan c ON b.print_plan_id = c.id
        WHERE
            a.school_id = #{schoolId}
                AND b.print_plan_id = #{printPlanId}
                AND a.course_code = #{courseCode}
                AND a.paper_number = #{paperNumber}
    </select>
    <select id="listExamDetailByExamIdAndPaperNumber"
            resultMap="BaseResultMap">
        SELECT
            *
        FROM
            exam_detail_course edc
        WHERE
            edc.paper_number = #{paperNumber}
          AND EXISTS( SELECT
                          1
                      FROM
                          exam_detail ed
                      WHERE
                          edc.school_id = ed.school_id
                        AND edc.exam_detail_id = ed.id
                        AND ed.school_id = #{schoolId}
                        AND ed.exam_id = #{examId})
    </select>
    <select id="getByExamDetailId" resultType="com.qmth.teachcloud.common.bean.result.TbTaskDetailResult">
        SELECT
            be.semester_id semesterId,
            ed.exam_id examId,
            GROUP_CONCAT(edc.course_code) courseCode,
            GROUP_CONCAT(edc.course_name) courseName,
            GROUP_CONCAT(concat(edc.course_name, '（',edc.course_code, '）')) courseNameCode,
            GROUP_CONCAT(edc.paper_number) paperNumber
        FROM
            exam_detail ed
                LEFT JOIN
            exam_detail_course edc ON ed.id = edc.exam_detail_id
                LEFT JOIN
            basic_exam be ON ed.exam_id = be.id
        WHERE
            ed.id = #{examDetailId}
        GROUP BY be.semester_id , ed.exam_id
    </select>
    <select id="listPaperNumberByPrintPlanId" resultType="java.lang.String">
        SELECT
        distinct a.paper_number paperNumber
        FROM
        exam_detail_course a
        LEFT JOIN
        exam_detail b ON a.exam_detail_id = b.id
        <where>
            <if test="printPlanIdList != null and printPlanIdList != '' and printPlanIdList.size() > 0">
                and b.print_plan_id IN
                <foreach collection="printPlanIdList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param != null and param != ''">
                and a.paper_number like concat('%', #{param},'%')
            </if>
            <if test="orgIds != null and orgIds != '' and orgIds.size > 0">
                AND b.org_id IN
                <foreach collection="orgIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
