using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Quartz;

namespace SlnMesnac.Controllers
{
    /// <summary>
    /// 任务调度
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class JobController
    {
        private readonly IScheduler _scheduler;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="scheduler"></param>
        public JobController(IScheduler scheduler)
        {
            _scheduler = scheduler;
        }

        /// <summary>
        /// 启动任务
        /// </summary>
        /// <param name="jobName">任务名称</param>
        /// <param name="groupName">任务分组</param>
        /// <returns></returns>
        [HttpPost("start")]
        public async Task<string> StartJob(string jobName, string groupName)
        {
            // 检查调度器是否已经启动
            if (!_scheduler.IsStarted)
            {
                await _scheduler.Start();
            }

            var myJobKey = new JobKey(jobName, groupName);
            await _scheduler.ResumeJob(myJobKey);

            return "Job started successfully.";
        }

        /// <summary>
        /// 停止任务
        /// </summary>
        /// <param name="jobName">任务名称</param>
        /// <param name="groupName">任务分组</param>
        /// <returns></returns>
        [HttpPost("stop")]
        public async Task<string> StopJob(string jobName, string groupName)
        {
            var myJobKey = new JobKey(jobName, groupName);
            await _scheduler.PauseJob(myJobKey);

            return "Job stopped successfully.";
        }
    }
}