using System; using System.Linq; using System.Data; using System.Threading; using Library; using LitJson; using MySystem.Models.Push; using System.Collections.Generic; namespace MySystem { public class RePushHelper { public readonly static RePushHelper Instance = new RePushHelper(); private RePushHelper() { } public void Start()//启动 { Thread thread = new Thread(threadStart); thread.IsBackground = true; thread.Start(); } private void threadStart() { while (true) { DoSomeThing(); Thread.Sleep(5000); } } //要执行的方法 public void DoSomeThing() { WebCMSEntities db = new WebCMSEntities(); //查找开通推送的商户 DateTime now = DateTime.Now; var pushs = db.RePushQueue.Select(m => new { m.Id, m.Times, m.RePushUrl, m.PushDataEncrypt, m.RePushDate }).Where(m => m.Times <= 15 && m.RePushDate <= now).ToList(); foreach(var push in pushs) { string NoticeUrl = push.RePushUrl; string PushDataEncrypt = push.PushDataEncrypt; int Status = 0; // string result = function.PostWebRequest(NoticeUrl, PushDataEncrypt, "application/json"); // if(result.Contains("\"code\":\"200\"")) // { // Status = 1; // } RePushQueue edit = db.RePushQueue.FirstOrDefault(m => m.Id == push.Id); if(edit != null) { if(Status == 1) { PushRecord record = db.PushRecord.FirstOrDefault(m => m.Id == edit.PushRecordId); if(record != null) { record.Status = 1; } db.RePushQueue.Remove(edit); } else { if(edit.Times >= 15) { PushRecord record = db.PushRecord.FirstOrDefault(m => m.Id == edit.PushRecordId); if(record != null) { record.Status = -1; } } else { edit.Times += 1; edit.RePushDate = GetNextTime(edit.Times); } } db.SaveChanges(); } } db.Dispose(); } public DateTime GetNextTime(int num) { if(num == 1) return DateTime.Now.AddSeconds(15); if(num == 2) return DateTime.Now.AddSeconds(15); if(num == 3) return DateTime.Now.AddSeconds(30); if(num == 4) return DateTime.Now.AddMinutes(3); if(num == 5) return DateTime.Now.AddMinutes(10); if(num == 6) return DateTime.Now.AddMinutes(20); if(num == 7) return DateTime.Now.AddMinutes(30); if(num == 8) return DateTime.Now.AddMinutes(30); if(num == 9) return DateTime.Now.AddMinutes(30); if(num == 10) return DateTime.Now.AddMinutes(60); if(num == 11) return DateTime.Now.AddHours(3); if(num == 12) return DateTime.Now.AddHours(3); if(num == 13) return DateTime.Now.AddHours(3); if(num == 14) return DateTime.Now.AddHours(6); if(num == 15) return DateTime.Now.AddHours(6); return DateTime.Now; } } }