CheckWeChatSignService.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.PxcModels;
  5. using Library;
  6. using LitJson;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9. namespace MySystem
  10. {
  11. public class CheckWeChatSignService
  12. {
  13. public readonly static CheckWeChatSignService Instance = new CheckWeChatSignService();
  14. private CheckWeChatSignService()
  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. string content = RedisDbconn.Instance.RPop<string>("WeChatSignQueue");
  27. if (!string.IsNullOrEmpty(content))
  28. {
  29. string result = StartDo(Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantSign>(content));
  30. if(result == "wait")
  31. {
  32. Thread.Sleep(10000);
  33. RedisDbconn.Instance.AddList("WeChatSignQueue", content);
  34. }
  35. }
  36. else
  37. {
  38. Thread.Sleep(2000);
  39. }
  40. }
  41. }
  42. public string StartDo(MerchantSign sign)
  43. {
  44. try
  45. {
  46. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  47. List<MerchantSign> deletes = new List<MerchantSign>();
  48. Dictionary<string, object> dic = WeChatFunction.Instance.QueryMerchant(sign.BusinessCode);
  49. if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_TO_BE_SIGNED")
  50. {
  51. string sign_url = dic["sign_url"].ToString();
  52. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.Status == 0);
  53. if (merchantadd != null)
  54. {
  55. merchantadd.Status = 1;
  56. merchantadd.SubMchid = dic["sub_mchid"].ToString();
  57. string SignUrlList = function.CheckNull(merchantadd.SeoKeyword);
  58. if (string.IsNullOrEmpty(SignUrlList))
  59. {
  60. SignUrlList = "WeChat:" + sign_url + ";";
  61. }
  62. else
  63. {
  64. SignUrlList += "WeChat:" + sign_url+ ";";
  65. }
  66. merchantadd.SeoKeyword = SignUrlList;
  67. PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  68. if (merchantadd.Status > 0 && merchantadd.QueryCount > 0)
  69. {
  70. merchant.Status = 1;
  71. }
  72. db.SaveChanges();
  73. }
  74. else
  75. {
  76. merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.Status == 1);
  77. if (merchantadd != null)
  78. {
  79. merchantadd.SubMchid = dic["sub_mchid"].ToString();
  80. string SignUrlList = function.CheckNull(merchantadd.SeoKeyword);
  81. if (string.IsNullOrEmpty(SignUrlList))
  82. {
  83. SignUrlList = "WeChat:" + sign_url + ";";
  84. }
  85. else
  86. {
  87. SignUrlList += "WeChat:" + sign_url+ ";";
  88. }
  89. merchantadd.SeoKeyword = SignUrlList;
  90. PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  91. if (merchantadd.Status > 0 && merchantadd.QueryCount > 0)
  92. {
  93. merchant.Status = 1;
  94. }
  95. db.SaveChanges();
  96. }
  97. }
  98. return "wait";
  99. }
  100. else if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_TO_BE_CONFIRMED")
  101. {
  102. string sign_url = dic["sign_url"].ToString();
  103. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.Status == 0);
  104. if (merchantadd != null)
  105. {
  106. merchantadd.Status = 1;
  107. string SignUrlList = function.CheckNull(merchantadd.SeoKeyword);
  108. if (string.IsNullOrEmpty(SignUrlList))
  109. {
  110. SignUrlList = "WeChat:" + sign_url + ";";
  111. }
  112. else
  113. {
  114. SignUrlList += "WeChat:" + sign_url+ ";";
  115. }
  116. merchantadd.SeoKeyword = SignUrlList;
  117. PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  118. if (merchantadd.Status > 0 && merchantadd.QueryCount > 0)
  119. {
  120. merchant.Status = 1;
  121. }
  122. db.SaveChanges();
  123. }
  124. return "wait";
  125. }
  126. else if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_FINISHED")
  127. {
  128. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.Status == 1);
  129. if (merchantadd != null)
  130. {
  131. merchantadd.Status = 2;
  132. // merchantadd.SubMchid = dic["sub_mchid"].ToString();
  133. RedisDbconn.Instance.Set("MerchantAddInfo:" + sign.MerchantAddInfoId, merchantadd);
  134. PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  135. if (merchantadd.Status == 2 && merchantadd.QueryCount == 2)
  136. {
  137. merchant.Status = 2;
  138. }
  139. db.SaveChanges();
  140. }
  141. }
  142. else if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_REJECTED")
  143. {
  144. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  145. if (merchantadd != null)
  146. {
  147. string Season = function.CheckNull(merchantadd.SeoDescription);
  148. if (string.IsNullOrEmpty(Season))
  149. {
  150. Season = "WeChat:" + dic["reject_reason"].ToString() + ";";
  151. }
  152. else
  153. {
  154. Season = Regex.Replace(Season, "WeChat:.*?;", "");
  155. Season += "WeChat:" + dic["reject_reason"].ToString() + ";";
  156. }
  157. merchantadd.SeoDescription = Season;
  158. merchantadd.Status = -1;
  159. PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  160. if (merchant != null)
  161. {
  162. merchant.Status = -1;
  163. }
  164. db.SaveChanges();
  165. deletes.Add(sign);
  166. }
  167. }
  168. else if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_AUDITING")
  169. {
  170. return "wait";
  171. }
  172. else
  173. {
  174. string sub_mchid = "";
  175. if (dic.ContainsKey("sub_mchid"))
  176. {
  177. sub_mchid = dic["sub_mchid"].ToString();
  178. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  179. if (merchantadd != null)
  180. {
  181. merchantadd.SubMchid = sub_mchid;
  182. db.SaveChanges();
  183. }
  184. }
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "微信进件回调队列异常");
  190. }
  191. return "";
  192. }
  193. }
  194. }