RePushHelper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Linq;
  3. using System.Data;
  4. using System.Threading;
  5. using Library;
  6. using LitJson;
  7. using MySystem.Models.Push;
  8. using System.Collections.Generic;
  9. namespace MySystem
  10. {
  11. public class RePushHelper
  12. {
  13. public readonly static RePushHelper Instance = new RePushHelper();
  14. private RePushHelper()
  15. {
  16. }
  17. public void Start()//启动
  18. {
  19. Thread thread = new Thread(threadStart);
  20. thread.IsBackground = true;
  21. thread.Start();
  22. }
  23. private void threadStart()
  24. {
  25. while (true)
  26. {
  27. DoSomeThing();
  28. Thread.Sleep(5000);
  29. }
  30. }
  31. //要执行的方法
  32. public void DoSomeThing()
  33. {
  34. WebCMSEntities db = new WebCMSEntities();
  35. //查找开通推送的商户
  36. DateTime now = DateTime.Now;
  37. 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();
  38. foreach(var push in pushs)
  39. {
  40. string NoticeUrl = push.RePushUrl;
  41. string PushDataEncrypt = push.PushDataEncrypt;
  42. int Status = 0;
  43. // string result = function.PostWebRequest(NoticeUrl, PushDataEncrypt, "application/json");
  44. // if(result.Contains("\"code\":\"200\""))
  45. // {
  46. // Status = 1;
  47. // }
  48. RePushQueue edit = db.RePushQueue.FirstOrDefault(m => m.Id == push.Id);
  49. if(edit != null)
  50. {
  51. if(Status == 1)
  52. {
  53. PushRecord record = db.PushRecord.FirstOrDefault(m => m.Id == edit.PushRecordId);
  54. if(record != null)
  55. {
  56. record.Status = 1;
  57. }
  58. db.RePushQueue.Remove(edit);
  59. }
  60. else
  61. {
  62. if(edit.Times >= 15)
  63. {
  64. PushRecord record = db.PushRecord.FirstOrDefault(m => m.Id == edit.PushRecordId);
  65. if(record != null)
  66. {
  67. record.Status = -1;
  68. }
  69. }
  70. else
  71. {
  72. edit.Times += 1;
  73. edit.RePushDate = GetNextTime(edit.Times);
  74. }
  75. }
  76. db.SaveChanges();
  77. }
  78. }
  79. db.Dispose();
  80. }
  81. public DateTime GetNextTime(int num)
  82. {
  83. if(num == 1) return DateTime.Now.AddSeconds(15);
  84. if(num == 2) return DateTime.Now.AddSeconds(15);
  85. if(num == 3) return DateTime.Now.AddSeconds(30);
  86. if(num == 4) return DateTime.Now.AddMinutes(3);
  87. if(num == 5) return DateTime.Now.AddMinutes(10);
  88. if(num == 6) return DateTime.Now.AddMinutes(20);
  89. if(num == 7) return DateTime.Now.AddMinutes(30);
  90. if(num == 8) return DateTime.Now.AddMinutes(30);
  91. if(num == 9) return DateTime.Now.AddMinutes(30);
  92. if(num == 10) return DateTime.Now.AddMinutes(60);
  93. if(num == 11) return DateTime.Now.AddHours(3);
  94. if(num == 12) return DateTime.Now.AddHours(3);
  95. if(num == 13) return DateTime.Now.AddHours(3);
  96. if(num == 14) return DateTime.Now.AddHours(6);
  97. if(num == 15) return DateTime.Now.AddHours(6);
  98. return DateTime.Now;
  99. }
  100. }
  101. }