432 lines
15 KiB
C#
432 lines
15 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MP.Data.Controllers
|
|
{
|
|
public class MpSpecController : IDisposable
|
|
{
|
|
#region Public Constructors
|
|
|
|
public MpSpecController(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
Log.Info("Avviata classe MpSpecController");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Elenco Gruppi tipo Azienda
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.AnagGruppi> AnagGruppiAziende()
|
|
{
|
|
return AnagGruppiGetTipo("AZIENDA");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Gruppi
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.AnagGruppi> AnagGruppiGetAll()
|
|
{
|
|
List<DatabaseModels.AnagGruppi> dbResult = new List<DatabaseModels.AnagGruppi>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetAnagGruppi
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.CodGruppo)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gruppi x tipo
|
|
/// </summary>
|
|
/// <param name="tipoGruppo"></param>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.AnagGruppi> AnagGruppiGetTipo(string tipoGruppo)
|
|
{
|
|
List<DatabaseModels.AnagGruppi> dbResult = new List<DatabaseModels.AnagGruppi>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetAnagGruppi
|
|
.Where(x => x.TipoGruppo == tipoGruppo)
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.CodGruppo)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco valori ammessi x Stati commessa (es Yacht Baglietto)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.ListValues> AnagStatiComm()
|
|
{
|
|
return ListValuesFilt("PODL", "StatoComm");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco valori ammessi x Tipo articoli
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.ListValues> AnagTipoArtLV()
|
|
{
|
|
return ListValuesFilt("AnagArticoli", "Tipo");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Eliminazione Record
|
|
/// </summary>
|
|
/// <param name="currRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ArticoliDeleteRecord(DatabaseModels.AnagArticoli currRec)
|
|
{
|
|
bool fatto = false;
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
var currVal = dbCtx
|
|
.DbSetArticoli
|
|
.Where(x => x.CodArticolo == currRec.CodArticolo)
|
|
.FirstOrDefault();
|
|
dbCtx
|
|
.DbSetArticoli
|
|
.Remove(currVal);
|
|
await dbCtx.SaveChangesAsync();
|
|
fatto = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ArticoliDeleteRecord{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <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
|
|
.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 tabella Articoli da filtro
|
|
/// </summary>
|
|
/// <param name="numRecord"></param>
|
|
/// <param name="azienda"></param>
|
|
/// <param name="searchVal"></param>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.AnagArticoli> ArticoliGetSearch(int numRecord, string azienda = "*", string searchVal = "")
|
|
{
|
|
List<DatabaseModels.AnagArticoli> dbResult = new List<DatabaseModels.AnagArticoli>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetArticoli
|
|
.AsNoTracking()
|
|
.Where(x => (x.Azienda == azienda || azienda == "*") && (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 tabella Articoli IMPIEGATI (da stored stp_ART_getUsed)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.AnagArticoli> ArticoliGetUsed()
|
|
{
|
|
List<DatabaseModels.AnagArticoli> dbResult = new List<DatabaseModels.AnagArticoli>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetArticoli
|
|
.FromSqlRaw("EXEC stp_ART_getUsed")
|
|
.AsNoTracking()
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update Record
|
|
/// </summary>
|
|
/// <param name="currRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ArticoliUpdateRecord(DatabaseModels.AnagArticoli editRec)
|
|
{
|
|
bool fatto = false;
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
var currRec = dbCtx
|
|
.DbSetArticoli
|
|
.Where(x => x.CodArticolo == editRec.CodArticolo)
|
|
.FirstOrDefault();
|
|
if (currRec != null)
|
|
{
|
|
currRec.Disegno = editRec.Disegno;
|
|
currRec.DescArticolo = editRec.DescArticolo;
|
|
currRec.Tipo = editRec.Tipo;
|
|
currRec.Azienda = editRec.Azienda;
|
|
dbCtx.Entry(currRec).State = EntityState.Modified;
|
|
}
|
|
else
|
|
{
|
|
dbCtx
|
|
.DbSetArticoli
|
|
.Add(editRec);
|
|
}
|
|
await dbCtx.SaveChangesAsync();
|
|
fatto = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ArticoliUpdateRecord{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco da tabella Config
|
|
/// </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
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.Chiave)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update record config
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool ConfigUpdate(DatabaseModels.ConfigModel updRec)
|
|
{
|
|
bool fatto = false;
|
|
DatabaseModels.ConfigModel dbResult = new DatabaseModels.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;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco valori link (x home e navMenu laterale)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.LinkMenu> ElencoLink()
|
|
{
|
|
return ListLinkFilt("SpecLink");
|
|
}
|
|
|
|
public List<DatabaseModels.LinkMenu> ListLinkFilt(string tipoLink)
|
|
{
|
|
List<DatabaseModels.LinkMenu> dbResult = new List<DatabaseModels.LinkMenu>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetLinkMenu
|
|
.Where(x => x.TipoLink == tipoLink)
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.ordine)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco ODL filtrati x stato, articolo, KeyRich (che contiene stato)
|
|
/// </summary>
|
|
/// <param name="inCorso">Stato ODL: true=in corso/completato</param>
|
|
/// <param name="codArt">Cod articolo</param>
|
|
/// <param name="keyRichPart">KeyRich (parziale) da cercare (es cod stato x yacht)</param>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.ODLModel> ListODLFilt(bool inCorso, string codArt, string keyRichPart)
|
|
{
|
|
List<DatabaseModels.ODLModel> dbResult = new List<DatabaseModels.ODLModel>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetODL
|
|
.Where(x => ((inCorso && x.DataFine == null) || (!inCorso && x.DataFine != null)) && (x.KeyRichiesta.Contains(keyRichPart) || keyRichPart == "*") && (codArt == "*" || x.CodArticolo.Contains(codArt)))
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.IdxOdl)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco PODL non avviati filtrati x articolo, KeyRich (che contiene stato)
|
|
/// </summary>
|
|
/// <param name="codArt">Cod articolo</param>
|
|
/// <param name="keyRichPart">KeyRich (parziale) da cercare (es cod stato x yacht)</param>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.PODLModel> ListPODLFilt(string codArt, string keyRichPart)
|
|
{
|
|
List<DatabaseModels.PODLModel> dbResult = new List<DatabaseModels.PODLModel>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetPODL
|
|
.Where(x => (x.IdxOdl == 0) && (x.KeyRichiesta.Contains(keyRichPart) || keyRichPart == "*") && (codArt == "*" || x.CodArticolo.Contains(codArt)))
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.InsertDate)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco valori ammessi x tabella/colonna
|
|
/// </summary>
|
|
/// <param name="tabName"></param>
|
|
/// <param name="fieldName"></param>
|
|
/// <returns></returns>
|
|
public List<DatabaseModels.ListValues> ListValuesFilt(string tabName, string fieldName)
|
|
{
|
|
List<DatabaseModels.ListValues> dbResult = new List<DatabaseModels.ListValues>();
|
|
using (var dbCtx = new MoonProContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetListValues
|
|
.Where(x => x.TableName == tabName && x.FieldName == fieldName)
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.ordinal)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <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
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.IdxMacchina)
|
|
.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)
|
|
.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
|
|
}
|
|
} |