package com.tofly.qualitycontrol.config;

import com.tofly.qualitycontrol.service.IRectifyOrderService;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.util.Date;

/**
 * 动态配置扫描频率
 *
 * @author B
 */
@Data
@Configuration
@RequiredArgsConstructor
public class ScanScheduler implements SchedulingConfigurer {

	private final IRectifyOrderService rectifyOrderService;
	/** 初始扫描频率 */
	private String cron = "0 0 */6 * * ?";

	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		taskRegistrar.addTriggerTask(new Runnable() {
			@Override
			public void run() {
				rectifyOrderService.scanAndUpdate();
			}
		},new Trigger() {
			@Override
			public Date nextExecutionTime(@NonNull TriggerContext triggerContext) {
				CronTrigger cronTrigger = new CronTrigger(cron);
				return cronTrigger.nextExecutionTime(triggerContext);
			}
		});
	}

}
