package com.storlead.centre.dispatch;

import com.storlead.centre.service.AttendanceSignRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.time.LocalTime;
import java.util.Date;

/**
 * @program: storlead-centre-platform
 * @description:
 * @author: chenkq
 * @create: 2026-01-07 14:57
 */

@Slf4j
@Component
public class AttendanceSignDispatchTask {

    @Resource
    private AttendanceSignRecordService attendanceSignRecordService;

    @Value("${environment}")
    private  String environment;
    /**
     * 同步考勤数据
     * @throws ParseException
     */
    @Scheduled(cron ="0 * * * * ? ")
    public void syncSignData() throws ParseException {

        if(!"prod".equals(environment)) {
            return;
        }
        LocalTime now = LocalTime.now();

        // ===== 总时间窗：06:00 – 22:30 =====
        if (now.isBefore(LocalTime.of(6, 0))
                || now.isAfter(LocalTime.of(22, 30))) {
            return;
        }

        boolean morningHighFrequency =
                (now.getHour() == 8 && now.getMinute() >= 30)
                        || (now.getHour() == 9 && now.getMinute() <= 10);

        boolean eveningHighFrequency =
                (now.getHour() == 18)
                        || (now.getHour() == 19 && now.getMinute() <= 40);

        boolean highFrequency = morningHighFrequency || eveningHighFrequency;
        // 非高频时间：只允许 5 分钟一次
        if (!highFrequency && now.getMinute() % 5 != 0) {
            return;
        }
        log.info("oa company dept job user sync task starting ----");
        try {
            attendanceSignRecordService.getSaveSignRecord();
        }catch (Exception e) {
            log.error("----getSaveSignRecord------error-"+e);
        }

        try {
            attendanceSignRecordService.getDelayedSign();
        }catch (Exception e) {
            log.error("----syncSignData------error-"+e);
        }
    }

}
