package com.zcloud.modules.job.controller;

import com.zcloud.common.annotation.SysLog;
import com.zcloud.common.utils.PageUtils;
import com.zcloud.common.utils.R;
import com.zcloud.common.validator.ValidatorUtils;
import com.zcloud.modules.job.entity.ScheduleJobEntity;
import com.zcloud.modules.job.service.ScheduleJobService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 定时任务
 *
 */
@RestController
@RequestMapping("/sys/schedule")
public class ScheduleJobController {
	@Autowired
	private ScheduleJobService scheduleJobService;

	/**
	 * 定时任务列表
	 */
	@RequestMapping("/list")
	//@RequiresPermission("sys:schedule:list")
	public R list(@RequestBody Map<String, Object> params){
		PageUtils page = scheduleJobService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 定时任务信息
	 */
	@RequestMapping("/info")
	//@RequiresPermission("sys:schedule:info")
	public R info(@RequestBody ScheduleJobEntity scheduleJob){
		ScheduleJobEntity schedule = scheduleJobService.getById(scheduleJob.getJobId());
		return R.ok().put("schedule", schedule);
	}

	/**
	 * 保存定时任务
	 */
	@SysLog("保存定时任务")
	@RequestMapping("/save")
	//@RequiresPermission("sys:schedule:save")
	public R save(@RequestBody ScheduleJobEntity scheduleJob){
		ValidatorUtils.validateEntity(scheduleJob);

		scheduleJobService.saveJob(scheduleJob);

		return R.ok();
	}

	/**
	 * 修改定时任务
	 */
	@SysLog("修改定时任务")
	@RequestMapping("/update")
	//@RequiresPermission("sys:schedule:update")
	public R update(@RequestBody ScheduleJobEntity scheduleJob){
		ValidatorUtils.validateEntity(scheduleJob);

		scheduleJobService.update(scheduleJob);

		return R.ok();
	}

	/**
	 * 删除定时任务
	 */
	@SysLog("删除定时任务")
	@RequestMapping("/delete")
	//@RequiresPermission("sys:schedule:delete")
	public R delete(@RequestBody ScheduleJobEntity scheduleJob){
		scheduleJobService.removeById(scheduleJob.getJobId());

		return R.ok();
	}

	/**
	 * 立即执行任务
	 */
	@SysLog("立即执行任务")
	@RequestMapping("/run")
	//@RequiresPermission("sys:schedule:run")
	public R run(@RequestBody ScheduleJobEntity scheduleJob){
		scheduleJobService.run(scheduleJob.getJobId());

		return R.ok();
	}

	/**
	 * 暂停定时任务
	 */
	@SysLog("暂停定时任务")
	@RequestMapping("/pause")
	//@RequiresPermission("sys:schedule:pause")
	public R pause(@RequestBody ScheduleJobEntity scheduleJob){
		scheduleJobService.pause(scheduleJob.getJobId());

		return R.ok();
	}

	/**
	 * 恢复定时任务
	 */
	@SysLog("恢复定时任务")
	@RequestMapping("/resume")
	//@RequiresPermission("sys:schedule:resume")
	public R resume(@RequestBody ScheduleJobEntity scheduleJob){
		scheduleJobService.resume(scheduleJob.getJobId());

		return R.ok();
	}

}
