public static bool SyncVesWagon() { string stringForLoggerAccepted = ""; string stringForLoggerRejected = ""; string stringForLoggerWrong = ""; //int lastID; List <WagonScale> wagonScales; List <Item> items; Int32.TryParse(ConfigurationManager.AppSettings["WagonDBSyncDepth_Days"], out int syncDepth); var dueDate = syncDepth == 0 ? default(DateTime) : DateTime.Now.AddDays(syncDepth * -1); try { using (CtsDbContext centralDB = new CtsDbContext()) { //var ltr = centralDB.WagonTransfers.Where(x => !x.ID.StartsWith("W")).OrderByDescending(t => t.TransferTimeStamp).Take(1000).Select(m => m.ID).ToArray(); //lastID = (ltr.Length != 0) ? ltr.Select(x => int.Parse(x)).OrderByDescending(m => m).FirstOrDefault() : 0; wagonScales = centralDB.WagonScales.Include(m => m.Location).ToList(); items = centralDB.Items.Include(m => m.Location).ToList(); } var transfers = new List <ves_vagon>(); using (var wagDB = new WagonDBcontext()) { //transfers = wagDB.ves_vagon.Where(x => x.id > lastID).Include(m => m.scales).Include(h => h.napravlenie) // .Include(n => n.otpravl).Include(k => k.poluch).ToList(); transfers = wagDB.ves_vagon.Where(x => x.id_operator != null) .Where(x => x.id_operator != 0) .Where(x => x.sync != 1) .Where(x => x.date_time_brutto >= dueDate) .Include(m => m.scales) .Include(h => h.napravlenie) .Include(n => n.otpravl).Include(k => k.poluch).ToList(); } if (transfers.Count > 0) { var acceptedTransfers = new List <WagonTransfer>(); using (var centralDB = new CtsDbContext()) { using (var transaction = centralDB.Database.BeginTransaction()) { foreach (var trans in transfers) { try { var scale = GetCTSWagonScale(trans.scales.name, wagonScales); var item = items.Where(x => x.Name == trans.gruz).FirstOrDefault(); var transfer = new WagonTransfer() { ID = trans.id.ToString(), TransferTimeStamp = trans.date_time_brutto, LasEditDateTime = DateTime.Now, OperatorName = "DBSync", LotName = trans.id_sostav.ToString() ?? "", SublotName = trans.vagon_num ?? "", OrderNumber = trans.nakladn ?? "", FromDestID = trans.otpravl.name ?? "", ToDest = trans.poluch.display_name ?? "", Tare = (float)trans.ves_tara / 1000, Brutto = (float)trans.ves_brutto / 1000, Netto = (float)trans.ves_netto / 1000, NettoByOrder = (float)trans.ves_netto_docs / 1000, EquipID = (scale != null) ? scale.ID : 1, ItemID = (item != null) ? item.ID : 1, Direction = trans.napravlenie.display_name ?? "", IsValid = true, Status = 0, }; var vc = new ValidationContext(transfer, null, null); var vResults = new List <ValidationResult>(); var isValid = Validator.TryValidateObject(transfer, vc, vResults, true); if (isValid && (transfer.SublotName != "")) { acceptedTransfers.Add(transfer); centralDB.WagonTransfers.AddOrUpdate(transfer); stringForLoggerAccepted = String.Concat(stringForLoggerAccepted, trans.id, ";"); } else { stringForLoggerRejected = String.Concat(stringForLoggerRejected, trans.id, ";"); } } catch (Exception) { stringForLoggerWrong = String.Concat(stringForLoggerWrong, trans.id, ";"); } } centralDB.SaveChanges(); transaction.Commit(); } } using (var wagDB = new WagonDBcontext()) { foreach (var t in acceptedTransfers) { var originalTransfer = wagDB.ves_vagon.Find(Int32.Parse(t.ID)); if (originalTransfer != null) { originalTransfer.sync = 1; wagDB.Entry(originalTransfer).State = EntityState.Modified; } } using (var transaction = wagDB.Database.BeginTransaction()) { wagDB.SaveChanges(); transaction.Commit(); } } } _logger.Trace(string.Format("Successfully Synchronized VesWagon: {0}", stringForLoggerAccepted)); _logger.Trace(string.Format("Successfully Synchronized VesWagon, not accepted transfers: {0}", stringForLoggerRejected)); _logger.Trace(string.Format("Successfully Synchronized VesWagon, transfers with wrong properties: {0}", stringForLoggerWrong)); return(true); } catch (Exception ex) { _logger.Error("Unsuccess with SyncVesWagon"); _logger.Error(ex.Message.ToString()); return(false); } }
private void GetAsyncWagon(DateTime from_ts, DateTime to_ts) { try { List <WagonScale> wagonScales; List <Item> items; using (CtsDbContext centralDB = new CtsDbContext()) { wagonScales = centralDB.WagonScales.Include(m => m.Location).ToList(); items = centralDB.Items.Include(m => m.Location).ToList(); } var transfer = new List <ves_vagon>(); var accept = new List <WagonTransfer>(); using (var vesWagon = new WagonDBcontext()) { transfer = vesWagon.ves_vagon/*.Where(x => x.id_operator != null)*/ .Where(x => x.id_operator != 0) .Where(x => x.sync != 1) .Where(x => x.date_time_brutto >= from_ts) .Where(x => x.date_time_brutto <= to_ts) .Include(m => m.scales).DefaultIfEmpty() .Include(h => h.napravlenie).DefaultIfEmpty() .Include(n => n.otpravl).DefaultIfEmpty() .Include(k => k.poluch).DefaultIfEmpty() .ToList(); } if (transfer.Count > 0) { using (var centralDB = new CtsDbContext()) { for (ves_vagon_records = transfer.Count; ves_vagon_records > 0; ves_vagon_records--) { ves_vagon trans = transfer[transfer.Count - ves_vagon_records]; if (trans != null) { var scale = GetCTSWagonScale(trans.scales.name, wagonScales); var item = items.Where(x => x.Name == trans.gruz).FirstOrDefault(); var transfers = new WagonTransfer() { ID = trans.id.ToString(), TransferTimeStamp = trans.date_time_brutto, LasEditDateTime = DateTime.Now, OperatorName = "DBSync", LotName = trans.id_sostav.ToString() == "" ? "???" : trans.id_sostav.ToString(), SublotName = trans.vagon_num, OrderNumber = trans.nakladn, Tare = (float)trans.ves_tara / 1000, Brutto = (float)trans.ves_brutto / 1000, Netto = (float)trans.ves_netto / 1000, NettoByOrder = (float)trans.ves_netto_docs / 1000, EquipID = (scale != null) ? scale.ID : 1, ItemID = (item != null) ? item.ID : 1, Direction = trans.napravlenie.display_name ?? "", IsValid = true, Status = 0 }; //if (trans.otpravl != null) //{ // transfers.FromDestID = (trans.otpravl.name == "0") ? "???" : trans.otpravl.name; //} //if (trans.poluch != null) //{ // transfers.ToDest = (trans.poluch.display_name == "0") ? "???" : trans.poluch.display_name; //} centralDB.WagonTransfers.AddOrUpdate(transfers); centralDB.SaveChanges(); using (var wagDB = new WagonDBcontext()) { var originalTransfer = wagDB.ves_vagon.Find(Int32.Parse(transfers.ID)); if (originalTransfer != null) { originalTransfer.sync = 1; wagDB.Entry(originalTransfer).State = EntityState.Modified; } wagDB.SaveChanges(); } } } } } } catch (DbEntityValidationException ex) { foreach (var eve in ex.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }