using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using MySystem.PxcModels; using Library; using LitJson; using System.Threading; namespace MySystem { public class MerchantConfirmService { public readonly static MerchantConfirmService Instance = new MerchantConfirmService(); private MerchantConfirmService() { } public void Start() { Thread th = new Thread(StartListen); th.IsBackground = true; th.Start(); } public void StartListen() { while (true) { string content = RedisDbconn.Instance.RPop("MerchantConfirmQueue"); if (!string.IsNullOrEmpty(content)) { try { JsonData jsonObj = JsonMapper.ToObject(content); int MerchantId = int.Parse(jsonObj["MerchantId"].ToString()); // int Kind = int.Parse(jsonObj["Kind"].ToString()); StartDo(MerchantId); } catch (Exception ex) { function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service"); } } else { Thread.Sleep(2000); } } } public void StartDo(int MerchantId, int Kind = 0) { PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities(); PxcModels.MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == MerchantId); PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId); if (AddInfo != null && merchant != null) { string BusinessCode = AddInfo.BusinessCode; if (string.IsNullOrEmpty(BusinessCode)) { BusinessCode = "KXS" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(5); AddInfo.BusinessCode = BusinessCode; } merchant.LoginPwd = function.MD532(AddInfo.MobilePhone.Substring(5)); PxcModels.MerchantParamSet query = db.MerchantParamSet.FirstOrDefault(m => m.Id == MerchantId); if (query == null) { query = new PxcModels.MerchantParamSet(); query.IsAll = 1; db.MerchantParamSet.Add(query); db.SaveChanges(); } PxcModels.MerchantForMobile merchantForMobile = db.MerchantForMobile.FirstOrDefault(m => m.Mobile == AddInfo.MobilePhone); if (merchantForMobile == null) { merchantForMobile = db.MerchantForMobile.Add(new PxcModels.MerchantForMobile() { Mobile = AddInfo.MobilePhone, }).Entity; db.SaveChanges(); } merchantForMobile.MerchantId = MerchantId; db.SaveChanges(); //微信 if (Kind == 0 || Kind == 2) { string result = new WeChatFunction().MerchantCreate(AddInfo, merchant); if (result.Contains("\"applyment_id\":")) { JsonData jsonObj = JsonMapper.ToObject(result); AddInfo.ApplymentId = jsonObj["applyment_id"].ToString(); AddInfo.Status = 0; db.SaveChanges(); List signs = RedisDbconn.Instance.GetList("MerchantSignList", 1, 10000000); bool op = signs.Any(m => m.BusinessCode == BusinessCode); if (!op) { RedisDbconn.Instance.AddList("WeChatSignQueue", new MerchantSign() { BusinessCode = BusinessCode, MerchantAddInfoId = MerchantId, Status = "" }); } signs.Clear(); AddInfo.ApplymentId = AddInfo.ApplymentId; AddInfo.Status = AddInfo.Status; db.SaveChanges(); } else if (result.Contains("\"message\":")) { JsonData jsonObj = JsonMapper.ToObject(result); AddInfo.Status = -1; merchant.Status = -1; string Season = function.CheckNull(AddInfo.SeoDescription); if (string.IsNullOrEmpty(Season)) { Season = "WeChat:" + jsonObj["message"].ToString() + ";"; } else { Season = Regex.Replace(Season, "WeChat:.*?;", ""); Season += "WeChat:" + jsonObj["message"].ToString() + ";"; } AddInfo.SeoDescription = Season; AddInfo.SeoDescription = AddInfo.SeoDescription; AddInfo.Status = AddInfo.Status; db.SaveChanges(); } } //支付宝 if (Kind == 0 || Kind == 1) { bool checkAlipay = true; string content = new AlipayFunction().GetBatchNo(AddInfo.CreateMan, AddInfo.IdCardName, AddInfo.MobilePhone, AddInfo.ContactEmail); JsonData json = JsonMapper.ToObject(content); if (json["alipay_open_agent_create_response"]["code"].ToString() == "10000") { string batch_no = json["alipay_open_agent_create_response"]["batch_no"].ToString(); string Qualifications = AddInfo.Qualifications; if (!string.IsNullOrEmpty(Qualifications)) { Qualifications = function.getPath("/bsserver_com" + Qualifications); } content = new AlipayFunction().CommonSign(batch_no, AddInfo.UpdateMan, AddInfo.LicenseNumber, function.getPath("/bsserver_com" + AddInfo.LicenseCopy), Qualifications); json = JsonMapper.ToObject(content); if (json["alipay_open_agent_common_sign_response"]["code"].ToString() == "10000") { content = new AlipayFunction().CommonSignConfirm(batch_no); json = JsonMapper.ToObject(content); if (json["alipay_open_agent_commonsign_confirm_response"]["code"].ToString() == "10000") { AddInfo.QueryCount = 0; AddInfo.SeoTitle = batch_no; db.SaveChanges(); List signs = RedisDbconn.Instance.GetList("MerchantSignList", 1, 10000000); bool op = signs.Any(m => m.BusinessCode == batch_no); if (!op) { RedisDbconn.Instance.AddList("AlipaySignQueue", new MerchantSign() { BusinessCode = batch_no, MerchantAddInfoId = MerchantId, Status = "" }); } signs.Clear(); if (db.MerchantInfo.Any(m => m.Id == merchant.Id) == false) { RedisDbconn.Instance.AddList("MerchantList:" + merchant.UserId, merchant); } } else { checkAlipay = false; } } else { checkAlipay = false; } } else { checkAlipay = false; } if (!checkAlipay) { JsonData jsonObj = JsonMapper.ToObject(content); AddInfo.QueryCount = -1; merchant.Status = -1; string Season = function.CheckNull(AddInfo.SeoDescription); if (string.IsNullOrEmpty(Season)) { Season = "Alipay:" + jsonObj["alipay_open_agent_create_response"]["sub_msg"].ToString() + ";"; } else { Season = Regex.Replace(Season, "Alipay:.*?;", ""); Season = "Alipay:" + jsonObj["alipay_open_agent_create_response"]["sub_msg"].ToString() + ";" + Season; } AddInfo.SeoDescription = Season; AddInfo.SeoDescription = AddInfo.SeoDescription; AddInfo.QueryCount = AddInfo.QueryCount; db.SaveChanges(); } } } db.Dispose(); } } }