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

    <sql id="commonHead">
        select
            taec.id,
            taec.course_code as courseCode,
            taec.course_name as courseName,
            taec.teach_college_id as teachCollegeId,
            taec.teach_college_name as teachCollegeName,
            taec.inspect_college_count as inspectCollegeCount,
            taec.reality_count as realityCount ,
            round(taec.current_reality_rate * 100,1) as currentRealityRate,
            taec.current_total_count as currentTotalCount,
            taec.current_reality_count as currentRealityCount,
            round(taec.past_reality_rate * 100,1) as pastRealityRate,
            taec.past_total_count as pastTotalCount,
            taec.past_reality_count as pastRealityCount,
            taec.absent_count as absentCount,
            taec.paper_avg_score as avgScoreBefore,
            round((1 - taec.paper_pass_rate) * 100,1) as notPassRateBefore,
            taec.paper_current_avg_score as currentAvgScoreBefore,
            round((1 - taec.paper_current_pass_rate) * 100,1) as currentNotPassRateBefore,
            taec.avg_score as avgScoreAfter,
            round((1 - taec.pass_rate) * 100,1) as notPassRateAfter,
            taec.current_avg_score as currentAvgScoreAfter,
            round((1 - taec.current_pass_rate) * 100,1) as currentNotPassRateAfter,
            taec.coefficient,
            (taec.reality_count - taec.paper_pass_count) as notPassCountBefore,
            (taec.current_reality_count - taec.paper_current_pass_count) as currentNotPassCountBefore,
            (taec.reality_count - taec.pass_count) as notPassCountAfter,
            (taec.current_reality_count - taec.current_pass_count) as currentNotPassCountAfter
    </sql>

    <sql id="commonCondition">
        <if test="schoolId != null and schoolId != ''">
            and taec.school_id = #{schoolId}
        </if>
        <if test="examId != null and examId != ''">
            and taec.exam_id = #{examId}
        </if>
    </sql>

    <sql id="commonMiddle">
        from t_a_exam_course taec
            join t_b_exam tbe
        on tbe.id = taec.exam_id
    </sql>

    <select id="surveyTeacherList" resultType="com.qmth.teachcloud.report.business.bean.result.TAExamCourseResult">
        select * from(<include refid="commonHead"/>
        <include refid="commonMiddle"/>
        <where>
            <include refid="commonCondition"/>
            <if test="semesterId != null and semesterId != ''">
                and tbe.semester_id = #{semesterId}
            </if>
            <if test="collegeId != null and collegeId != ''">
                and taec.teach_college_id = #{collegeId}
            </if>
        </where> ) t
        <if test="column != null and column != '' and order != null and order != ''">
            order by t.${column} ${order}
        </if>
    </select>

    <select id="surveyAspointsList" resultType="com.qmth.teachcloud.report.business.bean.result.TAExamCourseResult">
        select * from(<include refid="commonHead" />
        ,tbec.publish_status as status
        <include refid="commonMiddle" />
        join t_b_exam_course tbec
        on tbec.course_code = taec.course_code and tbec.exam_id = taec.exam_id
        and tbec.school_id = taec.school_id
        <where>
            <include refid="commonCondition" />
            <if test="courseCode != null and courseCode != ''">
                and taec.course_code = #{courseCode}
            </if>
            <if test="semesterId != null and semesterId != ''">
                and tbe.semester_id = #{semesterId}
            </if>
            <if test="publishStatus != null and publishStatus != ''">
                and tbec.publish_status = #{publishStatus}
            </if>
            <if test="collegeIds != null and collegeIds != '' and collegeIds.size > 0">
                    and taec.teach_college_id in
                <foreach collection="collegeIds" item="collegeId" index="index" open="(" separator="," close=")">
                    #{collegeId}
                </foreach>
            </if>
        </where> ) t
        <if test="column != null and column != '' and order != null and order != ''">
            order by t.${column} ${order}
        </if>
    </select>

    <select id="getOverview" resultType="com.qmth.teachcloud.report.business.bean.result.ExamCourseResult">
        SELECT
            b.exam_time examTime,
            a.course_name courseName,
            a.total_score totalScore,
            a.reality_count realityCount,
            a.inspect_college_count inspectCollegeCount,
            a.absent_count absentCount,
            a.teach_college_name teachCollegeName,
            a.inspect_college_names inspectCollegeName
        FROM
            t_a_exam_course a
                LEFT JOIN
            t_b_exam b ON a.exam_id = b.id
            join t_b_exam_course tbec on
                tbec.exam_id = a.exam_id and
                tbec.course_code = a.course_code
        <where>
            <if test="schoolId != null">
                and b.school_id = #{schoolId}
            </if>
            <if test="semesterId != null and semesterId != ''">
                and b.semester_id = #{semesterId}
            </if>
            <if test="examId != null">
                and a.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
                and tbec.publish_status = 'PUBLISH'
        </where>
    </select>

    <select id="getScoreAndRate" resultType="java.util.Map">
        SELECT
            round(a.avg_score, 1) avgScore,
            round(a.current_avg_score, 1) currentAvgScore,
            round((1 - IFNULL(a.pass_rate, 0)) * 100 ,1) notPassRate,
            round((1 - IFNULL(a.current_pass_rate, 0)) * 100, 1) currentNotPassRate
        FROM
            t_a_exam_course a
                LEFT JOIN
            t_b_exam b ON a.exam_id = b.id
        <where>
            and b.school_id = #{schoolId}
            <if test="semesterId != null and semesterId != ''">
                and b.semester_id = #{semesterId}
            </if>
            <if test="examId != null">
                and a.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
        </where>
    </select>

    <select id="surveyTeacherTeacherView" resultType="com.qmth.teachcloud.report.business.bean.result.TAExamCourseCollegeTeacherResult">
        select * from(select
            taecct.inspect_college_name as inspectCollegeName,
            taecct.teacher_name as teacherName,
            taecct.avg_score as currentAvgScoreBefore,
            taecct.avg_score_assign as currentAvgScoreAfter,
            taecct.teacher_rank as teacherRank
        from
            t_a_exam_course_college_teacher taecct
        <where>
            <if test="schoolId != null and schoolId != ''">
                and taecct.school_id = #{schoolId}
            </if>
            <if test="examId != null and examId != ''">
                and taecct.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and taecct.course_code = #{courseCode}
            </if>
        </where> ) t
        <if test="column != null and column != '' and order != null and order != ''">
            <choose>
                <when test="column != null and column == 'inspectCollegeName'">
                    order by t.${column} ${order},t.teacherRank ${order}
                </when>
                <otherwise>
                    order by t.${column} ${order}
                </otherwise>
            </choose>
        </if>
    </select>
</mapper>
