AutoSupplyGapPushJob.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 AutoSupplyGapPushJob : Job
  26. {
  27. private IApiClient _client;
  28. public AutoSupplyGapPushJob(IApiClient client)
  29. {
  30. _client = client;
  31. }
  32. // Begin 起始时间;Interval执行时间间隔,单位是毫秒,建议使用以下格式,此处为3小时;
  33. //SkipWhileExecuting是否等待上一个执行完成,true为等待;
  34. [Invoke(Begin = "2018-12-17 17:16", Interval = 1000 * 60 * 1 * 1, SkipWhileExecuting = true)]
  35. public void Run()
  36. {
  37. //Job要执行的逻辑代码
  38. //LogHelper.Info("Start crawling");
  39. //AddToLatestMovieList(100);
  40. //AddToHotMovieList();
  41. //LogHelper.Info("Finish crawling");
  42. //DataAccess
  43. WechatHelper.getAccessToken(_client);
  44. }
  45. }
  46. public class PushJob : IJob
  47. {
  48. //private IApiClient _client;
  49. public PushJob()
  50. {
  51. // _client = new DefaultApiClient();
  52. }
  53. public Task Execute(IJobExecutionContext context)
  54. {
  55. /*//获取推送上下限
  56. IList<PushStorLimit> pushLimits = PushRecordDAL.GetPushLimit();
  57. JobDataMap dataMap = context.JobDetail.JobDataMap;
  58. string LoginAccountId = dataMap.GetString("LoginAccountId");
  59. string RoleId = dataMap.GetString("RoleId");
  60. string StaffDocId = StaffDocDAL.GetStaffId(LoginAccountId);
  61. //获取待推送账户:上次供货业务员,取最低价格的业务员
  62. string filterstr = " and UserId IS NOT NULL and UserId !='' "
  63. // +" and FocusMicNo IS NOT NULL and FocusMicNo !='' "
  64. + ((RoleId == "1" || RoleId == "6" || RoleId == "7") ? "" : (" and SaleManId = '" + StaffDocId + "' ")) //只查询当前采购员的联系人
  65. ;
  66. Dictionary<string, PushStorLimit> gapnum;
  67. List<PushContactWithGoods> GetPushContacts = ContactBAL.GetPushContactWithGoods(filterstr,"", pushLimits, out gapnum);
  68. //判断参数是否合法
  69. if (GetPushContacts.Count == 0)
  70. {
  71. return Console.Out.WriteLineAsync("Greetings from HelloJob!" +
  72. "没有建立账户,或者业务员没有绑定");
  73. }
  74. //添加推送记录并推送
  75. var result = PushBAL.pushAll(GetPushContacts, gapnum, LoginAccountId, null);
  76. var codeids = result[result.Count - 1];
  77. Object Ids;
  78. codeids.TryGetValue("Ids", out Ids);
  79. List<String> ids = (List<String>)Ids;
  80. foreach (String recordId in ids)
  81. {
  82. CancelPushRecordTimedJob.CancelPushRecord(recordId, DateTime.Now, CancelPushRecordTimedJob.HoursToCancel);
  83. }
  84. */
  85. JobDataMap dataMap = context.JobDetail.JobDataMap;
  86. string LoginAccountId = dataMap.GetString("LoginAccountId");
  87. string RoleId = dataMap.GetString("RoleId");
  88. string FilterLimit = dataMap.GetString("FilterLimit");
  89. string FilterCont = dataMap.GetString("FilterCont");
  90. bool success = PushBAL.Push(LoginAccountId,RoleId, FilterLimit, FilterCont,out var result);
  91. //foreach (PushContactWithGoods contact in GetPushContacts)
  92. //{
  93. // int StoreGapNum = 0;
  94. // if (gapnum.ContainsKey(contact.GoodsId))
  95. // {
  96. // PushStorLimit limit;
  97. // gapnum.TryGetValue(contact.GoodsId, out limit);
  98. // StoreGapNum = Convert.ToInt32(limit.StoreGapNum);
  99. // }
  100. // string PushAccountId = contact.UserId;
  101. // DateTime EndTime = DateTime.Now.AddHours((double)24);
  102. // string LoginAccountId = "1";
  103. // //新建推送记录
  104. // var success = PushBAL.BatchPush(null, null,
  105. // contact.UserId, contact.GoodsId, contact.GoodsCode
  106. // , contact.GoodsName, StoreGapNum
  107. // , contact.GoodsSpec, contact.Manufacturer
  108. // , contact.BusinessId, contact.BusinessCode, contact.BusinessName
  109. // , contact.SuppliersId, contact.BrandName, contact.BrandId
  110. // , StoreGapNum, contact.LastPrice
  111. // , contact.EntId, contact.Remark
  112. // , LoginAccountId
  113. // , _client);
  114. // // Message msg = TimeTask.sendSupplyMassMesage(_client, Convert.ToInt32(PushAccountId),
  115. // // contact.GoodsName,
  116. // // contact.StoreGapNum,
  117. // // contact.GoodsSpec,
  118. // // contact.BrandName,
  119. // // contact.LastPrice,
  120. // // EndTime);
  121. // // PushRecordDAL.AddOrUpdatePushRecord(Convert.ToInt32(PushAccountId), contact.GoodsId, contact.GoodsCode//PushAccountId
  122. // //, contact.GoodsName, contact.StoreGapNum,
  123. // //contact.GoodsSpec,contact.Manufacturer,
  124. // //contact.BusinessId,contact.BusinessCode,contact.BusinessName
  125. // //, contact.SuppliersId,
  126. // //contact.BrandName, contact.BrandId
  127. // //, "","","", "", contact.LastPrice, contact.StoreGapNum);//entid remark feedbackid confirmperson
  128. //}
  129. return Console.Out.WriteLineAsync("Greetings from PushJob!:"+ result);
  130. }
  131. }
  132. //public class JobFactoryMy : IJobFactory
  133. //{
  134. // private readonly IContainer _container;
  135. // public JobFactoryMy(IContainer container)
  136. // {
  137. // _container = container;
  138. // }
  139. // public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
  140. // {
  141. // return (IJob)_container.Resolve(bundle.JobDetail.JobType);
  142. // }
  143. // public void ReturnJob(IJob job)
  144. // {
  145. // throw new NotImplementedException();
  146. // }
  147. //}
  148. class MyServiceJobFactory : IJobFactory
  149. {
  150. protected readonly IServiceProvider _container;
  151. public MyServiceJobFactory(IServiceProvider container)
  152. {
  153. _container = container;
  154. }
  155. public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
  156. {
  157. return _container.GetService(bundle.JobDetail.JobType) as IJob;
  158. }
  159. public void ReturnJob(IJob job)
  160. {
  161. }
  162. }
  163. public class TimedProgram
  164. {
  165. public static async Task RunProgram(string second , string minute, string hour,string RoleId,string LoginAccountId,string FilterLimit,string FilterCont, IApiClient _client)
  166. {
  167. #region 获取推送时间
  168. DataTable dt = new DataTable();
  169. string result;
  170. if (second == null || minute == null || hour == null)
  171. {
  172. List<SqlParameter> parameters1 = new List<SqlParameter>();
  173. bool success = DataAccess.GetValues("select hour,minute,second from " + Config.TablePrefix + "TimeTask where Id=1", ref dt, parameters1.ToArray(), out result);
  174. // 把DataTable转换为IList<UserInfo>
  175. IList<TimeTask> timets = new List<TimeTask>();
  176. if (dt!=null && dt.Rows.Count > 0)
  177. {
  178. // 把DataTable转换为IList<UserInfo>
  179. timets = ModelConvertHelper<TimeTask>.ConvertToModel(dt);
  180. TimeTask first = (TimeTask)timets.First();
  181. hour = first.Hour;
  182. minute = first.Minute;
  183. second = first.Second;
  184. }
  185. }
  186. if (hour == null || hour.Length == 0
  187. || minute == null || minute.Length == 0
  188. || second == null || second.Length == 0)
  189. {
  190. await Console.Error.WriteLineAsync("参数错误");
  191. }
  192. #endregion
  193. try
  194. {
  195. // Grab the Scheduler instance from the Factory
  196. NameValueCollection props = new NameValueCollection
  197. {
  198. { "quartz.serializer.type", "binary" }
  199. };
  200. StdSchedulerFactory factory = new StdSchedulerFactory(props);
  201. IScheduler scheduler = await factory.GetScheduler();
  202. // 启动任务调度器
  203. await scheduler.Start();
  204. // 定义一个 Job
  205. string key = "job_" + LoginAccountId + "_" + FilterLimit.Replace(" and GoodsId in ('", "").Replace("','", "-").Replace("') ", "")
  206. + "_" + FilterCont.Replace(" and GoodsId in ('", "").Replace(" and ContactId in ('", "").Replace("','", "-").Replace("') ", "");
  207. IJobDetail job = JobBuilder.Create<PushJob>()
  208. .WithIdentity(key, "group1")
  209. .UsingJobData("LoginAccountId", LoginAccountId)
  210. .UsingJobData("RoleId", RoleId)
  211. .UsingJobData("FilterLimit", FilterLimit)
  212. .UsingJobData("FilterCont", FilterCont)
  213. .Build();
  214. //ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
  215. // .WithIdentity("trigger1") // 给任务一个名字
  216. // .StartAt(DateTime.Now) // 设置任务开始时间
  217. // .ForJob("job1", "group1") //给任务指定一个分组
  218. // .WithSimpleSchedule(x => x
  219. // .WithIntervalInSeconds(30) //循环的时间 1秒1次
  220. // .RepeatForever())
  221. // .Build();
  222. //每天9:30:00执行
  223. CronTriggerImpl trigger = (CronTriggerImpl)TriggerBuilder.Create()
  224. .WithIdentity("trigger1") // 给任务一个名字
  225. .StartAt(DateTime.Now) // 设置任务开始时间
  226. .ForJob(key, "group1") //给任务指定一个分组 :每个业务员,每一次推送一个key
  227. .WithCronSchedule(second + " "+ minute + " " +hour+ " * * ?")
  228. .Build();
  229. GroupMatcher<JobKey> groupMatcher = GroupMatcher<JobKey>.GroupEquals("group1");
  230. IReadOnlyCollection<JobKey> set = await scheduler.GetJobKeys(groupMatcher);
  231. if (set.Count > 0)
  232. {
  233. foreach (JobKey jobKey in set)
  234. {
  235. if(jobKey.Name == key) {
  236. bool delsuccess = await scheduler.DeleteJob(jobKey);
  237. }
  238. }
  239. }
  240. // 等待执行任务
  241. await scheduler.ScheduleJob(job, trigger);
  242. // some sleep to show what's happening
  243. //await Task.Delay(TimeSpan.FromMilliseconds(2000));
  244. }
  245. catch (SchedulerException se)
  246. {
  247. await Console.Error.WriteLineAsync(se.ToString());
  248. }
  249. }
  250. }
  251. }