138 lines
4.4 KiB
C#
138 lines
4.4 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace MP.Data.Controllers
|
|
{
|
|
public class MpMonController : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration;
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
#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<DatabaseModels.AnagArticoli> ArticoliGetSearch(int numRecord, string searchVal = "")
|
|
{
|
|
List<DatabaseModels.AnagArticoli> dbResult = new List<DatabaseModels.AnagArticoli>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetArticoli
|
|
.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;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco da tabella Macchine
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.Macchine> MacchineGetAll()
|
|
{
|
|
List<DatabaseModels.Macchine> dbResult = new List<DatabaseModels.Macchine>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetMacchine
|
|
.OrderBy(x => x.IdxMacchina)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco da tabella Macchine
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.ConfigModel> ConfigGetAll()
|
|
{
|
|
List<DatabaseModels.ConfigModel> dbResult = new List<DatabaseModels.ConfigModel>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetConfig
|
|
.OrderBy(x => x.Chiave)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco da tabella MappaStatoExpl
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.MappaStatoExpl> MseGetAll(int maxAge = 2000)
|
|
{
|
|
List<DatabaseModels.MappaStatoExpl> dbResult = new List<DatabaseModels.MappaStatoExpl>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
var maxAgeSec = new SqlParameter("@maxAgeSec", maxAge);
|
|
|
|
dbResult = dbCtx
|
|
.DbSetMSE
|
|
.FromSqlRaw("EXEC stp_MSE_getData @maxAgeSec", maxAgeSec)
|
|
.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
|
|
}
|
|
} |