package com.uas.search.console.b2b.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uas.search.b2b.service.SearchService.Table_name;
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.util.ClassAndTableNameUtils;

@Controller
@RequestMapping("/schedule")
public class ScheduleController {

	@Autowired
	private UpdateVirtualColumnService updateVirtualColumnService;

	@Autowired
	private TaskService taskService;

	@RequestMapping("/updateOverdue")
	@ResponseBody
	public String updateOverdue(@RequestParam(required = true) String tableName,
			@RequestParam(required = true) Integer hour, @RequestParam(required = true) Integer minute,
			@RequestParam(required = true) Integer second) {
		Table_name table = Table_name.valueOf(tableName.toUpperCase());
		if (table != Table_name.PURC$INQUIRYMOULD && table != Table_name.PURC$INQUIRYITEMS) {
			return "不支持更新虚拟列overdue索引：" + tableName;
		}
		String message = "已开启：" + updateVirtualColumnService
				.newUpdateOverdueDailyTask(ClassAndTableNameUtils.toClass(table), hour, minute, second);
		restart();
		return message;
	}

	@RequestMapping("/tasks")
	@ResponseBody
	public List<TaskInformation> allTaskInformations() {
		return taskService.allTaskInformations();
	}

	@RequestMapping("/start")
	@ResponseBody
	public String start() {
		return taskService.start();
	}

	@RequestMapping("/stop")
	@ResponseBody
	public String stop() {
		return taskService.stop();
	}

	@RequestMapping("/restart")
	@ResponseBody
	public String restart() {
		if (!taskService.isStopped()) {
			taskService.stop();
		}
		return taskService.start();
	}

	@RequestMapping("/isStopped")
	@ResponseBody
	public boolean isStopped() {
		return taskService.isStopped();
	}

}
