示例#1
0
        private static bool RemoveOldLocal <T>(ConnectionStringSettings connectionStringSettings) where T : class, ITransfer
        {
            List <T> localTransferList = new List <T>();
            string   stringForLogger   = "";

            try
            {
                using (CtsTransferContext <T> localDB = new CtsTransferContext <T>(connectionStringSettings.ConnectionString))
                {
                    localTransferList.AddRange(localDB.DbSet.Where(x => x.TransferTimeStamp <= DbFunctions.AddDays(System.DateTime.Now, -3)));
                    localDB.DbSet.RemoveRange(localTransferList);
                    localDB.SaveChanges();
                    foreach (var t in localTransferList)
                    {
                        stringForLogger = String.Concat(stringForLogger, t.ID, ";");
                    }
                }

                return(true);
            }

            catch (Exception ex)
            {
                lock (log)
                {
                    log.Message(ex);
                }
                return(false);
            }
        }
示例#2
0
        public static bool ChangeTransfersStatus <TTransfer>(string transfers, bool isApproved, IIdentity user)
            where TTransfer : class, ITransfer
        {
            string[] transfersArray  = transfers.Split(',');
            var      dbcontext       = new CtsTransferContext <TTransfer>();
            var      editedTransfers = dbcontext.DbSet.Where(x => transfersArray.Contains(x.ID)).ToList();
            var      obsoleteArray   = editedTransfers.Where(x => x.Status == 2).Select(m => m.InheritedFrom).ToArray();

            editedTransfers.AddRange(dbcontext.DbSet.Where(x => obsoleteArray.Contains(x.ID)).ToList());
            using (var transaction = dbcontext.Database.BeginTransaction())
            {
                foreach (var t in editedTransfers)
                {
                    // What the magic is happening with t.Status?
                    // Refer to ApproveStatus Dictionary in ProjectConstants.cs and you'll get it :)
                    if (isApproved)
                    {
                        t.IsValid = !t.IsValid;
                        t.Status += 8;
                    }
                    else
                    {
                        t.Status += 4;
                    }
                    t.ApprovedBy             = user.Name;
                    dbcontext.Entry(t).State = EntityState.Modified;
                }
                dbcontext.SaveChanges();
                transaction.Commit();
            }

            WarehouseHandler.CalculateWarehouseTransferAfterApprove(editedTransfers);

            return(true);
        }
示例#3
0
        private static bool RemoveOldLocal <T>(ConnectionStringSettings connectionStringSettings) where T : class, ITransfer
        {
            List <T> localTransferList = new List <T>();
            string   stringForLogger   = "";

            try
            {
                using (CtsTransferContext <T> localDB = new CtsTransferContext <T>(connectionStringSettings.ConnectionString))
                {
                    localTransferList.AddRange(localDB.DbSet.Where(x => x.TransferTimeStamp <= DbFunctions.AddDays(System.DateTime.Now, -3)));
                    localDB.DbSet.RemoveRange(localTransferList);
                    localDB.SaveChanges();
                    foreach (var t in localTransferList)
                    {
                        stringForLogger = String.Concat(stringForLogger, t.ID, ";");
                    }
                }

                _logger.Trace(string.Format("{0} Successfully RemovedOldLocal {1}: {2}", connectionStringSettings.Name.ToString(), typeof(T).ToString(), stringForLogger));
                return(true);
            }

            catch (Exception ex)
            {
                _logger.Error(string.Format("{0} Unsuccess with RemoveOldLocal {1}", connectionStringSettings.Name.ToString(), typeof(T).ToString()));
                _logger.Error(ex.ToString());

                return(false);
            }
        }
        public IQueryable <TTransfer> GetTransfers <TTransfer>(int Id, DateTime fromDate, DateTime toDate) where TTransfer : class, ITransfer
        {
            var db = new CtsTransferContext <TTransfer>();

            var db2 = db.DbSet
                      .Where(s => s.EquipID == Id)
                      .Where(d => d.TransferTimeStamp >= fromDate && d.TransferTimeStamp <= toDate)
                      .Where(v => v.IsValid == true)
                      .OrderByDescending(t => t.TransferTimeStamp)
                      .AsNoTracking();

            //using (CtsDbContext db = new CtsDbContext())
            //{
            //    var search = db.BeltTransfers.Where(x => x.)
            //}



            foreach (var item in db2)
            {
                Debug.WriteLine(item.TransferTimeStamp);
            }

            return(db2);
        }
        public IQueryable <TTransfer> GetTransfersTime <TTransfer>(int Id, DateTime fromDate, DateTime toDate) where TTransfer : class, ITransfer
        {
            var db = new CtsTransferContext <TTransfer>();

            return(db.DbSet
                   .Where(s => s.EquipID == Id)
                   .Where(d => d.TransferTimeStamp <= fromDate && d.TransferTimeStamp <= toDate));
        }
