package com.zcxk.quartz.controller;

import java.util.List;

import com.zcxk.model.AjaxMessage;
import com.zcxk.model.ResultStatus;
import com.zcxk.quartz.entity.QuartzEntity;
import com.zcxk.quartz.service.JobAndTriggerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


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

@SuppressWarnings("rawtypes")
@Api(tags = "任务管理")
@Controller
@RequestMapping("/quartz/")
public class QuartzController {
	
	@Autowired
	JobAndTriggerService jobAndTriggerService;
	
	@ResponseBody
    @PostMapping("/job/save/")
    @ApiOperation(value = "保存作业")
	public AjaxMessage saveJob(@RequestBody QuartzEntity quartz){
		jobAndTriggerService.addJob(quartz);
		return new AjaxMessage(ResultStatus.OK);
	}
	@ResponseBody
    @PostMapping("/job/list/")
    @ApiOperation(value = "展示作业列表")
	public AjaxMessage<List<QuartzEntity>> listJob(){
		List<QuartzEntity> listJobs = jobAndTriggerService.listJobs();
		return new AjaxMessage<List<QuartzEntity>>(ResultStatus.OK,listJobs);
	}
	
	@ResponseBody
    @PostMapping("/job/trigger/")
    @ApiOperation(value = "触发作业")
	public AjaxMessage triggerJob(@ApiParam(value = "任务名称", required = true) @RequestParam(required = true) String jobName,
			@ApiParam(value = "任务分组", required = true) @RequestParam(required = true) String jobGroup){
		QuartzEntity quartz = new QuartzEntity();
		quartz.setJobName(jobName);
		quartz.setJobGroup(jobGroup);
		jobAndTriggerService.triggerJob(quartz);
		return new AjaxMessage(ResultStatus.OK);
	}
	@ResponseBody
    @PostMapping("/job/stop/")
    @ApiOperation(value = "暂停作业")
	public AjaxMessage  stopJob(@ApiParam(value = "任务名称", required = true) @RequestParam(required = true) String jobName,
			@ApiParam(value = "任务分组", required = true) @RequestParam(required = true) String jobGroup){
		QuartzEntity quartz = new QuartzEntity();
		quartz.setJobName(jobName);
		quartz.setJobGroup(jobGroup);
		jobAndTriggerService.pauseJob(quartz);
		return new AjaxMessage(ResultStatus.OK);
	}
	
	@ResponseBody
    @PostMapping("/job/resume/")
    @ApiOperation(value = "恢复作业")
	public AjaxMessage  resumeJob(
			@ApiParam(value = "任务名称", required = true) @RequestParam(required = true) String jobName,
			@ApiParam(value = "任务分组", required = true) @RequestParam(required = true) String jobGroup){
		QuartzEntity quartz = new QuartzEntity();
		quartz.setJobName(jobName);
		quartz.setJobGroup(jobGroup);
		jobAndTriggerService.resumeJob(quartz);
		return new AjaxMessage(ResultStatus.OK);
	}
	@ResponseBody
    @PostMapping("/job/delete/")
    @ApiOperation(value = "删除作业")
	public AjaxMessage  deleteJob(
			@ApiParam(value = "任务名称", required = true) @RequestParam(required = true) String jobName,
			@ApiParam(value = "任务分组", required = true) @RequestParam(required = true) String jobGroup){
		QuartzEntity quartz = new QuartzEntity();
		quartz.setJobName(jobName);
		quartz.setJobGroup(jobGroup);
		jobAndTriggerService.deleteJob(quartz);
		return new AjaxMessage(ResultStatus.OK);
	}
}
