CheckWeChatSignService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. if (merchantadd.Status == 2 && merchantadd.QueryCount == 2)
  141. {
  142. List<MerchantInfo> merchants = RedisDbconn.Instance.GetList<MerchantInfo>("TmpMerchantInfo:" + merchant.UserId, 1, 1000000);
  143. MerchantInfo check = merchants.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  144. if (check != null)
  145. {
  146. merchants.Remove(check);
  147. RedisDbconn.Instance.Clear("TmpMerchantInfo:" + merchant.UserId);
  148. RedisDbconn.Instance.AddList("TmpMerchantInfo:" + merchant.UserId, merchants.ToArray());
  149. }
  150. }
  151. deletes.Add(sign);
  152. }
  153. }
  154. else if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_REJECTED")
  155. {
  156. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  157. if (merchantadd != null)
  158. {
  159. string Season = function.CheckNull(merchantadd.SeoDescription);
  160. if (string.IsNullOrEmpty(Season))
  161. {
  162. Season = "WeChat:" + dic["reject_reason"].ToString() + ";";
  163. }
  164. else
  165. {
  166. Season = Regex.Replace(Season, "WeChat:.*?;", "");
  167. Season += "WeChat:" + dic["reject_reason"].ToString() + ";";
  168. }
  169. merchantadd.SeoDescription = Season;
  170. merchantadd.Status = -1;
  171. PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  172. if (merchant != null)
  173. {
  174. merchant.Status = -1;
  175. }
  176. db.SaveChanges();
  177. deletes.Add(sign);
  178. }
  179. }
  180. else if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_AUDITING")
  181. {
  182. return "wait";
  183. }
  184. else
  185. {
  186. string sub_mchid = "";
  187. if (dic.ContainsKey("sub_mchid"))
  188. {
  189. sub_mchid = dic["sub_mchid"].ToString();
  190. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  191. if (merchantadd != null)
  192. {
  193. merchantadd.SubMchid = sub_mchid;
  194. db.SaveChanges();
  195. }
  196. }
  197. }
  198. }
  199. catch (Exception ex)
  200. {
  201. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "微信进件回调队列异常");
  202. }
  203. return "";
  204. }
  205. }
  206. }