示例#6
0
        private static bool SyncFromCentralToLocal <TTransfer, TEquip>(ConnectionStringSettings connectionStringSettings)
            where TTransfer : class, ITransfer
            where TEquip : class, IEquip
        {
            List <TTransfer> centralTransferList = new List <TTransfer>();
            string           stringForLogger     = "";

            int[] scalesArray;

            try
            {
                List <DateTime> time = new List <DateTime>();

                using (CtsEquipContext <TEquip> centralDB = new CtsEquipContext <TEquip>("centralDBConnection"))
                {
                    scalesArray = centralDB.DbSet.Where(x => x.LocationID == connectionStringSettings.Name.ToString()).Select(m => m.ID).ToArray();
                }

                using (CtsTransferContext <TTransfer> centralDB = new CtsTransferContext <TTransfer>("centralDBConnection"))
                {
                    centralTransferList.AddRange(centralDB.DbSet.Where(x => scalesArray.Contains((int)x.EquipID))
                                                 .Where(n => n.OperatorName != "System Platform")
                                                 .Where(x => x.TransferTimeStamp > DbFunctions.AddDays(System.DateTime.Now, -2)));
                }

                if (centralTransferList.Count != 0)
                {
                    using (CtsTransferContext <TTransfer> localDB = new CtsTransferContext <TTransfer>(connectionStringSettings.ConnectionString))
                    {
                        using (var transaction = localDB.Database.BeginTransaction())
                        {
                            foreach (var t in centralTransferList)
                            {
                                if (t is IHaveAnalysis transfer)
                                {
                                    transfer.AnalysisID = null;
                                }
                                localDB.DbSet.AddOrUpdate(t as TTransfer);
                                stringForLogger = String.Concat(stringForLogger, t.ID, ";");
                            }

                            localDB.SaveChanges();
                            transaction.Commit();
                        }
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                lock (log)
                {
                    log.Message(ex);
                }
                return(false);
            }
        }
示例#7
0
        public IQueryable <WagonTransfer> GetWagonTransfersSendFromMine(Location toDestLocation, DateTime fromDate, DateTime toDate, string fromMineId)
        {
            var db = new CtsTransferContext <WagonTransfer>();

            return(db.DbSet
                   .Where(t => t.Equip.Location.ID == fromMineId && t.IsValid == true)
                   .Where(t => t.ToDest.Contains(toDestLocation.LocationName) || t.ToDest == toDestLocation.ID)
                   .Where(t => t.Direction == CTS_Core.ProjectConstants.WagonDirection_FromObject)
                   .Where(t => t.TransferTimeStamp >= fromDate && t.TransferTimeStamp <= toDate)
                   .AsNoTracking());
        }
示例#8
0
        public IQueryable <TTransfer> GetTransfers <TTransfer>(int Id, DateTime fromDate, DateTime toDate) where TTransfer : class, ITransfer
        {
            var db = new CtsTransferContext <TTransfer>();

            return(db.DbSet
                   .Where(s => s.EquipID == Id)
                   .Where(d => d.TransferTimeStamp >= fromDate && d.TransferTimeStamp <= toDate)
                   .Where(v => v.IsValid == true)
                   .OrderByDescending(t => t.TransferTimeStamp)
                   .AsNoTracking());
        }
示例#9
0
        public IQueryable <WagonTransfer> GetWagonTransfersArrivedFromMine(string fromMineId, DateTime fromDate, DateTime toDate, string arrivedLocationId)
        {
            var db   = new CtsTransferContext <WagonTransfer>();
            var test = db.DbSet
                       .Where(t => t.Equip.Location.ID == arrivedLocationId && t.IsValid == true)
                       .Where(t => t.TransferTimeStamp >= fromDate && t.TransferTimeStamp <= toDate).ToArray();

            return(db.DbSet
                   .Where(t => t.Equip.Location.ID == arrivedLocationId && t.IsValid == true)
                   .Where(t => t.FromDestID == fromMineId)
                   //.Where(t => t.Direction == CTS_Core.ProjectConstants.WagonDirection_ToObject)
                   .Where(t => t.TransferTimeStamp >= fromDate && t.TransferTimeStamp <= toDate)
                   .AsNoTracking());
        }
示例#10
0
        public IQueryable <TTransfer> GetTransfersFirst <TTransfer>(int Id, DateTime fromDate, DateTime toDate) where TTransfer : class, ITransfer
        {
            var db  = new CtsTransferContext <TTransfer>();
            var db2 = db.DbSet
                      .Where(s => s.EquipID == Id)
                      .Where(d => d.TransferTimeStamp > fromDate && d.TransferTimeStamp < toDate)
                      .Where(v => v.IsValid == true)
                      .OrderByDescending(t => t.TransferTimeStamp)
                      .AsNoTracking();

            foreach (var item in db2)
            {
                Debug.WriteLine("Time: " + item.TransferTimeStamp);
            }
            return(db2);
        }
示例#11
0
        public static TransfersToApprove <TTransfer> GetTransfersToApprove <TTransfer, TEquip>(CtsDbContext cdb, IIdentity user)
            where TTransfer : class, ITransfer
            where TEquip : class, IEquip
        {
            var equips      = EquipmentProvider.GetUserAuthorizedEquipment <TEquip>(cdb, user);
            var equipsArray = equips.Select(x => x.ID).ToArray();
            var dbcontext   = new CtsTransferContext <TTransfer>();
            var transfers   = dbcontext.DbSet.Where(t => equipsArray.Contains((int)t.EquipID))
                              .Where(d => d.TransferTimeStamp >= DbFunctions.AddDays(System.DateTime.Now, -2)).ToList();

            var transfersToApprove = new TransfersToApprove <TTransfer>();

            transfersToApprove.CreatedTransfers = transfers.Where(m => m.Status == 1).ToList();
            transfersToApprove.EditedTransfers  = transfers.Where(m => m.Status == 2).ToList();
            var obsoleteArray = transfersToApprove.EditedTransfers.Select(x => x.InheritedFrom).ToArray();

            transfersToApprove.ObsoleteTransfers = dbcontext.DbSet.Where(m => obsoleteArray.Contains(m.ID)).ToList();
            transfersToApprove.DeletedTransfers  = transfers.Where(m => m.Status == 4).ToList();

            return(transfersToApprove);
        }
示例#12
0
        private static bool SyncFromLocalToCental <TTransfer, TEquip>(ConnectionStringSettings connectionStringSettings)
            where TTransfer : class, ITransfer
            where TEquip : class, IEquip
        {
            List <TTransfer> localTransferList = new List <TTransfer>();

            int[] scalesArray;
            var   scalesTime = new Dictionary <int, DateTime>();

            try
            {
                using (CtsEquipContext <TEquip> centralDB = new CtsEquipContext <TEquip>("centralDBConnection"))
                {
                    scalesArray = centralDB.DbSet.Where(x => x.LocationID == connectionStringSettings.Name.ToString()).Select(m => m.ID).ToArray();
                }

                using (CtsTransferContext <TTransfer> centralDB = new CtsTransferContext <TTransfer>("centralDBConnection"))
                {
                    foreach (var scale in scalesArray)
                    {
                        DateTime lastTime = new DateTime(2018, 01, 01);
                        var      transfer = centralDB.DbSet.Where(x => x.EquipID == scale).OrderByDescending(t => t.TransferTimeStamp).FirstOrDefault();
                        if (transfer != null)
                        {
                            lastTime = transfer.TransferTimeStamp;
                        }
                        scalesTime.Add(scale, lastTime);
                    }
                }

                List <DateTime> time = new List <DateTime>();

                using (CtsTransferContext <TTransfer> localDB = new CtsTransferContext <TTransfer>(connectionStringSettings.ConnectionString))
                {
                    foreach (var scale in scalesTime)
                    {
                        localTransferList.AddRange(localDB.DbSet.Where(s => s.EquipID == scale.Key)
                                                   .Where(x => x.OperatorName == "System Platform").Where(t => t.TransferTimeStamp > scale.Value));

                        time = localDB.DbSet.Select(x => x.TransferTimeStamp).ToList();
                    }
                }

                if (localTransferList.Count != 0)
                {
                    using (CtsTransferContext <TTransfer> centralDB = new CtsTransferContext <TTransfer>("centralDBConnection"))
                    {
                        foreach (var t in localTransferList)
                        {
                            TTransfer transfer = t;
                            transfer.LasEditDateTime = System.DateTime.Now;
                            centralDB.DbSet.Add(transfer);
                        }
                        centralDB.SaveChanges();
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                lock (log)
                {
                    log.Message(ex);
                }
                return(false);
            }
        }
示例#13
0
        private static bool SyncFromLocalToCental <TTransfer, TEquip>(ConnectionStringSettings connectionStringSettings)
            where TTransfer : class, ITransfer
            where TEquip : class, IEquip
        {
            List <TTransfer> localTransferList = new List <TTransfer>();
            string           stringForLogger   = "";

            int[] scalesArray;
            var   scalesTime = new Dictionary <int, DateTime>();

            try
            {
                using (CtsEquipContext <TEquip> centralDB = new CtsEquipContext <TEquip>("centralDBConnection"))
                {
                    scalesArray = centralDB.DbSet.Where(x => x.LocationID == connectionStringSettings.Name.ToString()).Select(m => m.ID).ToArray();
                }

                using (CtsTransferContext <TTransfer> centralDB = new CtsTransferContext <TTransfer>("centralDBConnection"))
                {
                    foreach (var scale in scalesArray)
                    {
                        DateTime lastTime = new DateTime(2018, 01, 01);
                        var      transfer = centralDB.DbSet.Where(x => x.EquipID == scale).OrderByDescending(t => t.TransferTimeStamp).FirstOrDefault();
                        if (transfer != null)
                        {
                            lastTime = transfer.TransferTimeStamp;
                        }
                        scalesTime.Add(scale, lastTime);
                    }
                }

                using (CtsTransferContext <TTransfer> localDB = new CtsTransferContext <TTransfer>(connectionStringSettings.ConnectionString))
                {
                    foreach (var scale in scalesTime)
                    {
                        localTransferList.AddRange(localDB.DbSet.Where(s => s.EquipID == scale.Key)
                                                   .Where(x => x.OperatorName == "System Platform").Where(t => t.TransferTimeStamp > scale.Value));
                    }
                }

                if (localTransferList.Count != 0)
                {
                    using (CtsTransferContext <TTransfer> centralDB = new CtsTransferContext <TTransfer>("centralDBConnection"))
                    {
                        using (var transaction = centralDB.Database.BeginTransaction())
                        {
                            foreach (var t in localTransferList)
                            {
                                TTransfer transfer = t;
                                transfer.LasEditDateTime = System.DateTime.Now;
                                centralDB.DbSet.Add(transfer);
                                stringForLogger = String.Concat(stringForLogger, t.ID, ";");
                            }

                            centralDB.SaveChanges();
                            transaction.Commit();
                        }
                    }
                }

                _logger.Trace(string.Format("{0} Successfully synchronized FromLocalToCentral {1}: {2}", connectionStringSettings.Name.ToString(), typeof(TTransfer).ToString(), stringForLogger));
                return(true);
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("{0} Unsuccess with FromLocalToCentral {1}", connectionStringSettings.Name.ToString(), typeof(TTransfer).ToString()));
                _logger.Error(ex.ToString());

                return(false);
            }
        }