package com.malk.pake.schedule;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.http.HttpUtil;
import com.malk.pake.service.PkMaintenanceWarningService;
import com.malk.pake.service.PkProjectService;
import com.malk.pake.service.PkWorkingHoursService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @EnableScheduling 开启定时任务 [配置参考McScheduleTask]
 */
@Slf4j
@Configuration
@EnableScheduling
@ConditionalOnProperty(name = {"enable.scheduling"})
public class ScheduleTask {

    @Autowired
    private PkWorkingHoursService pkWorkingHoursService;
    @Autowired
    private PkMaintenanceWarningService pkMaintenanceWarningService;
    @Autowired
    private PkProjectService pkProjectService;

    /**
     * 工时填写提醒-按月
     * 每月5-8号10:00提醒一次
     */
    @Scheduled(cron = "0 2 10 5-8 * ?")
    public void fillInReminderMonth() {
        log.info("工时填写提醒-按月");
        try {
            pkWorkingHoursService.fillInReminderMonth();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 工时填写提醒-按天
     * 每日10:00提醒一次
     */
    @Scheduled(cron = "0 2 10 * * ?")
    public void fillInReminderDay() {
        log.info("工时填写提醒-按天");
        try {
            String result= HttpUtil.get("https://tool.bitefu.net/jiari/?d=".concat(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))));
            if(result.equals("0")){
                // 工作日
                pkWorkingHoursService.fillInReminderDay();
            }else{
                log.info("周末或休息日，不提醒。接口返回：{}",result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 工时填写提醒-指定人员
     * 每月3-4号10:00提醒一次
     */
    @Scheduled(cron = "0 2 10 3-4 * ?")
    public void fillInReminderPeople() {
        log.info("工时填写提醒-指定人员");
        try {
            pkWorkingHoursService.fillInReminderPeople();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 工程维保项目-维保预警
     * 每天10:00重新计算维保剩余天数及变更维保预警状态，根据状态发送提醒
     */
    @Scheduled(cron = "0 2 10 * * ?")
    public void maintenanceWarning() {
        log.info("工程维保项目-维保预警");
        try {
            pkMaintenanceWarningService.warning();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 阿里商旅火车票
     * 每天00:00获取阿里商旅昨日火车票数据
     */
    @Scheduled(cron = "0 10 0 * * ?")
    public void getTrainInternal() {
        log.info("阿里商旅火车票");
        LocalDate now = LocalDate.now();
        String formatToday = LocalDateTimeUtil.format(now, "yyyy-MM-dd");
        LocalDate yesterday = now.plusDays(-1);
        String formatYesterday = LocalDateTimeUtil.format(yesterday, "yyyy-MM-dd");
        try {
            pkProjectService.getTrainInternal(formatYesterday,formatToday);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 阿里商旅机票
     * 每天00:30获取阿里商旅机票昨日数据
     */
    @Scheduled(cron = "0 30 1 * * ?")
    public void getAirInternal() {
        log.info("阿里商旅机票");
        LocalDate now = LocalDate.now();
        String formatToday = LocalDateTimeUtil.format(now, "yyyy-MM-dd");
        LocalDate yesterday = now.plusDays(-1);
        String formatYesterday = LocalDateTimeUtil.format(yesterday, "yyyy-MM-dd");
        try {
            pkProjectService.getAirInternal(formatYesterday,formatToday);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}