HaoDaExtQueryHelper.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using System.Linq;
  6. using Microsoft.Extensions.Hosting;
  7. using MySystem;
  8. using MySystem.Models;
  9. using LitJson;
  10. using Library;
  11. using MySystem.Models.Main;
  12. public class HaoDaExtQueryHelper
  13. {
  14. public readonly static HaoDaExtQueryHelper Instance = new HaoDaExtQueryHelper();
  15. private HaoDaExtQueryHelper()
  16. {
  17. }
  18. public void StartWeChat()
  19. {
  20. Thread th = new Thread(StartWeChatDo);
  21. th.IsBackground = true;
  22. th.Start();
  23. }
  24. private void StartWeChatDo()
  25. {
  26. while (true)
  27. {
  28. string content = RedisDbconn.Instance.RPop<string>("WeChatResultForHaoDaQueue");
  29. if (!string.IsNullOrEmpty(content))
  30. {
  31. JsonData json = JsonMapper.ToObject(content);
  32. WeChatAddInfo(int.Parse(json["MerchantId"].ToString()), json["ApplymentId"].ToString());
  33. }
  34. else
  35. {
  36. Thread.Sleep(10000);
  37. }
  38. }
  39. }
  40. public void WeChatAddInfo(int Id, string ApplymentId)
  41. {
  42. try
  43. {
  44. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "查询微信商户意愿申请状态");
  45. LogHelper.Instance.WriteLog("start", "查询微信商户意愿申请状态");
  46. WebCMSEntities db = new WebCMSEntities();
  47. MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
  48. string result = WeChatFunctionForHD.Instance.QueryMerchant(ApplymentId);
  49. LogHelper.Instance.WriteLog(result, "查询微信商户意愿申请状态");
  50. JsonData jsonObj = JsonMapper.ToObject(result);
  51. if (jsonObj["applyment_state"].ToString() == "APPLYMENT_STATE_WAITTING_FOR_CONFIRM_CONTACT")
  52. {
  53. LogHelper.Instance.WriteLog("starts", "查询支付宝商户意愿申请状态");
  54. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  55. info.Status = 1;
  56. merchant.Status = 1;
  57. info.WeChatRemark = "";
  58. info.WeChatSignUrl = jsonObj["qrcode_data"].ToString();
  59. db.SaveChanges();
  60. RedisDbconn.Instance.AddList("HaoDaAuthQueryHelper", "{\"MerchantId\":\"" + Id + "\",\"MchtNo\":\"" + info.MchtNo + "\",\"StoreNo\":\"" + info.StoreNo + "\"}");
  61. LogHelper.Instance.WriteLog("startsend", "查询支付宝商户意愿申请状态");
  62. }
  63. else if (jsonObj["applyment_state"].ToString() == "APPLYMENT_STATE_REJECTED")
  64. {
  65. LogHelper.Instance.WriteLog("start1", "查询支付宝商户意愿申请状态");
  66. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  67. info.Status = -1;
  68. merchant.Status = -1;
  69. info.WeChatRemark = jsonObj["reject_reason"].ToString();
  70. LogHelper.Instance.WriteLog("start1end", "查询支付宝商户意愿申请状态");
  71. db.SaveChanges();
  72. }
  73. else
  74. {
  75. LogHelper.Instance.WriteLog("starte", "查询支付宝商户意愿申请状态");
  76. RedisDbconn.Instance.AddList("WeChatResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"ApplymentId\":\"" + ApplymentId + "\"}");
  77. LogHelper.Instance.WriteLog("start2end", "查询支付宝商户意愿申请状态");
  78. }
  79. db.Dispose();
  80. }
  81. catch (Exception ex)
  82. {
  83. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "微信开户意愿申请异常");
  84. }
  85. }
  86. public void StartAlipay()
  87. {
  88. Thread th = new Thread(StartAlipayDo);
  89. th.IsBackground = true;
  90. th.Start();
  91. }
  92. private void StartAlipayDo()
  93. {
  94. while (true)
  95. {
  96. string content = RedisDbconn.Instance.RPop<string>("AlipayResultForHaoDaQueue");
  97. if (!string.IsNullOrEmpty(content))
  98. {
  99. JsonData json = JsonMapper.ToObject(content);
  100. AlipayAddInfo(int.Parse(json["MerchantId"].ToString()), json["order_no"].ToString());
  101. }
  102. else
  103. {
  104. Thread.Sleep(10000);
  105. }
  106. }
  107. }
  108. public void AlipayAddInfo(int Id, string OrderNo)
  109. {
  110. try
  111. {
  112. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "查询支付宝商户意愿申请状态");
  113. LogHelper.Instance.WriteLog("start", "查询支付宝商户意愿申请状态");
  114. WebCMSEntities db = new WebCMSEntities();
  115. MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
  116. string result = AlipayFunctionForHD.Instance.AuthOrderQuery(OrderNo);
  117. JsonData jsonObj = JsonMapper.ToObject(result);
  118. LogHelper.Instance.WriteLog(result, "查询支付宝商户意愿申请状态");
  119. if (jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["code"].ToString() == "10000")
  120. {
  121. LogHelper.Instance.WriteLog("starts", "查询支付宝商户意愿申请状态");
  122. string order_status = jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["order_status"].ToString();
  123. if (order_status == "CONTACT_CONFIRM")
  124. {
  125. LogHelper.Instance.WriteLog("start1", "查询支付宝商户意愿申请状态");
  126. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  127. info.QueryCount = 1;
  128. merchant.QueryCount = 1;
  129. info.AlipayRemark = "";
  130. info.AlipaySignUrl = jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["qr_code"].ToString();
  131. db.SaveChanges();
  132. RedisDbconn.Instance.AddList("HaoDaAuthQueryHelper", "{\"MerchantId\":\"" + Id + "\",\"MchtNo\":\"" + info.MchtNo + "\",\"StoreNo\":\"" + info.StoreNo + "\"}");
  133. LogHelper.Instance.WriteLog("start1end", "查询支付宝商户意愿申请状态");
  134. }
  135. else if (order_status == "AUDIT_REJECT" || order_status == "AUDIT_FREEZE")
  136. {
  137. LogHelper.Instance.WriteLog("start2", "查询支付宝商户意愿申请状态");
  138. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  139. info.QueryCount = -1;
  140. merchant.QueryCount = -1;
  141. info.AlipayRemark = jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["msg"].ToString();
  142. LogHelper.Instance.WriteLog("start2end", "查询支付宝商户意愿申请状态");
  143. db.SaveChanges();
  144. }
  145. else if (order_status == "AUDIT_PASS")
  146. {
  147. LogHelper.Instance.WriteLog("start2", "查询支付宝商户意愿申请状态");
  148. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  149. info.QueryCount = 2;
  150. merchant.QueryCount = 2;
  151. info.AlipayRemark = "";
  152. LogHelper.Instance.WriteLog("start2end", "查询支付宝商户意愿申请状态");
  153. db.SaveChanges();
  154. }
  155. else
  156. {
  157. RedisDbconn.Instance.AddList("AlipayResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"order_no\":\"" + OrderNo + "\"}");
  158. }
  159. }
  160. else
  161. {
  162. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  163. info.QueryCount = -1;
  164. merchant.QueryCount = -1;
  165. info.AlipayRemark = jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["msg"].ToString();
  166. db.SaveChanges();
  167. }
  168. db.Dispose();
  169. }
  170. catch (Exception ex)
  171. {
  172. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "支付宝开户意愿申请异常");
  173. }
  174. }
  175. }