using EgtBEAMWALL.DataLayer.DatabaseModels; using EgwProxy.MagMan.DTO; using NLog; using System; using System.Collections.Generic; using System.Linq; namespace EgtBEAMWALL.DataLayer.Controllers { /// /// Gestione MagmanSync su DB (registrazione operazioni di sync con MagMan online) /// public class MagmanSyncController : IDisposable { #region Public Constructors public MagmanSyncController() { } #endregion Public Constructors #region Public Methods /// /// Purge dei record inviati oltre periodo di giorni indicato /// /// Num gg max da mantenere in registro /// public bool DeleteOlder(int NumDayMax) { bool done = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { DateTime adesso = DateTime.Now; var items2del = localDbCtx .SyncList .Where(x => x.DtExe != null && adesso.Subtract(x.DtReq).TotalDays > NumDayMax); try { // Add to database localDbCtx.SyncList.RemoveRange(items2del); // Commit changes localDbCtx.SaveChanges(); done = true; } catch (Exception exc) { Log.Error($"EXCEPTION on MagmanSync.DeleteOlder | NumDayMax: {NumDayMax}{Environment.NewLine}{exc}"); } } return done; } /// /// Purge dei record NON INVIATI date condizioni filtro /// /// Id progetto x cui filtrare /// Tipo di record da cercare /// DataOra limite (max) x cui cercare record non inviati /// public bool DeleteUnsentFilt(int ProjCloudId, string SyncType, DateTime DtLimit) { bool done = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { // cercl candidati var items2del = localDbCtx .SyncList .Where(x => x.DtExe == null && x.CloudId == ProjCloudId && x.SyncType == SyncType && x.DtReq <= DtLimit); // se ne ho trovato... if (items2del != null && items2del.Count() > 0) { try { // Add to database localDbCtx.SyncList.RemoveRange(items2del); // Commit changes localDbCtx.SaveChanges(); done = true; } catch (Exception exc) { Log.Error($"EXCEPTION on MagmanSync.DeleteUnsentFilt | ProjCloudId: {ProjCloudId} | SyncType: {SyncType} | DtLimit: {DtLimit}{Environment.NewLine}{exc}"); } } } return done; } public void Dispose() { } /// /// Lista record filtrati /// /// Tipo di record richiesti, se "" = tutti /// Data-Ora limite per recupero ordinato DESC /// num max di record da restituire /// public List GetFilt(string SyncType, DateTime DtMax, int NumMax) { List dbResult = new List(); using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { dbResult = localDbCtx .SyncList .Where(x => (string.IsNullOrEmpty(SyncType) || x.SyncType == SyncType) && x.DtReq <= DtMax) .OrderByDescending(x => x.DtReq) .Take(NumMax) .ToList(); } return dbResult; } /// /// Lista record NON inviati filtrati x tipo e num max /// /// Tipo di record richiesti, se "" = tutti /// Data-Ora limite per recupero ordinato ASC /// num max di record da restituire /// public List GetUnsentFilt(string SyncType, DateTime DtMax, int NumMax) { List dbResult = new List(); using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { dbResult = localDbCtx .SyncList .Where(x => x.DtExe == null && (string.IsNullOrEmpty(SyncType) || x.SyncType == SyncType) && x.DtReq <= DtMax) .OrderBy(x => x.DtReq) .Take(NumMax) .ToList(); } return dbResult; } /// /// Insert record MagmanSync /// /// /// ID record inserito public int Insert(MagmanSyncModel newRec) { int newId = 0; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { try { // inserisco record localDbCtx.SyncList.Add(newRec); // Commit changes localDbCtx.SaveChanges(); newId = newRec.SyncId; } catch (Exception exc) { Log.Error($"EXCEPTION on MagmanSync.Insert: {Environment.NewLine}{exc}"); } } return newId; } /// /// Update con indicazione DtExt di completamento /// /// ID record /// Data-Ora exe /// public bool SetCompleted(int SyncId, DateTime DtExe) { bool fatto = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { try { var item2update = localDbCtx .SyncList .Where(x => x.SyncId == SyncId) .SingleOrDefault(); if (item2update != null) { // update item2update.DtExe = DtExe; localDbCtx.Entry(item2update).State = System.Data.Entity.EntityState.Modified; } // Commit changes localDbCtx.SaveChanges(); fatto = true; } catch (Exception exc) { Log.Error($"EXCEPTION on MagmanSync.SetCompleted: {Environment.NewLine}{exc}"); } } return fatto; } /// /// Update or insert record MagmanSync /// /// /// public bool Upsert(MagmanSyncModel updItem) { bool fatto = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { try { var item2update = localDbCtx .SyncList .Where(x => x.SyncId == updItem.SyncId) .SingleOrDefault(); if (item2update != null) { // update, vers 1... localDbCtx.Entry(item2update).CurrentValues.SetValues(updItem); } else { localDbCtx.SyncList.Add(updItem); } // Commit changes localDbCtx.SaveChanges(); fatto = true; } catch (Exception exc) { Log.Error($"EXCEPTION on MagmanSync.Upsert: {Environment.NewLine}{exc}"); } } return fatto; } /// /// Update or insert Alias record list /// /// /// public bool UpsertList(List updList) { bool fatto = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { try { foreach (var updItem in updList) { var item2update = localDbCtx .SyncList .Where(x => x.SyncId == updItem.SyncId) .SingleOrDefault(); if (item2update != null) { localDbCtx.Entry(item2update).CurrentValues.SetValues(updItem); } else { localDbCtx.SyncList.Add(updItem); } } // Commit changes localDbCtx.SaveChanges(); fatto = true; } catch (Exception exc) { Log.Error($"EXCEPTION on MagmanSync.UpsertList: {Environment.NewLine}{exc}"); } } return fatto; } #endregion Public Methods #region Private Fields /// /// Istanza logger /// private Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields } }