package com.sqx.modules.job.controller;

import com.sqx.common.annotation.SysLog;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.common.validator.ValidatorUtils;
import com.sqx.modules.job.dto.RunJobDTO;
import com.sqx.modules.job.entity.ScheduleJobEntity;
import com.sqx.modules.job.service.ScheduleJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

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

	private final ApplicationContext applicationContext;

    public ScheduleJobController(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }


    /**
	 * 定时任务列表
	 */
	@RequestMapping("/list")
	public Result list(@RequestParam Map<String, Object> params){
		PageUtils page = scheduleJobService.queryPage(params);

		return Result.success().put("page", page);
	}

	/**
	 * 定时任务信息
	 */
	@RequestMapping("/info/{jobId}")
	public Result info(@PathVariable("jobId") Long jobId){
		ScheduleJobEntity schedule = scheduleJobService.getById(jobId);

		return Result.success().put("schedule", schedule);
	}

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

		scheduleJobService.saveJob(scheduleJob);

		return Result.success();
	}

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

		scheduleJobService.update(scheduleJob);

		return Result.success();
	}

	/**
	 * 删除定时任务
	 */
	@SysLog("删除定时任务")
	@RequestMapping("/delete")
	public Result delete(@RequestBody Long[] jobIds){
		scheduleJobService.deleteBatch(jobIds);

		return Result.success();
	}

	/**
	 * 立即执行任务
	 */

	@SysLog("立即执行任务")
	@RequestMapping("/run")
	public Result run(@RequestBody Long[] jobIds) throws Exception {

		return Result.success().put("data", scheduleJobService.run(jobIds));
	}

//	@SysLog("立即执行任务")
//	@RequestMapping("/run")
//	public Result run(@RequestBody Long[] jobIds){
//		scheduleJobService.run(jobIds);
//
//		return Result.success();
//	}

//	/**
//	 * 暂停定时任务
//	 */
//	@SysLog("暂停定时任务")
//	@RequestMapping("/pause")
//	public Result pause(@RequestBody Long[] jobIds){
//		scheduleJobService.pause(jobIds);
//
//		return Result.success();
//	}
//
//	/**
//	 * 恢复定时任务
//	 */
//	@SysLog("恢复定时任务")
//	@RequestMapping("/resume")
//	public Result resume(@RequestBody Long[] jobIds){
//		scheduleJobService.resume(jobIds);
//
//		return Result.success();
//	}

}
