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

    <select id="findByBatchNoJoin" resultType="com.qmth.distributed.print.business.entity.TCStatisticsTemp">
        select distinct
        tcst.college_id as collegeId,
        tcst.college_name as collegeName,
        tcst.course_name as courseName,
        tcst.course_code as courseCode,
        tcst.teaching_room_id as teachingRoomId,
        tcst.teaching_room_name as teachingRoomName,
        tcst.teacher_name as teacherName,
        tcst.clazz_id as clazzId,
        tcst.clazz_name as clazzName,
        et.paper_number as paperNumber,
        epp.id as printPlanId,
        edc.id as examDetailCourseId,
        etd.id as examTaskDetailId,
        ed.id as examDetailId,
        'JOIN' as dataSource
        from
        t_c_statistics_temp tcst
        INNER JOIN
        exam_detail_course edc ON edc.course_code = tcst.course_code
        AND edc.clazz_id = tcst.clazz_id
        LEFT JOIN
        exam_detail ed ON ed.id = edc.exam_detail_id
        LEFT JOIN
        exam_print_plan epp ON epp.id = ed.print_plan_id
        LEFT JOIN
        exam_task et ON et.course_code = tcst.course_code
        AND edc.paper_number = et.paper_number
        AND epp.exam_id = et.exam_id
        AND et.teaching_room_id = tcst.teaching_room_id
        LEFT JOIN
        exam_task_detail etd ON etd.exam_task_id = et.id
        <where>
            <if test="batchNo != null and batchNo != ''">
                and tcst.batch_no = #{batchNo}
            </if>
            and IF(epp.id is null, true,
            et.enable = true
            and etd.enable = true
            <if test="schoolId != null and schoolId != ''">
                and et.school_id = #{schoolId}
            </if>)
        </where>
    </select>
</mapper>
