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

    <select id="finalScoreList" resultType="com.qmth.distributed.print.business.entity.TCFinalScore">
        select * from t_c_final_score t
        <where>
            <if test="examId != null and examId != ''">
                and t.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and t.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and t.paper_number = #{paperNumber}
            </if>
            <if test="obeCourseOutlineId != null and obeCourseOutlineId != ''">
                and t.teach_course_id = #{obeCourseOutlineId}
            </if>
        </where>
        order by t.student_code
    </select>

    <select id="finalScoreScoreOverView" resultType="com.qmth.distributed.print.business.bean.dto.FinalScoreDto">
        select count(1) as studentCount,
            max(case when tcfs.score is not null then tcfs.score else null end) finalScoreMaxScore,
            min(case when tcfs.score is not null then tcfs.score else null end) finalScoreMinScore,
            avg(case when tcfs.score is not null then tcfs.score else null end) finalScoreAvgScore
            from t_c_final_score tcfs
            join t_c_usual_score tcus on tcfs.exam_id = tcus.exam_id and tcfs.course_code = tcus.course_code and tcfs.teach_course_id = tcus.teach_course_id and tcfs.student_code = tcus.student_code
        <where>
            <if test="examId != null and examId != ''">
                and tcfs.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and tcfs.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and tcfs.paper_number = #{paperNumber}
            </if>
            <if test="obeCourseOutlineId != null and obeCourseOutlineId != ''">
                and tcfs.teach_course_id = #{obeCourseOutlineId}
            </if>
                and tcfs.enable = true and tcus.enable = true
        </where>
    </select>

    <select id="getCountByScoreRange" resultType="int">
        SELECT count(1) FROM t_c_final_score tcfs
        <where>
            <if test="examId != null and examId != ''">
                and tcfs.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and tcfs.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and tcfs.paper_number = #{paperNumber}
            </if>
            <if test="obeCourseOutlineId != null and obeCourseOutlineId != ''">
                and tcfs.teach_course_id = #{obeCourseOutlineId}
            </if>
                and tcfs.score &gt;= #{start} and tcfs.score &lt;= #{end}
                and tcfs.enable = true
        </where>
    </select>

    <select id="examStudentOverview" resultType="com.qmth.distributed.print.business.bean.result.FinalScoreResult">
        select
            tcfs.name,
            tcfs.student_code as studentCode,
            tcfs.score_detail as finalScoreDetail,
            tcus.score as usualScore,
            es.teach_clazz_name as administrativeClass
        from t_c_final_score tcfs
        join t_c_usual_score tcus on tcfs.exam_id = tcus.exam_id and tcfs.course_code = tcus.course_code and tcfs.teach_course_id = tcus.teach_course_id and tcfs.student_code = tcus.student_code
        left join exam_student es on es.exam_id = tcfs.exam_id and es.paper_number = tcfs.paper_number and es.student_code = tcfs.student_code
        <where>
            <if test="examId != null and examId != ''">
                and tcfs.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and tcfs.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and tcfs.paper_number = #{paperNumber}
            </if>
            <if test="obeCourseOutlineId != null and obeCourseOutlineId != ''">
                and tcfs.teach_course_id = #{obeCourseOutlineId}
            </if>
                and tcfs.enable = true and tcus.enable = true
        </where>
            order by tcfs.student_code
    </select>
</mapper>
