ProfitShareService.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. WebCMSEntities db = new WebCMSEntities();
  52. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == OrderNo && m.Status == 2);
  53. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  54. JsonData result = JsonMapper.ToObject(HaoDaHelper.Instance.OrderDivideAccountsQuery(ApplyNo, MchtNo, merchantAdd.BrandId));
  55. string state = result["data"]["status"].ToString();
  56. if (result["data"]["status"].ToString() == "0")
  57. {
  58. if (order != null)
  59. {
  60. RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":List", order.Id.ToString());
  61. }
  62. db.Dispose();
  63. }
  64. else
  65. {
  66. RedisDbconn.Instance.AddList("ProfitShareHdQueue", content);
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "分账队列异常");
  72. }
  73. }
  74. // public void StartListen()
  75. // {
  76. // while (true)
  77. // {
  78. // string content = RedisDbconn.Instance.RPop<string>("ProfitShareHdQueue");
  79. // if (!string.IsNullOrEmpty(content))
  80. // {
  81. // StartDo(content);
  82. // }
  83. // else
  84. // {
  85. // Thread.Sleep(2000);
  86. // }
  87. // }
  88. // }
  89. // public void StartDo(string content)
  90. // {
  91. // try
  92. // {
  93. // JsonData jsonObj = JsonMapper.ToObject(content);
  94. // string ApplyNo = jsonObj["ApplyNo"].ToString(); //微信订单号
  95. // string MchtNo = jsonObj["MchtNo"].ToString(); //商户订单号
  96. // string OrderNo = jsonObj["OrderNo"].ToString(); //商户订单号
  97. // JsonData result = JsonMapper.ToObject(HaoDaHelper.Instance.OrderDivideAccountsQuery(ApplyNo, MchtNo));
  98. // string state = result["data"]["status"].ToString();
  99. // if (result["data"]["status"].ToString() == "0")
  100. // {
  101. // WebCMSEntities db = new WebCMSEntities();
  102. // ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == OrderNo && m.Status == 2);
  103. // if (order != null)
  104. // {
  105. // RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":List", order.Id.ToString());
  106. // }
  107. // db.Dispose();
  108. // }
  109. // else
  110. // {
  111. // RedisDbconn.Instance.AddList("ProfitShareHdQueue", content);
  112. // }
  113. // }
  114. // catch (Exception ex)
  115. // {
  116. // LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "分账队列异常");
  117. // }
  118. // }
  119. }
  120. }