package org.dromara.main.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
import org.dromara.main.domain.MainTrainingLearnRecord;
import org.dromara.main.domain.vo.MainTrainingLearnRecordVo;
import org.dromara.main.domain.vo.StudentTrainingRecordVo;

import java.util.List;

/**
 * 培训学习记录Mapper接口
 *
 * @author ruoyi
 */
@Mapper
public interface MainTrainingLearnRecordMapper extends BaseMapperPlus<MainTrainingLearnRecord, MainTrainingLearnRecordVo> {

    /**
     * 查询学员视频培训学习记录 (从学员角度查看培训列表)
     * 这里将培训名称映射到 VO 的 name 字段，以便前端统一显示
     */
    @Select("""
        SELECT r.id,
               r.training_id AS trainingId,
               t.name AS name,
               r.student_id AS studentId,
               r.learned_time AS learnedTime,
               r.remaining_time AS remainingTime,
               r.progress AS progress,
               r.finish_time AS finishTime,
               r.last_learn_time AS lastLearnTime
          FROM main_training_learn_record r
          LEFT JOIN main_training t ON t.id = r.training_id AND t.del_flag = '0'
         WHERE r.student_id = #{studentId}
           AND r.del_flag = '0'
         ORDER BY r.last_learn_time DESC
        """)
    List<MainTrainingLearnRecordVo> selectRecordListByStudentId(@Param("studentId") Long studentId);

    /**
     * 查询学员全部培训记录（合并线上视频培训 + 线下培训报名）
     * 关联 main_training 表获取培训名称、类型、时间
     */
    @Select("""
        SELECT r.id,
               r.training_id AS trainingId,
               r.student_id AS studentId,
               t.name AS name,
               t.training_type AS trainingType,
               t.training_start_time AS trainingStartTime,
               t.training_end_time AS trainingEndTime,
               r.progress AS progress,
               r.learn_status AS learnStatus,
               NULL AS enrollStatus,
               NULL AS checkInTime
          FROM main_training_learn_record r
          LEFT JOIN main_training t ON t.id = r.training_id AND t.del_flag = '0'
         WHERE r.student_id = #{studentId}
           AND r.del_flag = '0'

        UNION ALL

        SELECT e.id,
               e.training_id AS trainingId,
               e.student_id AS studentId,
               t.name AS name,
               t.training_type AS trainingType,
               t.training_start_time AS trainingStartTime,
               t.training_end_time AS trainingEndTime,
               NULL AS progress,
               NULL AS learnStatus,
               e.enroll_status AS enrollStatus,
               e.check_in_time AS checkInTime
          FROM main_training_enrollment e
          LEFT JOIN main_training t ON t.id = e.training_id AND t.del_flag = '0'
         WHERE e.student_id = #{studentId}
           AND e.del_flag = '0'

         ORDER BY trainingStartTime DESC
        """)
    List<StudentTrainingRecordVo> selectAllTrainingByStudentId(@Param("studentId") Long studentId);
}
