Files
Mapo-IOB/SMGen.Data/Controllers/SMGenController.cs
T
2023-07-26 17:00:31 +02:00

119 lines
4.3 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using NLog;
using SMGen.Data.DbModels;
namespace SMGen.Data.Controllers
{
public class SMGenController : IDisposable
{
private static IConfiguration _configuration;
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
public SMGenController(IConfiguration configuration)
{
_configuration = configuration;
}
public void Dispose()
{ }
/// <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
// .DbSetAnagEventiTemp
// .AddRange(newEvRecs);
//await localDbCtx.SaveChangesAsync();
//fatto = true;
foreach (var item in newEvRecs)
{
var currRec = localDbCtx
.DbSetAnagEventiTemp
.Where(x => (x.IdxTipo == item.IdxTipo))
.FirstOrDefault();
if (currRec != null)
{
currRec.Nome = item.Nome;
localDbCtx.Entry(currRec).State = EntityState.Modified;
}
else
{
localDbCtx
.DbSetAnagEventiTemp
.Add(item);
}
}
await localDbCtx.SaveChangesAsync();
fatto = true;
Log.Info($"Gli stati sono OK!");
}
catch (Exception exc)
{
Log.Error($"Eccezione durante AnagEventinInsert: {Environment.NewLine}{exc}");
}
}
return fatto;
}
}
}