155 lines
5.4 KiB
C#
155 lines
5.4 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;
|
|
|
|
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.Nome);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante AnagEventiGetAll: {Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return events;
|
|
}
|
|
|
|
/// <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;
|
|
|
|
#if false
|
|
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);
|
|
}
|
|
}
|
|
#endif
|
|
await localDbCtx.SaveChangesAsync();
|
|
fatto = true;
|
|
Log.Info($"Gli stati sono OK!");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante AnagEventinInsert: {Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
|
|
}
|
|
} |