package org.security.trans.taskCheck.task;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.security.common.Constant;
import org.security.common.eum.FrequencyEnum;
import org.security.trans.task.entity.Task;
import org.security.trans.task.service.ITaskService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zj 2022/4/13
 */
@Slf4j
@AllArgsConstructor
@Component
public class TaskCheckTask {
	private final ITaskService taskService;

	/**
	 * 检查任务提报状态
	 * 检查各个该提报机构是否按时提报，
	 * 若未提报，则生成对应超时未提报记录
	 */
	@Async("securityThreadPool")
	@Scheduled(cron = "0 0 1 * * ?")
	public void check() {
		log.info("===============任务检查开始执行===============");
		List<Task> taskList = taskService.getTaskCheckList(null, Constant.TASK_STATE_PUBLISH, null, null);
		if (taskList.isEmpty()) {
			log.info("===============不存在状态为[已发布]的任务列表");
			return;
		}
		taskList.forEach(task -> {
			ITaskCheck taskCheck = TaskCheckFactory.getTaskCheck(FrequencyEnum.wrapper(task.getFrequency()));
			taskCheck.handle(task);
		});
		log.info("===============任务检查执行完成===============");
	}
}
