package com.leanwo.activiti.rest.base;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.inject.Inject;
import com.leanwo.activiti.dto.BaseResponse;
import com.leanwo.activiti.dto.base.TaskDefineDto;
import com.leanwo.activiti.dto.base.TaskType;
import com.leanwo.activiti.service.base.HttpTaskEngine;
import com.leanwo.activiti.service.base.ProcessReportEngine;
import com.leanwo.activiti.service.base.QuartzEngine;
import com.leanwo.activiti.service.base.impl.HttpTaskEngineImpl;

/**
 * 任务流程资源类
 * 
 * @author Crazy Lei
 * @date 2022-06-13
 *
 */
@RestController
@RequestMapping("/workflow-prodog/TaskDefineResource")
public class TaskDefineResource {

	@Autowired
	private HttpTaskEngine httpTaskEngine;

	@Autowired
	private ProcessReportEngine processReportEngine;

	/**
	 * 添加任务
	 * 
	 * @param processInstanceDocRequest
	 * @return
	 * @throws SchedulerException
	 */
	@RequestMapping(value = "/addJob", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public BaseResponse addJob(@RequestBody TaskDefineDto taskDefineDto) throws SchedulerException {
		BaseResponse baseResponse = new BaseResponse();
		try {
			if (taskDefineDto == null) {
				baseResponse.setErrorCode(1);
				baseResponse.setErrorMessage("未获取到任务流程信息");
			} else {
				if (taskDefineDto.getTaskType().equals(TaskType.HTTP)) {
					baseResponse = httpTaskEngine.addJob(taskDefineDto);
				} else {
					baseResponse = processReportEngine.addJob(taskDefineDto);
				}
			}
		} catch (Exception e) {
			baseResponse.setErrorCode(-1);
			baseResponse.setErrorMessage("添加任务异常:" + e.getMessage());
		}
		return baseResponse;
	}

	/**
	 * 删除任务
	 * 
	 * @param processInstanceDocRequest
	 * @return
	 * @throws SchedulerException
	 */
	@RequestMapping(value = "/deleteJob", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public BaseResponse deleteJob(@RequestBody TaskDefineDto taskDefineDto) throws SchedulerException {
		BaseResponse baseResponse = new BaseResponse();
		try {
			if (taskDefineDto == null) {
				baseResponse.setErrorCode(1);
				baseResponse.setErrorMessage("未获取到任务流程信息");
			} else {
				if (taskDefineDto.getTaskType().equals(TaskType.HTTP)) {
					baseResponse = httpTaskEngine.deleteJob(taskDefineDto);
				} else {
					baseResponse = processReportEngine.deleteJob(taskDefineDto);
				}
			}
		} catch (Exception e) {
			baseResponse.setErrorCode(-1);
			baseResponse.setErrorMessage("删除任务异常:" + e.getMessage());
		}
		return baseResponse;
	}

}
