package com.uas.search.console.b2b;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.util.ContextUtils;
import com.uas.search.console.b2b.model.B2BSaleTenderSimpleInfo;
import com.uas.search.console.b2b.model.PurchaseInquiryItemSimpleInfo;
import com.uas.search.console.b2b.model.PurchaseInquiryMouldSimpleInfo;
import com.uas.search.console.b2b.model.PurchaseTenderSimpleInfo;
import com.uas.search.console.b2b.model.SaleQuotationSimpleInfo;
import com.uas.search.console.b2b.schedule.model.TaskInformation;
import com.uas.search.console.b2b.schedule.service.TaskService;
import com.uas.search.console.b2b.service.UpdateVirtualColumnService;
import com.uas.search.console.b2b.support.RealTimeUpdateMonitor;
import com.uas.search.console.b2b.util.SearchConstants.DataSourceQualifier;

@EnableTransactionManagement
@EnableWebMvc
@SpringBootApplication(scanBasePackages = "com.uas.search.console.b2b")
@ImportResource({ "classpath:spring/dubbo.xml" })
public class Application {

	public static void main(String[] args) throws NotSerializableException, ClassNotFoundException, IOException {
		File logFile = new File("logs/log.log");
		if (!logFile.getParentFile().exists()) {
			logFile.getParentFile().mkdir();
		}
		System.setErr(new PrintStream(new FileOutputStream(logFile, true)));
		SpringApplication application = new SpringApplication(Application.class);
		application.addListeners(new ContextUtils());
		application.run(args);
		SystemProperties systemProperties = ContextUtils.getBean(SystemProperties.class);
		if (systemProperties.isTaskAutoStart()) {
			startTask(systemProperties.getDailyTaskHour(), systemProperties.getDailyTaskMinute(),
					systemProperties.getDailyTaskSecond());
		}
	}

	/**
	 * 开启定时任务
	 * 
	 * @param hour
	 *            可为空
	 * @param minute
	 *            可为空
	 * @param second
	 *            可为空
	 * @return 定时任务信息
	 * @throws NotSerializableException
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public static List<TaskInformation> startTask(Integer hour, Integer minute, Integer second)
			throws NotSerializableException, ClassNotFoundException, IOException {
		UpdateVirtualColumnService updateVirtualColumnService = ContextUtils.getBean(UpdateVirtualColumnService.class);
		// 需更新的虚拟列 定时更新采购询价单明细虚拟列索引
		Map<String, Class<?>> purchaseInquiryItemFields = new HashMap<>();
		purchaseInquiryItemFields.put("overdue", Short.class);
		purchaseInquiryItemFields.put("invalid", Short.class);
		updateVirtualColumnService.newDailyTask(PurchaseInquiryItemSimpleInfo.class, purchaseInquiryItemFields, hour,
				minute, second);

		// 定时更新主动报价单虚拟列索引
		Map<String, Class<?>> saleQuotationFields = new HashMap<>();
		saleQuotationFields.put("overdue", Short.class);
		updateVirtualColumnService.newDailyTask(SaleQuotationSimpleInfo.class, saleQuotationFields, hour, minute,
				second);

		// 需更新的虚拟列 定时更新模具询价单虚拟列索引
		Map<String, Class<?>> inquiryMouldFields = new HashMap<>();
		inquiryMouldFields.put("overdue", Short.class);
		updateVirtualColumnService.newDailyTask(PurchaseInquiryMouldSimpleInfo.class, inquiryMouldFields, hour, minute,
				second);

		// 定时更新招标单虚拟列索引
		Map<String, Class<?>> purchaseTenderFields = new HashMap<>();
		purchaseTenderFields.put("overdue", Short.class);
		purchaseTenderFields.put("result", Short.class);
		updateVirtualColumnService.newDailyTask(PurchaseTenderSimpleInfo.class, purchaseTenderFields, hour, minute,
				second);

		// 定时更新投标单虚拟列索引
		Map<String, Class<?>> saleTenderFields = new HashMap<>();
		saleTenderFields.put("overdue", Short.class);
		saleTenderFields.put("result", Short.class);
		updateVirtualColumnService.newDailyTask(B2BSaleTenderSimpleInfo.class, saleTenderFields, hour, minute, second);

		// 定时监控实时更新异常情况
		new RealTimeUpdateMonitor(DataSourceQualifier.PLATFORMB2B_DATASOURCE).newTask();
		new RealTimeUpdateMonitor(DataSourceQualifier.PLATFORMMANAGER_DATASOURCE).newTask();

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