/**
 * 
 */
package org.springblade.etl.service;

import java.util.Optional;

import org.springblade.etl.entity.Job;
import org.springblade.etl.enums.InstanceRunType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wookvn
 *
 */
@Slf4j
@Component
public class XxlJobEtlTaskHandler {
	
	@Autowired
	private JobService jobService;

	/**
	 * 
	 * @return
	 */
	@XxlJob("jobHandler")
	public boolean jobHandler() {
		String param = Optional.<String>ofNullable(XxlJobHelper.getJobParam()).orElse("0");
		Integer jobId = Integer.valueOf(param);
		if(jobId <= 0) {
			return XxlJobHelper.handleFail("参数无效");
		}
		Job job = jobService.getById(jobId);
		if(job.getTriggerStatus() == 0) { //未开启任务调度
			log.warn("未开启任务调度|jobId={}", jobId);
			return XxlJobHelper.handleFail("未开启任务调度");
		}
		return jobService.jobRun(jobId, InstanceRunType.AUTO);
	}
}
