package com.kxs.daemon.quartz.util;

import cn.hutool.core.util.StrUtil;
import com.kxs.common.core.util.SpringContextHolder;
import com.kxs.daemon.quartz.constants.JobTypeQuartzEnum;
import com.kxs.daemon.quartz.exception.TaskException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Hccake
 * @version 1.0
 * @date 2021/8/8 15:40 TaskInvok工厂类
 */
@Slf4j
public class TaskInvokFactory {

	/**
	 * 根据对应jobType获取对应 invoker
	 * @param jobType
	 * @return
	 * @throws TaskException
	 */
	public static ITaskInvoke getInvoker(String jobType) throws TaskException {
		if (StrUtil.isBlank(jobType)) {
			log.info("获取TaskInvok传递参数有误，jobType:{}", jobType);
			throw new TaskException("");
		}

		ITaskInvoke iTaskInvoke = null;
		if (JobTypeQuartzEnum.JAVA.getType().equals(jobType)) {
			iTaskInvoke = SpringContextHolder.getBean("javaClassTaskInvoke");
		}
		else if (JobTypeQuartzEnum.SPRING_BEAN.getType().equals(jobType)) {
			iTaskInvoke = SpringContextHolder.getBean("springBeanTaskInvoke");
		}
		else if (JobTypeQuartzEnum.REST.getType().equals(jobType)) {
			iTaskInvoke = SpringContextHolder.getBean("restTaskInvoke");
		}
		else if (JobTypeQuartzEnum.JAR.getType().equals(jobType)) {
			iTaskInvoke = SpringContextHolder.getBean("jarTaskInvoke");
		}
		else if (StrUtil.isBlank(jobType)) {
			log.info("定时任务类型无对应反射方式，反射类型:{}", jobType);
			throw new TaskException("");
		}

		return iTaskInvoke;
	}

}
