<?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.CoreExaminationStudentMapper">
    <resultMap id="BaseResultMap" type="com.yida.data.common.core.entity.exam.CoreExaminationStudent">
        <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_student_id" property="coreStudentId" jdbcType="INTEGER"/>
        <result column="student_app_id" property="studentAppId" jdbcType="VARCHAR"/>
        <result column="student_name" property="studentName" jdbcType="VARCHAR"/>
        <result column="student_id_number" property="studentIdNumber" jdbcType="VARCHAR"/>
        <result column="student_number" property="studentNumber" 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="grade_rank" property="gradeRank" jdbcType="INTEGER"/>
        <result column="class_rank" property="classRank" jdbcType="INTEGER"/>
        <result column="receipt_status" property="receiptStatus" jdbcType="VARCHAR"/>

        <collection property="studentCourseList"
                    ofType="com.yida.data.common.core.entity.exam.CoreExaminationStudentCourse"
                    select="selectCourse" column="id">
            <result column="student_course_score" property="score" jdbcType="INTEGER"/>
            <result column="student_grade_rank" property="gradeRank" jdbcType="INTEGER"/>
            <result column="student_class_rank" property="classRank" jdbcType="INTEGER"/>
            <result column="core_examination_course_id" property="coreExaminationCourseId"/>
            <result column="core_examination_course_name" property="coreExaminationCourseName"/>
        </collection>
    </resultMap>

    <select id="selectCourse"
            resultType="com.yida.data.common.core.entity.exam.CoreExaminationStudentCourse">
        select *
        from core_examination_student_course
        where core_examination_student_id=#{id} and del_flag='0'
        order by core_examination_course_id
    </select>

    <sql id="listStudentSql">
        SELECT
        *
        FROM
        core_examination_student ces
        WHERE
        ces.del_flag = '0'
        AND ces.core_examination_id = #{coreExaminationId}
        <if test='searchName != null and searchName != ""'>
            AND (
            ces.student_name LIKE CONCAT('%', #{searchName}, '%')
            OR ces.student_number LIKE CONCAT('%', #{searchName}, '%')
            )
        </if>
        <if test='classId != null and classId != "0"'>
            AND ces.class_id = #{classId}
        </if>
        ORDER BY
        ces.create_date DESC
    </sql>

    <!--查询本次考试对应的考试学生列表数据-->
    <select id="findExaminationStudentPage"
            resultType="com.yida.data.common.core.entity.exam.CoreExaminationStudent">
        <include refid="listStudentSql">
        </include>
    </select>

    <select id="findExaminationStudentList"
            resultType="com.yida.data.common.core.entity.exam.CoreExaminationStudent">
        <include refid="listStudentSql">
        </include>
    </select>

    <!--查询本次考试的排行榜数据列表-->
    <select id="findRankPageList" resultMap="BaseResultMap">
        SELECT
        ces.id AS id,
        ces.core_student_id,
        ces.student_name AS student_name,
        ces.student_number AS student_number,
        ces.examination_number AS examination_number,
        ces.grade_id AS grade_id,
        ces.grade_name AS grade_name,
        ces.class_name AS class_name,
        ces.score AS score,
        ces.grade_rank AS grade_rank,
        ces.class_rank AS class_rank,
        ces.miss AS miss
        FROM
        core_examination_student ces
        WHERE
        ces.del_flag = '0'
        AND ces.core_examination_id = #{coreExaminationId}
        <if test='studentName != null and studentName != ""'>
            AND ces.student_name LIKE CONCAT('%', #{studentName}, '%')
        </if>
        <!--<if test='coreExaminationCourseId != "0"'>
            AND cesc.core_examination_course_id = #{coreExaminationCourseId}
        </if>-->
        <if test='classId != null and classId != 0'>
            AND ces.class_id = #{classId}
        </if>
        ORDER BY
        ces.grade_rank is null,ces.grade_rank ASC
    </select>

    <!--查询本次考试单个考生的信息列表-->
    <select id="findCoreExaminationStudentDetail" resultMap="BaseResultMap">
        SELECT
        ces.id AS id,
        ces.student_name AS student_name,
        ces.student_number AS student_number,
        ces.examination_number AS examination_number,
        ces.student_id_number AS student_id_number,
        ces.seat_number AS seat_number,
        ces.grade_id AS grade_id,
        ces.grade_name AS grade_name,
        ces.class_id AS class_id,
        ces.class_name AS class_name,
        ces.score AS score,
        ces.grade_rank AS grade_rank,
        ces.class_rank AS class_rank,
        ces.miss AS miss
        FROM
        core_examination_student ces
        WHERE
        ces.del_flag = '0' AND ces.id = #{coreExaminationStudentId}
        AND ces.core_examination_id = #{coreExaminationId}
        <if test='studentName != null and studentName != ""'>
            AND ces.student_name LIKE CONCAT('%', #{studentName}, '%')
        </if>
        <!--<if test='coreExaminationCourseId != "0"'>
            AND cesc.core_examination_course_id = #{coreExaminationCourseId}
        </if>-->
        <if test='classId != "" and classId != null'>
            AND ces.class_id = #{classId}
        </if>
        ORDER BY
        ces.grade_rank is null,ces.grade_rank ASC
    </select>

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

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

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

    <select id="findClassByExam" resultType="com.yida.data.school.vo.exam.ClassVO">
        select distinct class_id,class_name
        from core_examination_student
        where del_flag=0
        and core_examination_id=#{examId}
        and class_id is not null
        and class_name is not null
        group by class_id,class_name
    </select>

    <select id="getScoreAnalysis" resultType="com.yida.data.school.vo.exam.ExamScoreAnalysisVO">
        select count(1) studentNum,
        null classId,
        null className,
        sum( score )/ count( 1 ) avgScore,
        sum(if(score*100/#{score} <![CDATA[>]]> #{highScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{highScoreRateEnd},1,0))
        highScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{excellentScoreRateStart} and score*100/#{score}
        <![CDATA[<=]]>
        #{excellentScoreRateEnd},1,0))
        excellentScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{goodScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{goodScoreRateEnd},1,0))
        goodScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{passScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{passScoreRateEnd},1,0))
        passScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{lowScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{lowScoreRateEnd},1,0))
        lowScoreNum,
        max(score) maxScore,
        min(score) minScore
        from core_examination_student
        <where>
            miss = 0
            and del_flag=0
            and core_examination_id=#{examId}
        </where>
        union all
        select count(1) studentNum,
        class_id classId,
        class_name className,
        sum(score)/count(1) avgScore,
        sum(if(score*100/#{score} <![CDATA[>]]> #{highScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{highScoreRateEnd},1,0))
        highScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{excellentScoreRateStart} and score*100/#{score}
        <![CDATA[<=]]>
        #{excellentScoreRateEnd},1,0))
        excellentScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{goodScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{goodScoreRateEnd},1,0))
        goodScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{passScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{passScoreRateEnd},1,0))
        passScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{lowScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{lowScoreRateEnd},1,0))
        lowScoreNum,
        max(score) maxScore,
        min(score) minScore
        from core_examination_student
        <where>
            miss = 0
            and del_flag=0
            and core_examination_id=#{examId}
        </where>
        group by class_id,class_name
    </select>

    <select id="getCourseScoreAnalysis" resultType="com.yida.data.school.vo.exam.ExamScoreAnalysisVO">
        select count(1) studentNum,
        null classId,
        null className,
        sum( score )/ count( 1 ) avgScore,
        sum(if(score*100/#{score} <![CDATA[>]]> #{highScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{highScoreRateEnd},1,0))
        highScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{excellentScoreRateStart} and score*100/#{score}
        <![CDATA[<=]]>
        #{excellentScoreRateEnd},1,0))
        excellentScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{goodScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{goodScoreRateEnd},1,0))
        goodScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{passScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{passScoreRateEnd},1,0))
        passScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{lowScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{lowScoreRateEnd},1,0))
        lowScoreNum,
        max(score) maxScore,
        min(score) minScore
        from core_examination_student_course
        <where>
            miss = 0
            and del_flag=0
            and core_examination_id=#{examId}
            <if test="courseId != null">
                and core_examination_course_id=#{courseId}
            </if>
        </where>
        union all
        select count(1) studentNum,
        class_id classId,
        class_name className,
        sum(score)/count(1) avgScore,
        sum(if(score*100/#{score} <![CDATA[>]]> #{highScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{highScoreRateEnd},1,0))
        highScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{excellentScoreRateStart} and score*100/#{score}
        <![CDATA[<=]]>
        #{excellentScoreRateEnd},1,0))
        excellentScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{goodScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{goodScoreRateEnd},1,0))
        goodScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{passScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{passScoreRateEnd},1,0))
        passScoreNum,
        sum(if(score*100/#{score} <![CDATA[>]]> #{lowScoreRateStart} and score*100/#{score} <![CDATA[<=]]>
        #{lowScoreRateEnd},1,0))
        lowScoreNum,
        max(score) maxScore,
        min(score) minScore
        from core_examination_student_course
        <where>
            miss = 0
            and del_flag=0
            and core_examination_id=#{examId}
            <if test="courseId != null">
                and core_examination_course_id=#{courseId}
            </if>
        </where>
        group by class_id,class_name
    </select>

    <select id="getScoreSectionStastic" resultType="com.yida.data.school.vo.exam.ScoreSectionStasticVO">
        select count(1) studentNum,
        null classId,
        null className
        from core_examination_student
        <where>
            miss = 0
            and del_flag=0
            and core_examination_id=#{examId}
        </where>
        <if test="classIds != null and classIds.size() != 0">
            union all
            select count(1) studentNum,
            class_id classId,
            class_name className
            from core_examination_student
            <where>
                miss = 0
                and del_flag=0
                and core_examination_id=#{examId}
                and class_id in
                <foreach collection="classIds" separator="," open="(" close=")" item="item">
                    #{item}
                </foreach>
            </where>
            group by class_id,class_name
        </if>
    </select>

    <select id="getCourseScoreSectionStastic" resultType="com.yida.data.school.vo.exam.ScoreSectionStasticVO">
        select count(1) studentNum,
        null classId,
        null className
        from core_examination_student_course
        <where>
            miss = 0
            and del_flag=0
            and core_examination_id=#{examId}
            and core_examination_course_id=#{courseId}
        </where>
        <if test="classIds != null and classIds.size() != 0">
            union all
            select count(1) studentNum,
            class_id classId,
            class_name className
            from core_examination_student_course
            <where>
                miss = 0
                and del_flag=0
                and core_examination_id=#{examId}
                and core_examination_course_id=#{courseId}
                and class_id in
                <foreach collection="classIds" separator="," open="(" close=")" item="item">
                    #{item}
                </foreach>
            </where>
            group by class_id,class_name
        </if>
    </select>

    <select id="findScoreTrend" resultType="com.yida.data.school.vo.exam.ScoreTrendVO">
        select a.start_date date,b.score*100/a.score scoreRate,b.score,a.score examScore
        from core_examination a
        left join core_examination_student b on a.id=b.core_examination_id
        <where>
            b.miss=0
            and a.del_flag=0
            and b.del_flag=0
            and a.publish=1
            and a.start_date between #{startDate} and #{endDate}
            and b.core_student_id=#{studentId}
        </where>
        order by a.start_date
    </select>
</mapper>