using System; using System.Collections.Generic; using System.Linq; using MySystem.Models; using Library; using LitJson; using System.Text.RegularExpressions; using System.Threading; namespace MySystem { public class CheckAlipaySignService { public readonly static CheckAlipaySignService Instance = new CheckAlipaySignService(); private CheckAlipaySignService() { } public void Start() { Thread th = new Thread(StartListen); th.IsBackground = true; th.Start(); } public void StartListen() { while (true) { string content = RedisDbconn.Instance.RPop("AlipaySignQueue"); if (!string.IsNullOrEmpty(content)) { string result = StartDo(Newtonsoft.Json.JsonConvert.DeserializeObject(content)); if(result == "wait") { Thread.Sleep(10000); RedisDbconn.Instance.AddList("AlipaySignQueue", content); } } else { Thread.Sleep(2000); } } } public string StartDo(MerchantSign sign) { try { WebCMSEntities db = new WebCMSEntities(); string content = AlipayFunction.Instance.QuerySignStatus(sign.BusinessCode); JsonData dic = JsonMapper.ToObject(content); if (dic["alipay_open_agent_order_query_response"]["code"].ToString() == "10000") { if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_CONFIRM") { string sign_url = dic["alipay_open_agent_order_query_response"]["confirm_url"].ToString(); MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.QueryCount == 0); if (merchantadd != null) { merchantadd.QueryCount = 1; MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId); if (merchant != null) { merchant.QueryCount = 1; } string confirm_url = "/static/alipay_confirm_url/"; string fileName = function.MD5_16(Guid.NewGuid().ToString()); function.CreateQRCode2(sign_url, fileName, "/" + confirm_url); merchantadd.AlipaySignUrl = confirm_url; merchantadd.AlipayPid = dic["alipay_open_agent_order_query_response"]["merchant_pid"].ToString(); db.SaveChanges(); } return "wait"; } else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_CONFIRM_SUCCESS") { MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.QueryCount < 2); if (merchantadd != null) { merchantadd.QueryCount = 2; merchantadd.AlipayPid = dic["alipay_open_agent_order_query_response"]["merchant_pid"].ToString(); MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId); if (merchant != null) { merchant.QueryCount = 2; } db.SaveChanges(); } } else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_APPLY_ORDER_CANCELED") { MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId); if (merchantadd != null) { string Season = function.CheckNull(merchantadd.SeoDescription); if (string.IsNullOrEmpty(Season)) { Season = "Alipay:" + dic["alipay_open_agent_order_query_response"]["reject_reason"].ToString() + ";"; } else { Season = Regex.Replace(Season, "Alipay:.*?;", ""); Season += "Alipay:" + dic["alipay_open_agent_order_query_response"]["reject_reason"].ToString() + ";"; } merchantadd.SeoDescription = Season; merchantadd.QueryCount = -1; MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId); if (merchant != null) { merchant.QueryCount = -1; } db.SaveChanges(); } } else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_AUDITING") { return "wait"; } } } catch (Exception ex) { function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public:merchant:signerr"); } return ""; } } }