Files
2023-09-20 11:55:48 +02:00

195 lines
6.7 KiB
C#

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<int, string> AnagEventiGetAll()
{
Dictionary<int, string> events = new Dictionary<int, string>();
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<int, string> AnagStatiGetAll()
{
Dictionary<int, string> states = new Dictionary<int, string>();
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<FamIngressiViewModel> FamIngressiGetAll()
{
List<FamIngressiViewModel> dbResult = new List<FamIngressiViewModel>();
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<FamStatiViewModel> FamStatiGetAll()
{
List<FamStatiViewModel> dbResult = new List<FamStatiViewModel>();
using (SMGDataContext localDbCtx = new SMGDataContext(_configuration))
{
try
{
dbResult = localDbCtx
.DbSetFamStati
.ToList();
}
catch (Exception exc)
{
Log.Error($"Eccezione durante FamStatiGetAll: {Environment.NewLine}{exc}");
}
}
return dbResult;
}
/// <summary>
/// Aggiunta in bulk delle transizioni/ingressi
/// </summary>
/// <param name="newTIRecs"></param>
/// <returns></returns>
public async Task<bool> TranInInsert(List<TransizioneIngressiModelTemp> 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;
}
/// <summary>
/// Aggiunta in bulk delle transizioni/ingressi
/// </summary>
/// <param name="newEvRecs"></param>
/// <returns></returns>
public async Task<bool> AnagEventinInsert(List<AnagEventiModelTemp> 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;
}
}
}