LeaderTimeoutSendMessageService.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Linq;
  5. using System.Data;
  6. using MySystem;
  7. using MySystem.Models;
  8. using Library;
  9. using LitJson;
  10. using GraphQL;
  11. /// <summary>
  12. /// 盟主过期消息推送
  13. /// </summary>
  14. public class LeaderTimeoutSendMessageService
  15. {
  16. public readonly static LeaderTimeoutSendMessageService Instance = new LeaderTimeoutSendMessageService();
  17. private LeaderTimeoutSendMessageService()
  18. {
  19. }
  20. public void Start()
  21. {
  22. Thread th = new Thread(doSomething);
  23. th.IsBackground = true;
  24. th.Start();
  25. }
  26. private void doSomething()
  27. {
  28. while (true)
  29. {
  30. if (DateTime.Now.Hour < 9)
  31. {
  32. try
  33. {
  34. string check = function.ReadInstance("/LeaderTimeoutSendMessage/check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  35. if (string.IsNullOrEmpty(check))
  36. {
  37. function.WritePage("/LeaderTimeoutSendMessage/", "check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  38. WebCMSEntities db = new WebCMSEntities();
  39. var leaders = db.Leaders.Select(m => new { m.UserId, m.ExpiredDate }).ToList();
  40. var time = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
  41. foreach (var item in leaders)
  42. {
  43. var user = db.Users.FirstOrDefault(m => m.Id == item.UserId) ?? new Users();
  44. var leaderTimeOutDate = DateTime.Parse(item.ExpiredDate.Value.ToString("yyyy-MM-dd"));
  45. var day = leaderTimeOutDate - time;
  46. if (time >= leaderTimeOutDate.AddDays(-5) && time <= leaderTimeOutDate)
  47. {
  48. string flag = RedisDbconn.Instance.Get<string>("MsgPersonalFirst" + item.UserId);
  49. if(string.IsNullOrEmpty(flag))
  50. {
  51. RedisDbconn.Instance.Set("MsgPersonalFirst" + item.UserId, "1");
  52. int timespan = 60 * 60 * 24 * 6;
  53. RedisDbconn.Instance.SetExpire("MsgPersonalFirst" + item.UserId, timespan);
  54. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  55. {
  56. UserId = item.UserId, //创客
  57. Title = "盟主权益即将到期提醒", //标题
  58. Content = "<div class='f16'>尊敬的" + user.RealName + "盟主您好:<br/>您的盟主权益将在" + day.Days + "天后到期,为保障您的收益持续到账,请尽快续期!</ div > ",//内容
  59. CreateDate = DateTime.Now,
  60. }));
  61. }
  62. }
  63. if (time > leaderTimeOutDate && time <= leaderTimeOutDate.AddMonths(1))
  64. {
  65. int number = 1; //提示标记(第几次)
  66. int timespan = 60 * 60 * 24 * 8;
  67. TimeSpan ts = time - leaderTimeOutDate;
  68. if(ts.Days == 0 || ts.Days == 1)
  69. {
  70. number = 1;
  71. timespan = 60 * 60 * 24 * 8;
  72. }
  73. if(ts.Days == 7 || ts.Days == 8)
  74. {
  75. number = 2;
  76. timespan = 60 * 60 * 24 * 8;
  77. }
  78. if(ts.Days == 14 || ts.Days == 15)
  79. {
  80. number = 3;
  81. timespan = 60 * 60 * 24 * 15;
  82. }
  83. if(ts.Days == 28 || ts.Days == 29)
  84. {
  85. number = 4;
  86. timespan = 60 * 60 * 24 * 5;
  87. }
  88. string flag = RedisDbconn.Instance.Get<string>("MsgPersonalAfter:" + number + ":" + item.UserId);
  89. if(string.IsNullOrEmpty(flag))
  90. {
  91. RedisDbconn.Instance.Set("MsgPersonalAfter:" + number + ":" + item.UserId, "1");
  92. RedisDbconn.Instance.SetExpire("MsgPersonalAfter:" + number + ":" + item.UserId, timespan);
  93. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  94. {
  95. UserId = item.UserId, //创客
  96. Title = "盟主权益到期提醒", //标题
  97. Content = "<div class='f16'>尊敬的" + user.RealName + "盟主您好:<br/>您的盟主已经过期,重新开通后可恢复相关权益!</ div > ",//内容
  98. CreateDate = DateTime.Now,
  99. }));
  100. }
  101. }
  102. }
  103. }
  104. }
  105. catch (Exception ex)
  106. {
  107. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "盟主过期消息推送线程异常");
  108. }
  109. }
  110. Thread.Sleep(1000);
  111. }
  112. }
  113. }