package com.ym.mec.task.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.task.dal.model.Task;
import com.ym.mec.task.queryinfo.TaskQueryInfo;
import com.ym.mec.task.service.TaskService;
import com.ym.mec.util.collection.MapUtil;

@RestController
@RequestMapping(value = "task")
@Api(tags = "任务调度")
public class TaskController extends BaseController {

	private final static Logger LOGGER = LoggerFactory.getLogger(TaskController.class);

	@Autowired
	private TaskService taskService;

	@Autowired
	private Scheduler scheduler;

	@ApiOperation("任务列表分页查询")
	@GetMapping("/list")
	public HttpResponseResult<PageInfo<Task>> list(TaskQueryInfo queryInfo) {
		PageInfo<Task> pageInfo = new PageInfo<Task>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);

		List<Task> dataList = null;
		int count = taskService.queryCount(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = taskService.queryPage(params);
		}
		if (count == 0) {
			dataList = new ArrayList<Task>();
		}
		pageInfo.setRows(dataList);
		return succeed(pageInfo);
	}

	@ApiOperation("执行当前任务")
	@ApiImplicitParam(name = "taskId", value = "任务编号", required = true, dataType = "Integer", paramType = "path")
	@PostMapping("/execute/{taskId}")
	public HttpResponseResult<Object> execute(@PathVariable("taskId") int taskId) {
		boolean result = false;
		try {
			result = taskService.execute(taskId, scheduler);
		} catch (SchedulerException e) {
			LOGGER.error("手工执行任务[{}]失败", taskId, e);
			return failed(e.getMessage());
		}
		return result ? succeed() : failed();
	}

	@ApiOperation("暂停当前任务")
	@ApiImplicitParam(name = "taskId", value = "任务编号", required = true, dataType = "Integer", paramType = "path")
	@PostMapping("/pause/{taskId}")
	public HttpResponseResult<Object> pause(@PathVariable("taskId") int taskId) {
		boolean result = false;
		try {
			result = taskService.pause(taskId, scheduler);
		} catch (SchedulerException e) {
			LOGGER.error("手工暂停任务[{}]失败", taskId, e);
			return failed(e.getMessage());
		}
		return result ? succeed() : failed();
	}

	@ApiOperation("暂停当前任务")
	@ApiImplicitParam(name = "taskId", value = "任务编号", required = true, dataType = "Integer", paramType = "path")
	@PostMapping("/resume/{taskId}")
	public HttpResponseResult<Object> resume(@PathVariable("taskId") int taskId) {
		boolean result = false;
		try {
			result = taskService.resume(taskId, scheduler);
		} catch (SchedulerException e) {
			LOGGER.error("手工恢复任务[{}]失败", taskId, e);
			return failed(e.getMessage());
		}
		return result ? succeed() : failed();
	}
}
