204 lines
7.8 KiB
C#
204 lines
7.8 KiB
C#
using GPW.CORE.Data.DbModels;
|
|
using GPW.CORE.Data.DTO;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
|
|
namespace GPW.CORE.Data.Controllers
|
|
{
|
|
public class GPWController : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration = null!;
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public GPWController(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Recupera l'elenco dei dettagli giornalieri attività di un dipendente, dato periodo riferimento
|
|
/// </summary>
|
|
/// <param name="idxDipendente">Dipendente interessato</param>
|
|
/// <param name="dtInizio">Data di riferimento (ultima/corrente)</param>
|
|
/// <param name="dtFine">NUm settimane precedenti da recuperare</param>
|
|
/// <returns></returns>
|
|
public List<DailyDataDTO> DailyDetails(int idxDipendente, DateTime dtInizio, DateTime dtFine)
|
|
{
|
|
// init dati necessari
|
|
List<DailyDataDTO> dbResult = new List<DailyDataDTO>();
|
|
List<TimbratureExplModel> rawTimbExp = new List<TimbratureExplModel>();
|
|
List<TimbratureModel> rawTimbr = new List<TimbratureModel>();
|
|
List<RegAttivitaModel> rawRegAtt = new List<RegAttivitaModel>();
|
|
|
|
// cerco su DB recuperando set di dati....
|
|
using (GPWContext localDbCtx = new GPWContext(_configuration))
|
|
{
|
|
// recupero intero set dati timbrature e attività....
|
|
rawTimbExp = localDbCtx
|
|
.DbSetTimbratureExpl
|
|
.Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.DataLav && x.DataLav <= dtFine)
|
|
.ToList();
|
|
|
|
rawTimbr = localDbCtx
|
|
.DbSetTimbrature
|
|
.Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.DataOra && x.DataOra <= dtFine)
|
|
.ToList();
|
|
|
|
rawRegAtt = localDbCtx
|
|
.DbSetRegAttivita
|
|
.Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.Inizio && x.Inizio <= dtFine)
|
|
.ToList();
|
|
|
|
// calcolo intervallo date...
|
|
int numDays = dtFine.Subtract(dtInizio).Days;
|
|
|
|
// x ogni giorno scompongo il set di info...
|
|
for (int i = 0; i < numDays; i++)
|
|
{
|
|
DateTime thisDay = dtInizio.AddDays(i);
|
|
DailyDataDTO currDayDto = new DailyDataDTO()
|
|
{
|
|
IdxDipendente = idxDipendente,
|
|
DtRif = thisDay,
|
|
ListRA = rawRegAtt
|
|
.Where(x => x.Inizio >= thisDay && x.Inizio <= thisDay.AddDays(1))
|
|
.ToList(),
|
|
ListTimbr = rawTimbr
|
|
.Where(x => x.DataOra >= thisDay && x.DataOra <= thisDay.AddDays(1))
|
|
.ToList(),
|
|
TimbrExpl = rawTimbExp
|
|
.Where(x => x.DataLav == thisDay)
|
|
.FirstOrDefault()
|
|
};
|
|
|
|
dbResult.Add(currDayDto);
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
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 List<DipendentiModel> DipendentiGetAll()
|
|
{
|
|
List<DipendentiModel> dbResult = new List<DipendentiModel>();
|
|
using (GPWContext localDbCtx = new GPWContext(_configuration))
|
|
{
|
|
dbResult = localDbCtx
|
|
.DbSetDipendenti
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Log.Info("Dispose di GPWController");
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera overview di un periodo settimanale x dipendente, data riferimento, numero settimane precedenti
|
|
/// </summary>
|
|
/// <param name="idxDipendente">Dipendente interessato</param>
|
|
/// <param name="dtRif">Data di riferimento (ultima/corrente)</param>
|
|
/// <param name="numWeek">NUm settimane precedenti da recuperare</param>
|
|
/// <returns></returns>
|
|
public List<WeekStatDTO> WeekOverview(int idxDipendente, DateTime dtRif, int numWeek)
|
|
{
|
|
// init dati necessari
|
|
List<WeekStatDTO> dbResult = new List<WeekStatDTO>();
|
|
List<WeekData> weekList = new List<WeekData>();
|
|
|
|
// aggiungo sett corrente + quelle richeiste...
|
|
for (int i = numWeek; i >= 0; i--)
|
|
{
|
|
var currWeek = new WeekData(dtRif.AddDays(-i * 7));
|
|
weekList.Add(currWeek);
|
|
}
|
|
|
|
// cerco su DB settimana x settimana....
|
|
using (GPWContext localDbCtx = new GPWContext(_configuration))
|
|
{
|
|
foreach (var item in weekList)
|
|
{
|
|
var totOreTim = localDbCtx
|
|
.DbSetTimbratureExpl
|
|
.Where(x => x.IdxDipendente == idxDipendente && x.DataLav >= item.inizio && x.DataLav <= item.fine)
|
|
.Sum(x => (double)x.HLav);
|
|
var totMinLav = localDbCtx
|
|
.DbSetRegAttivitaExpl
|
|
.Where(x => x.IdxDipendente == idxDipendente && x.DataLav >= item.inizio && x.DataLav <= item.fine)
|
|
.Sum(x => (int)x.MinRegAtt);
|
|
|
|
// aggiungo alla lista...
|
|
var weekData = new WeekStatDTO()
|
|
{
|
|
IdxDipendente = idxDipendente,
|
|
Anno = item.anno,
|
|
WeekNumber = item.weekNumber,
|
|
Inizio = item.inizio,
|
|
Fine = item.fine,
|
|
SumOreLav = totOreTim,
|
|
SumOreComm = (double)totMinLav / 60,
|
|
};
|
|
dbResult.Add(weekData);
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |