package com.jpsoft.shinestar.scheduled;

import cn.hutool.core.date.DateUtil;
import com.jpsoft.shinestar.config.OSSConfig;
import com.jpsoft.shinestar.config.WxConfig;
import com.jpsoft.shinestar.modules.base.constant.AttendanceTypes;
import com.jpsoft.shinestar.modules.base.entity.*;
import com.jpsoft.shinestar.modules.base.service.*;
import com.jpsoft.shinestar.modules.business.entity.WorkAttendance;
import com.jpsoft.shinestar.modules.business.service.WorkAttendanceService;
import com.jpsoft.shinestar.modules.common.utils.*;
import com.jpsoft.shinestar.modules.sys.entity.SysLog;
import com.jpsoft.shinestar.modules.sys.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.joda.time.DateTime;
import org.joda.time.Minutes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
@Slf4j
@Profile(value = {"deprecated"})
public class UnmeasureTemperatureAlarmTask {
    @Autowired
    private AlarmConfigService alarmConfigService;

    @Autowired
    private NoticeStatAsyncTask noticeStatAsyncTask;

    //每1分钟执行一次 fixedDelay：表示上一次任务执行完成后多久再次执行，参数类型为long，单位ms
//    @Scheduled(cron = "0 0/1 * * * *")
    public void run() {
        log.warn("测温提醒任务开始");

        DateTime now = DateTime.now();

        String time = now.toString("HH:mm");

        //查询包含当前时间的考勤时段
        List<AlarmConfig> configList = alarmConfigService.findByTime(time);

        String date = now.toString("yyyy-MM-dd");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        int intervalMinute = 1;

        for (AlarmConfig alarmConfig : configList) {
            try {
                DateTime startTime = new DateTime(sdf.parse(date + " " + alarmConfig.getStartTime()));
                DateTime attendanceTime = new DateTime(sdf.parse(date + " " + alarmConfig.getAttendanceTime()));
                DateTime endTime = new DateTime(sdf.parse(date + " " + alarmConfig.getEndTime()));

                DateTime alarmTime = null;

                if (alarmConfig.getClassifier() != null && alarmConfig.getClassifier().equals(1)) {
                    //todo 上班考勤时间提前10分钟通知未打卡人员
                    alarmTime = attendanceTime.plusMinutes(-10);
                } else {
                    //todo 下班考勤时间延迟10分钟通知未打卡人员
                    alarmTime = attendanceTime.plusMinutes(10);
                }

                int status = 0;

                //todo 上下班打卡提醒
                if (now.compareTo(alarmTime) >= 0 && now.compareTo(alarmTime.plusMinutes(intervalMinute)) < 0) {
                    status = 1;
                }
                //todo 考勤时间已到
                else if (now.compareTo(attendanceTime) >= 0 && now.compareTo(attendanceTime.plusMinutes(intervalMinute)) < 0) {
                    status = 2;
                }

                if(status==1 || status==2) {
                    noticeStatAsyncTask.noticeAndStat(now, alarmConfig, startTime, attendanceTime, endTime, status);
                }

                //todo 截止时间已到(可能与考勤时间重合)
                if (now.compareTo(endTime) >= 0 && now.compareTo(endTime.plusMinutes(intervalMinute)) < 0) {
                    status = 3;
                    noticeStatAsyncTask.noticeAndStat(now, alarmConfig, startTime, attendanceTime, endTime, status);
                }
            } catch (Exception ex) {
                log.error(ex.getMessage(),ex);
            }
        }

        log.warn("测温提醒任务结束");
    }
}
