package com.seer.teach.teacher.module.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.teacher.module.entity.ReadingRecordsEntity;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 用户阅读记录表 Mapper 接口
 * </p>
 *
 * @author Captain
 * @since 2025-06-13
 */
public interface ReadingRecordsMapper extends BaseMapper<ReadingRecordsEntity> {

    @Select(" SELECT r.*" +
            "        FROM teacher_reading_records r" +
            "                 INNER JOIN (SELECT book_id, MAX(last_read_time) AS max_time" +
            "                             FROM teacher_reading_records" +
            "                             WHERE user_id = #{userId}" +
            "                             GROUP BY book_id) t ON r.book_id = t.book_id AND r.last_read_time = t.max_time" +
            "        WHERE r.user_id = #{userId}" +
            "        ORDER BY r.last_read_time DESC")
    IPage<ReadingRecordsEntity> selectLatestReadingByUser(Page<?> page, Integer userId);

}
