PaidBackMsgPushJob.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using Common.Model;
  2. using Common.Wechat;
  3. using CoreEntity.BAL;
  4. using CoreEntity.DAL;
  5. using CoreEntity.Entity;
  6. using CoreEntity.TimeJob;
  7. using JCSoft.WX.Framework.Api;
  8. using Pomelo.AspNetCore.TimedJob;
  9. using Quartz;
  10. using Quartz.Impl;
  11. using Quartz.Impl.Matchers;
  12. using Quartz.Impl.Triggers;
  13. using Quartz.Spi;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Collections.Specialized;
  17. using System.Data;
  18. using System.Data.SqlClient;
  19. using System.Linq;
  20. using System.Threading.Tasks;
  21. using ZcPeng.PublicLibrary;
  22. using ZcPeng.weixin.PublicAccount;
  23. namespace CoreEntity.TimedTask
  24. {
  25. public class PaidBackMsgPushJob : IJob
  26. {
  27. //private IApiClient _client;
  28. public PaidBackMsgPushJob()
  29. {
  30. // _client = new DefaultApiClient();
  31. }
  32. public Task Execute(IJobExecutionContext context)
  33. {
  34. JobDataMap dataMap = context.JobDetail.JobDataMap;
  35. string LoginAccountId = dataMap.GetString("LoginAccountId");
  36. string RoleId = dataMap.GetString("RoleId");
  37. string Filter = dataMap.GetString("Filter");
  38. bool success = PushBAL.PushPaidBackMsg(LoginAccountId,RoleId, Filter,out var result);
  39. return Console.Out.WriteLineAsync("Greetings from PaidBackMsgPushJob!:" + result);
  40. }
  41. }
  42. public class PaidBackMsgProgram
  43. {
  44. public static async Task RunProgram(string second , string minute, string hour,
  45. string RoleId,string LoginAccountId,string Filter, IApiClient _client)
  46. {
  47. try
  48. {
  49. // Grab the Scheduler instance from the Factory
  50. NameValueCollection props = new NameValueCollection
  51. {
  52. { "quartz.serializer.type", "binary" }
  53. };
  54. StdSchedulerFactory factory = new StdSchedulerFactory(props);
  55. IScheduler scheduler = await factory.GetScheduler();
  56. // 启动任务调度器
  57. await scheduler.Start();
  58. // 定义一个 Job
  59. string key = "job_" + "PaidBackMsg";
  60. IJobDetail job = JobBuilder.Create<PaidBackMsgPushJob>()
  61. .WithIdentity(key, "adminPushGroup")
  62. .UsingJobData("LoginAccountId", LoginAccountId)
  63. .UsingJobData("RoleId", RoleId)
  64. .UsingJobData("FilterLimit", Filter)
  65. .Build();
  66. //ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
  67. // .WithIdentity("trigger1") // 给任务一个名字
  68. // .StartAt(DateTime.Now) // 设置任务开始时间
  69. // .ForJob("job1", "group1") //给任务指定一个分组
  70. // .WithSimpleSchedule(x => x
  71. // .WithIntervalInSeconds(30) //循环的时间 1秒1次
  72. // .RepeatForever())
  73. // .Build();
  74. //每天9:30:00执行
  75. CronTriggerImpl trigger = (CronTriggerImpl)TriggerBuilder.Create()
  76. .WithIdentity("trigger"+ key) // 给任务一个名字
  77. .StartAt(DateTime.Now) // 设置任务开始时间
  78. .ForJob(key, "adminPushGroup") //给任务指定一个分组 :管理员,每一次推送一个key
  79. .WithCronSchedule(second + " "+ minute + " " +hour+ " * * ?")
  80. .Build();
  81. GroupMatcher<JobKey> groupMatcher = GroupMatcher<JobKey>.GroupEquals("adminPushGroup");
  82. IReadOnlyCollection<JobKey> set = await scheduler.GetJobKeys(groupMatcher);
  83. if (set.Count > 0)
  84. {
  85. foreach (JobKey jobKey in set)
  86. {
  87. if(jobKey.Name == key) {
  88. bool delsuccess = await scheduler.DeleteJob(jobKey);
  89. }
  90. }
  91. }
  92. // 等待执行任务
  93. await scheduler.ScheduleJob(job, trigger);
  94. // some sleep to show what's happening
  95. //await Task.Delay(TimeSpan.FromMilliseconds(2000));
  96. }
  97. catch (SchedulerException se)
  98. {
  99. await Console.Error.WriteLineAsync(se.ToString());
  100. }
  101. }
  102. }
  103. }