package cn.novalon.gym.manage.groupcourse.scheduler;

import cn.novalon.gym.manage.groupcourse.service.IGroupCourseBookingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 团课缺席会员定时任务
 * 
 * 功能：定期检查已开始但会员未到场的课程，自动更新预约记录状态为缺席
 * 
 * @author 张翔
 * @date 2026-06-02
 */
@Component
public class GroupCourseAbsentScheduler {

    private static final Logger logger = LoggerFactory.getLogger(GroupCourseAbsentScheduler.class);

    private final IGroupCourseBookingService bookingService;

    public GroupCourseAbsentScheduler(IGroupCourseBookingService bookingService) {
        this.bookingService = bookingService;
    }

    /**
     * 每分钟检查一次已开始课程但未到场会员的预约记录
     * 使用fixedRate保证任务间隔稳定执行
     */
    @Scheduled(fixedRate = 60000)
    public void checkAndUpdateAbsentMembers() {
        logger.debug("定时任务开始检查已开始课程但未到场会员的预约记录");
        
        bookingService.processAbsentMembers()
                .subscribe(
                        count -> logger.debug("定时任务完成，更新了 {} 条缺席记录", count),
                        error -> logger.error("定时任务执行失败：{}", error.getMessage(), error)
                );
    }
}