刘磊
2025-04-19 bd02cb3fa0fa75ffafb2cf17501929b1b1e0029b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#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;
        }
    }
}