package com.haha.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.haha.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;
import java.util.Map;

/**
 * 签到记录Mapper
 */
@Mapper
public interface CheckinRecordMapper extends BaseMapper<CheckinRecord> {

    /**
     * 分页查询签到记录
     */
    @Select("<script>" +
            "SELECT cr.*, " +
            "  s.name as shop_name, " +
            "  d.name as device_name " +
            "FROM t_checkin_record cr " +
            "LEFT JOIN t_shop s ON cr.shop_id = s.id " +
            "LEFT JOIN t_device d ON cr.device_id = d.device_id " +
            "WHERE 1=1 " +
            "<if test='checkinType != null and checkinType != \"\"'> " +
            "  AND cr.checkin_type = #{checkinType} " +
            "</if> " +
            "<if test='userId != null'> " +
            "  AND cr.user_id = #{userId} " +
            "</if> " +
            "<if test='shopId != null'> " +
            "  AND cr.shop_id = #{shopId} " +
            "</if> " +
            "<if test='status != null and status != \"\"'> " +
            "  AND cr.status = #{status} " +
            "</if> " +
            "<if test='startDate != null and startDate != \"\"'> " +
            "  AND DATE(cr.checkin_time) &gt;= #{startDate} " +
            "</if> " +
            "<if test='endDate != null and endDate != \"\"'> " +
            "  AND DATE(cr.checkin_time) &lt;= #{endDate} " +
            "</if> " +
            "ORDER BY cr.checkin_time DESC " +
            "</script>")
    IPage<Map<String, Object>> selectCheckinPage(
            Page<Map<String, Object>> page,
            @Param("checkinType") String checkinType,
            @Param("userId") Long userId,
            @Param("shopId") Long shopId,
            @Param("status") String status,
            @Param("startDate") String startDate,
            @Param("endDate") String endDate);

    /**
     * 查询用户今日是否已签到（指定类型）
     */
    @Select("SELECT COUNT(*) FROM t_checkin_record " +
            "WHERE user_id = #{userId} " +
            "AND checkin_type = #{checkinType} " +
            "AND DATE(checkin_time) = CURDATE()")
    int countTodayCheckinByUserAndType(@Param("userId") Long userId, @Param("checkinType") String checkinType);

    /**
     * 查询用户今日签到记录
     */
    @Select("SELECT * FROM t_checkin_record " +
            "WHERE user_id = #{userId} " +
            "AND DATE(checkin_time) = CURDATE() " +
            "ORDER BY checkin_time DESC")
    List<CheckinRecord> selectTodayCheckinByUser(@Param("userId") Long userId);

    /**
     * 统计签到数据
     */
    @Select("SELECT " +
            "  COUNT(CASE WHEN DATE(checkin_time) = CURDATE() THEN 1 END) as today_count, " +
            "  COUNT(CASE WHEN YEARWEEK(checkin_time, 1) = YEARWEEK(CURDATE(), 1) THEN 1 END) as week_count, " +
            "  COUNT(CASE WHEN DATE_FORMAT(checkin_time, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m') THEN 1 END) as month_count, " +
            "  COUNT(CASE WHEN checkin_type = 'inventory_tally' THEN 1 END) as inventory_tally_count, " +
            "  COUNT(CASE WHEN checkin_type = 'delivery_replenish' THEN 1 END) as delivery_replenish_count, " +
            "  COUNT(CASE WHEN status = 'pending' THEN 1 END) as pending_sync_count " +
            "FROM t_checkin_record")
    Map<String, Object> selectCheckinStats();

    /**
     * 根据离线ID查询签到记录
     */
    @Select("SELECT * FROM t_checkin_record WHERE offline_id = #{offlineId}")
    CheckinRecord selectByOfflineId(@Param("offlineId") String offlineId);

    /**
     * 查询待同步的签到记录
     */
    @Select("SELECT * FROM t_checkin_record WHERE status = 'pending' ORDER BY checkin_time ASC LIMIT #{limit}")
    List<CheckinRecord> selectPendingSyncRecords(@Param("limit") int limit);

    /**
     * 按日期统计签到数量
     */
    @Select("SELECT " +
            "  DATE(checkin_time) as date, " +
            "  COUNT(*) as count " +
            "FROM t_checkin_record " +
            "WHERE checkin_time >= #{startDate} " +
            "AND checkin_time &lt;= #{endDate} " +
            "GROUP BY DATE(checkin_time) " +
            "ORDER BY date ASC")
    List<Map<String, Object>> selectCheckinCountByDateRange(
            @Param("startDate") LocalDate startDate,
            @Param("endDate") LocalDate endDate);
}
