package com.pqs9900.task;

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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class MySchudler {
    private static final Logger logger = LoggerFactory.getLogger(MySchudler.class);
    @Autowired
    private Scheduler scheduler;
    private Map<MyTrigger, MyJobDetail> map = new HashMap<>();

    public Map<MyTrigger, MyJobDetail> getMap() {
        return map;
    }

    public void startJob(MyJobDetail myJobDetail, MyTrigger myTrigger, Integer state) {
        try {
            TriggerKey triggerKey = myTrigger.getCronTrigger().getKey();

            if (scheduler.getTriggerState(triggerKey) == Trigger.TriggerState.NONE) {
                if (state == 0) {
                    scheduler.scheduleJob(myJobDetail.getJobDetail(), myTrigger.getCronTrigger());
                }

                map.put(myTrigger, myJobDetail);
            } else {
                scheduler.resumeTrigger(myTrigger.getCronTrigger().getKey());
            }


        } catch (SchedulerException e) {
           logger.error("产生了异常，异常是"+e.getMessage());
        }
    }

    public void stopJob(MyTrigger myTrigger) {
        try {
            scheduler.pauseTrigger(myTrigger.getCronTrigger().getKey());
        } catch (SchedulerException e) {
            logger.error("产生了异常，异常是"+e.getMessage());
        }
    }

    public void destry(){
        try {
            scheduler.shutdown(true);
        } catch (SchedulerException e) {
            logger.error("产生了异常，异常是"+e.getMessage());
        }
    }
}
