예제 #1
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);
        }
예제 #2
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);
            }
        }
예제 #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);
            }
        }
예제 #4
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);
            }
        }
예제 #5
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);
            }
        }
예제 #6
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);
            }
        }