package com.sundot.airport.quartz.task;

import cn.hutool.core.date.DateUtil;
import com.sundot.airport.attendance.domain.AttendancePostRecord;
import com.sundot.airport.attendance.service.IAttendancePostRecordService;
import com.sundot.airport.common.utils.DateUtils;
import com.sundot.airport.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component("attendanceTask")
public class AttendanceTask {
    private static final Logger log = LoggerFactory.getLogger(AttendanceTask.class);

    @Autowired
    private IAttendancePostRecordService attendancePostRecordService;


    @Autowired
    private ISysConfigService configService;

    /**
     * 每天9:00自动下通道
     */
    public void autoCheckOutAtNine() {
        try {
            log.info("开始执行自动下通道任务");
            String start = configService.selectConfigByKey("attendance.post.record.start.time");
            String end = configService.selectConfigByKey("attendance.post.record.end.time");
            // 获取昨天9:00到今天9:00之间的在岗记录
            Date yesterday = DateUtils.addDays(new Date(), -1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String yesterdayStr = sdf.format(yesterday);
            String todayStr = sdf.format(new Date());

            Date startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(yesterdayStr + " " + start);
            Date endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(todayStr + " " + end);

            // 查询在该时间段内未签退的记录
            AttendancePostRecord queryParam = new AttendancePostRecord();
            queryParam.setCheckInTimeStart(startTime);
            queryParam.setCheckInTimeEnd(endTime);
            queryParam.setCheckOutTimeType(1); // 未签退的记录

            List<AttendancePostRecord> records = attendancePostRecordService.selectAttendancePostRecordList(queryParam);

            // 自动签退这些记录
            Date autoCheckOutTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(todayStr + " " + end);
            int count = 0;
            for (AttendancePostRecord record : records) {
                try {
                    record.setCheckOutTime(autoCheckOutTime);
                    record.setWorkDuration((autoCheckOutTime.getTime() - record.getCheckInTime().getTime()) / 1000 / 60);
                    record.setUpdateBy("SYSTEM");
                    record.setUpdateTime(new Date());
                    record.setRemark((record.getRemark() != null ? record.getRemark() : "") + " 系统自动签退");
                    record.setLocked(false);
                    attendancePostRecordService.updateAttendancePostRecord(record);
                    count++;
                } catch (Exception e) {
                    log.error("自动签退记录失败，ID: " + record.getId(), e);
                }
            }

            log.info("自动下通道任务执行完成，共处理 {} 条记录", count);
        } catch (Exception e) {
            log.error("执行自动下通道任务失败", e);
        }
    }
}
