379 lines
14 KiB
C#
379 lines
14 KiB
C#
using LiMan.DB.DBModels;
|
|
using LiMan.DB.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 LiMan.DB.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 (LMDbContext localDbCtx = new LMDbContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
localDbCtx.DbForceMigrate();
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in DbForceMigrate");
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database context
|
|
//Log.Info("Dispose di GWMSController");
|
|
}
|
|
|
|
public List<ApplicativoDTO> GetApplicativiFilt(bool OnlyActive, string CodApp, string CodInst, bool hideData)
|
|
{
|
|
List<ApplicativoDTO> dbResult = new List<ApplicativoDTO>();
|
|
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
|
|
{
|
|
DateTime oggi = DateTime.Today;
|
|
|
|
StringBuilder sbRandEnigma = new StringBuilder();
|
|
StringBuilder sbRandPayload = new StringBuilder();
|
|
string stdEnigma = "";
|
|
string stdPayLoad = "";
|
|
DateTime stdDataEnigma = DateTime.Today;
|
|
if (hideData)
|
|
{
|
|
int numChar = 80;
|
|
Random random = new Random();
|
|
|
|
//random data enigma
|
|
stdDataEnigma = DateTime.Today.AddDays(random.Next(0, 15));
|
|
|
|
// random enigma
|
|
for (int i = 0; i < numChar; i++)
|
|
{
|
|
// Generate floating point numbers
|
|
double myFloat = random.NextDouble();
|
|
// Generate the char using below logic
|
|
var myChar = Convert.ToChar(Convert.ToInt32(Math.Floor(25 * myFloat) + 65));
|
|
sbRandEnigma.Append(myChar);
|
|
}
|
|
stdEnigma = $"{sbRandEnigma}";
|
|
|
|
// random payload
|
|
for (int i = 0; i < numChar; i++)
|
|
{
|
|
// Generate floating point numbers
|
|
double myFloat = random.NextDouble();
|
|
// Generate the char using below logic
|
|
var myChar = Convert.ToChar(Convert.ToInt32(Math.Floor(25 * myFloat) + 65));
|
|
sbRandPayload.Append(myChar);
|
|
}
|
|
stdPayLoad = $"{sbRandPayload}";
|
|
}
|
|
|
|
// conteggio preliminare licenze impiegate
|
|
var countSubLic = localDbCtx
|
|
.DbSetSubLicenze
|
|
.GroupBy(x => x.IdxLic)
|
|
.Select(g => new
|
|
{
|
|
IdxLic = g.Key,
|
|
NumLic = g.Count()
|
|
});
|
|
|
|
// calcolo DTO applicativi
|
|
dbResult = localDbCtx
|
|
.DbSetLicenze
|
|
.Where(x => (x.CodApp == CodApp || string.IsNullOrEmpty(CodApp)) && (x.CodInst == CodInst || string.IsNullOrEmpty(CodInst)) && (!OnlyActive || x.Scadenza > oggi))
|
|
.OrderByDescending(o => o.Scadenza)
|
|
.Select(x => new ApplicativoDTO()
|
|
{
|
|
IdxLic = x.IdxLic,
|
|
CodApp = x.CodApp,
|
|
CodInst = x.CodInst,
|
|
Descrizione = x.Descrizione,
|
|
Tipo = x.Tipo,
|
|
Scadenza = x.Scadenza,
|
|
NumLicenze = x.NumLicenze,
|
|
NumLicenzeAttive = countSubLic.Where(s => s.IdxLic == x.IdxLic).Select(c => c.NumLic).FirstOrDefault(),
|
|
DataEnigma = hideData ? stdDataEnigma : x.DataEnigma,
|
|
Enigma = hideData ? stdEnigma : x.Enigma,
|
|
Payload = hideData ? stdPayLoad : x.Payload
|
|
})
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public List<ApplicativoModel> GetApplicazioni()
|
|
{
|
|
List<ApplicativoModel> dbResult = new List<ApplicativoModel>();
|
|
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
|
|
{
|
|
dbResult = localDbCtx
|
|
.DbSetApp
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public List<InstallazioneModel> GetInstallazioni()
|
|
{
|
|
List<InstallazioneModel> dbResult = new List<InstallazioneModel>();
|
|
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
|
|
{
|
|
dbResult = localDbCtx
|
|
.DbSetInst
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public LicenzaModel GetLicenza(int IdxLic)
|
|
{
|
|
LicenzaModel dbResult = new LicenzaModel();
|
|
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
|
|
{
|
|
dbResult = localDbCtx
|
|
.DbSetLicenze
|
|
.Where(x => x.IdxLic == IdxLic)
|
|
.FirstOrDefault();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public List<LicenzaModel> GetLicenze()
|
|
{
|
|
List<LicenzaModel> dbResult = new List<LicenzaModel>();
|
|
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
|
|
{
|
|
dbResult = localDbCtx
|
|
.DbSetLicenze
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public List<LicenzaModel> GetLicenzeFilt(bool OnlyActive, string CodApp, string CodInst)
|
|
{
|
|
List<LicenzaModel> dbResult = new List<LicenzaModel>();
|
|
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
|
|
{
|
|
DateTime oggi = DateTime.Today;
|
|
dbResult = localDbCtx
|
|
.DbSetLicenze
|
|
.Where(x => (x.CodApp == CodApp || string.IsNullOrEmpty(CodApp)) && (x.CodInst == CodInst || string.IsNullOrEmpty(CodInst)) && (!OnlyActive || x.Scadenza > oggi))
|
|
.OrderByDescending(o => o.Scadenza)
|
|
.ToList();
|
|
}
|
|
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 (LMDbContext localDbCtx = new LMDbContext(_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;
|
|
}
|
|
|
|
public bool UpsertApplicazione(ApplicativoModel updItem)
|
|
{
|
|
bool done = false;
|
|
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
ApplicativoModel currData = localDbCtx
|
|
.DbSetApp
|
|
.Where(x => x.CodApp == updItem.CodApp)
|
|
.FirstOrDefault();
|
|
if (currData != null)
|
|
{
|
|
// aggiorno valori
|
|
currData.Descrizione = updItem.Descrizione;
|
|
localDbCtx.Entry(currData).State = EntityState.Modified;
|
|
}
|
|
else
|
|
{
|
|
localDbCtx
|
|
.DbSetApp
|
|
.Add(updItem);
|
|
}
|
|
localDbCtx.SaveChanges();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in UpdateApplicazioni:{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
public bool UpsertInstallazione(InstallazioneModel updItem)
|
|
{
|
|
bool done = false;
|
|
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
InstallazioneModel currData = localDbCtx
|
|
.DbSetInst
|
|
.Where(x => x.CodInst == updItem.CodInst)
|
|
.FirstOrDefault();
|
|
if (currData != null)
|
|
{
|
|
// aggiorno valori
|
|
currData.Descrizione = updItem.Descrizione;
|
|
currData.Cliente = updItem.Cliente;
|
|
currData.Contatto = updItem.Contatto;
|
|
currData.Email = updItem.Email;
|
|
localDbCtx.Entry(currData).State = EntityState.Modified;
|
|
}
|
|
else
|
|
{
|
|
localDbCtx
|
|
.DbSetInst
|
|
.Add(updItem);
|
|
}
|
|
localDbCtx.SaveChanges();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in UpdateApplicazioni:{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
public bool UpsertLicenza(LicenzaModel updItem)
|
|
{
|
|
bool done = false;
|
|
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
LicenzaModel currData = localDbCtx
|
|
.DbSetLicenze
|
|
.Where(x => x.IdxLic == updItem.IdxLic)
|
|
.FirstOrDefault();
|
|
if (currData != null)
|
|
{
|
|
// storicizzo vecchia licenza in log
|
|
var newLogLic = new LogLicenzaModel()
|
|
{
|
|
Chiave = currData.Chiave,
|
|
CodApp = currData.CodApp,
|
|
CodInst = currData.CodInst,
|
|
Descrizione = currData.Descrizione,
|
|
IdxLic = currData.IdxLic,
|
|
Locked = true,
|
|
NumLicenze = currData.NumLicenze,
|
|
Scadenza = currData.Scadenza,
|
|
Tipo = currData.Tipo
|
|
};
|
|
|
|
localDbCtx
|
|
.DbSetLogLicenze
|
|
.Add(newLogLic);
|
|
|
|
// aggiorno valori licenza
|
|
currData.CodApp = updItem.CodApp;
|
|
currData.CodInst = updItem.CodInst;
|
|
currData.NumLicenze = updItem.NumLicenze;
|
|
currData.Descrizione = updItem.Descrizione;
|
|
currData.Chiave = updItem.ChiaveCalc;
|
|
currData.Scadenza = updItem.Scadenza;
|
|
currData.Locked = updItem.Locked;
|
|
currData.Payload = updItem.Payload;
|
|
currData.DataEnigma = updItem.DataEnigma;
|
|
currData.Enigma = updItem.Enigma;
|
|
localDbCtx.Entry(currData).State = EntityState.Modified;
|
|
}
|
|
else
|
|
{
|
|
localDbCtx
|
|
.DbSetLicenze
|
|
.Add(updItem);
|
|
}
|
|
localDbCtx.SaveChanges();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in UpdateLicenze:{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
public bool UpsertLogLic(LogLicenzaModel updItem)
|
|
{
|
|
bool done = false;
|
|
using (LMDbContext localDbCtx = new LMDbContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
localDbCtx
|
|
.DbSetLogLicenze
|
|
.Add(updItem);
|
|
localDbCtx.SaveChanges();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in UpdateApplicazioni:{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |