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


import cn.hutool.core.util.StrUtil;
import cn.superdesk.uniorder.dao.entity.scheduler.SysQuartzJob;
import cn.superdesk.uniorder.dubbo.consumer.scheduler.SysQuartzJobDubboConsumerService;
import cn.superdesk.uniorder.executor.scheduler.utils.QuartzManage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2020/12/23 上午10:11
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020/12/23 上午10:11
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class JobRunner implements ApplicationRunner {

	@Resource
	private SysQuartzJobDubboConsumerService sysQuartzJobDubboConsumerService;

	@Resource
	private QuartzManage quartzManage;


	/**
	 * 项目启动时重新激活启用的定时任务
	 *
	 * @param applicationArguments /
	 */
	@Override
	public void run(ApplicationArguments applicationArguments) {

		log.info("-------------------- 定时任务调度器 初始化 ... ---------------------");

		// 查出 运行状态 启用状态
		List<SysQuartzJob> quartzJobs = sysQuartzJobDubboConsumerService.findSysQuartzJobByIsPauseEq0();

//		log.info("{}", JsonUtils.toJson(quartzJobs));


//		quartzJobs.forEach(quartzManage::addJob);

		StringBuffer stringBuffer = new StringBuffer();

		quartzJobs.forEach(quartzJob -> {

			quartzManage.addJob(quartzJob);

			stringBuffer.append(StrUtil.format("\n加载... 任务 -> {}({}) {} 频次 ->{}<-", quartzJob.getJobName(), quartzJob.getParams(), quartzJob.getRemark(), quartzJob.getCronExpression()));
		});

		log.info("\n{}\n", stringBuffer.toString());

		log.info("-------------------- 定时任务调度器 加载完成. ---------------------");
	}
}
