Spostamento aree progetto
This commit is contained in:
@@ -0,0 +1,260 @@
|
||||
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.ToUpper() == updItem.nam.fn.ToUpper() && x.Nome.ToUpper() == updItem.nam.gn.ToUpper())
|
||||
.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];
|
||||
}
|
||||
|
||||
if (currUser != null)
|
||||
{
|
||||
// fisso dipendente
|
||||
idxDip = currUser.IdxDipendente;
|
||||
}
|
||||
|
||||
string rawPayload = JsonConvert.SerializeObject(updItem);
|
||||
CheckVc19 newItem = new CheckVc19()
|
||||
{
|
||||
IdxDipendente = idxDip,
|
||||
DtCheck = DateTime.Now,
|
||||
Cognome = updItem.nam.fn.ToUpper(),
|
||||
Nome = updItem.nam.gn.ToUpper(),
|
||||
Payload = rawPayload,
|
||||
DOB = updItem.dob.Date
|
||||
};
|
||||
// aggiungo!
|
||||
localDbCtx
|
||||
.DbSetCheckVc19
|
||||
.Add(newItem);
|
||||
|
||||
// ora salvo!
|
||||
localDbCtx.SaveChanges();
|
||||
|
||||
// se trovato procedo ANCHE x timbratura
|
||||
if (idxDip > 0)
|
||||
{
|
||||
// verifico se ho timbratura ingresso
|
||||
var listTimbra = localDbCtx
|
||||
.DbSetTimbrature
|
||||
.Where(x => x.IdxDipendente == idxDip && x.DataOra.Date == DateTime.Today && x.Entrata == true)
|
||||
.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,
|
||||
Approv = 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 InsertCheck:{Environment.NewLine}{exc}");
|
||||
}
|
||||
}
|
||||
return done;
|
||||
}
|
||||
|
||||
public bool InsertManual(int idxDip, string clientIp)
|
||||
{
|
||||
bool done = false;
|
||||
using (GPWContext localDbCtx = new GPWContext(_configuration))
|
||||
{
|
||||
try
|
||||
{
|
||||
// se trovato procedo
|
||||
if (idxDip > 0)
|
||||
{
|
||||
string rawPayload = "MANUAL";
|
||||
CheckVc19 newItem = new CheckVc19()
|
||||
{
|
||||
IdxDipendente = idxDip,
|
||||
DtCheck = DateTime.Now,
|
||||
Payload = rawPayload
|
||||
};
|
||||
// aggiungo!
|
||||
localDbCtx
|
||||
.DbSetCheckVc19
|
||||
.Add(newItem);
|
||||
|
||||
// ora salvo!
|
||||
localDbCtx.SaveChanges();
|
||||
done = true;
|
||||
}
|
||||
// altrimenti NON FA NULLA
|
||||
else
|
||||
{
|
||||
Log.Info($"Attenzione, dipendente non trovato: {idxDip}");
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Error($"Eccezione in InsertManual:{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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user