Files
mapo-core/MP.Data/Controllers/MpMonController.cs
T
Samuele E. Locatelli 50d65eebaa MP.DATA, riorganizzazioni varie:
- renaming classi gestione DbModels in
- spostamento anagrafica flussi da auth a generale
2025-03-08 10:40:09 +01:00

190 lines
6.3 KiB
C#

using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using MP.Data.DbModels;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MP.Data.Controllers
{
public class MpMonController : IDisposable
{
#region Public Constructors
public MpMonController(IConfiguration configuration)
{
_configuration = configuration;
Log.Info("Avviata classe MpMonController");
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Elenco tabella Articoli da filtro
/// </summary>
/// <param name="numRecord"></param>
/// <param name="searchVal"></param>
/// <returns></returns>
public List<DbModels.StatsAnagArticoli> ArticoliGetSearch(int numRecord, string searchVal = "")
{
List<DbModels.StatsAnagArticoli> dbResult = new List<DbModels.StatsAnagArticoli>();
using (var dbCtx = new MoonProContext(_configuration))
{
dbResult = dbCtx
.DbSetStatArticoli
.AsNoTracking()
.Where(x => x.CodArticolo.Contains(searchVal) || x.DescArticolo.Contains(searchVal) || x.Disegno.Contains(searchVal) || string.IsNullOrEmpty(searchVal))
.OrderBy(x => x.CodArticolo)
.Take(numRecord)
.ToList();
}
return dbResult;
}
/// <summary>
/// Elenco da tabella Macchine
/// </summary>
/// <returns></returns>
public List<DbModels.ConfigModel> ConfigGetAll()
{
List<DbModels.ConfigModel> dbResult = new List<DbModels.ConfigModel>();
using (var dbCtx = new MoonProContext(_configuration))
{
dbResult = dbCtx
.DbSetConfig
.AsNoTracking()
.OrderBy(x => x.Chiave)
.ToList();
}
return dbResult;
}
public void Dispose()
{
}
/// <summary>
/// Elenco da tabella Macchine
/// </summary>
/// <returns></returns>
public List<DbModels.Macchine> MacchineGetAll()
{
List<DbModels.Macchine> dbResult = new List<DbModels.Macchine>();
using (var dbCtx = new MoonProContext(_configuration))
{
dbResult = dbCtx
.DbSetMacchine
.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}");
}
// update: ordinamento x Locazione
dbResult = dbResult
.Where(x => !string.IsNullOrEmpty(x.locazione))
.OrderBy(x => x.locazione).ToList();
return dbResult;
}
/// <summary>
/// Elenco da tabella MappaStatoExpl
/// </summary>
/// <returns></returns>
public List<DbModels.MappaStatoExpl> MseGetAll(int maxAge = 2000)
{
List<DbModels.MappaStatoExpl> dbResult = new List<DbModels.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;
}
#endregion Public Methods
#region Private Fields
private static IConfiguration _configuration;
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
}
}