CheckWeChatSignService.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. namespace MySystem
  9. {
  10. public class CheckWeChatSignService
  11. {
  12. public readonly static CheckWeChatSignService Instance = new CheckWeChatSignService();
  13. private CheckWeChatSignService()
  14. { }
  15. public void Start(JobMqMsg jobInfo)
  16. {
  17. try
  18. {
  19. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  20. List<MerchantSign> deletes = new List<MerchantSign>();
  21. List<MerchantSign> signs = RedisDbconn.Instance.GetList<MerchantSign>("WeChatSignList", 1, 10000000); // TODO: 换成mysql存储遍历
  22. foreach (MerchantSign sign in signs)
  23. {
  24. Dictionary<string, object> dic = new WeChatFunction().QueryMerchant(sign.BusinessCode);
  25. if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_TO_BE_SIGNED")
  26. {
  27. string sign_url = dic["sign_url"].ToString();
  28. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.Status == 0);
  29. if (merchantadd != null)
  30. {
  31. merchantadd.Status = 1;
  32. string SignUrlList = function.CheckNull(merchantadd.SeoKeyword);
  33. if (string.IsNullOrEmpty(SignUrlList))
  34. {
  35. SignUrlList = "WeChat:" + sign_url + ";";
  36. }
  37. else
  38. {
  39. SignUrlList += "WeChat:" + sign_url+ ";";
  40. }
  41. merchantadd.SeoKeyword = SignUrlList;
  42. PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  43. if (merchantadd.Status > 0 && merchantadd.QueryCount > 0)
  44. {
  45. merchant.Status = 1;
  46. }
  47. db.SaveChanges();
  48. RedisDbconn.Instance.Set("MerchantAddInfo:" + sign.MerchantAddInfoId, merchantadd);
  49. }
  50. }
  51. else if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_FINISHED")
  52. {
  53. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.Status == 1);
  54. if (merchantadd != null)
  55. {
  56. merchantadd.Status = 2;
  57. RedisDbconn.Instance.Set("MerchantAddInfo:" + sign.MerchantAddInfoId, merchantadd);
  58. PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  59. if (merchantadd.Status == 2 && merchantadd.QueryCount == 2)
  60. {
  61. merchant.Status = 2;
  62. }
  63. db.SaveChanges();
  64. if (merchantadd.Status == 2 && merchantadd.QueryCount == 2)
  65. {
  66. List<MerchantInfo> merchants = RedisDbconn.Instance.GetList<MerchantInfo>("TmpMerchantInfo:" + merchant.UserId, 1, 1000000);
  67. MerchantInfo check = merchants.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  68. if (check != null)
  69. {
  70. merchants.Remove(check);
  71. RedisDbconn.Instance.Clear("TmpMerchantInfo:" + merchant.UserId);
  72. RedisDbconn.Instance.AddList("TmpMerchantInfo:" + merchant.UserId, merchants.ToArray());
  73. }
  74. }
  75. deletes.Add(sign);
  76. }
  77. }
  78. else if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_REJECTED")
  79. {
  80. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  81. if (merchantadd != null)
  82. {
  83. string Season = function.CheckNull(merchantadd.SeoDescription);
  84. if (string.IsNullOrEmpty(Season))
  85. {
  86. Season = "WeChat:" + dic["reject_reason"].ToString() + ";";
  87. }
  88. else
  89. {
  90. Season = Regex.Replace(Season, "WeChat:.*?;", "");
  91. Season += "WeChat:" + dic["reject_reason"].ToString() + ";";
  92. }
  93. merchantadd.SeoDescription = Season;
  94. merchantadd.Status = -1;
  95. db.SaveChanges();
  96. RedisDbconn.Instance.Set("MerchantAddInfo:" + sign.MerchantAddInfoId, merchantadd);
  97. PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  98. if (merchant != null)
  99. {
  100. merchant.Status = -1;
  101. RedisDbconn.Instance.Set("MerchantInfo:" + sign.MerchantAddInfoId, merchant);
  102. }
  103. deletes.Add(sign);
  104. }
  105. }
  106. else
  107. {
  108. string sub_mchid = "";
  109. if (dic.ContainsKey("sub_mchid"))
  110. {
  111. sub_mchid = dic["sub_mchid"].ToString();
  112. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  113. if (merchantadd != null)
  114. {
  115. merchantadd.SubMchid = sub_mchid;
  116. db.SaveChanges();
  117. RedisDbconn.Instance.Set("MerchantAddInfo:" + sign.MerchantAddInfoId, merchantadd);
  118. }
  119. }
  120. }
  121. }
  122. if (deletes.Count > 0)
  123. {
  124. foreach (MerchantSign delete in deletes)
  125. {
  126. signs.Remove(delete);
  127. }
  128. RedisDbconn.Instance.Clear("WeChatSignList");
  129. RedisDbconn.Instance.AddList("WeChatSignList", signs.ToArray());
  130. }
  131. // jobInfo.Status = 1;
  132. // jobInfo.Msg = "执行完成";
  133. // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
  134. }
  135. catch (Exception ex)
  136. {
  137. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public:merchant:signerr");
  138. }
  139. }
  140. }
  141. }