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

    <select id="list" resultType="com.qmth.distributed.print.business.bean.dto.TCStatisticsDto">
        select
        t.id,
        t.collegeId,
        t.collegeName,
        t.teachingRoomId,
        t.teachingRoomName,
        t.courseName,
        t.courseCode,
        t.teacherName,
        t.clazzId,
        t.clazzName,
        IF(t.examStudentCount = 0,null,t.paperNumber) as paperNumber,
        t.printPlanId,
        t.createId,
        IF(t.examStudentCount = 0,null,t.printPlanName) as printPlanName,
        t.examStudentCount,
        IF(t.examStudentCount = 0,null,t.examStudentCount + (t.drawCount * t.backupCount)) as printSum,
        IF(t.examStudentCount = 0,null,t.status) as status,
        t.batchNo
        from
        (
        select
        distinct tcs.id,
        tcs.college_id as collegeId,
        tcs.college_name as collegeName,
        tcs.course_name as courseName,
        tcs.course_code as courseCode,
        tcs.teaching_room_id as teachingRoomId,
        tcs.teaching_room_name as teachingRoomName,
        tcs.teacher_name as teacherName,
        tcs.clazz_id as clazzId,
        tcs.clazz_name as clazzName,
        tcs.batch_no as batchNo,
        tcs.paper_number as paperNumber,
        tcs.create_id as createId,
        epp.id as printPlanId,
        epp.name as printPlanName,
        epp.exam_id as examId,
        be.semester_id as semesterId,
        (
        select
        etd.draw_count
        from
        exam_task_detail etd
        where
        FIND_IN_SET(etd.id, tcs.exam_task_detail_ids)) as drawCount,
        IFNULL(epp.backup_count, 0) as backupCount,
        IFNULL((
        select
        count(1)
        from
        exam_student es
        where
        FIND_IN_SET(es.exam_detail_course_id, tcs.exam_detail_course_ids)
        and es.clazz_id = tcs.clazz_id
        and es.school_id = #{schoolId}),0) as examStudentCount,
        case
        when ed.status is null then 'UN_JOIN'
        when ed.status = 'FINISH' then 'FINISH'
        else 'UN_FINISH'
        end status
        from
        t_c_statistics tcs
        left join exam_print_plan epp on
        epp.id = tcs.print_plan_id
        left join basic_exam be on epp.exam_id = be.id
        left join exam_detail ed on
        ed.id = tcs.exam_detail_id and ed.print_plan_id = epp.id
        where 1 = 1
            and IF(epp.id is null, true,
        <if test="schoolId != null and schoolId != ''">
             epp.school_id = #{schoolId}
        </if>)) t
        <where>
            <if test="userId != null and userId != ''">
                and t.createId = #{userId}
            </if>
            <if test="collegeId != null and collegeId != ''">
                and t.collegeId = #{collegeId}
            </if>
            <if test="semesterId != null and semesterId != ''">
                and t.semesterId = #{semesterId}
            </if>
            <if test="examId != null and examId != ''">
                and t.examId = #{examId}
            </if>
            <if test="teachingRoomId != null and teachingRoomId != ''">
                and t.teachingRoomId = #{teachingRoomId}
            </if>
            <if test="status != null and status != ''">
                and t.status = #{status}
            </if>
            <if test="courseName != null and courseName != ''">
                and t.courseName like concat('%',#{courseName},'%')
            </if>
            <if test="teacherName != null and teacherName != ''">
                and t.teacherName like concat('%',#{teacherName},'%')
            </if>
        </where>
        order by t.id
    </select>

    <select id="findByBatchNoCount" resultType="com.qmth.distributed.print.business.bean.dto.TCStatisticsDto">
        select
            t.collegeId,
            t.collegeName,
            t.courseName,
            t.courseCode,
            t.teachingRoomId,
            t.teachingRoomName,
            t.teacherName,
            t.clazzId,
            t.clazzName,
            t.paperNumber,
            t.printPlanId,
            t.printPlanName,
            t.batchNo,
            t.status,
            case
                when sum(t.examStudentCount) = 0 then 0
                else sum(t.examStudentCount) + (t.drawCount * t.backupCount)
            end printSum,
            t.examDetailId,
            GROUP_CONCAT(distinct t.examDetailCourseId) as examDetailCourseIds,
            GROUP_CONCAT(distinct t.examTaskDetailId) as examTaskDetailIds
        from
            (
            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,
                tcst.batch_no as batchNo,
                et.paper_number as paperNumber,
                edc.id as examDetailCourseId,
                etd.id as examTaskDetailId,
                epp.id as printPlanId,
                epp.name as printPlanName,
                ed.id as examDetailId,
                IFNULL(etd.draw_count, 0) as drawCount,
                IFNULL((
                select
                    count(1)
                from
                    exam_student es
                where
                    es.exam_detail_course_id = edc.id
                    and es.clazz_id = tcst.clazz_id
                    and es.school_id = #{schoolId}),0) as examStudentCount,
                IFNULL(epp.backup_count, 0) as backupCount,
                case
                    when ed.status is null then 'UN_JOIN'
                    when ed.status = 'FINISH' then 'FINISH'
                    else 'UN_FINISH'
                end status
            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>
                <if test="orgIds != null and orgIds != '' and orgIds.size() > 0">
                    and epp.org_id IN
                    <foreach collection="orgIds" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </where>)) t
        group by
            t.collegeId,
            t.collegeName,
            t.courseName,
            t.courseCode,
            t.teachingRoomId,
            t.teachingRoomName,
            t.teacherName,
            t.clazzId,
            t.clazzName,
            t.paperNumber,
            t.printPlanId,
            t.printPlanName,
            t.status,
            t.drawCount,
            t.batchNo,
            t.examDetailId
        order by
            t.printPlanId,
            t.clazzId
    </select>

    <select id="findByBatchNoCountJoin" resultType="com.qmth.distributed.print.business.bean.dto.TCStatisticsDto">
        select
        t.collegeId,
        t.collegeName,
        t.courseName,
        t.courseCode,
        t.teachingRoomId,
        t.teachingRoomName,
        t.teacherName,
        t.clazzId,
        t.clazzName,
        t.paperNumber,
        t.printPlanId,
        t.printPlanName,
        t.batchNo,
        t.status,
        case
        when sum(t.examStudentCount) = 0 then 0
        else sum(t.examStudentCount) + (t.drawCount * t.backupCount)
        end printSum,
        t.examDetailId,
        GROUP_CONCAT(distinct t.examDetailCourseId) as examDetailCourseIds,
        GROUP_CONCAT(distinct t.examTaskDetailId) as examTaskDetailIds
        from
        (
        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,
        tcst.batch_no as batchNo,
        et.paper_number as paperNumber,
        edc.id as examDetailCourseId,
        etd.id as examTaskDetailId,
        epp.id as printPlanId,
        epp.name as printPlanName,
        ed.id as examDetailId,
        IFNULL(etd.draw_count, 0) as drawCount,
        IFNULL((
        select
        count(1)
        from
        exam_student es
        where
        es.exam_detail_course_id = edc.id
        and es.clazz_id = tcst.clazz_id
        and es.school_id = #{schoolId}),0) as examStudentCount,
        IFNULL(epp.backup_count, 0) as backupCount,
        case
        when ed.status is null then 'UN_JOIN'
        when ed.status = 'FINISH' then 'FINISH'
        else 'UN_FINISH'
        end status
        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>
            <if test="orgIds != null and orgIds != '' and orgIds.size() > 0">
                and epp.org_id IN
                <foreach collection="orgIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>)) t
        group by
        t.collegeId,
        t.collegeName,
        t.courseName,
        t.courseCode,
        t.teachingRoomId,
        t.teachingRoomName,
        t.teacherName,
        t.clazzId,
        t.clazzName,
        t.paperNumber,
        t.printPlanId,
        t.printPlanName,
        t.status,
        t.drawCount,
        t.batchNo,
        t.examDetailId
        order by
        t.printPlanId,
        t.clazzId
    </select>
    <select id="findByExamIdCourseCode"
            resultType="com.qmth.distributed.print.business.bean.dto.ExamTaskPlanStatisticDto">
        SELECT
            epp.exam_id as examId,
            epp.id as examPrintPlanId,
            epp.backup_method as backupMethod,
            epp.backup_count as backupCount,
            epp.name as examPrintPlanName,
            ed.id as examDetailId,
            ed.status as examDetailStatus,
            edc.id as examDetailCourseId,
            edc.paper_number as paperNumber,
            edc.course_code as courseCode,
            edc.course_name as courseName,
            edc.clazz_id as clazzIds,
            edc.total_subjects as studentCount
        FROM
            exam_detail_course edc
                LEFT JOIN
            exam_detail ed ON edc.exam_detail_id = ed.id
                LEFT JOIN
            exam_print_plan epp ON ed.print_plan_id = epp.id
        <where>
            <if test="examId != null and examId != ''">
                AND epp.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                AND edc.course_code = #{courseCode}
            </if>
        </where>
    </select>
    <select id="findExamTaskPrintStatistic"
            resultType="com.qmth.distributed.print.business.bean.result.TCStatisticResult">
        SELECT
            tcs.id AS id,
            tcs.exam_id AS examId,
            be.name AS examName,
            tcs.college_name AS collegeName,
            tcs.teaching_room_name AS teachingRoom,
            tcs.course_code AS courseCode,
            tcs.course_name AS courseName,
            tcs.teacher_name AS teacher,
            tcs.clazz_name AS clazzNames,
            tcs.paper_number AS paperNumber,
            epp.name AS examPrintPlanName,
            tcs.print_sum AS printCount,
            tcs.status AS status,
            tcs.clazz_content AS clazzContent
        FROM
            t_c_statistics tcs
                LEFT JOIN
            basic_exam be ON tcs.exam_id = be.id
                LEFT JOIN
            exam_print_plan epp ON tcs.print_plan_id = epp.id
                LEFT JOIN
            basic_semester bs ON be.semester_id = bs.id
        <where>
            <if test="semesterId != null and semesterId != ''">
                and bs.id = #{semesterId}
            </if>
            <if test="examId != null and examId != ''">
                and tcs.exam_id = #{examId}
            </if>
            <if test="collegeId != null and collegeId != ''">
                and tcs.college_id = #{collegeId}
            </if>
            <if test="teachingRoomId != null and teachingRoomId != ''">
                and tcs.teaching_room_id = #{teachingRoomId}
            </if>
            <if test="status != null">
                and tcs.status = #{status}
            </if>
            <if test="courseName != null and courseName != ''">
                and tcs.course_name like concat('%',#{courseName},'%')
            </if>
            <if test="teacherName != null and teacherName != ''">
                and tcs.teacher_name like concat('%',#{teacherName},'%')
            </if>
            <if test="userId != null and userId != ''">
                and tcs.create_Id = #{userId}
            </if>
        </where>
    </select>
</mapper>
