LeaderTimeoutSendMessageService.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. /// <summary>
  11. /// 盟主过期消息推送
  12. /// </summary>
  13. public class LeaderTimeoutSendMessageService
  14. {
  15. public readonly static LeaderTimeoutSendMessageService Instance = new LeaderTimeoutSendMessageService();
  16. private LeaderTimeoutSendMessageService()
  17. {
  18. }
  19. public void Start()
  20. {
  21. Thread th = new Thread(doSomething);
  22. th.IsBackground = true;
  23. th.Start();
  24. }
  25. private void doSomething()
  26. {
  27. while (true)
  28. {
  29. if (DateTime.Now.Hour < 8)
  30. {
  31. try
  32. {
  33. string check = function.ReadInstance("/LeaderTimeoutSendMessage/check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  34. if (string.IsNullOrEmpty(check))
  35. {
  36. function.WritePage("/LeaderTimeoutSendMessage/", "check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  37. WebCMSEntities db = new WebCMSEntities();
  38. var leaders = db.Leaders.Select(m => new { m.UserId, m.ExpiredDate }).ToList();
  39. var time = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
  40. foreach (var item in leaders)
  41. {
  42. var user = db.Users.FirstOrDefault(m => m.Id == item.UserId) ?? new Users();
  43. var leaderTimeOutDate = DateTime.Parse(item.ExpiredDate.Value.ToString("yyyy-MM-dd"));
  44. var day = leaderTimeOutDate - time;
  45. if (time >= leaderTimeOutDate.AddDays(-5) && time <= leaderTimeOutDate)
  46. {
  47. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  48. {
  49. UserId = item.UserId, //创客
  50. Title = "盟主权益即将到期提醒", //标题
  51. Content = "<div class='f16'>尊敬的" + user.RealName + "盟主您好:<br/>您的盟主权益将在" + day.Days + "天后到期,为保障您的收益持续到账,请尽快续期!</ div > ",//内容
  52. CreateDate = DateTime.Now,
  53. }));
  54. }
  55. if (time > leaderTimeOutDate && time <= leaderTimeOutDate.AddMonths(1))
  56. {
  57. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  58. {
  59. UserId = item.UserId, //创客
  60. Title = "盟主权益到期提醒", //标题
  61. Content = "<div class='f16'>尊敬的" + user.RealName + "盟主您好:<br/>您的盟主已经过期,重新开通后可恢复相关权益!</ div > ",//内容
  62. CreateDate = DateTime.Now,
  63. }));
  64. }
  65. }
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "盟主过期消息推送线程异常");
  71. }
  72. }
  73. Thread.Sleep(1000);
  74. }
  75. }
  76. }