package org.dromara.main.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.MainTraining;
import org.dromara.main.domain.vo.MainTrainingLearnRecordVo;
import org.dromara.main.domain.vo.MainTrainingParticipantVo;
import org.dromara.main.domain.vo.MainTrainingVo;

import java.util.List;

@Mapper
public interface MainTrainingMapper extends BaseMapperPlus<MainTraining, MainTrainingVo> {

    /**
     * 统计培训的学习记录数
     */
    @Select("SELECT COUNT(*) FROM main_training_learn_record WHERE training_id = #{trainingId} AND del_flag = '0'")
    Long countLearnRecords(@Param("trainingId") Long trainingId);

    /**
     * 统计培训的报名人数
     */
    @Select("SELECT COUNT(*) FROM main_training_enrollment WHERE training_id = #{trainingId} AND del_flag = '0'")
    Long countEnrollments(@Param("trainingId") Long trainingId);

    /**
     * 查询视频培训学习记录分页
     */
    @Select("""
        SELECT r.student_id AS studentId,
               s.name AS name,
               s.mobile AS mobile,
               s.avatar AS avatar,
               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_student s ON s.id = r.student_id AND s.del_flag = '0'
         WHERE r.training_id = #{trainingId}
           AND r.del_flag = '0'
         ORDER BY r.last_learn_time DESC, r.id DESC
        """)
    IPage<MainTrainingLearnRecordVo> selectLearnRecordPage(Page<MainTrainingLearnRecordVo> page, @Param("trainingId") Long trainingId);

    /**
     * 按学员ID导出视频培训学习记录
     */
    @Select("""
        <script>
        SELECT r.student_id AS studentId,
               s.name AS name,
               s.mobile AS mobile,
               s.avatar AS avatar,
               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_student s ON s.id = r.student_id AND s.del_flag = '0'
         WHERE r.training_id = #{trainingId}
           AND r.del_flag = '0'
           AND r.student_id IN
           <foreach collection='studentIds' item='studentId' open='(' separator=',' close=')'>
             #{studentId}
           </foreach>
         ORDER BY r.last_learn_time DESC, r.id DESC
        </script>
        """)
    List<MainTrainingLearnRecordVo> selectLearnRecordListByStudentIds(@Param("trainingId") Long trainingId, @Param("studentIds") List<Long> studentIds);

    /**
     * 查询线下培训参与人分页
     */
    @Select("""
        SELECT s.id AS studentId,
               s.name AS name,
               s.mobile AS mobile,
               s.avatar AS avatar,
               e.enroll_time AS enrollTime,
               e.check_in_time AS checkInTime
          FROM main_training_enrollment e
          LEFT JOIN main_student s ON s.id = e.student_id AND s.del_flag = '0'
         WHERE e.training_id = #{trainingId}
           AND e.del_flag = '0'
         ORDER BY e.enroll_time DESC, e.id DESC
        """)
    IPage<MainTrainingParticipantVo> selectOfflineParticipantPage(Page<MainTrainingParticipantVo> page, @Param("trainingId") Long trainingId);

    /**
     * 按学员ID导出线下培训参与人
     */
    @Select("""
        <script>
        SELECT s.id AS studentId,
               s.name AS name,
               s.mobile AS mobile,
               s.avatar AS avatar,
               e.enroll_time AS enrollTime,
               e.check_in_time AS checkInTime
          FROM main_training_enrollment e
          LEFT JOIN main_student s ON s.id = e.student_id AND s.del_flag = '0'
         WHERE e.training_id = #{trainingId}
           AND e.del_flag = '0'
           AND e.student_id IN
           <foreach collection='studentIds' item='studentId' open='(' separator=',' close=')'>
             #{studentId}
           </foreach>
         ORDER BY e.enroll_time DESC, e.id DESC
        </script>
        """)
    List<MainTrainingParticipantVo> selectOfflineParticipantListByStudentIds(@Param("trainingId") Long trainingId, @Param("studentIds") List<Long> studentIds);
}
