using GPW.Data.DBModels; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using NLog; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GPW.Data.Controllers { public class DbController : IDisposable { #region Private Fields private static IConfiguration _configuration; private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields #region Public Constructors public DbController(IConfiguration configuration) { _configuration = configuration; } #endregion Public Constructors #region Public Methods public bool DbForceMigrate() { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { localDbCtx.DbForceMigrate(); answ = true; } catch (Exception exc) { Log.Error($"Eccezione in DbForceMigrate{Environment.NewLine}{exc}"); } } return answ; } public void Dispose() { // Clear database context Log.Info("Dispose di DbController"); } public List GetChecksVC19(int maxNum) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { int numRec = localDbCtx .DbSetCheckVc19 .Count(); // se ho meno record che quelli richiesti --> riduco maxNum = numRec > maxNum ? maxNum : numRec; dbResult = localDbCtx .DbSetCheckVc19 .OrderByDescending(x => x.DtCheck) .Take(maxNum) .ToList(); } return dbResult; } public List GetChecksVC19Filt(int idxDip, DateTime dtStart, DateTime dtEnd) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { DateTime oggi = DateTime.Today; dbResult = localDbCtx .DbSetCheckVc19 .Where(x => (idxDip == 0 || x.IdxDipendente == idxDip) && (x.DtCheck >= dtStart && x.DtCheck <= dtEnd)) .OrderByDescending(x => x.DtCheck) .ToList(); } return dbResult; } public bool InsertCheck(DCCDecode updItem, string clientIp) { bool done = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { int idxDip = 0; Dipendenti currUser = new Dipendenti() { IdxDipendente = 0 }; // per prima cosa cerca il dipendente x cognome/nome... var userList = localDbCtx .DbSetDipendenti .Where(x => x.Cognome == updItem.nam.fn && x.Nome == updItem.nam.gn) .ToList(); // se ne ho solo 1 ok... if (userList.Count > 1) { currUser = userList .Where(x => x.DataNascita != null && updItem.dob.Date == ((DateTime)x.DataNascita).Date) .FirstOrDefault(); } // altrimenti cerco anche con DOB... else if (userList.Count == 1) { // se lo trova inserisce check... currUser = userList[0]; } // fisso dipendente idxDip = currUser.IdxDipendente; // se trovato procedo if (idxDip > 0) { string rawPayload = JsonConvert.SerializeObject(updItem); CheckVc19 newItem = new CheckVc19() { IdxDipendente = idxDip, DtCheck = DateTime.Now, Payload = rawPayload }; // aggiungo! localDbCtx .DbSetCheckVc19 .Add(newItem); // verifico se ho timbratura ingresso var listTimbra = localDbCtx .DbSetTimbrature .Where(x => x.IdxDipendente == idxDip) .ToList(); // se non ci fosse aggiungo if (listTimbra == null || listTimbra.Count == 0) { var newIngresso = new Timbrature() { IdxDipendente = idxDip, DataOra = DateTime.Now, CodTipoTimb = "Web", Ipv4 = clientIp, Entrata = true, }; // aggiungo! localDbCtx .DbSetTimbrature .Add(newIngresso); } // ora salvo! localDbCtx.SaveChanges(); done = true; } // altrimenti NON FA NULLA else { Log.Info($"Attenzione, dipendente non trovato: {updItem.nam.fn} {updItem.nam.gn}"); } } catch (Exception exc) { Log.Error($"Eccezione in UpdateApplicazioni:{Environment.NewLine}{exc}"); } } return done; } /// /// Annulla modifiche su una specifica entity (cancel update) /// /// /// public bool rollBackEntity(object item) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { if (localDbCtx.Entry(item).State == EntityState.Deleted || localDbCtx.Entry(item).State == EntityState.Modified) { localDbCtx.Entry(item).Reload(); } } catch (Exception exc) { Log.Error($"Eccezione in rollBackEntity{Environment.NewLine}{exc}"); } } return answ; } #endregion Public Methods } }