123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- 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<string>("WeChatSignQueue");
- if (!string.IsNullOrEmpty(content))
- {
- string result = StartDo(Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantSign>(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<MerchantSign> deletes = new List<MerchantSign>();
- Dictionary<string, object> dic = WeChatFunction.Instance.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();
- }
- else
- {
- merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.Status == 1);
- if (merchantadd != null)
- {
- 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_TO_BE_CONFIRMED")
- {
- 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;
- 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<MerchantInfo> merchants = RedisDbconn.Instance.GetList<MerchantInfo>("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 "";
- }
- }
- }
|