package com.mvc.task;

import com.mvc.forwarmana.ForwarderThread;
import com.mvc.forwarmana.ZHYForwarder;
import com.mvc.service.TDeviceOperationService;
import com.mvc.service.aiiden.TaskReviewService;
import com.mvc.service.alarm.AlarmNoticeMana;
import com.mvc.service.alarm.TAlarmSubscriptionMana;
import com.mvc.service.copy.TCheckService;
import com.mvc.service.extend.CompleteData;
import com.mvc.service.sthjb.TPipelineLeakageService;
import com.mvc.service.taizhang.DeviceAlarmService;
import com.mvc.service.taizhang.TaskBatchRemoteService;
import com.mvc.service.upgrade.TUpgradePlanService;
import com.mvc.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class ScheduleTask {
    private final static Logger log = LoggerFactory.getLogger(com.mvc.task.ScheduleTask.class);

    @Resource
    private TaskReviewService taskReviewService;

    @Resource
    private DeviceAlarmService deviceAlarmService;

    @Resource
    private TaskBatchRemoteService taskBatchRemoteService;

    @Resource
    private TCheckService tCheckService;

    @Resource
    private AlarmNoticeMana alarmNoticeMana;

    @Resource
    private TUpgradePlanService upgradePlanService;

    @Resource
    private ForwarderThread forwarderThread;

    @Resource
    private ZHYForwarder zhyForwarder;

    @Resource
    private CompleteData completeData;

    @Resource
    private TAlarmSubscriptionMana alarmSubscription;

    @Resource
    private TDeviceOperationService deviceOperationService;

    @Scheduled(cron = "0 50 7 * * ? ")//经会议讨论,改为10点生成 2024-08-28 guozhao//改为8点,考虑研究生上课时间 2025-02-17
    private void generateTaskReview() {
        log.info("------生成审核数据任务开始------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
        String taskDate = DateUtil.getCurrentTime("yyyyMMdd");
        //taskReviewService.generateTaskReviewByStationAcc(taskDate, DeviceType.WATER_METER.getCode());

        taskReviewService.generateReviewDataByTaskDate(taskDate);//改为站点可配置审核时间 2024-07-22 guozhao
        log.info("------生成审核数据任务结束------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
    }

    @Scheduled(cron = "0 10 20 * * ? ")
    private void generateDeviceAlarmByTime() {
        log.info("------生成数据预警信息开始------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
        deviceAlarmService.generateDeviceAlarmByTime();
        log.info("------生成数据预警信息结束------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
    }

    @Resource
    private TPipelineLeakageService pipelineLeakageService;

    @Scheduled(cron = "0 */1 * * * ?")
    protected void executeTaskBatchRemote() {
        log.info("------执行批量远程任务开始------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
        taskBatchRemoteService.executeTaskBatchRemote();
        log.info("------执行批量远程任务结束------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));

        log.info("------拷机自动托管任务开始------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
        tCheckService.checkUnfinishedPlan();
        log.info("------拷机自动托管任务结束------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));

        //处理多次升级失败的设备
        upgradePlanService.handleMultipleFailedDevices();

        Date now = new Date();
        //23点30分判断是否有漏损任务 2025-07-02 guozhao
        if (DateUtil.getHours(now) == 23 && DateUtil.getMinutes(now) == 30) {
            log.info("------检测漏损任务开始------");
            pipelineLeakageService.createStartTask();
            log.info("------检测漏损任务结束------");
        }else if(DateUtil.getHours(now) == 0 && DateUtil.getMinutes(now) == 31){
            log.info("------完成漏损检测任务开始------");
            pipelineLeakageService.createEndTask();
        }
    }

    //@Scheduled(cron = "0 30 23 * * ? ")
    private void resendNoReceiptData() {
        log.info("------补发审核数据任务开始------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
        taskReviewService.resendNoReceiptData();
        log.info("------补发审核数据任务结束------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
    }

    /**
     * 系统空闲执行的任务(多个)
     */
    @Scheduled(cron = "0 15 7 * * ? ")
    private void systemIdleTaskScheduler() {
        //当前定时任务来处理一些非即时性,耗时的任务
        log.info("------生成燃气表数据丢失记录开始------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
        alarmNoticeMana.handleDataMissing();
        log.info("------生成燃气表数据丢失记录结束------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
    }

    //审核数据生成,将燃气表与水表时间统一了
    /*@Scheduled(cron = "0 55 6 * * ? ")
    private void generateTaskReview_GAS() {
        log.info("------生成燃气表审核数据任务开始------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
        String taskDate = DateUtil.getCurrentTime("yyyyMMdd");
        //taskReviewService.generateTaskReviewByStationAcc(taskDate, DeviceType.GAS_METER.getCode());

        taskReviewService.generateReviewDataByDateAndReviewTimeType(taskDate, "1");//改为站点可配置审核时间 2024-07-22 guozhao
        log.info("------生成燃气表审核数据任务结束------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
    }*/

    @Scheduled(cron = "0 36 0 * * ? ")
    private void forwarderData() {
        log.info("------数据转发任务开始------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
        forwarderThread.dealForwardData();
        log.info("------数据转发任务结束------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
    }

    @Scheduled(cron = "0 46 0 * * ? ")
    private void generateSlsDataQueue() {
        log.info("------生成转发水利所任务开始------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
        zhyForwarder.generateSlsDataQueue();
        log.info("------生成转发水利所任务结束------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
    }

    //@Scheduled(cron = "0 10 0,4,8,12,16,20 * * *")+
    @Scheduled(cron = "0 10 0,12 * * *")
    private void dealCyForwardData() {
        log.info("------处理朝阳数据任务开始------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
        forwarderThread.dealCyForwardData();
        log.info("------处理朝阳数据任务结束------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
    }

    @Scheduled(cron = "0 15 0 * * ? ")
    private void completeDataIfIncomplete() {
        log.info("------补昨日数据任务开始------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
        completeData.completeDataIfIncomplete();
        log.info("------补昨日数据任务结束------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
    }

    @Scheduled(cron = "0 10 12 * * ? ")
    private void handleSubscribeAlarm() {//在审核时间之后
        log.info("------报警通知任务开始------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
        alarmSubscription.handleSubscribeAlarm();
        log.info("------报警通知任务结束------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
    }

    @Scheduled(cron = "0 0 9-18 * * ?")  // 每天9~18点整触发
    private void handleMaintenanceNotify() {
        log.info("------运维安装提醒开始------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
        deviceOperationService.filterAndNotifyByEmail();
        log.info("------运维安装提醒结束------" + DateUtil.getCurrentTime(DateUtil.PATTERN_DATA_NORMAL));
    }
}
