package com.ozs.da.service.job.impl;

import com.ozs.da.constant.Constant;
import com.ozs.da.entity.task.Task;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.Calendar;
import java.util.Date;

/**
 * @author 许明
 * on 2019/3/7.
 * @version 2.0.0
 * 频次触发调度
 */
public class FrequencyTriggerJob implements org.quartz.Job{

    private static final Logger logger = LogManager.getLogger(FrequencyTriggerJob.class);

    private static final String JOB_KEY = "jobid";

    /**
     * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
     */
    public void execute(JobExecutionContext context) throws JobExecutionException
    {
//        JobDetail jobDetail = context.getJobDetail();
//        Date current = new Date();
//        // 获取本次要执行的job信息，以及对应的时间出发表达式信息
//        String jobid = (String) jobDetail.getJobDataMap().get(JOB_KEY);
//        logger.debug("quartz任务JOBID[" + jobid + "]达到运行条件");
//
//        Task thisJob = JobServer.MEM_JOB_CACHE.get(jobid);
//        SimpleDateFormat format = new SimpleDateFormat();
//        format.applyPattern(thisJob.getDataFormat());
//        if (Constant.JOB_RUN_SUSPEND == thisJob.getRunStatus() || Constant.JOB_RUN_HANG_UP == thisJob.getRunStatus()
//                || Constant.JOB_RUN_LOCK == thisJob.getRunStatus())
//        {
//            logger.debug("job[" + jobid + "]处于暂停|挂起|锁定状态不进行触发");
//        }
//        else if ((thisJob.getTaskStatus() == Constant.JOB_RUN_WAIT || thisJob.getRunStatus() == Constant.JOB_RUN_START)
//                && thisJob.getIsParallel() == Constant.CANNOT_PARALLEL)
//        {
//            // 写执行日志，当前时间未执行
//            logger.warn("job[" + jobid + "]处于正在运行且不能并行，等待下次运行");
//        }
//        else
//        {
//            Date jobDataDate = getJobDataDate(thisJob, current);
//            Task runJob = new Task();
//            BeanUtils.copyProperties(thisJob, runJob);
//            runJob.setJobDataDate(format.format(jobDataDate));
//            runJob.setJobRunTriggerType(Constant.JOB_NORMAL_TRIGGER_TYPE);
//            // 修改执行状态,当前丢入可执行队列，准备运行
//            thisJob.setTaskStatus(Constant.JOB_RUN_WAIT);
//            ScheDisruptInitService scheDisruptInitService = (ScheDisruptInitService) BeanFactory.getBean("scheDisruptInitServiceImpl");
//            scheDisruptInitService.publishJobParseEvent(runJob);
//            logger.debug("Job[JOB_ID=" + jobid + "]达到运行条件，放入可执行队列运行");
//        }
    }

    private Date getJobDataDate(Task job, Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        switch (job.getPeriodType())
        {
            case Constant.JOB_PERIOD_YEAR:
                cal.add(Calendar.YEAR, -1);
                break;
            case Constant.JOB_PERIOD_WEEK:
                cal.add(Calendar.WEEK_OF_YEAR, -1);
                break;
            case Constant.JOB_PERIOD_MONTH:
                cal.add(Calendar.MONTH, -1);
                break;
            case Constant.JOB_PERIOD_DAY:
                cal.add(Calendar.DAY_OF_YEAR, -1);
                break;
            case Constant.JOB_PERIOD_HOUR:
                cal.add(Calendar.HOUR, -1);
                break;
            case Constant.JOB_PERIOD_MINUTE:
                cal.add(Calendar.MINUTE, -1);
                break;
            case Constant.JOB_PERIOD_SECONDS:
                cal.add(Calendar.SECOND, -1);
                break;
            default:
                logger.error("JOB[" + job.getTaskName() + "]的周期类型错误");
                break;
        }
        return cal.getTime();
    }
}
