<?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.yida.data.school.exam.mapper.CoreExaminationStudentCourseMapper">
    <resultMap id="BaseResultMap" type="com.yida.data.common.core.entity.exam.CoreExaminationStudentCourse">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="create_id" property="createId" jdbcType="VARCHAR"/>
        <result column="create_date" property="createDate" jdbcType="TIMESTAMP"/>
        <result column="update_id" property="updateId" jdbcType="VARCHAR"/>
        <result column="update_date" property="updateDate" jdbcType="TIMESTAMP"/>
        <result column="del_flag" property="delFlag" jdbcType="CHAR"/>
        <result column="core_examination_id" property="coreExaminationId" jdbcType="INTEGER"/>
        <result column="core_examination_student_id" property="coreExaminationStudentId" jdbcType="INTEGER"/>
        <result column="core_examination_student_app_id" property="coreExaminationStudentAppId"
                jdbcType="VARCHAR"/>
        <result column="core_examination_course_id" property="coreExaminationCourseId" jdbcType="INTEGER"/>
        <result column="core_examination_course_name" property="coreExaminationCourseName"
                jdbcType="VARCHAR"/>
        <result column="school_id" property="schoolId" jdbcType="INTEGER"/>
        <result column="school_name" property="schoolName" jdbcType="VARCHAR"/>
        <result column="grade_id" property="gradeId" jdbcType="INTEGER"/>
        <result column="grade_name" property="gradeName" jdbcType="VARCHAR"/>
        <result column="class_id" property="classId" jdbcType="INTEGER"/>
        <result column="class_name" property="className" jdbcType="VARCHAR"/>
        <result column="examination_room" property="examinationRoom" jdbcType="VARCHAR"/>
        <result column="examination_number" property="examinationNumber" jdbcType="VARCHAR"/>
        <result column="seat_number" property="seatNumber" jdbcType="INTEGER"/>
        <result column="score" property="score" jdbcType="INTEGER"/>
        <result column="miss" property="miss" jdbcType="CHAR"/>
        <result column="class_rank" property="classRank" jdbcType="INTEGER"/>
        <result column="grade_rank" property="gradeRank" jdbcType="INTEGER"/>
        <result column="back" property="back" jdbcType="CHAR"/>
    </resultMap>

    <!--根据考试主键id查询学生科目成绩-->
    <select id="findByCoreExaminationId" parameterType="java.lang.Long"
            resultType="com.yida.data.common.core.entity.exam.CoreExaminationPublishRecord">
        SELECT
        cesc.core_examination_id AS core_examination_id,
        ces.core_student_id AS core_student_id,
        cesc.core_examination_student_id AS core_examination_student_id,
        cesc.core_examination_course_id AS core_examination_course_id,
        cesc.core_examination_course_name AS core_examination_course_name,
        cesc.school_id AS school_id,
        cesc.school_name AS school_name,
        cesc.grade_id AS grade_id,
        cesc.grade_name AS grade_name,
        cesc.class_id AS class_id,
        cesc.class_name AS class_name,
        cesc.examination_room AS examination_room,
        cesc.examination_number AS examination_number,
        cesc.seat_number AS seat_number,
        cesc.score AS score,
        cec.core_course_score AS course_score,
        cesc.miss AS miss,
        cesc.class_rank AS class_rank,
        cesc.grade_rank AS grade_rank,
        cesc.school_rank AS school_rank
        FROM
        core_examination_student_course cesc
        LEFT JOIN core_examination_course cec ON cesc.core_examination_course_id = cec.core_course_id
        AND cesc.core_examination_id = cec.core_examination_id
        LEFT JOIN core_examination_student ces ON cesc.core_examination_student_id = ces.id
        WHERE
        cesc.del_flag = '0'
        AND cec.del_flag = '0'
        AND ces.del_flag = '0'
        AND cesc.core_examination_id = #{coreExaminationId}
    </select>

    <select id="findByCoreExaminationStudentId"
            resultType="com.yida.data.common.core.entity.exam.CoreExaminationStudentCourse">
        SELECT
        cesc.id AS id,
        cesc.core_examination_id AS core_examination_id,
        cesc.core_examination_student_id AS core_examination_student_id,
        cesc.core_examination_course_id AS core_examination_course_id,
        cesc.core_examination_course_name AS core_examination_course_name,
        cesc.school_id AS school_id,
        cesc.school_name AS school_name,
        cesc.grade_id AS grade_id,
        cesc.grade_name AS grade_name,
        cesc.class_id AS class_id,
        cesc.class_name AS class_name,
        cesc.examination_room AS examination_room,
        cesc.examination_number AS examination_number,
        cesc.seat_number AS seat_number,
        cesc.score AS score,
        cesc.miss AS miss,
        cesc.class_rank AS class_rank,
        cesc.grade_rank AS grade_rank,
        cesc.school_rank AS school_rank
        FROM
        core_examination_student_course cesc
        WHERE
        cesc.del_flag = '0'
        AND cesc.core_examination_student_id = #{coreExaminationStudentId} ORDER BY
        cesc.core_examination_course_id
    </select>

    <select id="findById" resultType="com.yida.data.common.core.entity.exam.CoreExaminationStudentCourse">
        SELECT
        cesc.id AS id,
        cesc.core_examination_id AS core_examination_id,
        cesc.core_examination_student_id AS core_examination_student_id,
        cesc.core_examination_course_id AS core_examination_course_id,
        cesc.core_examination_course_name AS core_examination_course_name,
        cesc.school_id AS school_id,
        cesc.school_name AS school_name,
        cesc.grade_id AS grade_id,
        cesc.grade_name AS grade_name,
        cesc.class_id AS class_id,
        cesc.class_name AS class_name,
        cesc.examination_room AS examination_room,
        cesc.examination_number AS examination_number,
        cesc.seat_number AS seat_number,
        cesc.score AS score,
        cesc.miss AS miss,
        cesc.class_rank AS class_rank,
        cesc.grade_rank AS grade_rank,
        cesc.school_rank AS school_rank
        FROM
        core_examination_student_course cesc
        WHERE
        cesc.del_flag = '0'
        AND cesc.id = #{id} AND cesc.core_examination_student_id = #{coreExaminationStudentId}
    </select>

    <select id="findByCoreExaminationIdAndGradeIdCourseId"
            parameterType="com.yida.data.common.core.entity.exam.CoreExaminationStudentCourse"
            resultType="com.yida.data.common.core.entity.exam.CoreExaminationStudentCourse">
        select id, score, @curRank := IF(@prevRank = score, @curRank, @incRank) as gradeRank,
        @incRank := @incRank + 1,@prevRank := score from core_examination_student_course p , (select @curRank
        := 0, @prevRank := null, @incRank := 1)
        r WHERE p.core_examination_id = #{examId} and p.grade_id = #{gradeId} and
        p.core_examination_course_id = #{courseId} and p.del_flag='0'
        and p.miss = '0' ORDER BY score DESC
    </select>

    <select id="findByCoreExaminationIdAndGradeIdClassIdCourseId"
            parameterType="com.yida.data.common.core.entity.exam.CoreExaminationStudentCourse"
            resultType="com.yida.data.common.core.entity.exam.CoreExaminationStudentCourse">
        select id, score, @curRank := IF(@prevRank = score, @curRank, @incRank) as classRank,
        @incRank := @incRank + 1,@prevRank := score from core_examination_student_course p , (select @curRank
        := 0, @prevRank := null, @incRank := 1)
        r WHERE p.core_examination_id = #{examId} and p.grade_id = #{gradeId} and
        p.core_examination_course_id = #{courseId} and p.del_flag='0' and
        p.class_id = #{classId} and p.miss = '0' ORDER BY score DESC
    </select>

    <!-- 批量更新第一种方法，通过接收传进来的参数list进行循环着组装sql -->
    <update id="updateGradleRankOrClassRank" parameterType="java.util.List">
        update core_examination_student_course
        <set>
            <if test="gradeRank != null">
                grade_rank = #{gradeRank,jdbcType=INTEGER},
            </if>
            <if test="classRank != null">
                class_rank = #{classRank,jdbcType=INTEGER},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
</mapper>