package com.ann.demo.job;

import com.ann.demo.primaryRepository.ScanningFileRepository;
import com.ann.demo.primaryEntity.ScanningFile;
import com.ann.demo.service.AnalysisService;
import com.ann.demo.utils.QuartzUtils;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ScheduledFuture;

/**
 * 取从属表未解析的数据
 *
 * @Author: LeiJiaXin
 * @Date: 2019/8/16 10:11
 */
@DisallowConcurrentExecution
public class ScheduledJob implements Job {

    static final Logger logger = LoggerFactory.getLogger(ScheduledJob.class);

    @Autowired
    private ScanningFileRepository scanningFileRepository;

    @Autowired
    private AnalysisService analysisService;

    @Autowired
    private Scheduler scheduler;

    @Override
    public void execute(JobExecutionContext context) {
        QuartzUtils.pauseScheduleJob(scheduler, "group1", "job1");
        try {
            List<ScanningFile> scanningFiles = scanningFileRepository.findScanningFiles();
            analysisService.handleScanningFile(scanningFiles);
        } catch (Exception e) {
            // 如果报错--  捕捉异常 继续执行
            logger.error("执行定时任务出错：{}" ,e.getMessage());
        }
        QuartzUtils.resumeScheduleJob(scheduler, "group1", "job1");
        logger.error("结束时间：{}" ,LocalDateTime.now());
    }

}

