package com.foreverwin.mesnac.quartz.job;

import com.foreverwin.mesnac.anomaly.service.AnomalyJobService;
import com.foreverwin.mesnac.common.util.StringUtil;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description TODO
 * @Author zhaojiawei
 * @Since 2021-07-22
 */
@DisallowConcurrentExecution
public class AnomalyJob implements Job {
    private static final String SITE_UPPERCASE = "SITE";
    private static final String SITE_LOWERCASE = "site";

    private Logger logger = LoggerFactory.getLogger(AnomalyJob.class);

    @Autowired
    private AnomalyJobService anomalyJobService;

    @Override
    public void execute(JobExecutionContext context) {
        logger.info("异常定时任务开始执行");
        //获取站点，若站点为空则抛出异常
        String site = getSite(context);
        if (StringUtil.isBlank(site)) {
            logger.error("异常定时任务未配置站点参数！");
            return;
        }
        anomalyJobService.anomalyJobProcess(site);
    }

    private String getSite(JobExecutionContext context) {
        String site = context.getJobDetail().getJobDataMap().getString(SITE_LOWERCASE);
        if (StringUtil.isBlank(site)) {
            site = context.getJobDetail().getJobDataMap().getString(SITE_UPPERCASE);
        }
        return site;
    }
}
