<?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="org.dromara.system.mapper.score.ScoreDataStudentMapper">

    <resultMap type="org.dromara.system.domain.score.vo.ScoreDataStudentVo" id="ScoreDataStudentResult">
        <result property="scoreStudentId"    column="score_student_id"    />
        <result property="scoreId"    column="score_id"    />
        <result property="scoreSort"    column="score_sort"    />
        <result property="scoreDataName"    column="score_data_name"    />
        <result property="scoreDataNameId"    column="score_data_name_id"    />
        <result property="studentNumber"    column="student_number"    />
        <result property="zongfen"    column="zongfen"    />
        <result property="avg"    column="avg"    />
        <collection property="scoreDataDetailVoList"   javaType="java.util.List"           resultMap="ScoreDataDetailResult"   />
    </resultMap>

    <resultMap type="org.dromara.system.domain.score.vo.ScoreDataDetailVo" id="ScoreDataDetailResult">
        <result property="scoreDetailId"    column="sdd_score_detail_id"    />
        <result property="scoreSort"    column="sdd_score_sort"    />
        <result property="scoreId"    column="sdd_score_id"    />
        <result property="xueke"    column="sdd_xueke"    />
        <result property="score"    column="sdd_score"    />
        <result property="scoreDataName"    column="sdd_score_data_name"    />
        <result property="scoreDataNameId"    column="sdd_score_data_name_id"    />
        <result property="studentNumber"    column="sdd_student_number"    />
    </resultMap>
    <select id="selectScoreDataByScoreClassId" parameterType="string" resultMap="ScoreDataStudentResult">
        select
            s.zongfen,s.score_id,s.avg,s.details_type,s.score_sort,s.score_data_name,s.student_number
        from
            score_data_student s
        WHERE
                s.score_id = (
                SELECT
                    score_id
                FROM
                    score_data
                WHERE
                    score_class_id = #{scoreClassId} order by score_time DESC limit 1)
        order by s.zongfen DESC
    </select>


    <select id="selectScoreDataForParents" parameterType="org.dromara.system.domain.score.ScoreData" resultMap="ScoreDataStudentResult">
        SELECT
            b.zongfen ,
            b.score_sort ,
            b.score_data_name ,
            b.score_data_name_id ,
            b.student_number ,
            d.xueke as sdd_xueke,
            d.score as sdd_score,
            d.score_data_name as sdd_score_data_name
        FROM
            score_data_student b
                LEFT JOIN score_data_detail d ON d.student_number = b.student_number
        where b.score_id = #{scoreId} and  find_in_set(b.score_data_name_id,#{params.stdId}) order by  b.score_sort
    </select>

    <select id="selectScoreByScoreId" parameterType="Long" resultMap="ScoreDataStudentResult">
        SELECT
            b.zongfen ,
            b.score_sort ,
            b.score_data_name ,
            b.score_data_name_id ,
            b.student_number ,
            d.xueke as sdd_xueke,
            d.score as sdd_score,
            d.score_data_name as sdd_score_data_name
        FROM
            score_data_student b
                LEFT JOIN score_data_detail d ON d.student_number = b.student_number
        where b.score_id = #{scoreId} order by  b.score_sort
    </select>

    <select id="selectScoreByScoreIds"  resultMap="ScoreDataStudentResult">
        SELECT
            b.score_id,
            b.student_number,
            b.zongfen
        FROM
            score_data_student b
        where
        b.score_id in
        <foreach item="scoreId" open="(" close=")" collection="array" separator=",">
           #{scoreId}
        </foreach>
    </select>

    <select id="gradeRank" parameterType="org.dromara.system.domain.score.ScoreData" resultMap="ScoreDataStudentResult">
        select ( @INDEX := @INDEX + 1 ) AS `score_sort`,
        a.score_data_name,
        a.score_data_name_id,
        a.student_number,
        a.zongfen,
        a.score_id
        FROM
        (select s.score_data_name,s.score_data_name_id,s.zongfen,s.score_id,s.student_number
        FROM score_data_student s
        <where>
         score_id IN (
            select d.score_id
            FROM score_data d
        <where>
            <if test="scoreType != null  and scoreType != ''"> and d.score_type = #{scoreType}</if>
            <if test="scoreTime != null "> and d.score_time = #{scoreTime}</if>
            <if test="scoreClassName != null  and scoreClassName != ''"> and d.score_class_name like concat( #{scoreClassName}, '%')</if>
        </where>
        )
            <if test="scoreDataName != null  and scoreDataName != ''"> and s.score_data_name = #{scoreDataName}</if>
        </where>
        order by s.zongfen  DESC) a,(select @INDEX:=0) init
    </select>
    <select id="xuekeRank" parameterType="org.dromara.system.domain.score.bo.ScoreDataBo" resultMap="ScoreDataDetailResult">

        select  (@INDEX := @INDEX + 1) AS `sdd_score_sort`,
        a.score_data_name as sdd_score_data_name,
        a.score_data_name_id as sdd_score_data_name_id,
        a.student_number as sdd_student_number,
        a.xueke as sdd_xueke,
        a.score as sdd_score
        FROM
       ( SELECT
        dd.score_data_name ,
        dd.score_data_name_id,
        dd.student_number,
        dd.xueke as xueke,
        dd.score as score
        FROM
        score_data_detail dd
        WHERE
        score_id IN (
        SELECT
        d.score_id
        FROM
        score_data d
        <where>
            <if test="scoreType != null  and scoreType != ''"> and d.score_type = #{scoreType}</if>
            <if test="scoreTime != null "> and d.score_time = #{scoreTime}</if>
            <if test="scoreId != null "> and d.score_id = #{scoreId}</if>
            <if test="scoreClassName != null  and scoreClassName != ''"> and d.score_class_name like concat( #{scoreClassName}, '%')</if>
        </where>
            ) and dd.xueke=#{xueke}
          <if test="scoreDataName != null and scoreDataName">and dd.score_data_name = #{scoreDataName}</if>
        order by dd.score  DESC) a,(select @INDEX:=0) init
    </select>

</mapper>
