package org.springblade.flow.scheduletask.controller;

import java.util.List;

import javax.validation.Valid;

import org.springblade.flow.base.ActionResult;
import org.springblade.flow.base.Pagination;
import org.springblade.flow.base.vo.PageListVO;
import org.springblade.flow.base.vo.PaginationVO;
import org.springblade.flow.database.exception.DataException;
import org.springblade.flow.scheduletask.entity.TimeTaskEntity;
import org.springblade.flow.scheduletask.entity.TimeTaskLogEntity;
import org.springblade.flow.scheduletask.model.TaskCrForm;
import org.springblade.flow.scheduletask.model.TaskInfoVO;
import org.springblade.flow.scheduletask.model.TaskLogVO;
import org.springblade.flow.scheduletask.model.TaskPage;
import org.springblade.flow.scheduletask.model.TaskUpForm;
import org.springblade.flow.scheduletask.model.TaskVO;
import org.springblade.flow.scheduletask.service.TimetaskService;
import org.springblade.flow.util.JsonUtil;
import org.springblade.flow.util.JsonUtilEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 任务调度
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月27日 上午9:18
 */
@Api(tags = "任务调度", value = "TimeTask")
@RestController
@RequestMapping("/paas/scheduletask")
public class TimeTaskController {

	@Autowired
	private TimetaskService timetaskService;

	/**
	 * 获取任务调度列表
	 *
	 * @param pagination
	 * @return
	 */
	@ApiOperation("获取任务调度列表")
	@GetMapping
	public ActionResult<PageListVO<TaskVO>> list(Pagination pagination) {
		List<TimeTaskEntity> data = timetaskService.getList(pagination);
		List<TaskVO> list = JsonUtil.getJsonToList(data, TaskVO.class);
		PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
		return ActionResult.page(list, paginationVO);
	}

	/**
	 * 获取任务调度日志列表
	 *
	 * @param pagination
	 * @param taskId     任务Id
	 * @return
	 */
	@ApiOperation("获取任务调度日志列表")
	@GetMapping("/{id}/TaskLog")
	public ActionResult<PageListVO<TaskLogVO>> list(@PathVariable("id") String taskId, TaskPage pagination) {
		List<TimeTaskLogEntity> data = timetaskService.getTaskLogList(taskId, pagination);
		List<TaskLogVO> list = JsonUtil.getJsonToList(data, TaskLogVO.class);
		PaginationVO pageModel = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
		return ActionResult.page(list, pageModel);
	}

	/**
	 * 获取任务调度信息
	 *
	 * @param id 主键值
	 * @return
	 */
	@ApiOperation("获取任务调度信息")
	@GetMapping("/Info/{id}")
	public ActionResult<TaskInfoVO> info(@PathVariable("id") String id) throws DataException {
		TimeTaskEntity entity = timetaskService.getInfo(id);
		TaskInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, TaskInfoVO.class);
		return ActionResult.success(vo);
	}

	/**
	 * 新建任务调度
	 *
	 * @param taskCrForm
	 * @return
	 */
	@ApiOperation("新建任务调度")
	@PostMapping
	public ActionResult create(@RequestBody @Valid TaskCrForm taskCrForm) {
		TimeTaskEntity entity = JsonUtil.getJsonToBean(taskCrForm, TimeTaskEntity.class);
		if (timetaskService.isExistByFullName(entity.getFullName(), entity.getId())) {
			return ActionResult.fail("任务名称不能重复");
		}
		timetaskService.create(entity);
		return ActionResult.success("新建成功");
	}

	/**
	 * 修改任务调度
	 *
	 * @param id         主键值
	 * @param taskUpForm
	 * @return
	 */
	@ApiOperation("修改任务调度")
	@PutMapping("/{id}")
	public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid TaskUpForm taskUpForm) {
		TimeTaskEntity entity = JsonUtil.getJsonToBean(taskUpForm, TimeTaskEntity.class);
		if (timetaskService.isExistByFullName(entity.getFullName(), id)) {
			return ActionResult.fail("任务名称不能重复");
		}
		TimeTaskEntity taskEntity = timetaskService.getInfo(id);
		if (taskEntity == null) {
			return ActionResult.fail("更新失败，数据不存在");
		}
		entity.setEnabledMark(taskEntity.getEnabledMark());
		entity.setRunCount(taskEntity.getRunCount());
		boolean flag = timetaskService.update(id, entity);
		if (flag == false) {
			return ActionResult.fail("更新失败，数据不存在");
		}
		return ActionResult.success("更新成功");
	}

	/**
	 * 删除任务
	 *
	 * @param id 主键值
	 * @return
	 */
	@ApiOperation("删除任务")
	@DeleteMapping("/{id}")
	public ActionResult delete(@PathVariable("id") String id) {
		TimeTaskEntity entity = timetaskService.getInfo(id);
		if (entity != null) {
			timetaskService.delete(entity);
			return ActionResult.success("删除成功");
		}
		return ActionResult.fail("删除失败，数据不存在");
	}

	/**
	 * 停止任务调度
	 *
	 * @param id 主键值
	 * @return
	 */
	@ApiOperation("停止任务调度")
	@PutMapping("/{id}/Actions/Stop")
	public ActionResult stop(@PathVariable("id") String id) {
		TimeTaskEntity entity = timetaskService.getInfo(id);
		if (entity != null) {
			entity.setEnabledMark(0);
			timetaskService.update(id, entity);
			return ActionResult.success("操作成功");
		}
		return ActionResult.fail("操作失败，任务不存在");
	}

	/**
	 * 启动任务调度
	 *
	 * @param id 主键值
	 * @return
	 */
	@ApiOperation("启动任务调度")
	@PutMapping("/{id}/Actions/Enable")
	public ActionResult enable(@PathVariable("id") String id) {
		TimeTaskEntity entity = timetaskService.getInfo(id);
		if (entity != null) {
			entity.setEnabledMark(1);
			timetaskService.update(id, entity);
			return ActionResult.success("操作成功");
		}
		return ActionResult.fail("操作失败，任务不存在");
	}
}
