using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using NLog; using SMGen.Data.Data; using SMGen.Data.DbModels; using System.Collections.Generic; namespace SMGen.Data.Controllers { public class SMGenController : IDisposable { private static IConfiguration _configuration = null!; private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); public SMGenController(IConfiguration configuration) { _configuration = configuration; } public void Dispose() { } public Dictionary AnagEventiGetAll() { Dictionary events = new Dictionary(); using (SMGDataContext localDbCtx = new SMGDataContext(_configuration)) { try { var currRec = localDbCtx .DbSetAnagEventi .ToList(); if (currRec != null) { foreach (var rec in currRec) { events.Add(rec.IdxTipo, rec.KeyEvento); } } } catch (Exception exc) { Log.Error($"Eccezione durante AnagEventiGetAll: {Environment.NewLine}{exc}"); } } return events; } public Dictionary AnagStatiGetAll() { Dictionary states = new Dictionary(); using (SMGDataContext localDbCtx = new SMGDataContext(_configuration)) { try { var currRec = localDbCtx .DbSetAnagStati .ToList(); if (currRec != null) { foreach (var rec in currRec) { states.Add(rec.IdxStato, rec.KeyStato); } } } catch (Exception exc) { Log.Error($"Eccezione durante AnagEventiGetAll: {Environment.NewLine}{exc}"); } } return states; } public List FamIngressiGetAll() { List dbResult = new List(); using (SMGDataContext localDbCtx = new SMGDataContext(_configuration)) { try { dbResult = localDbCtx .DbSetFamIngressi .ToList(); } catch (Exception exc) { Log.Error($"Eccezione durante FamIngressiGetAll: {Environment.NewLine}{exc}"); } } return dbResult; } public List FamStatiGetAll() { List dbResult = new List(); using (SMGDataContext localDbCtx = new SMGDataContext(_configuration)) { try { dbResult = localDbCtx .DbSetFamStati .ToList(); } catch (Exception exc) { Log.Error($"Eccezione durante FamStatiGetAll: {Environment.NewLine}{exc}"); } } return dbResult; } /// /// Aggiunta in bulk delle transizioni/ingressi /// /// /// public async Task TranInInsert(List newTIRecs, int currIdxFamiglia) { bool fatto = false; using (SMGDataContext localDbCtx = new SMGDataContext(_configuration)) { try { foreach (var item in newTIRecs) { var currRec = localDbCtx .DbSetTranIngTemp .Where(x => (x.IdxFamigliaIngresso == item.IdxFamigliaIngresso) && (x.IdxMicroStato == item.IdxMicroStato) && (x.ValoreIngresso == item.ValoreIngresso)) .FirstOrDefault(); if (currRec != null) { currRec.IdxTipoEvento = item.IdxTipoEvento; currRec.next_IdxMicroStato = item.next_IdxMicroStato; localDbCtx.Entry(currRec).State = EntityState.Modified; } else { localDbCtx .DbSetTranIngTemp .Add(item); } } await localDbCtx.SaveChangesAsync(); fatto = true; Log.Info($"Scritto su db idxFamiglia: {currIdxFamiglia} "); } catch (Exception exc) { Log.Error($"Eccezione durante TranInInsert ({currIdxFamiglia}) : {Environment.NewLine}{exc}"); } } return fatto; } /// /// Aggiunta in bulk delle transizioni/ingressi /// /// /// public async Task AnagEventinInsert(List newEvRecs) { bool fatto = false; using (SMGDataContext localDbCtx = new SMGDataContext(_configuration)) { try { localDbCtx .Database .ExecuteSqlRaw("DELETE FROM AnagraficaEventi_Chk"); localDbCtx .DbSetAnagEventiTemp .AddRange(newEvRecs); await localDbCtx.SaveChangesAsync(); fatto = true; Log.Info($"Gli stati sono OK!"); } catch (Exception exc) { Log.Error($"Eccezione durante AnagEventinInsert: {Environment.NewLine}{exc}"); } } return fatto; } } }