package com.lance.quartz.web;

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

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

import com.alibaba.fastjson.JSON;
import com.lance.quartz.common.exception.ServiceException;
import com.lance.quartz.common.json.ResultInfo;
import com.lance.quartz.domain.TaskInfo;
import com.lance.quartz.service.TaskServiceImpl;

/**
 * 任务管理
 * @author lance
 */
@Controller
public class TaskManageController {
	@Autowired
	private TaskServiceImpl taskServiceImpl;

	/**
	 * Index.jsp
	 * 2016年10月8日下午6:39:15
	 */
	@RequestMapping(value={"", "/", "index"})
	public String info(){
		return "index.jsp";
	}
	
	/**
	 * 任务列表
	 * @return
	 * 2016年10月9日上午11:36:03
	 */
	@ResponseBody
	@RequestMapping(value="list", method=RequestMethod.POST)
	public String list(){
		Map<String, Object> map = new HashMap<>();
		List<TaskInfo> infos = taskServiceImpl.list();
		map.put("rows", infos);
		map.put("total", infos.size());
		return JSON.toJSONString(map);
	}
	
	/**
	 * 保存定时任务
	 * @param info
	 * 2016年10月9日下午1:36:59
	 */
	@ResponseBody
	@RequestMapping(value="save", method=RequestMethod.POST, produces = "application/json; charset=UTF-8")
	public String save(TaskInfo info){
		try {
			if(info.getId() == 0) {
				taskServiceImpl.addJob(info);
			}else{
				taskServiceImpl.edit(info);
			}
		} catch (ServiceException e) {
			return ResultInfo.error(-1, e.getMessage());
		}
		return ResultInfo.success();
	}
	
	/**
	 * 删除定时任务
	 * @param jobName
	 * @param jobGroup
	 * 2016年10月9日下午1:52:20
	 */
	@ResponseBody
	@RequestMapping(value="delete/{jobName}/{jobGroup}", produces = "application/json; charset=UTF-8")
	public String delete(@PathVariable String jobName, @PathVariable String jobGroup){
		try {
			taskServiceImpl.delete(jobName, jobGroup);
		} catch (ServiceException e) {
			return ResultInfo.error(-1, e.getMessage());
		}
		return ResultInfo.success();
	}
}

@@ -2219,10 +2219,927 @@
 s();%0A%09%7D%0A
+%09%0A%09/**%0A%09 * %E6%9A%82%E5%81%9C%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1%0A%09 * @param jobName%0A%09 * @param jobGroup%0A%09 * 2016%E5%B9%B410%E6%9C%8810%E6%97%A5%E4%B8%8A%E5%8D%889:41:25%0A%09 */%0A%09@ResponseBody%0A%09@RequestMapping(value=%22pause/%7BjobName%7D/%7BjobGroup%7D%22, produces = %22application/json; charset=UTF-8%22)%0A%09public String pause(@PathVariable String jobName, @PathVariable String jobGroup)%7B%0A%09%09try %7B%0A%09%09%09taskServiceImpl.pause(jobName, jobGroup);%0A%09%09%7D catch (ServiceException e) %7B%0A%09%09%09return ResultInfo.error(-1, e.getMessage());%0A%09%09%7D%0A%09%09return ResultInfo.success();%0A%09%7D%0A%09%0A%09/**%0A%09 * %E9%87%8D%E6%96%B0%E5%BC%80%E5%A7%8B%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1%0A%09 * @param jobName%0A%09 * @param jobGroup%0A%09 * 2016%E5%B9%B410%E6%9C%8810%E6%97%A5%E4%B8%8A%E5%8D%889:41:40%0A%09 */%0A%09@ResponseBody%0A%09@RequestMapping(value=%22resume/%7BjobName%7D/%7BjobGroup%7D%22, produces = %22application/json; charset=UTF-8%22)%0A%09public String resume(@PathVariable String jobName, @PathVariable String jobGroup)%7B%0A%09%09try %7B%0A%09%09%09taskServiceImpl.resume(jobName, jobGroup);%0A%09%09%7D catch (ServiceException e) %7B%0A%09%09%09return ResultInfo.error(-1, e.getMessage());%0A%09%09%7D%0A%09%09return ResultInfo.success();%0A%09%7D%0A
 %7D%0A

add resume job method