Files
GPW/GPW.CORE.Data/Controllers/GPWController.cs
T

143 lines
4.9 KiB
C#

using GPW.CORE.Data.DbModels;
using GPW.CORE.Data.DTO;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GPW.CORE.Data.Controllers
{
public class GPWController : IDisposable
{
private static IConfiguration _configuration = null!;
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
public GPWController(IConfiguration configuration)
{
_configuration = configuration;
}
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()
{
Log.Info("Dispose di GPWController");
}
public List<DipendentiModel> DipendentiGetAll()
{
List<DipendentiModel> dbResult = new List<DipendentiModel>();
using (GPWContext localDbCtx = new GPWContext(_configuration))
{
dbResult = localDbCtx
.DbSetDipendenti
.ToList();
}
return dbResult;
}
/// <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 oreTim = 1;// localDbCtx
//.DbSetTimbrature
//.Where(x => x.IdxDipendente==idxDipendente && x.DataOra >= item.inizio && x.DataOra <= item.fine)
//.Sum(x => x.IdxDipendente);
var listRA = localDbCtx
.DbSetRegAttivita
.Where(x => x.IdxDipendente == idxDipendente && x.Inizio >= item.inizio && x.Inizio <= item.fine)
.Select(x => x.Fine.Subtract(x.Inizio).TotalHours)
.ToList();
//.Sum(x => x.Fine.Subtract(x.Inizio).TotalHours);
double oreLav = listRA.Sum(x => x);
// aggiungo alla lista...
var weekData = new WeekStatDTO()
{
IdxDipendente = idxDipendente,
Anno = item.anno,
WeekNumber = item.weekNumber,
Inizio = item.inizio,
Fine = item.fine,
SumOreLav = oreTim,
SumOreComm = oreLav,
};
dbResult.Add(weekData);
}
}
return dbResult;
}
/// <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;
}
}
}