<?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.hotwj.platform.securityManagement.trainingCourseConfig.mapper.HotTrainingCourseConfigMapper">

    <select id="selectTrainingUserCourseStudyDetail"
            resultType="com.hotwj.platform.securityManagement.trainingCourseConfig.domain.vo.TrainingUserCourseStudyDetailVo">
        SELECT c.id            AS trainingCourseId,
               c.course_name   AS courseName,
               c.sort_no       AS sortNo,
               r.progress_rate AS progressRate,
               r.complete_time AS completeTime
        FROM hot_training_course_config c
                 LEFT JOIN (SELECT training_course_id, progress_rate, complete_time, rn
                            FROM (SELECT t.training_course_id,
                                         t.progress_rate,
                                         t.complete_time,
                                         ROW_NUMBER() OVER (
                        PARTITION BY t.training_course_id
                        ORDER BY t.complete_time DESC, t.create_time DESC, t.id DESC
                    ) AS rn
                                  FROM hot_training_course_record t
                                  WHERE t.company_id = #{companyId}
                                    AND t.training_id = #{trainingId}
                                    AND t.user_id = #{userId}
                                    AND t.is_deleted = 0) x
                            WHERE x.rn = 1) r ON r.training_course_id = c.id
        WHERE c.company_id = #{companyId}
          AND c.training_id = #{trainingId}
          AND c.is_deleted = 0
        ORDER BY c.sort_no ASC, c.id ASC
    </select>
</mapper>
