using System; using System.Collections.Generic; using System.Linq; using MySystem.SpModels; using Library; using LitJson; using System.Threading; namespace MySystem { public class SycnSpMerchantRecordService { public readonly static SycnSpMerchantRecordService Instance = new SycnSpMerchantRecordService(); private SycnSpMerchantRecordService() { } 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(-30); int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/MerchantRecordId.txt"))); var Mers = spdb.MerchantRecord.Where(m => m.Id >= StartId && m.CreateTime >= start && m.Status == 1 && (m.ProductType == "12" || m.ProductType == "30")).OrderByDescending(m => m.Id).Take(10).ToList(); foreach (var Mer in Mers) { var tran = db.Database.BeginTransaction(); try { if(Mer.ProductType == "12") { Mer.MerSnNo = RedisDbconn.Instance.Get("sppos:" + Mer.MerNo); RedisDbconn.Instance.SetExpire("sppos:" + Mer.MerNo, -1); } if(Mer.ProductType == "30") { Mer.MerSnNo = RedisDbconn.Instance.Get("sppos:" + Mer.Field3); RedisDbconn.Instance.SetExpire("sppos:" + Mer.MerNo, -1); if(!db.LklMerNo.Any(m => m.ExtMerNo == Mer.Field3)) { db.LklMerNo.Add(new PxcModels.LklMerNo() { ExtMerNo = Mer.Field3, MerNo = Mer.MerNo, }); db.SaveChanges(); } } PosPushDataNewHelper.Bind2(Mer); MerchantRecord edit = spdb.MerchantRecord.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); } } } }