<?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.ynfy.buss.exam.examstatistics.mapper.ExamStatisticsMapper">
    <resultMap id="examDimensionMap" type="com.ynfy.buss.exam.examstatistics.dto.ExamStatisticsDTO">
        <id column="id" property="userExamId" jdbcType="VARCHAR"/>
        <result column="user_id" property="userId" jdbcType="VARCHAR"/>
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <result column="realname" property="realname" jdbcType="VARCHAR"/>
        <result column="exam_id" property="examId" jdbcType="VARCHAR"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="open_type" property="openType" jdbcType="INTEGER"/>
        <result column="total_score" property="totalScore" jdbcType="DOUBLE"/>
        <result column="qualify_score" property="qualifyScore" jdbcType="DOUBLE"/>
        <result column="user_time" property="userTime" jdbcType="INTEGER"/>
        <result column="user_score" property="userScore" jdbcType="DOUBLE"/>
        <result column="commit_time" property="commitExamTime" jdbcType="TIMESTAMP"/>
        <result column="create_time" property="startExamTime" jdbcType="TIMESTAMP"/>
        <result column="passed" property="passed" jdbcType="INTEGER"/>
        <result column="try_count" property="tryCount" jdbcType="INTEGER"/>
    </resultMap>

    <select id="calcExamDimension" resultMap="examDimensionMap">
        SELECT uea.id,
               uea.user_id,
               u.username,
               u.realname,
               uea.exam_id,
               e.title,
               e.open_type,
               uea.total_score,
               uea.qualify_score,
               uea.user_time,
               uea.user_score,
               uea.commit_time,
               uea.create_time,
               uea.passed,
               uer.try_count
        FROM user_exam uea
                 INNER JOIN (SELECT tmp.user_id,
                                    tmp.exam_id,
                                    tmp.user_score,
                                    MAX(update_time) AS update_time
                             FROM user_exam ue
                                      INNER JOIN (SELECT user_id,
                                                         exam_id,
                                                         MAX(user_score) AS user_score
                                                  FROM user_exam
                                                  WHERE exam_id = #{examId}
                                                  GROUP BY user_id,
                                                           exam_id) tmp ON tmp.user_id = ue.user_id
                                 AND tmp.exam_id = ue.exam_id
                                 AND tmp.user_score = ue.user_score
                             GROUP BY tmp.user_id,
                                      tmp.exam_id,
                                      tmp.user_score) tmp1 ON tmp1.user_id = uea.user_id
            AND tmp1.exam_id = uea.exam_id
            AND tmp1.user_score = uea.user_score
            AND tmp1.update_time = uea.update_time
                 LEFT JOIN sys_user u ON u.ID = uea.user_id
                 LEFT JOIN exam e ON e.ID = uea.exam_id
                 LEFT JOIN user_exam_result uer on uer.user_id = uea.user_id and uer.exam_id = uea.exam_id
        WHERE uea.STATE = 2
        ORDER BY uea.user_score desc
    </select>

</mapper>