using System.Linq; using System.Threading.Tasks; using ZhonTai.Admin.Core.Dto; using ZhonTai.Admin.Services.TaskScheduler.Dto; using ZhonTai.Admin.Domain.Task.Dto; using ZhonTai.DynamicApi; using ZhonTai.DynamicApi.Attributes; using Microsoft.AspNetCore.Mvc; using ZhonTai.Admin.Core.Consts; using FreeScheduler; using Microsoft.AspNetCore.Mvc.ModelBinding; using ZhonTai.Common.Extensions; using ZhonTai.Admin.Repositories; using ZhonTai.Admin.Core.Validators; namespace ZhonTai.Admin.Services.TaskScheduler; /// /// 任务服务 /// [Order(70)] [DynamicApi(Area = AdminConsts.AreaName)] public class TaskService : BaseService, ITaskService, IDynamicApi { private ITaskRepository _taskInfoRepository => LazyGetRequiredService(); public TaskService() { } /// /// 查询 /// /// /// public async Task GetAsync(long id) { var result = await _taskInfoRepository.GetAsync(id); return result; } /// /// 查询分页 /// /// /// [HttpPost] public async Task> GetPageAsync(PageInput input) { var topic = input.Filter?.Topic; var list = await _taskInfoRepository.Select .WhereDynamicFilter(input.DynamicFilter) .WhereIf(topic.NotNull(), a => a.Topic.Contains(topic)) .Count(out var total) .OrderByDescending(true, c => c.Id) .Page(input.CurrentPage, input.PageSize) .ToListAsync(); var data = new PageOutput() { List = list, Total = total }; return data; } /// /// 新增 /// /// /// public string Add(TaskAddInput input) { if (input.IntervalArgument.IsNull()) { throw ResultOutput.Exception("请输入定时参数"); } var scheduler = LazyGetRequiredService(); string id = null; switch (input.Interval) { case TaskInterval.SEC when input.Round == -1: id = scheduler.AddTask(input.Topic, input.Body, input.Round, input.IntervalArgument.ToInt()); break; case TaskInterval.SEC when input.Round > 0: { int[] seconds = System.Array.Empty(); var intervalArguments = input.IntervalArgument.Split(","); foreach (var arg in intervalArguments) { seconds.Append(arg.ToInt()); } id = scheduler.AddTask(input.Topic, input.Body, seconds); break; } case TaskInterval.RunOnDay when input.Round > 0: id = scheduler.AddTaskRunOnDay(input.Topic, input.Body, input.Round, input.IntervalArgument); break; case TaskInterval.RunOnWeek when input.Round > 0: id = scheduler.AddTaskRunOnWeek(input.Topic, input.Body, input.Round, input.IntervalArgument); break; case TaskInterval.RunOnMonth when input.Round > 0: id = scheduler.AddTaskRunOnMonth(input.Topic, input.Body, input.Round, input.IntervalArgument); break; case TaskInterval.Custom when input.Round > 0: id = scheduler.AddTaskCustom(input.Topic, input.Body, input.IntervalArgument); break; } return id; } /// /// 修改 /// /// /// public async Task UpdateAsync(TaskUpdateInput input) { var entity = await _taskInfoRepository.GetAsync(a => a.Id == input.Id); if (entity != null && entity.Id.NotNull()) { throw ResultOutput.Exception("任务不存在!"); } Mapper.Map(input, entity); await _taskInfoRepository.UpdateAsync(entity); } /// /// 暂停任务 /// /// /// public void Pause([BindRequired][ValidateRequired("请选择任务")]string id) { var scheduler = LazyGetRequiredService(); scheduler.PauseTask(id); } /// /// 启动任务 /// /// /// public void Resume([BindRequired][ValidateRequired("请选择任务")] string id) { var scheduler = LazyGetRequiredService(); scheduler.ResumeTask(id); } /// /// 执行任务 /// /// /// public void Run([BindRequired][ValidateRequired("请选择任务")] string id) { var scheduler = LazyGetRequiredService(); scheduler.RunNowTask(id); } /// /// 删除任务 /// /// /// public void Delete([BindRequired][ValidateRequired("请选择任务")] string id) { var scheduler = LazyGetRequiredService(); scheduler.RemoveTask(id); } }