ProfitShareService.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.Models.Main;
  5. using Library;
  6. using LitJson;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9. namespace MySystem
  10. {
  11. public class ProfitShareService
  12. {
  13. public readonly static ProfitShareService Instance = new ProfitShareService();
  14. private ProfitShareService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(StartListen);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. public void StartListen()
  23. {
  24. while (true)
  25. {
  26. WebCMSEntities db = new WebCMSEntities();
  27. DateTime checkDate = DateTime.Now.AddMinutes(-2);
  28. var list = db.ConsumerOrders.Select(m => new { m.Id, m.DivideDate, m.DivideFlag, m.IsAct, m.PayMode }).Where(m => m.DivideFlag == 1 && m.IsAct == 1 && m.DivideDate < checkDate).OrderBy(m => m.Id).Take(100).ToList();
  29. foreach (var sub in list)
  30. {
  31. RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + sub.PayMode + ":List", sub.Id.ToString());
  32. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == sub.Id);
  33. if (order != null)
  34. {
  35. order.DivideFlag = 2;
  36. db.SaveChanges();
  37. }
  38. }
  39. db.Dispose();
  40. Thread.Sleep(5000);
  41. }
  42. }
  43. public void StartDo(string content)
  44. {
  45. try
  46. {
  47. JsonData jsonObj = JsonMapper.ToObject(content);
  48. string ApplyNo = jsonObj["ApplyNo"].ToString(); //微信订单号
  49. string MchtNo = jsonObj["MchtNo"].ToString(); //商户订单号
  50. string OrderNo = jsonObj["OrderNo"].ToString(); //商户订单号
  51. JsonData result = JsonMapper.ToObject(HaoDaHelper.Instance.OrderDivideAccountsQuery(ApplyNo, MchtNo));
  52. string state = result["data"]["status"].ToString();
  53. if (result["data"]["status"].ToString() == "0")
  54. {
  55. WebCMSEntities db = new WebCMSEntities();
  56. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == OrderNo && m.Status == 2);
  57. if (order != null)
  58. {
  59. RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":List", order.Id.ToString());
  60. }
  61. db.Dispose();
  62. }
  63. else
  64. {
  65. RedisDbconn.Instance.AddList("ProfitShareHdQueue", content);
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "分账队列异常");
  71. }
  72. }
  73. // public void StartListen()
  74. // {
  75. // while (true)
  76. // {
  77. // string content = RedisDbconn.Instance.RPop<string>("ProfitShareHdQueue");
  78. // if (!string.IsNullOrEmpty(content))
  79. // {
  80. // StartDo(content);
  81. // }
  82. // else
  83. // {
  84. // Thread.Sleep(2000);
  85. // }
  86. // }
  87. // }
  88. // public void StartDo(string content)
  89. // {
  90. // try
  91. // {
  92. // JsonData jsonObj = JsonMapper.ToObject(content);
  93. // string ApplyNo = jsonObj["ApplyNo"].ToString(); //微信订单号
  94. // string MchtNo = jsonObj["MchtNo"].ToString(); //商户订单号
  95. // string OrderNo = jsonObj["OrderNo"].ToString(); //商户订单号
  96. // JsonData result = JsonMapper.ToObject(HaoDaHelper.Instance.OrderDivideAccountsQuery(ApplyNo, MchtNo));
  97. // string state = result["data"]["status"].ToString();
  98. // if (result["data"]["status"].ToString() == "0")
  99. // {
  100. // WebCMSEntities db = new WebCMSEntities();
  101. // ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == OrderNo && m.Status == 2);
  102. // if (order != null)
  103. // {
  104. // RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":List", order.Id.ToString());
  105. // }
  106. // db.Dispose();
  107. // }
  108. // else
  109. // {
  110. // RedisDbconn.Instance.AddList("ProfitShareHdQueue", content);
  111. // }
  112. // }
  113. // catch (Exception ex)
  114. // {
  115. // LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "分账队列异常");
  116. // }
  117. // }
  118. }
  119. }