413 lines
14 KiB
C#
413 lines
14 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using MP.Data.DatabaseModels;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MP.Data.Controllers
|
|
{
|
|
public class MpIocController : IDisposable
|
|
{
|
|
#region Public Constructors
|
|
|
|
public MpIocController(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
Log.Info("Avviata classe MpIocController");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
|
|
/// <summary>
|
|
/// Elenco da tabella Config
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<ConfigModel> ConfigGetAll()
|
|
{
|
|
List<ConfigModel> dbResult = new List<ConfigModel>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetConfig
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.Chiave)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update record config
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool ConfigUpdate(ConfigModel updRec)
|
|
{
|
|
bool fatto = false;
|
|
ConfigModel dbResult = new ConfigModel();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetConfig
|
|
.Where(x => x.Chiave == updRec.Chiave)
|
|
.FirstOrDefault();
|
|
if (dbResult != null)
|
|
{
|
|
dbResult.Valore = updRec.Valore;
|
|
dbCtx.SaveChanges();
|
|
fatto = true;
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Intera tab dati macchina
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DatiMacchineModel> DatiMacchineGetAll()
|
|
{
|
|
List<DatiMacchineModel> dbResult = new List<DatiMacchineModel>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetDatiMacchine
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.IdxMacchina)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_configuration = null;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Aggiunta record EventList
|
|
/// </summary>
|
|
/// <param name="newRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> EvListInsert(EventListModel newRec)
|
|
{
|
|
bool fatto = false;
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
var currRec = dbCtx
|
|
.DbSetEvList
|
|
.Add(newRec);
|
|
await dbCtx.SaveChangesAsync();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante EvListInsert{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
await Task.Delay(1);
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco ultimi n record flux log dato macchina e flusso (ordinato x data registrazione)
|
|
/// </summary>
|
|
/// <param name="DtMax">Data massima x eventi</param>
|
|
/// <param name="DtMin">Data minima x eventi</param>
|
|
/// <param name="IdxMacchina">* = tutte, altrimenti solo x una data macchina</param>
|
|
/// <param name="CodFlux">*=tutti, altrimenti solo selezionato</param>
|
|
/// <param name="MaxRec">numero massimo record da restituire</param>
|
|
/// <returns></returns>
|
|
public List<FluxLog> FluxLogGetLastFilt(DateTime DtMax, DateTime DtMin, string IdxMacchina, string CodFlux, int MaxRec)
|
|
{
|
|
List<FluxLog> dbResult = new List<FluxLog>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetFluxLog
|
|
.AsNoTracking()
|
|
.Where(x => (x.dtEvento >= DtMin && x.dtEvento <= DtMax) && (IdxMacchina == "*" || x.IdxMacchina == IdxMacchina) && (CodFlux == "*" || x.CodFlux == CodFlux))
|
|
.OrderByDescending(x => x.dtEvento)
|
|
.Take(MaxRec)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public bool KeepAliveUpsert(string IdxMacc, DateTime OraServer, DateTime OraMacc)
|
|
{
|
|
bool fatto = false;
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
var currRec = dbCtx
|
|
.DbSetKeepAlive
|
|
.Where(x => x.IdxMacchina == IdxMacc)
|
|
.FirstOrDefault();
|
|
if (currRec != null)
|
|
{
|
|
currRec.DataOraServer = OraServer;
|
|
currRec.DataOraMacchina = OraMacc;
|
|
dbCtx.Entry(currRec).State = EntityState.Modified;
|
|
}
|
|
else
|
|
{
|
|
KeepAliveModel newRec = new KeepAliveModel()
|
|
{
|
|
IdxMacchina = IdxMacc,
|
|
DataOraMacchina = OraMacc,
|
|
DataOraServer = OraServer,
|
|
DataOraStart = DateTime.Now
|
|
};
|
|
dbCtx
|
|
.DbSetKeepAlive
|
|
.Add(newRec);
|
|
}
|
|
dbCtx.SaveChanges();
|
|
fatto = true;
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
|
|
public List<LinkMenu> ListLinkFilt(string tipoLink)
|
|
{
|
|
List<LinkMenu> dbResult = new List<LinkMenu>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetLinkMenu
|
|
.Where(x => x.TipoLink == tipoLink)
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.ordine)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Intera tabella relazione master/slave in machine (gestione setup master --> slave)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<Macchine2SlaveModel> Macchine2Slave()
|
|
{
|
|
List<Macchine2SlaveModel> dbResult = new List<Macchine2SlaveModel>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetM2S
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.IdxMacchina)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco da tabella Macchine
|
|
/// </summary>
|
|
/// <param name="codGruppo"></param>
|
|
/// <returns></returns>
|
|
public List<Macchine> MacchineGetFilt(string codGruppo)
|
|
{
|
|
List<Macchine> dbResult = new List<Macchine>();
|
|
try
|
|
{
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
if (codGruppo == "*")
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetMacchine
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.IdxMacchina)
|
|
.ToList();
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetGrp2Macc
|
|
.Where(g => g.CodGruppo == codGruppo)
|
|
.Join(dbCtx.DbSetMacchine,
|
|
g => g.IdxMacchina,
|
|
m => m.IdxMacchina,
|
|
(g, m) => m
|
|
)
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.IdxMacchina)
|
|
.ToList();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in MacchineGetFilt{Environment.NewLine}{exc}");
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco da tabella MappaStatoExpl
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<MappaStatoExpl> MseGetAll(int maxAge = 2000)
|
|
{
|
|
List<MappaStatoExpl> dbResult = new List<MappaStatoExpl>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
var maxAgeSec = new SqlParameter("@maxAgeSec", maxAge);
|
|
|
|
dbResult = dbCtx
|
|
.DbSetMSE
|
|
.FromSqlRaw("EXEC stp_MSE_getData @maxAgeSec", maxAgeSec)
|
|
.AsNoTracking()
|
|
.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 (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
if (dbCtx.Entry(item).State == Microsoft.EntityFrameworkCore.EntityState.Deleted || dbCtx.Entry(item).State == Microsoft.EntityFrameworkCore.EntityState.Modified)
|
|
{
|
|
dbCtx.Entry(item).Reload();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in rollBackEntity{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Intera tabella state machine ingressi 2 eventi
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<TransizioneIngressiModel> StateMachineIngressi(int idxFam)
|
|
{
|
|
List<TransizioneIngressiModel> dbResult = new List<TransizioneIngressiModel>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
var IdxFamIn = new SqlParameter("@IdxFamigliaIngresso", idxFam);
|
|
dbResult = dbCtx
|
|
.DbSetSMI
|
|
.FromSqlRaw("exec dbo.stp_TRI_getByIdxFamIng @IdxFamigliaIngresso", IdxFamIn)
|
|
.AsNoTracking()
|
|
.AsEnumerable()
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stato prod macchina
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <returns></returns>
|
|
public PzProdModel PezziProdMacchina(string idxMacchina)
|
|
{
|
|
PzProdModel dbResult = new PzProdModel();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina);
|
|
dbResult = dbCtx
|
|
.DbSetPzProd
|
|
.FromSqlRaw("EXEC stp_PzProd_getByMacchina @IdxMacchina", IdxMacchina)
|
|
.AsNoTracking()
|
|
.FirstOrDefault();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Intera vista v_MSFD
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<VMSFDModel> VMSFDGetAll()
|
|
{
|
|
List<VMSFDModel> dbResult = new List<VMSFDModel>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetMSFD
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.IdxMacchina)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Vista v_MSFD x singola macchina (da stored) - singolo record
|
|
/// </summary>
|
|
/// <param name="idxMacc"></param>
|
|
/// <returns></returns>
|
|
public List<VMSFDModel> VMSFDGetByMacc(string idxMacc)
|
|
{
|
|
List<VMSFDModel> dbResult = new List<VMSFDModel>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacc);
|
|
|
|
dbResult = dbCtx
|
|
.DbSetMSFD
|
|
.FromSqlRaw("exec dbo.stp_MSFD_getMacc @IdxMacchina", IdxMacchina)
|
|
.AsNoTracking()
|
|
.AsEnumerable()
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
/// <summary>
|
|
/// Vista v_MSFD delle machine MULTI filtrato x macchina (da stored)
|
|
/// </summary>
|
|
/// <param name="idxMacc"></param>
|
|
/// <returns></returns>
|
|
public List<VMSFDModel> VMSFDGetMultiByMacc(string idxMacc)
|
|
{
|
|
List<VMSFDModel> dbResult = new List<VMSFDModel>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacc);
|
|
|
|
dbResult = dbCtx
|
|
.DbSetMSFD
|
|
.FromSqlRaw("exec dbo.stp_MSFD_getMulti @IdxMacchina", IdxMacchina)
|
|
.AsNoTracking()
|
|
.AsEnumerable()
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration;
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |