<?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.teachcloud.report.business.mapper.TAExamCourseRecordMapper">

    <select id="listExamCourseRecord"
            resultType="com.qmth.teachcloud.report.business.entity.TAExamCourseRecord">
        SELECT
        a.assigned_score assignedScore,
        a.student_code studentCode,
        a.student_current studentCurrent,
        a.absent
        FROM
        t_a_exam_course_record a
        LEFT JOIN
        t_b_exam b ON a.exam_id = b.id
        <where>
            and b.school_id = #{schoolId}
            <if test="semester != null and semester != ''">
                AND b.semester = #{semester}
            </if>
            <if test="examId != null and examId != ''">
                AND a.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                AND a.course_code = #{courseCode}
            </if>
        </where>
    </select>

    <select id="getStudent" resultType="com.qmth.teachcloud.report.business.bean.result.ExamStudentResult">
        SELECT
        a.rank_level level,
        c.name collegeName,
        d.name courseName,
        b.exam_name examName,
        e.name examStudentName,
        a.student_code studentCode,
        b.exam_time time,
        f.clazz_name className
        FROM
        t_a_exam_course_record a
        LEFT JOIN
        t_b_exam b ON a.exam_id = b.id
        LEFT JOIN
        sys_org c ON a.inspect_college_id = c.id
        LEFT JOIN
        basic_course d ON a.school_id = d.school_id
        AND a.course_code = d.code
        LEFT JOIN
        t_b_student e ON a.student_id = e.id
        LEFT JOIN
        t_b_school_clazz f ON a.clazz_id = f.id
        <where>
            and a.school_id = #{schoolId}
            and a.exam_id = #{examId}
            and a.student_code = #{studentCode}
            and a.course_code = #{courseCode}
        </where>
    </select>

    <select id="getSynthesisResult"
            resultType="com.qmth.teachcloud.report.business.bean.result.SynthesisResult">
        SELECT
        ROUND(a.assigned_score, 1) myScore,
        b.reality_count actualCount,
        ROUND(a.over_college_rate * 100, 1) overRate,
        ROUND(b.min_score, 1) collegeMinScore,
        ROUND(b.avg_score, 1) collegeAvgScore,
        ROUND(b.max_score, 1) collegeMaxScore,
        ROUND(c.min_score, 1) clazzMinScore,
        ROUND(c.avg_score, 1) clazzAvgScore,
        ROUND(c.max_score, 1) clazzMaxScore,
        ROUND(d.total_score, 1) fullScore,
        ROUND(d.current_score_rate, 1) difficult,
        d.difficulty difficultInfo
        FROM
        t_a_exam_course_record a
        LEFT JOIN
        t_a_exam_course_college_inspect b ON a.school_id = b.school_id
        AND a.exam_id = b.exam_id
        AND a.course_code = b.course_code
        AND a.inspect_college_id = b.college_id
        LEFT JOIN
        t_a_exam_course_clazz c ON a.school_id = c.school_id
        AND a.exam_id = c.exam_id
        AND a.course_code = c.course_code
        AND a.clazz_id = c.clazz_id
        LEFT JOIN
        t_a_exam_course d ON a.school_id = d.school_id
        AND a.exam_id = d.exam_id
        AND a.course_code = d.course_code
        <where>
            and a.school_id = #{schoolId}
            and a.exam_id = #{examId}
            and a.student_code = #{studentCode}
            and a.course_code = #{courseCode}
        </where>
    </select>

    <select id="findExamRecordByExamIdAndCourseCode" resultType="com.qmth.teachcloud.report.business.bean.result.ExamRecordResult">
        SELECT
            tbes.ticket_number AS ticketNumber,
            (select so.name from sys_org so where so.id = taecr.inspect_college_id) AS collegeName,
            taecr.inspect_college_id AS collegeId,
            assigned_score AS assignedScore,
            percent_grade AS percentGrade
        FROM
            t_a_exam_course_record taecr
            join t_b_exam_record tber
            on tber.id = taecr.exam_record_id
            join t_b_exam_student tbes
            on tber.exam_student_id = tbes.id
           <where>
               <if test="examId != null">
                   AND taecr.exam_id = #{examId}
               </if>
               <if test="courseCode != null and courseCode != ''">
                   AND taecr.course_code = #{courseCode}
               </if>
               and taecr.absent = 0
           </where>
    </select>

    <select id="surveyTeacherDistribution" resultType="com.qmth.teachcloud.report.business.bean.result.SurveyTeacherDistributionResult">
        SELECT
            taecr.total_score as totalScore,
            taecr.assigned_score as assignedScore,
            taecr.percent_grade as percentGrade,
            taecr.score_level as scoreLevel,
            taecr.rank_level as rankLevel,
            taecr.col_rank as colRank,
            taecr.student_current as studentCurrent,
            tbes.absent
        FROM
            t_a_exam_course_record taecr
        join t_b_exam_record tber on
            tber.id = taecr.exam_record_id
        join t_b_exam_student tbes on
            tber.exam_student_id = tbes.id
        <if test="filter != null and filter == true">
            join t_b_exam_course tbec on
            tbec.course_code = taecr.course_code
        </if>
        <where>
            <if test="examId != null and examId != ''">
                AND taecr.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                AND taecr.course_code = #{courseCode}
            </if>
            <if test="filter != null and filter == true">
                and tbec.publish_status = 'PUBLISH'
            </if>
        </where>
    </select>
</mapper>
