package org.security.trans.taskCheck.task;

import org.security.common.eum.FrequencyEnum;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author zj 2022/4/11
 * 根据类型获取对应
 */
@Component
public class TaskCheckFactory implements ApplicationContextAware {
	private static ApplicationContext context;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context = applicationContext;
	}

	/**
	 * 获取对应taskCheck
	 *
	 * @param frequencyEnum 类型
	 * @return ITaskCheck
	 */
	public static ITaskCheck getTaskCheck(FrequencyEnum frequencyEnum) {
		Map<String, ITaskCheck> map = context.getBeansOfType(ITaskCheck.class);
		for (ITaskCheck taskCheck : map.values()) {
			if (taskCheck.support(frequencyEnum)) {
				return taskCheck;
			}
		}
		return (ITaskCheck) context.getBean("unknowTaskCheck");
	}
}
