package com.fs.course.task;


import com.fs.common.core.redis.RedisCache;
import com.fs.course.service.IFsCourseWatchLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class CourseWatchLogScheduler {


    @Autowired
    RedisCache redisCache;


    @Autowired
    private IFsCourseWatchLogService courseWatchLogService;


    /**
     * 检查飞书看课状态
     * 每分钟执行一次
     */
    @Scheduled(fixedRate = 60000)
    public void checkFeiShuWatchStatus() {
        if (!redisCache.setIfAbsent("checkFeiShuWatchStatus", "checkFeiShuWatchStatus", 3, TimeUnit.MINUTES)) {
            log.warn("检查飞书看课中任务执行 - 上一个任务尚未完成，跳过此次执行");
            return;
        }
        try {
            log.info("检查飞书看课中任务执行>>>>>>>>>>>>");
            courseWatchLogService.scheduleFeiShuBatchUpdateToDatabase();
            courseWatchLogService.checkFeiShuWatchStatus();
            log.info("检查飞书看课中任务执行完成>>>>>>>>>>>>");
        }catch (Exception e) {
            log.error("检查飞书看课中任务执行完成 - 定时任务执行失败", e);
        } finally {
            redisCache.deleteObject("checkFeiShuWatchStatus");
        }
    }

}
