TimeOutPosSendMessageService.cs 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading;
  6. using MySystem.Models;
  7. using Library;
  8. namespace MySystem
  9. {
  10. /// <summary>
  11. /// 过期机具预扣费消息推送
  12. /// </summary>
  13. public class TimeOutPosSendMessageService
  14. {
  15. public readonly static TimeOutPosSendMessageService Instance = new TimeOutPosSendMessageService();
  16. private TimeOutPosSendMessageService()
  17. { }
  18. public void Start()
  19. {
  20. Thread th = new Thread(doSomething);
  21. th.IsBackground = true;
  22. th.Start();
  23. }
  24. public void doSomething()
  25. {
  26. while (true)
  27. {
  28. if (DateTime.Now.Hour < 3)
  29. {
  30. try
  31. {
  32. string check = function.ReadInstance("/TimeOutPosSendMessage/check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  33. if (string.IsNullOrEmpty(check))
  34. {
  35. function.WritePage("/TimeOutPosSendMessage/", "check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  36. WebCMSEntities db = new WebCMSEntities();
  37. var date = DateTime.Now.AddDays(-55).ToString("yyyy-MM-dd 00:00:00");//过期限制时间
  38. var time = DateTime.Parse(date);//过期限制时间
  39. DataTable dt = CustomerSqlConn.dtable("SELECT BuyUserId FROM PosMachinesTwo WHERE `Status`>-1 AND BuyUserId>0 AND BindingState=0 AND ActivationState=0 AND ScanQrTrade=0 AND RecycEndDate <'" + date + "' GROUP BY BuyUserId", MysqlConn.SqlConnStr);//消息推送创客
  40. var query = db.PosMachinesTwo.Where(m => m.Status > -1 && m.BuyUserId > 0 && m.BindingState == 0 && m.ActivationState == 0 && m.ScanQrTrade == 0 && m.RecycEndDate < time).ToList();//循环过期超过10天机具
  41. var brandInfo = db.KqProducts.ToList();
  42. foreach (DataRow item in dt.Rows)
  43. {
  44. int BuyUserId = int.Parse(item["BuyUserId"].ToString());
  45. var BrandName = "";
  46. var PosSn = "";
  47. var SendInfo = "";
  48. foreach (var items in brandInfo)
  49. {
  50. int BrandId = int.Parse(items.Id.ToString());
  51. BrandName = items.Name.ToString();
  52. var posInfo = query.Where(m => m.BuyUserId == BuyUserId && m.BrandId == BrandId);
  53. foreach (var pos in posInfo)
  54. {
  55. PosSn += BrandName +":"+pos.PosSn + "," + "<br/>";
  56. }
  57. }
  58. SendInfo = PosSn.TrimEnd(',') + "<br/>";
  59. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  60. {
  61. UserId = BuyUserId, //创客
  62. Title = "机具循环过期提醒", //标题
  63. Content = "<div class='f16'>尊敬的创客您好:<br/>您的" + SendInfo + "循环已过期五十五天以上,请在五日内完成激活或通过机具回收回寄该机具。</ div > ",//内容
  64. Summary = "系统检测到您的部分机具可回收,请打开我的-回收机具及时处理!",
  65. CreateDate = DateTime.Now,
  66. }));
  67. }
  68. }
  69. }
  70. catch (Exception ex)
  71. {
  72. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "过期机具预扣费消息推送异常");
  73. }
  74. }
  75. Thread.Sleep(1000);
  76. }
  77. }
  78. }
  79. }