package cn.superdesk.uniorder.executor.scheduler.utils;

import cn.superdesk.uniorder.dao.entity.scheduler.SysQuartzJob;
import cn.superdesk.uniorder.dto.param.scheduler.SysQuartzJobParam;
import cn.superdesk.uniorder.dto.param.scheduler.SysQuartzLogParam;
import cn.superdesk.uniorder.dubbo.consumer.scheduler.SysQuartzJobDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.scheduler.SysQuartzLogDubboConsumerService;
import cn.superdesk.uniorder.executor.scheduler.config.thread.ThreadPoolExecutorUtil;
import cn.superdesk.uniorder.common.exception.utils.ExceptionThrowableUtils;
import cn.superdesk.uniorder.common.spring.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2020/12/23 上午9:53
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020/12/23 上午9:53
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Async
public class ExecutionJob extends QuartzJobBean {


	/**
	 * 该处仅供参考
	 */
	private final static ThreadPoolExecutor EXECUTOR = ThreadPoolExecutorUtil.getPoll();

	@Override
	@SuppressWarnings("unchecked")
	protected void executeInternal(JobExecutionContext context) {

		SysQuartzJob sysQuartzJob = (SysQuartzJob) context.getMergedJobDataMap().get(SysQuartzJobParam.JOB_KEY);

		// 获取spring bean
		SysQuartzJobDubboConsumerService sysQuartzJobDubboConsumerService = SpringContextHolder.getBean(SysQuartzJobDubboConsumerService.class);
		SysQuartzLogDubboConsumerService sysQuartzLogDubboConsumerService = SpringContextHolder.getBean(SysQuartzLogDubboConsumerService.class);

		SysQuartzLogParam sysQuartzLogParam = new SysQuartzLogParam();
		sysQuartzLogParam.setJobName(sysQuartzJob.getJobName());
		sysQuartzLogParam.setBaenName(sysQuartzJob.getBeanName());
		sysQuartzLogParam.setMethodName(sysQuartzJob.getMethodName());
		sysQuartzLogParam.setParams(sysQuartzJob.getParams());
		long startTime = System.currentTimeMillis();
		sysQuartzLogParam.setCronExpression(sysQuartzJob.getCronExpression());

		try {
			// 执行任务
//			log.info("任务准备执行,任务名称:{}", sysQuartzJob.getJobName());

			QuartzRunnable task   = new QuartzRunnable(sysQuartzJob.getBeanName(), sysQuartzJob.getMethodName(), sysQuartzJob.getParams());
			Future<?>      future = EXECUTOR.submit(task);
			future.get();

			long times = System.currentTimeMillis()-startTime;
			sysQuartzLogParam.setTime(times);
			// 任务状态
			sysQuartzLogParam.setIsSuccess(1);

//			log.info("任务执行完毕,任务名称:{} 总共耗时:{} 毫秒", sysQuartzJob.getJobName(), times);

		} catch (Exception e) {

			log.error("任务执行失败,任务名称:{}", sysQuartzJob.getJobName(), e);

			long times = System.currentTimeMillis()-startTime;

			sysQuartzLogParam.setTime(times);
			// 任务状态 是否成功:1-成功;-1-失败; TODO 写死
			sysQuartzLogParam.setIsSuccess(1);
			sysQuartzLogParam.setExceptionDetail(ExceptionThrowableUtils.getStackTrace(e));

			// 状态 是否暂停:1-暂停;0-启用;
			sysQuartzJob.setIsPause(1);

			// 更新状态
			sysQuartzJobDubboConsumerService.updateIsPause(sysQuartzJob);

		} finally {
			sysQuartzLogDubboConsumerService.addSysQuartzLog(sysQuartzLogParam);
		}
	}
}
