Files
gpw_next/GPW.Api/GPW.Data/Controllers/DbController.cs
T
2021-10-18 17:52:17 +02:00

211 lines
7.2 KiB
C#

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<CheckVc19> GetChecksVC19(int maxNum)
{
List<CheckVc19> dbResult = new List<CheckVc19>();
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<CheckVc19> GetChecksVC19Filt(int idxDip, DateTime dtStart, DateTime dtEnd)
{
List<CheckVc19> dbResult = new List<CheckVc19>();
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;
}
/// <summary>
/// Annulla modifiche su una specifica entity (cancel update)
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
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
}
}