LeaderTimeoutSendMessageService.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 StartEverTime()
  20. {
  21. Thread th = new Thread(StartEverTimeDo);
  22. th.IsBackground = true;
  23. th.Start();
  24. }
  25. private void StartEverTimeDo()
  26. {
  27. while (true)
  28. {
  29. WebCMSEntities db = new WebCMSEntities();
  30. try
  31. {
  32. var leaders = db.Leaders.Select(m => new { m.UserId }).ToList();
  33. var time = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
  34. foreach (var item in leaders)
  35. {
  36. var user = db.Users.FirstOrDefault(m => m.Id == item.UserId) ?? new Users();
  37. var userRankWhite = db.UserRankWhite.FirstOrDefault(m => m.Id == item.UserId) ?? new UserRankWhite();
  38. var leaderTimeOutDate = DateTime.Parse(userRankWhite.UpdateDate.Value.ToString("yyyy-MM-dd"));
  39. if (time <= leaderTimeOutDate.AddDays(-5))
  40. {
  41. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  42. {
  43. UserId = item.UserId, //创客
  44. Title = "盟主权益即将到期提醒", //标题
  45. Content = "<div class='f16'>尊敬的" + user.RealName + "创客您好:<br/>您的盟主即将到期,为保障您的收益持续到账,请尽快续期!</ div > ",//内容
  46. CreateDate = DateTime.Now,
  47. }));
  48. }
  49. if (time >= leaderTimeOutDate)
  50. {
  51. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  52. {
  53. UserId = item.UserId, //创客
  54. Title = "盟主权益到期提醒", //标题
  55. Content = "<div class='f16'>尊敬的" + user.RealName + "创客您好:<br/>您的盟主已经过期,重新开通后可恢复相关权益!</ div > ",//内容
  56. CreateDate = DateTime.Now,
  57. }));
  58. }
  59. }
  60. }
  61. catch (Exception ex)
  62. {
  63. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "盟主过期消息推送线程异常");
  64. }
  65. db.Dispose();
  66. Thread.Sleep(1000);
  67. }
  68. }
  69. }