package com.uas.search.console.b2b;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.uas.search.console.b2b.core.util.ContextUtils;
import com.uas.search.console.b2b.model.PurchaseInquiryItemSimpleInfo;
import com.uas.search.console.b2b.model.PurchaseInquiryMouldSimpleInfo;
import com.uas.search.console.b2b.schedule.service.TaskService;
import com.uas.search.console.b2b.service.RealTimeUpdateMonitorService;
import com.uas.search.console.b2b.service.UpdateVirtualColumnService;

@EnableTransactionManagement
@EnableWebMvc
@SpringBootApplication(scanBasePackages = "com.uas.search.console.b2b")
@ImportResource({ "classpath:spring/dubbo.xml" })
public class Application {
	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(Application.class);
		application.addListeners(new ContextUtils());
		application.run(args);

		startTask();
	}

	/**
	 * 开启定时任务
	 */
	public static void startTask() {
		SystemProperties systemProperties = ContextUtils.getBean(SystemProperties.class);
		if (systemProperties.isTaskAutoStart()) {
			UpdateVirtualColumnService updateVirtualColumnService = ContextUtils
					.getBean(UpdateVirtualColumnService.class);
			// 定时更新模具询价单虚拟列索引
			updateVirtualColumnService.newUpdateOverdueDailyTask(PurchaseInquiryMouldSimpleInfo.class,
					systemProperties.getDailyTaskHour(), systemProperties.getDailyTaskMinute(),
					systemProperties.getDailyTaskSecond());
			// 定时更新采购询价单明细虚拟列索引
			updateVirtualColumnService.newUpdateOverdueDailyTask(PurchaseInquiryItemSimpleInfo.class,
					systemProperties.getDailyTaskHour(), systemProperties.getDailyTaskMinute(),
					systemProperties.getDailyTaskSecond());

			// 定时监控实时更新异常情况
			RealTimeUpdateMonitorService realTimeUpdateMonitorService = ContextUtils
					.getBean(RealTimeUpdateMonitorService.class);
			realTimeUpdateMonitorService.newTask();

			// 开启定时任务
			TaskService taskService = ContextUtils.getBean(TaskService.class);
			if (!taskService.isStopped()) {
				taskService.stop();
			}
			taskService.start();
		}
	}
}
