using System; using System.Collections.Generic; using System.Linq; using MySystem.PxcModels; using Library; using LitJson; using System.Text.RegularExpressions; using System.Threading; namespace MySystem { public class CheckWeChatSignService { public readonly static CheckWeChatSignService Instance = new CheckWeChatSignService(); private CheckWeChatSignService() { } public void Start() { Thread th = new Thread(StartListen); th.IsBackground = true; th.Start(); } public void StartListen() { while (true) { string content = RedisDbconn.Instance.RPop("WeChatSignQueue"); if (!string.IsNullOrEmpty(content)) { string result = StartDo(Newtonsoft.Json.JsonConvert.DeserializeObject(content)); if(result == "wait") { Thread.Sleep(10000); RedisDbconn.Instance.AddList("WeChatSignQueue", content); } } else { Thread.Sleep(2000); } } } public string StartDo(MerchantSign sign) { try { PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities(); List deletes = new List(); Dictionary dic = new WeChatFunction().QueryMerchant(sign.BusinessCode); if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_TO_BE_SIGNED") { string sign_url = dic["sign_url"].ToString(); PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.Status == 0); if (merchantadd != null) { merchantadd.Status = 1; merchantadd.SubMchid = dic["sub_mchid"].ToString(); string SignUrlList = function.CheckNull(merchantadd.SeoKeyword); if (string.IsNullOrEmpty(SignUrlList)) { SignUrlList = "WeChat:" + sign_url + ";"; } else { SignUrlList += "WeChat:" + sign_url+ ";"; } merchantadd.SeoKeyword = SignUrlList; PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId); if (merchantadd.Status > 0 && merchantadd.QueryCount > 0) { merchant.Status = 1; } db.SaveChanges(); } return "wait"; } else if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_FINISHED") { PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.Status == 1); if (merchantadd != null) { merchantadd.Status = 2; merchantadd.SubMchid = dic["sub_mchid"].ToString(); RedisDbconn.Instance.Set("MerchantAddInfo:" + sign.MerchantAddInfoId, merchantadd); PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId); if (merchantadd.Status == 2 && merchantadd.QueryCount == 2) { merchant.Status = 2; } db.SaveChanges(); if (merchantadd.Status == 2 && merchantadd.QueryCount == 2) { List merchants = RedisDbconn.Instance.GetList("TmpMerchantInfo:" + merchant.UserId, 1, 1000000); MerchantInfo check = merchants.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId); if (check != null) { merchants.Remove(check); RedisDbconn.Instance.Clear("TmpMerchantInfo:" + merchant.UserId); RedisDbconn.Instance.AddList("TmpMerchantInfo:" + merchant.UserId, merchants.ToArray()); } } deletes.Add(sign); } } else if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_REJECTED") { PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId); if (merchantadd != null) { string Season = function.CheckNull(merchantadd.SeoDescription); if (string.IsNullOrEmpty(Season)) { Season = "WeChat:" + dic["reject_reason"].ToString() + ";"; } else { Season = Regex.Replace(Season, "WeChat:.*?;", ""); Season += "WeChat:" + dic["reject_reason"].ToString() + ";"; } merchantadd.SeoDescription = Season; merchantadd.Status = -1; PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId); if (merchant != null) { merchant.Status = -1; } db.SaveChanges(); deletes.Add(sign); } } else if (dic["applyment_state"].ToString() == "APPLYMENT_STATE_AUDITING") { return "wait"; } else { string sub_mchid = ""; if (dic.ContainsKey("sub_mchid")) { sub_mchid = dic["sub_mchid"].ToString(); PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId); if (merchantadd != null) { merchantadd.SubMchid = sub_mchid; db.SaveChanges(); } } } } catch (Exception ex) { function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "微信进件回调队列异常"); } return ""; } } }