CheckWeChatSignService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. StartDo(Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantSign>(content));
  30. }
  31. else
  32. {
  33. Thread.Sleep(2000);
  34. }
  35. }
  36. }
  37. public void StartDo(MerchantSign sign)
  38. {
  39. try
  40. {
  41. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  42. List<MerchantSign> deletes = new List<MerchantSign>();
  43. Dictionary<string, object> dic = new WeChatFunction().QueryMerchant(sign.BusinessCode);
  44. if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_TO_BE_SIGNED")
  45. {
  46. string sign_url = dic["sign_url"].ToString();
  47. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.Status == 0);
  48. if (merchantadd != null)
  49. {
  50. merchantadd.Status = 1;
  51. string SignUrlList = function.CheckNull(merchantadd.SeoKeyword);
  52. if (string.IsNullOrEmpty(SignUrlList))
  53. {
  54. SignUrlList = "WeChat:" + sign_url + ";";
  55. }
  56. else
  57. {
  58. SignUrlList += "WeChat:" + sign_url+ ";";
  59. }
  60. merchantadd.SeoKeyword = SignUrlList;
  61. PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  62. if (merchantadd.Status > 0 && merchantadd.QueryCount > 0)
  63. {
  64. merchant.Status = 1;
  65. }
  66. db.SaveChanges();
  67. }
  68. }
  69. else if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_FINISHED")
  70. {
  71. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.Status == 1);
  72. if (merchantadd != null)
  73. {
  74. merchantadd.Status = 2;
  75. RedisDbconn.Instance.Set("MerchantAddInfo:" + sign.MerchantAddInfoId, merchantadd);
  76. PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  77. if (merchantadd.Status == 2 && merchantadd.QueryCount == 2)
  78. {
  79. merchant.Status = 2;
  80. }
  81. db.SaveChanges();
  82. if (merchantadd.Status == 2 && merchantadd.QueryCount == 2)
  83. {
  84. List<MerchantInfo> merchants = RedisDbconn.Instance.GetList<MerchantInfo>("TmpMerchantInfo:" + merchant.UserId, 1, 1000000);
  85. MerchantInfo check = merchants.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  86. if (check != null)
  87. {
  88. merchants.Remove(check);
  89. RedisDbconn.Instance.Clear("TmpMerchantInfo:" + merchant.UserId);
  90. RedisDbconn.Instance.AddList("TmpMerchantInfo:" + merchant.UserId, merchants.ToArray());
  91. }
  92. }
  93. deletes.Add(sign);
  94. }
  95. }
  96. else if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_REJECTED")
  97. {
  98. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  99. if (merchantadd != null)
  100. {
  101. string Season = function.CheckNull(merchantadd.SeoDescription);
  102. if (string.IsNullOrEmpty(Season))
  103. {
  104. Season = "WeChat:" + dic["reject_reason"].ToString() + ";";
  105. }
  106. else
  107. {
  108. Season = Regex.Replace(Season, "WeChat:.*?;", "");
  109. Season += "WeChat:" + dic["reject_reason"].ToString() + ";";
  110. }
  111. merchantadd.SeoDescription = Season;
  112. merchantadd.Status = -1;
  113. PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  114. if (merchant != null)
  115. {
  116. merchant.Status = -1;
  117. }
  118. db.SaveChanges();
  119. deletes.Add(sign);
  120. }
  121. }
  122. else
  123. {
  124. string sub_mchid = "";
  125. if (dic.ContainsKey("sub_mchid"))
  126. {
  127. sub_mchid = dic["sub_mchid"].ToString();
  128. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  129. if (merchantadd != null)
  130. {
  131. merchantadd.SubMchid = sub_mchid;
  132. db.SaveChanges();
  133. }
  134. }
  135. }
  136. }
  137. catch (Exception ex)
  138. {
  139. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "微信进件回调队列异常");
  140. }
  141. }
  142. }
  143. }