package com.zhentao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhentao.entity.CheckinRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * 签到记录Mapper接口
 */
@Mapper
public interface CheckinRecordMapper extends BaseMapper<CheckinRecord> {
    
    /**
     * 查询用户在指定日期的签到记录
     */
    @Select("SELECT * FROM checkin_record WHERE user_id = #{userId} AND checkin_date = #{date} LIMIT 1")
    CheckinRecord selectByUserIdAndDate(@Param("userId") Long userId, @Param("date") LocalDate date);
    
    /**
     * 查询用户最近一次签到记录
     */
    @Select("SELECT * FROM checkin_record WHERE user_id = #{userId} ORDER BY checkin_date DESC LIMIT 1")
    CheckinRecord selectLatestByUserId(@Param("userId") Long userId);
    
    /**
     * 查询用户累计签到天数
     */
    @Select("SELECT COUNT(DISTINCT checkin_date) FROM checkin_record WHERE user_id = #{userId}")
    Integer countTotalDays(@Param("userId") Long userId);
    
    /**
     * 查询用户在指定月份的所有签到日期
     */
    @Select("SELECT checkin_date FROM checkin_record WHERE user_id = #{userId} " +
            "AND YEAR(checkin_date) = #{year} AND MONTH(checkin_date) = #{month} " +
            "ORDER BY checkin_date")
    List<LocalDate> selectMonthCheckinDates(@Param("userId") Long userId, 
                                             @Param("year") int year, 
                                             @Param("month") int month);
}

