ProfitShareService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using System.Threading;
  6. using Library;
  7. using LitJson;
  8. using MySystem.Models.Main;
  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. // string content = RedisDbconn.Instance.RPop<string>("PartOrderStatusQueue");
  42. // if(!string.IsNullOrEmpty(content))
  43. // {
  44. // StartExcute(content);
  45. // }
  46. // else
  47. // {
  48. // Thread.Sleep(60000);
  49. // }
  50. }
  51. }
  52. public void StartExcute(string content)
  53. {
  54. JsonData jsonData = JsonMapper.ToObject(content);
  55. string applyNo = jsonData["applyNo"].ToString();
  56. string mchtNo = jsonData["mchtNo"].ToString();
  57. string status = jsonData["status"].ToString();
  58. string payTime = jsonData["payTime"].ToString();
  59. WebCMSEntities db = new WebCMSEntities();
  60. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.TradePayNo == applyNo);
  61. if(order != null)
  62. {
  63. if(status == "0")
  64. {
  65. order.DivideFlag = 2;
  66. order.DivideDate = DateTime.Now;
  67. }
  68. else
  69. {
  70. order.DivideFlag = -1;
  71. string desc = jsonData["desc"].ToString();
  72. order.DivideLog = desc;
  73. }
  74. db.SaveChanges();
  75. RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":List", order.Id.ToString());
  76. }
  77. db.Dispose();
  78. }
  79. public void StartCheck()
  80. {
  81. Thread th = new Thread(StartCheckListen);
  82. th.IsBackground = true;
  83. th.Start();
  84. }
  85. public void StartCheckListen()
  86. {
  87. while (true)
  88. {
  89. try
  90. {
  91. if(DateTime.Now.Hour > 0 && DateTime.Now.Hour < 20)
  92. {
  93. string CheckDate = DateTime.Now.ToString("yyyyMMdd");
  94. string check = function.ReadInstance("/CheckYesterdayPartOrder/" + CheckDate + ".txt");
  95. if (string.IsNullOrEmpty(check))
  96. {
  97. function.WritePage("/CheckYesterdayPartOrder/", CheckDate + ".txt", DateTime.Now.ToString());
  98. WebCMSEntities db = new WebCMSEntities();
  99. DateTime checkDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
  100. var list = db.ConsumerOrders.Select(m => new { m.Id, m.DivideDate, m.DivideFlag, m.IsAct, m.PayMode }).Where(m => m.DivideFlag < 2 && m.IsAct == 1 && m.DivideDate < checkDate).OrderBy(m => m.Id).Take(100).ToList();
  101. foreach (var sub in list)
  102. {
  103. StartCheckDo(sub.Id);
  104. }
  105. db.Dispose();
  106. }
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "分账复核头天订单异常");
  112. }
  113. Thread.Sleep(3600000);
  114. }
  115. }
  116. public void StartCheckDo(int Id)
  117. {
  118. WebCMSEntities db = new WebCMSEntities();
  119. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == Id) ?? new ConsumerOrders();
  120. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  121. JsonData result = JsonMapper.ToObject(HaoDaHelper.Instance.OrderDivideAccountsQuery(order.TradePayNo, merchantAdd.OutMchtNo, merchantAdd.BrandId));
  122. if(result["resultCode"].ToString() == "1")
  123. {
  124. string status = result["data"]["status"].ToString();
  125. string desc = result["data"]["desc"].ToString();
  126. if (status == "0")
  127. {
  128. order.DivideFlag = 2;
  129. order.DivideDate = DateTime.Now;
  130. RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":List", order.Id.ToString());
  131. }
  132. else
  133. {
  134. order.DivideFlag = -1;
  135. }
  136. order.DivideLog = desc;
  137. }
  138. else
  139. {
  140. string errorCode = result["errorCode"].ToString();
  141. string errorDesc = result["errorDesc"].ToString();
  142. order.DivideFlag = -1;
  143. order.DivideLog = errorCode + ":" + errorDesc;
  144. }
  145. db.SaveChanges();
  146. db.Dispose();
  147. }
  148. // public void StartDo(string content)
  149. // {
  150. // try
  151. // {
  152. // JsonData jsonObj = JsonMapper.ToObject(content);
  153. // string ApplyNo = jsonObj["ApplyNo"].ToString(); //微信订单号
  154. // string MchtNo = jsonObj["MchtNo"].ToString(); //商户订单号
  155. // string OrderNo = jsonObj["OrderNo"].ToString(); //商户订单号
  156. // JsonData result = JsonMapper.ToObject(HaoDaHelper.Instance.OrderDivideAccountsQuery(ApplyNo, MchtNo));
  157. // string state = result["data"]["status"].ToString();
  158. // if (result["data"]["status"].ToString() == "0")
  159. // {
  160. // WebCMSEntities db = new WebCMSEntities();
  161. // ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == OrderNo && m.Status == 2);
  162. // if (order != null)
  163. // {
  164. // RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":List", order.Id.ToString());
  165. // }
  166. // db.Dispose();
  167. // }
  168. // else
  169. // {
  170. // RedisDbconn.Instance.AddList("ProfitShareHdQueue", content);
  171. // }
  172. // }
  173. // catch (Exception ex)
  174. // {
  175. // LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "分账队列异常");
  176. // }
  177. // }
  178. }
  179. }