LeaderTimeoutSendMessageService.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. if (time >= leaderTimeOutDate.AddDays(-5) && time <= leaderTimeOutDate)
  45. {
  46. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  47. {
  48. UserId = item.UserId, //创客
  49. Title = "盟主权益即将到期提醒", //标题
  50. Content = "<div class='f16'>尊敬的" + user.RealName + "创客您好:<br/>您的盟主即将到期,为保障您的收益持续到账,请尽快续期!</ div > ",//内容
  51. CreateDate = DateTime.Now,
  52. }));
  53. }
  54. if (time > leaderTimeOutDate && time <= leaderTimeOutDate.AddMonths(1))
  55. {
  56. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  57. {
  58. UserId = item.UserId, //创客
  59. Title = "盟主权益到期提醒", //标题
  60. Content = "<div class='f16'>尊敬的" + user.RealName + "创客您好:<br/>您的盟主已经过期,重新开通后可恢复相关权益!</ div > ",//内容
  61. CreateDate = DateTime.Now,
  62. }));
  63. }
  64. }
  65. }
  66. }
  67. catch (Exception ex)
  68. {
  69. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "盟主过期消息推送线程异常");
  70. }
  71. }
  72. Thread.Sleep(1000);
  73. }
  74. }
  75. }