| #region << 版 本 注 释 >> | 
| /*---------------------------------------------------------------- | 
|  * 命名空间:WIDESEAWCS_QuartzJob | 
|  * 创建者:胡童庆 | 
|  * 创建时间:2024/8/2 16:13:36 | 
|  * 版本:V1.0.0 | 
|  * 描述:调度服务配置业务实现层 | 
|  * | 
|  * ---------------------------------------------------------------- | 
|  * 修改人: | 
|  * 修改时间: | 
|  * 版本:V1.0.1 | 
|  * 修改说明: | 
|  *  | 
|  *----------------------------------------------------------------*/ | 
| #endregion << 版 本 注 释 >> | 
|   | 
| using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime; | 
| using Quartz.Impl.Matchers; | 
| using Quartz; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEAWCS_Core; | 
| using WIDESEAWCS_Core.BaseRepository; | 
| using WIDESEAWCS_Core.BaseServices; | 
| using WIDESEAWCS_Core.Helper; | 
|   | 
| using WIDESEAWCS_QuartzJob.DeviceEnum; | 
| using WIDESEAWCS_QuartzJob.DTO; | 
| using WIDESEAWCS_QuartzJob.Models; | 
| using WIDESEAWCS_QuartzJob.Repository; | 
| using Quartz.Impl; | 
| using System.Collections.Specialized; | 
|   | 
| namespace WIDESEAWCS_QuartzJob.Service | 
| { | 
|     public class DispatchInfoService : ServiceBase<Dt_DispatchInfo, IDispatchInfoRepository>, IDispatchInfoService | 
|     { | 
|         private readonly IUnitOfWorkManage _unitOfWorkManage; | 
|         private readonly IDeviceInfoRepository _deviceInfoRepository; | 
|         private readonly ISchedulerCenter _schedulerCenter; | 
|         public DispatchInfoService(IDispatchInfoRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, IDeviceInfoRepository deviceInfoRepository, ISchedulerCenter schedulerCenter) : base(BaseDal) | 
|         { | 
|             _unitOfWorkManage = unitOfWorkManage; | 
|             _deviceInfoRepository = deviceInfoRepository; | 
|             _schedulerCenter = schedulerCenter; | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 查询调度服务Job与对应的设备信息。 | 
|         /// </summary> | 
|         /// <returns>返回调度服务JobDTO集合。</returns> | 
|         public List<DispatchInfoDTO> QueryDispatchInfos() | 
|         { | 
|             return Db.Queryable<Dt_DispatchInfo, Dt_DeviceInfo>((a, b) => a.JobGroup == b.DeviceType && b.DeviceStatus == ((int)DeviceStatusEnum.Enable).ToString()).Select((a, b) => new DispatchInfoDTO | 
|             { | 
|                 JobGroup = a.JobGroup, | 
|                 AssemblyName = a.AssemblyName, | 
|                 BeginTime = a.BeginTime, | 
|                 ClassName = a.ClassName, | 
|                 CreateDate = a.CreateDate, | 
|                 Creater = a.Creater, | 
|                 CycleHasRunTimes = 0, | 
|                 EndTime = a.EndTime, | 
|                 Id = b.Id, | 
|                 IntervalSecond = a.IntervalSecond, | 
|                 Modifier = a.Modifier, | 
|                 ModifyDate = a.ModifyDate, | 
|                 Name = a.Name, | 
|                 Remark = a.Remark, | 
|                 DeviceType = b.DeviceType | 
|             }).ToList(); | 
|         } | 
|   | 
|         public async Task<WebResponseContent> GetDispatchInfosAsync() | 
|         { | 
|             WebResponseContent content = new WebResponseContent(); | 
|             NameValueCollection collection = new NameValueCollection | 
|             { | 
|                 { "quartz.serializer.type", "binary" }, | 
|             }; | 
|             StdSchedulerFactory factory = new StdSchedulerFactory(collection); | 
|             IScheduler scheduler = await factory.GetScheduler(); | 
|             var jobKeys = await scheduler.GetJobKeys(GroupMatcher<JobKey>.AnyGroup()); | 
|             foreach (var jobKey in jobKeys) | 
|             { | 
|                 // 在这里处理每个JobKey | 
|                 IJobDetail jobDetail = await scheduler.GetJobDetail(jobKey); | 
|                 if (jobDetail != null) | 
|                 { | 
|                     // 可以获取Job的描述信息 | 
|                     string jobDescription = jobDetail.Description; | 
|                     // 以及Job的数据映射(JobDataMap) | 
|                     JobDataMap jobDataMap = jobDetail.JobDataMap; | 
|                 } | 
|             } | 
|   | 
|             var triggerKeys = await scheduler.GetTriggerKeys(GroupMatcher<TriggerKey>.AnyGroup()); | 
|             foreach (var triggerKey in triggerKeys) | 
|             { | 
|                 // 在这里处理每个TriggerKey | 
|                 ITrigger trigger = await scheduler.GetTrigger(triggerKey); | 
|                 if (trigger != null) | 
|                 { | 
|                     // 获取下一次触发时间(如果有) | 
|                     DateTimeOffset? nextFireTime = trigger.GetNextFireTimeUtc(); | 
|                     // 获取上次触发时间(如果有) | 
|                     DateTimeOffset? previousFireTime = trigger.GetPreviousFireTimeUtc(); | 
|                     // 获取触发类型(如SimpleTrigger、CronTrigger) | 
|                     string triggerType = trigger.GetType().Name; | 
|                     // 对于CronTrigger,还可以获取Cron表达式(如果是) | 
|                     //if (trigger is CronTrigger cronTrigger) | 
|                     //{ | 
|                     //    string cronExpression = cronTrigger.CronExpressionString; | 
|                     //} | 
|                     if (!previousFireTime.HasValue && nextFireTime.HasValue) | 
|                     { | 
|                         Console.WriteLine($"Job处于等待触发状态,Trigger名称: {triggerKey.Name}"); | 
|                     } | 
|                 } | 
|             } | 
|             return content; | 
|         } | 
|     } | 
| } |