From 104cfb24630e7cd8c0e90e3fc42ba103952936d9 Mon Sep 17 00:00:00 2001 From: j4587698 Date: Wed, 12 Feb 2020 15:23:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=9A=E6=97=B6=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Admin/Controllers/CollectionController.cs | 25 ++++++++++++++++--- .../Admin/Views/Collection/Index.cshtml | 5 +++- CMS/JXCMS.CMS.Movie/Entity/ClassifyEntity.cs | 11 ++++++++ CMS/JXCMS.CMS.Movie/Jobs/CollectionJob.cs | 19 ++++++++++++++ Core/JXCMS.Core/JXCMS.Core.csproj | 2 ++ Core/JXCMS.Core/TimingTask/QuartzTask.cs | 25 ++++++++++++++++--- 6 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 CMS/JXCMS.CMS.Movie/Entity/ClassifyEntity.cs create mode 100644 CMS/JXCMS.CMS.Movie/Jobs/CollectionJob.cs diff --git a/CMS/JXCMS.CMS.Movie/Admin/Controllers/CollectionController.cs b/CMS/JXCMS.CMS.Movie/Admin/Controllers/CollectionController.cs index c77f2a5..4a63e4f 100644 --- a/CMS/JXCMS.CMS.Movie/Admin/Controllers/CollectionController.cs +++ b/CMS/JXCMS.CMS.Movie/Admin/Controllers/CollectionController.cs @@ -1,9 +1,13 @@ +using System.Collections.ObjectModel; using System.Linq; using JXCMS.CMS.Attribute; using JXCMS.CMS.Movie.Entity; +using JXCMS.CMS.Movie.Jobs; +using JXCMS.Core.TimingTask; using Microsoft.AspNetCore.Mvc; +using Quartz; -namespace JXCMS.CMS.Admin.Controllers +namespace JXCMS.CMS.Movie.Admin.Controllers { [Area("Admin")] [AdminAuthentication] @@ -24,25 +28,39 @@ namespace JXCMS.CMS.Admin.Controllers public IActionResult AddWebSite(WebSiteEntity webSiteEntity) { webSiteEntity.Save(); + if (webSiteEntity.IsEnable) + { + QuartzTask.Instance().AddTask(typeof(CollectionJob), webSiteEntity.Cron, webSiteEntity.Id.ToString(), "default", ("id", webSiteEntity.Id)); + } + return Redirect(Url.Action("Index")); } public IActionResult DeleteWebSite(int id) { WebSiteEntity.Find(id).Delete(); + QuartzTask.Instance().DeleteTask(id.ToString()); return Redirect(Url.Action("Index")); } public IActionResult DeleteWebSiteBatch(int[] ids) { WebSiteEntity.Where(x => ids.Contains(x.Id)).ToDelete().ExecuteAffrows(); + QuartzTask.Instance() + .DeleteTasks( + new ReadOnlyCollection(ids.Select(x => new JobKey(x.ToString(), "default")).ToList())); return Redirect(Url.Action("Index")); } public IActionResult EnableWebSiteBatch(int[] ids) { - WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate() - .Set(y => y.IsEnable, true).ExecuteAffrows(); + var webSiteEntities = WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate() + .Set(y => y.IsEnable, true).ExecuteUpdated(); + foreach (var webSiteEntity in webSiteEntities) + { + QuartzTask.Instance().AddTask(typeof(CollectionJob), webSiteEntity.Cron, webSiteEntity.Id.ToString(), + "default", ("id", webSiteEntity.Id)); + } return Redirect(Url.Action("Index")); } @@ -50,6 +68,7 @@ namespace JXCMS.CMS.Admin.Controllers { WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate() .Set(y => y.IsEnable, false).ExecuteAffrows(); + QuartzTask.Instance().DeleteTasks(new ReadOnlyCollection(ids.Select(x => new JobKey(x.ToString(), "default")).ToList())); return Redirect(Url.Action("Index")); } diff --git a/CMS/JXCMS.CMS.Movie/Admin/Views/Collection/Index.cshtml b/CMS/JXCMS.CMS.Movie/Admin/Views/Collection/Index.cshtml index b0c74b3..f7a512c 100644 --- a/CMS/JXCMS.CMS.Movie/Admin/Views/Collection/Index.cshtml +++ b/CMS/JXCMS.CMS.Movie/Admin/Views/Collection/Index.cshtml @@ -70,7 +70,10 @@ - + + + + diff --git a/CMS/JXCMS.CMS.Movie/Entity/ClassifyEntity.cs b/CMS/JXCMS.CMS.Movie/Entity/ClassifyEntity.cs new file mode 100644 index 0000000..bc47c86 --- /dev/null +++ b/CMS/JXCMS.CMS.Movie/Entity/ClassifyEntity.cs @@ -0,0 +1,11 @@ +using FreeSql; + +namespace JXCMS.CMS.Movie.Entity +{ + public class ClassifyEntity : BaseEntity + { + public string Name { get; set; } + + public int ParentId { get; set; } + } +} \ No newline at end of file diff --git a/CMS/JXCMS.CMS.Movie/Jobs/CollectionJob.cs b/CMS/JXCMS.CMS.Movie/Jobs/CollectionJob.cs new file mode 100644 index 0000000..7114da0 --- /dev/null +++ b/CMS/JXCMS.CMS.Movie/Jobs/CollectionJob.cs @@ -0,0 +1,19 @@ +using System; +using System.Threading.Tasks; +using JXCMS.CMS.Movie.Entity; +using Quartz; + +namespace JXCMS.CMS.Movie.Jobs +{ + public class CollectionJob : IJob + { + public Task Execute(IJobExecutionContext context) + { + var id = context.MergedJobDataMap.GetIntValue("id"); + var websiteEntity = WebSiteEntity.Find(id); + + Console.WriteLine(id); + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/Core/JXCMS.Core/JXCMS.Core.csproj b/Core/JXCMS.Core/JXCMS.Core.csproj index 37b5684..007924d 100755 --- a/Core/JXCMS.Core/JXCMS.Core.csproj +++ b/Core/JXCMS.Core/JXCMS.Core.csproj @@ -20,11 +20,13 @@ + + diff --git a/Core/JXCMS.Core/TimingTask/QuartzTask.cs b/Core/JXCMS.Core/TimingTask/QuartzTask.cs index d558a1f..56677ba 100644 --- a/Core/JXCMS.Core/TimingTask/QuartzTask.cs +++ b/Core/JXCMS.Core/TimingTask/QuartzTask.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.ObjectModel; using System.Threading.Tasks; using Quartz; using Quartz.Impl; @@ -29,12 +30,28 @@ namespace JXCMS.Core.TimingTask return _instance; } - public async Task AddTask(Type jobType, string cron, string jobName, string jobGroup = "default") + public async Task AddTask(Type jobType, string cron, string jobName, string jobGroup = "default", params (string name, object value)[] param) { - var job = JobBuilder.Create(jobType).WithIdentity("job", "job").Build(); - var trigger = TriggerBuilder.Create().WithIdentity(jobName + "tigger").StartNow().WithCronSchedule(cron).Build(); - await _scheduler.ScheduleJob(job, trigger); + var time = DateTime.Now; + var job = JobBuilder.Create(jobType).WithIdentity(jobName, jobGroup).Build(); + var trigger = TriggerBuilder.Create().WithIdentity(jobName + "trigger").StartNow().WithCronSchedule(cron).Build(); + foreach (var par in param) + { + job.JobDataMap.Put(par.name, par.value); + } + var offset = await _scheduler.ScheduleJob(job, trigger); return true; } + + public async Task DeleteTask(string jobName, string jobGroup = "default") + { + return await _scheduler.DeleteJob(new JobKey(jobName, jobGroup)); + } + + public async Task DeleteTasks(ReadOnlyCollection jobKeys) + { + return await _scheduler.DeleteJobs(jobKeys); + } + } } \ No newline at end of file