using System; using System.Collections.Generic; using System.Linq; using MySystem.SpModels; using Library; using LitJson; using System.Threading; namespace MySystem { public class SycnSpBindService { public readonly static SycnSpBindService Instance = new SycnSpBindService(); private SycnSpBindService() { } public void SimDo(string content) { try { RadioCardItem Bind = Newtonsoft.Json.JsonConvert.DeserializeObject(content); WebCMSEntities spdb = new WebCMSEntities(); PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities(); DateTime start = DateTime.Now.AddDays(-60); PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.iccId) ?? new PxcModels.MachineForSnNo(); PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId); if (pos != null) { string KindName = Bind.productTypeName; //商品类型名称 string Name = Bind.productName; //商品名称 pos.BindingState = 1; pos.BindingTime = DateTime.Parse(Bind.orderCreateTime); pos.Status = 0; pos.IsFirst = 1; if(Name == "激活后补卡") { PxcModels.MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.phoneNo); if(forMerNo != null) { PxcModels.PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forMerNo.SnId); if(oldpos != null) { oldpos.IsFirst = 0; pos.BindingState = oldpos.BindingState; pos.BindingTime = oldpos.BindingTime; pos.ActivationState = oldpos.ActivationState; pos.ActivationTime = oldpos.ActivationTime; } } } decimal ActualAmount = Bind.paidInAmount; //实付金额 string ActStatus = Bind.activationStatusName; //激活状态名称 if (KindName == "基础套餐" && Name == "双百套餐" && ActStatus == "已激活" && ActualAmount >= 100) { pos.Detail = "0"; } else { pos.Detail = "1"; } PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.phoneNo); if (merFor == null) { merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo() { MerNo = Bind.phoneNo, SnId = pos.Id, }).Entity; } else { merFor.SnId = pos.Id; } PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Bind.phoneNo && m.KqSnNo == Bind.iccId); if (add == null) { add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo() { CreateDate = DateTime.Parse(Bind.orderCreateTime), KqMerNo = Bind.phoneNo, KqSnNo = Bind.iccId, MerchantNo = Bind.phoneNo.Length > 16 ? Bind.phoneNo.Substring(0, 16) : Bind.phoneNo, }).Entity; db.SaveChanges(); } string json = Newtonsoft.Json.JsonConvert.SerializeObject(Bind); string ProductName = ""; //商品名称 string ActivityName = ""; //促销活动名称 string ProductTypeName = ""; //商品类型名称 string ActStatusName = ""; //激活状态名称 string ActAmount = ""; //实付金额 string EmployeeNo = ""; //员工工号 string EmployeeName = ""; //员工姓名 if(!string.IsNullOrEmpty(json)) { JsonData jsonObj = JsonMapper.ToObject(json); ProductName = jsonObj["productName"].ToString(); ProductTypeName = jsonObj["productTypeName"].ToString(); ActAmount = jsonObj["paidInAmount"].ToString(); ActStatusName = jsonObj["activationStatusName"].ToString(); EmployeeNo = jsonObj["jobId"].ToString(); } PxcModels.SimposMerchantInfo sim = db.SimposMerchantInfo.FirstOrDefault(m => m.KqMerNo == Bind.phoneNo); if (sim == null) { sim = db.SimposMerchantInfo.Add(new PxcModels.SimposMerchantInfo() { Id = add.Id, CreateDate = DateTime.Parse(Bind.orderCreateTime), KqMerNo = Bind.phoneNo, MerchantNo = Bind.phoneNo.Length > 16 ? Bind.phoneNo.Substring(0, 16) : Bind.phoneNo, }).Entity; db.SaveChanges(); } sim.KqSnNo = Bind.iccId; sim.ProductName = ProductName; sim.ActivityName = ActivityName; sim.ProductTypeName = ProductTypeName; sim.ActStatusName = ActStatusName; sim.ActAmount = decimal.Parse(ActAmount); sim.EmployeeNo = EmployeeNo; sim.EmployeeName = EmployeeName; pos.BindMerchantId = add.Id; pos.LastMerchantId = add.Id; if(pos.CardType < 2) { pos.IsPurchase = 1; } // sim.UpdateDate = Bind.UpdateTime; sim.TopUserId = 1; sim.BrandId = pos.BrandId; sim.SnStoreId = pos.StoreId; if(ActStatus == "已激活") { sim.MerStandardDate = DateTime.Now; sim.ActiveStatus = 1; } sim.UserId = pos.UserId; // sim.MgrName = Bind.MerName; sim.MerStatus = 1; sim.KqSnNo = pos.PosSn; // sim.MerRealName = Bind.MerName; sim.MerchantMobile = Bind.phoneNo; sim.MerchantName = Bind.phoneNo; sim.Type = pos.CardType + 1; db.SaveChanges(); string IdBrand = pos.UserId + "_" + pos.BrandId; PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand); if (MachineData == null) { MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData() { IdBrand = IdBrand, }).Entity; db.SaveChanges(); } MachineData.BindCount += 1; MachineData.UnBindCount -= 1; if(Bind.activationStatusName == "已激活") { bool PrizeFlag = pos.CardType > 0; SycnSpTradeService.Instance.ActPos(db, pos, 0, 30, PrizeFlag, Bind.activationTime); } db.SaveChanges(); } spdb.SaveChanges(); spdb.Dispose(); db.SaveChanges(); db.Dispose(); } catch (Exception ex) { function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常"); } } } }