using System; using System.Collections.Generic; using System.Linq; using MySystem.SpModels; using Library; using LitJson; using System.Threading; namespace MySystem { public class SycnSpMerchantService { public readonly static SycnSpMerchantService Instance = new SycnSpMerchantService(); private SycnSpMerchantService() { } public void Start() { Thread th = new Thread(StartDo); th.IsBackground = true; th.Start(); } public void StartDo() { while (true) { try { WebCMSEntities spdb = new WebCMSEntities(); PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities(); DateTime start = DateTime.Now.AddDays(-5); var Mers = spdb.Merchants.Where(m => m.CreateTime >= start && m.Status == 1).OrderByDescending(m => m.Id).ToList(); foreach (var Mer in Mers) { var tran = db.Database.BeginTransaction(); try { PxcModels.MachineForMerNo posFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Mer.MerNo) ?? new PxcModels.MachineForMerNo(); PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId) ?? new PxcModels.PosMachinesTwo(); if (pos.BindingState == 1) { PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new PxcModels.Users(); int TopUserId = 0; if (!string.IsNullOrEmpty(user.ParentNav)) { TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]); } int BrandId = int.Parse(Mer.ProductType); if (BrandId == 1 && Mer.MerNo.StartsWith("M900")) { BrandId = 3; } if (BrandId == 4 && Mer.Field1 == "200") { BrandId = 5; } PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mer.MerNo); if (add == null) { int IsFirst = 0; if(!string.IsNullOrEmpty(Mer.MerIdcardNo)) { string startNo = Mer.MerIdcardNo.Substring(0, 6); string endNo = Mer.MerIdcardNo.Substring(Mer.MerIdcardNo.Length - 4, 4); string Name = Mer.MerName; if (Mer.ProductType == "2") { if (Name.Contains("-")) { Name = Name.Split('-')[1]; } else if (Name.Contains("_")) { Name = Name.Split('_')[1]; } } else if (Mer.ProductType == "4") { Name = Mer.SeoTitle; } Name = Name.Replace("个体户", ""); Name = Name.Replace("个体商户", ""); bool check = db.PosMerchantInfo.Any(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerRealName.Contains(Name)); if(!check) { IsFirst = 1; } pos.IsFirst = IsFirst; } add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo() { CreateDate = Mer.CreateTime, KqMerNo = Mer.MerNo, MerchantNo = Mer.MerNo, }).Entity; db.SaveChanges(); } add.UpdateDate = Mer.UpdateTime; add.TopUserId = TopUserId; add.BrandId = BrandId; add.SnStoreId = pos.StoreId; add.SnType = pos.PosSnType; add.UserId = pos.UserId; add.MgrName = Mer.AgentName; add.MerStatus = 1; add.KqSnNo = Mer.SnNo; add.MerIdcardNo = Mer.MerIdcardNo; add.MerRealName = Mer.MerRealName; add.MerchantMobile = Mer.MerMobile; add.MerchantName = Mer.MerName; pos.BindMerchantId = add.Id; db.SaveChanges(); Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id); if (edit != null) { edit.Status = 2; spdb.SaveChanges(); } tran.Commit(); } } catch (Exception ex) { tran.Rollback(); function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户数据到MAIN异常"); } tran.Dispose(); } spdb.SaveChanges(); spdb.Dispose(); db.SaveChanges(); db.Dispose(); } catch (Exception ex) { function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户数据到MAIN异常"); } Thread.Sleep(1000); } } } }