diff --git a/MP.Data/Controllers/MpSpecRepository.cs b/MP.Data/Controllers/MpSpecRepository.cs
index 224fb848..97c67690 100644
--- a/MP.Data/Controllers/MpSpecRepository.cs
+++ b/MP.Data/Controllers/MpSpecRepository.cs
@@ -253,6 +253,8 @@ namespace MP.Data.Controllers
}
#endif
+
+#if false
///
/// Elenco codice articoli che abbiano dati Dossier
///
@@ -268,6 +270,8 @@ namespace MP.Data.Controllers
.ToListAsync();
}
+#endif
+
#if false
///
/// Conteggio num articoli Async
@@ -488,6 +492,7 @@ namespace MP.Data.Controllers
return await dbCtx.SaveChangesAsync() > 0;
}
#endif
+#if false
///
/// Elenco da tabella Config Async
@@ -1085,6 +1090,8 @@ namespace MP.Data.Controllers
.ToListAsync();
}
+#endif
+
///
/// Elenco ODL filtrati x stato, articolo, KeyRich (che contiene stato)
///
diff --git a/MP.Data/DataServiceCollectionExtensions.cs b/MP.Data/DataServiceCollectionExtensions.cs
index f649a7a0..5985cf22 100644
--- a/MP.Data/DataServiceCollectionExtensions.cs
+++ b/MP.Data/DataServiceCollectionExtensions.cs
@@ -3,8 +3,12 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
using MP.AppAuth.Services;
using MP.Data.Controllers;
using MP.Data.Repository.Anag;
+using MP.Data.Repository.Dossier;
+using MP.Data.Repository.FluxLog;
using MP.Data.Repository.IOC;
using MP.Data.Repository.Mtc;
+using MP.Data.Repository.Production;
+using MP.Data.Repository.System;
using MP.Data.Repository.Utils;
using MP.Data.Services;
using MP.Data.Services.IOC;
@@ -53,6 +57,10 @@ namespace MP.Data
services.TryAddSingleton();
// Scoped
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
+ services.TryAddScoped();
// ---------- End Repository ----------
diff --git a/MP.Data/Repository/Dossier/DossierRepository.cs b/MP.Data/Repository/Dossier/DossierRepository.cs
new file mode 100644
index 00000000..91f26562
--- /dev/null
+++ b/MP.Data/Repository/Dossier/DossierRepository.cs
@@ -0,0 +1,109 @@
+using Microsoft.Data.SqlClient;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Configuration;
+using MP.Data.DbModels;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace MP.Data.Repository.Dossier
+{
+ public class DossierRepository : IDossierRepository
+ {
+ #region Private Fields
+
+ private readonly IConfiguration _configuration;
+
+ #endregion
+
+ #region Public Constructors
+
+ public DossierRepository(IConfiguration configuration)
+ {
+ _configuration = configuration;
+ }
+
+ #endregion
+
+ #region Public Methods
+
+ ///
+ public async Task DossiersDeleteRecordAsync(DossierModel currRec)
+ {
+ await using var dbCtx = new MoonPro_FluxContext(_configuration);
+ var currVal = await dbCtx
+ .DbSetDossiers
+ .Where(x => x.IdxDossier == currRec.IdxDossier)
+ .FirstOrDefaultAsync();
+ dbCtx
+ .DbSetDossiers
+ .Remove(currVal);
+
+ return await dbCtx.SaveChangesAsync() > 0;
+ }
+
+ ///
+ public async Task> DossiersGetLastFiltAsync(string IdxMacchina, string CodArticolo, DateTime DtStart, DateTime DtEnd, int MaxRec)
+ {
+ await using var dbCtx = new MoonPro_FluxContext(_configuration);
+ return await dbCtx
+ .DbSetDossiers
+ .AsNoTracking()
+ .Where(x => (IdxMacchina == "*" || x.IdxMacchina == IdxMacchina) && (CodArticolo == "*" || x.CodArticolo == CodArticolo) && (x.DtRif >= DtStart && x.DtRif <= DtEnd))
+ .Include(m => m.MachineNav)
+ .Include(a => a.ArticoloNav)
+ .OrderByDescending(x => x.DtRif)
+ .Take(MaxRec)
+ .ToListAsync();
+ }
+
+ ///
+ public async Task DossiersInsertAsync(DossierModel newRec)
+ {
+ await using var dbCtx = new MoonPro_FluxContext(_configuration);
+ await dbCtx
+ .DbSetDossiers
+ .AddAsync(newRec);
+ return await dbCtx.SaveChangesAsync() > 0;
+ }
+
+ ///
+ public async Task DossiersTakeParamsSnapshotLastAsync(string idxMacchina, DateTime dtMin, DateTime dtMax)
+ {
+ await using var dbCtx = new MoonPro_FluxContext(_configuration);
+ var pIdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina);
+ var pDtMin = new SqlParameter("@DtMin", dtMin);
+ var pDtMax = new SqlParameter("@DtMax", dtMax);
+
+ var dbResult = await dbCtx
+ .Database
+ .ExecuteSqlRawAsync("EXEC stp_FL_TakeSnapshotLast @IdxMacchina,@DtMin,@DtMax", pIdxMacchina, pDtMin, pDtMax);
+ return dbResult != 0;
+ }
+
+ ///
+ public async Task DossiersUpdateValoreAsync(DossierModel editRec)
+ {
+ await using var dbCtx = new MoonPro_FluxContext(_configuration);
+ var currRec = await dbCtx
+ .DbSetDossiers
+ .Where(x => x.IdxDossier == editRec.IdxDossier)
+ .FirstOrDefaultAsync();
+ if (currRec != null)
+ {
+ currRec.Valore = editRec.Valore;
+ dbCtx.Entry(currRec).State = EntityState.Modified;
+ }
+ else
+ {
+ await dbCtx
+ .DbSetDossiers
+ .AddAsync(editRec);
+ }
+ return await dbCtx.SaveChangesAsync() > 0;
+ }
+
+ #endregion
+ }
+}
diff --git a/MP.Data/Repository/Dossier/IDossierRepository.cs b/MP.Data/Repository/Dossier/IDossierRepository.cs
new file mode 100644
index 00000000..3f6852bd
--- /dev/null
+++ b/MP.Data/Repository/Dossier/IDossierRepository.cs
@@ -0,0 +1,24 @@
+using MP.Data.DbModels;
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+
+namespace MP.Data.Repository.Dossier
+{
+ public interface IDossierRepository
+ {
+ #region Public Methods
+
+ Task DossiersDeleteRecordAsync(DossierModel currRec);
+
+ Task> DossiersGetLastFiltAsync(string IdxMacchina, string CodArticolo, DateTime DtStart, DateTime DtEnd, int MaxRec);
+
+ Task DossiersInsertAsync(DossierModel newRec);
+
+ Task DossiersTakeParamsSnapshotLastAsync(string idxMacchina, DateTime dtMin, DateTime dtMax);
+
+ Task DossiersUpdateValoreAsync(DossierModel editRec);
+
+ #endregion
+ }
+}
diff --git a/MP.Data/Repository/FluxLog/FluxLogRepository.cs b/MP.Data/Repository/FluxLog/FluxLogRepository.cs
new file mode 100644
index 00000000..6032c730
--- /dev/null
+++ b/MP.Data/Repository/FluxLog/FluxLogRepository.cs
@@ -0,0 +1,176 @@
+using Microsoft.Data.SqlClient;
+using Microsoft.Extensions.Configuration;
+using MP.Core.DTO;
+using MP.Core.Objects;
+using MP.Data.DbModels;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace MP.Data.Repository.FluxLog
+{
+ public class FluxLogRepository : IFluxLogRepository
+ {
+ #region Private Fields
+
+ private readonly IConfiguration _configuration;
+ private static NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();
+
+ #endregion
+
+ #region Public Constructors
+
+ public FluxLogRepository(IConfiguration configuration)
+ {
+ _configuration = configuration;
+ }
+
+ #endregion
+
+ #region Public Methods
+
+ ///
+ public async Task> FluxLogDataReduxAsync(string idxMaccSel, List fluxList, Periodo currPeriodo, Enums.ValSelection valMode, Enums.DataInterval intReq, int maxItem)
+ {
+ List procStats = new List();
+ Log.Info($"Inizio FluxLogDataReduxAsync | idxMaccSel: {idxMaccSel} | periodo: {currPeriodo.Inizio:yyyy-MM-dd} --> {currPeriodo.Fine:yyyy-MM-dd}");
+ TimeSpan step = TimeSpan.FromHours(1);
+ switch (intReq)
+ {
+ case Enums.DataInterval.minute:
+ step = TimeSpan.FromMinutes(1.00 / maxItem);
+ break;
+
+ case Enums.DataInterval.hour:
+ step = TimeSpan.FromHours(1.00 / maxItem);
+ break;
+
+ case Enums.DataInterval.day:
+ step = TimeSpan.FromDays(1.00 / maxItem);
+ break;
+
+ default:
+ break;
+ }
+
+ var pIdxMacchina = new SqlParameter("@IdxMacchina", idxMaccSel);
+ var pOnlyTest = new SqlParameter("@OnlyTest", false);
+
+ await using var dbCtx = new MoonPro_FluxContext(_configuration);
+ foreach (var item in fluxList)
+ {
+ Log.Info($"FluxLogDataReduxAsync | Flux: {item}");
+ int numRecProc = 0;
+ Stopwatch sw = new Stopwatch();
+ sw.Start();
+ var pCodFlux = new SqlParameter("@CodFlux", item);
+ DateTime dtCursStart = currPeriodo.Inizio;
+ DateTime dtCursEnd = dtCursStart.Add(step);
+ bool setCompleted = false;
+ while (!setCompleted)
+ {
+ var currFlux = await dbCtx
+ .DbSetFluxLog
+ .Where(x => (x.CodFlux == item) && (x.dtEvento >= dtCursStart && x.dtEvento < dtCursEnd) && (x.IdxMacchina == idxMaccSel))
+ .ToListAsync();
+
+ int numRec = currFlux.Count;
+ numRecProc += numRec;
+ if (numRec > maxItem)
+ {
+ List listPeriodi = new List();
+
+ switch (valMode)
+ {
+ case Enums.ValSelection.First:
+ var recStart = currFlux.Skip(1).FirstOrDefault();
+ listPeriodi.Add(new Periodo(recStart.dtEvento, dtCursEnd));
+ break;
+
+ case Enums.ValSelection.Last:
+ var recEnd = currFlux.LastOrDefault();
+ listPeriodi.Add(new Periodo(dtCursStart, recEnd.dtEvento));
+ break;
+
+ case Enums.ValSelection.Center:
+ int idx = 1;
+ var recCent = currFlux.Skip(idx / (maxItem + 1)).FirstOrDefault();
+ listPeriodi.Add(new Periodo(dtCursStart, recCent.dtEvento));
+ if (maxItem > 1)
+ {
+ for (int i = 2; i < maxItem; i++)
+ {
+ DateTime dtInizio = recCent.dtEvento;
+ recCent = currFlux.Skip(i / (maxItem + 1)).FirstOrDefault();
+ listPeriodi.Add(new Periodo(dtInizio, recCent.dtEvento));
+ }
+ }
+ listPeriodi.Add(new Periodo(recCent.dtEvento.AddSeconds(1), dtCursEnd));
+ break;
+
+ default:
+ break;
+ }
+
+ foreach (var slot in listPeriodi)
+ {
+ var pDtStart = new SqlParameter("@DtStart", slot.Inizio);
+ var pDtEnd = new SqlParameter("@DtEnd", slot.Fine);
+ await dbCtx
+ .Database
+ .ExecuteSqlRawAsync("EXEC man.stp_ReduceFluxLog @IdxMacchina, @CodFlux, @DtStart, @DtEnd, @OnlyTest", pIdxMacchina, pCodFlux, pDtStart, pDtEnd, pOnlyTest);
+ }
+ }
+
+ dtCursStart = dtCursEnd;
+ dtCursEnd = dtCursStart.Add(step);
+ setCompleted = dtCursStart >= currPeriodo.Fine;
+ }
+ sw.Stop();
+ StatDedupDTO currStat = new StatDedupDTO()
+ {
+ IdxMacchina = idxMaccSel,
+ CodFlux = item,
+ Interval = intReq,
+ Num4Int = maxItem,
+ NumRec = numRecProc,
+ ProcTime = sw.Elapsed.TotalSeconds
+ };
+ procStats.Add(currStat);
+ }
+ Log.Info($"FINE FluxLogDataReduxAsync | idxMaccSel: {idxMaccSel} | periodo: {currPeriodo.Inizio:yyyy-MM-dd} --> {currPeriodo.Fine:yyyy-MM-dd}");
+ return procStats;
+ }
+
+ ///
+ public async Task> FluxLogGetLastFiltAsync(DateTime DtMax, DateTime DtMin, string IdxMacchina, string CodFlux, int MaxRec)
+ {
+ await using var dbCtx = new MoonPro_FluxContext(_configuration);
+ return await 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)
+ .ToListAsync() ?? new();
+ }
+
+ ///
+ public async Task> FluxLogParetoAsync(string idxMacchina, DateTime dtFrom, DateTime dtTo)
+ {
+ await using var dbCtx = new MoonPro_FluxContext(_configuration);
+ return await dbCtx
+ .DbSetFluxLog
+ .Where(x => (string.IsNullOrEmpty(idxMacchina) || x.IdxMacchina == idxMacchina) && (dtFrom <= x.dtEvento && x.dtEvento <= dtTo))
+ .AsNoTracking()
+ .GroupBy(x => x.CodFlux)
+ .Select(g => new ParetoFluxLogDTO() { IdxMacchina = idxMacchina, CodFlux = g.Key, Qty = g.Count() })
+ .OrderByDescending(x => x.Qty)
+ .ToListAsync() ?? new();
+ }
+
+ #endregion
+ }
+}
diff --git a/MP.Data/Repository/FluxLog/IFluxLogRepository.cs b/MP.Data/Repository/FluxLog/IFluxLogRepository.cs
new file mode 100644
index 00000000..0d50ca31
--- /dev/null
+++ b/MP.Data/Repository/FluxLog/IFluxLogRepository.cs
@@ -0,0 +1,17 @@
+using MP.Core.DTO;
+using MP.Data.DbModels;
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+
+namespace MP.Data.Repository.FluxLog
+{
+ public interface IFluxLogRepository
+ {
+ Task> FluxLogDataReduxAsync(string idxMaccSel, List fluxList, Periodo currPeriodo, Enums.ValSelection valMode, Enums.DataInterval intReq, int maxItem);
+
+ Task> FluxLogGetLastFiltAsync(DateTime DtMax, DateTime DtMin, string IdxMacchina, string CodFlux, int MaxRec);
+
+ Task> FluxLogParetoAsync(string idxMacchina, DateTime dtFrom, DateTime dtTo);
+ }
+}
diff --git a/MP.Data/Repository/Production/BaseRepository.cs b/MP.Data/Repository/Production/BaseRepository.cs
new file mode 100644
index 00000000..b7d20e56
--- /dev/null
+++ b/MP.Data/Repository/Production/BaseRepository.cs
@@ -0,0 +1,31 @@
+using Microsoft.EntityFrameworkCore;
+using System.Threading.Tasks;
+
+namespace MP.Data.Repository.Production
+{
+ public abstract class BaseRepository
+ {
+ #region Protected Fields
+
+ protected readonly IDbContextFactory _ctxFactory;
+
+ #endregion Protected Fields
+
+ #region Protected Constructors
+
+ protected BaseRepository(IDbContextFactory ctxFactory) => _ctxFactory = ctxFactory;
+
+ #endregion Protected Constructors
+
+ #region Protected Methods
+
+ ///
+ /// Creazione dbcontext per singola transazione
+ ///
+ ///
+ protected async Task CreateContextAsync() => await _ctxFactory.CreateDbContextAsync();
+
+ #endregion Protected Methods
+
+ }
+}
diff --git a/MP.Data/Repository/Production/IProductionRepository.cs b/MP.Data/Repository/Production/IProductionRepository.cs
new file mode 100644
index 00000000..68e464a4
--- /dev/null
+++ b/MP.Data/Repository/Production/IProductionRepository.cs
@@ -0,0 +1,115 @@
+using MP.Core.DTO;
+using MP.Data.DbModels;
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+
+namespace MP.Data.Repository.Production
+{
+ public interface IProductionRepository
+ {
+ #region ODL Methods
+
+ Task> ListODLFiltAsync(bool inCorso, string codArt, string keyRichPart, string Reparto, string IdxMacchina, DateTime startDate, DateTime endDate);
+
+ Task> OdlByKeyAsync(int IdxOdl);
+
+ Task ODLCloseAsync(int idxOdl, string idxMacchina, int matrOpr, bool confPezzi, bool confRett, int modoConfProd);
+
+ Task> OdlGetCurrentAsync();
+
+ Task> OdlGetStatAsync(int IdxOdl);
+
+ Task> OdlByBatchAsync(string batchSel);
+
+ #endregion
+
+ #region PODL Methods
+
+ Task> ListPODLFiltAsync(bool lanciato, string keyRichPart, string idxMacchina, string codGruppo, DateTime startDate, DateTime endDate);
+
+ Task> ListPODL_ByCodArtAsync(string CodArticolo, bool OnlyAvail);
+
+ Task> ListPODL_ByKitParentAsync(int IdxPodlParent);
+
+ Task> ListPODL_KitFiltAsync(bool lanciato, string keyRichPart, string idxMacchina, string codGruppo, DateTime startDate, DateTime endDate);
+
+ Task PODL_getByKeyAsync(int idxPODL);
+
+ Task PODL_getByOdlAsync(int idxODL);
+
+ Task> PODL_getDictOdlPodlAsync(List missingIds);
+
+ Task PODL_startSetup(PODLExpModel editRec, int matrOpr, double tcRich, int pzPallet, string note, DateTime dtEvent);
+
+ Task PODL_updateRecipe(int idxPODL, string recipeName);
+
+ Task PODLDeleteRecordAsync(PODLExpModel currRec);
+
+ Task PODLUpdateRecordAsync(PODLModel editRec);
+
+ Task PodlIstKitDeleteAsync(int IdxPODL);
+
+ #endregion
+
+ #region Kit Methods
+
+ Task IstKitDeleteAsync(IstanzeKitModel rec2del);
+
+ Task> IstKitFiltAsync(string keyKit, string keyExtOrd);
+
+ Task IstKitInsertByWKSAsync(string CodArtParent, string KeyFilt);
+
+ Task IstKitUpsertAsync(IstanzeKitModel editRec);
+
+ Task TemplateKitDeleteAsync(TemplateKitModel rec2del);
+
+ Task> TemplateKitFiltAsync(string KitCode, string codChild);
+
+ Task TemplateKitUpsertAsync(TemplateKitModel editRec, string codAzienda);
+
+ Task WipKitDeleteAsync(WipSetupKitModel rec2del);
+
+ Task WipKitDeleteOlderAsync(DateTime dateLimit);
+
+ Task> WipKitFiltAsync(string KeyFilt);
+
+ Task WipKitUpsertAsync(WipSetupKitModel editRec);
+
+ Task> TksScoreAsync(string KeyFilt, int MaxResult);
+
+ #endregion
+
+ #region Macchine / Gruppi Methods
+
+ Task> MacchineGetFiltAsync(string codGruppo);
+
+ Task> MacchineByMatrOperAsync(int MatrOpr);
+
+ Task> MacchineWithFluxAsync(DateTime dtStart, DateTime dtEnd);
+
+ Task Grp2MaccDeleteAsync(Gruppi2MaccModel rec2del);
+
+ Task Grp2MaccInsertAsync(Gruppi2MaccModel upsRec);
+
+ Task Grp2OperDeleteAsync(Gruppi2OperModel rec2del);
+
+ Task Grp2OperInsertAsync(Gruppi2OperModel upsRec);
+
+ Task> StatoMacchinaAsync(string idxMacchina);
+
+ #endregion
+
+ #region Misc Production Methods
+
+ Task> MseGetAllAsync(int maxAge = 2000);
+
+ Task> ListGiacenzeAsync(int IdxOdl);
+
+ Task> OperatoriGetFiltAsync(string codGruppo);
+
+ Task> ParametriGetFiltAsync(string IdxMacchina);
+
+ #endregion
+ }
+}
diff --git a/MP.Data/Repository/Production/ProductionRepository.cs b/MP.Data/Repository/Production/ProductionRepository.cs
new file mode 100644
index 00000000..1d5196ac
--- /dev/null
+++ b/MP.Data/Repository/Production/ProductionRepository.cs
@@ -0,0 +1,864 @@
+using Microsoft.Data.SqlClient;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Configuration;
+using MP.Core.DTO;
+using MP.Data.DbModels;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace MP.Data.Repository.Production
+{
+ public class ProductionRepository : IProductionRepository
+ {
+ #region Private Fields
+
+ private readonly IDbContextFactory _ctxFactory;
+ private readonly IConfiguration _configuration;
+
+ #endregion
+
+ #region Public Constructors
+
+ public ProductionRepository(IDbContextFactory ctxFactory, IConfiguration configuration)
+ {
+ _ctxFactory = ctxFactory;
+ _configuration = configuration;
+ }
+
+ #endregion
+
+ #region Private Methods
+
+ private async Task GetMoonProContextAsync() => await _ctxFactory.CreateDbContextAsync();
+
+ #endregion
+
+ #region Public Methods - ODL
+
+ ///
+ public async Task> ListODLFiltAsync(bool inCorso, string codArt, string keyRichPart, string Reparto, string IdxMacchina, DateTime startDate, DateTime endDate)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+
+ var InCorso = new SqlParameter("@InCorso", inCorso);
+ var CodArt = new SqlParameter("@CodArt", codArt);
+ var KeyRich = new SqlParameter("@KeyRich", keyRichPart);
+ var CodGruppo = new SqlParameter("@CodGruppo", Reparto);
+ var IdxMacc = new SqlParameter("@IdxMacchina", IdxMacchina);
+ var DataFrom = new SqlParameter("@DataFrom", startDate);
+ var DataTo = new SqlParameter("@DataTo", endDate);
+
+ return await dbCtx
+ .DbSetODLExp
+ .FromSqlRaw("EXEC stp_ODL_getByFiltSpec @InCorso, @CodArt, @KeyRich, @CodGruppo, @IdxMacchina, @DataFrom, @DataTo", InCorso, CodArt, KeyRich, CodGruppo, IdxMacc, DataFrom, DataTo)
+ .AsNoTracking()
+ .ToListAsync();
+ }
+
+ ///
+ public async Task> OdlByKeyAsync(int IdxOdl)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ return await dbCtx
+ .DbSetODLExp
+ .AsNoTracking()
+ .FirstOrDefaultAsync(x => x.IdxOdl == IdxOdl);
+ }
+
+ ///
+ public async Task ODLCloseAsync(int idxOdl, string idxMacchina, int matrOpr, bool confPezzi, bool confRett, int modoConfProd)
+ {
+ bool fatto = false;
+ if (idxOdl > 0)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ // preparo i parametri
+ var IdxODL = new SqlParameter("@IdxODL", idxOdl);
+ var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina);
+
+ try
+ {
+ var dbResult = await dbCtx
+ .Database
+ .ExecuteSqlRawAsync("EXEC stp_ODL_fineProd @IdxODL, @IdxMacchina", IdxODL, IdxMacchina);
+ fatto = dbResult != 0;
+ }
+ catch (Exception exc)
+ {
+ NLog.LogManager.GetCurrentClassLogger().Error($"Eccezione durante ODLCloseAsync{Environment.NewLine}{exc}");
+ }
+ }
+ return fatto;
+ }
+
+ ///
+ public async Task> OdlGetCurrentAsync()
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ return await dbCtx
+ .DbSetODL
+ .Where(x => x.DataInizio != null && x.DataFine == null)
+ .ToListAsync();
+ }
+
+ ///
+ public async Task> OdlGetStatAsync(int IdxOdl)
+ {
+ List dbResult = new List();
+ if (IdxOdl > 0)
+ {
+ await using var dbCtx = new MoonPro_STATSContext(_configuration);
+ var IdxODL = new SqlParameter("@IdxODL", IdxOdl);
+
+ dbResult = await dbCtx
+ .DbSetODL
+ .FromSqlRaw("EXEC stp_STAT_ODL @IdxODL", IdxODL)
+ .AsNoTracking()
+ .ToListAsync();
+ }
+ return dbResult;
+ }
+
+ ///
+ public async Task> OdlByBatchAsync(string batchSel)
+ {
+ await using var dbCtx = new MoonPro_InveContext(_configuration);
+ return await dbCtx
+ .DbGiacenzeData
+ .AsNoTracking()
+ .Where(x => x.IdxOdl > 0)
+ .Select(x => x.IdxOdl)
+ .ToListAsync();
+ }
+
+ #endregion
+
+ #region Public Methods - PODL
+
+ ///
+ public async Task> ListPODLFiltAsync(bool lanciato, string keyRichPart, string idxMacchina, string codGruppo, DateTime startDate, DateTime endDate)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var Lanc = new SqlParameter("@Lanciato", lanciato);
+ var KeyRich = new SqlParameter("@KeyRich", keyRichPart);
+ var CodGrp = new SqlParameter("@CodGruppo", codGruppo);
+ var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina);
+ var DateFrom = new SqlParameter("@DtInizio", startDate);
+ var DateTo = new SqlParameter("@DtFine", endDate);
+
+ return await dbCtx
+ .DbSetPODLExp
+ .FromSqlRaw("EXEC stp_PODL_getByFiltSpec @Lanciato, @KeyRich, @CodGruppo, @IdxMacchina, @DtInizio, @DtFine", Lanc, KeyRich, CodGrp, IdxMacc, DateFrom, DateTo)
+ .AsNoTracking()
+ .ToListAsync();
+ }
+
+ ///
+ public async Task> ListPODL_ByCodArtAsync(string CodArticolo, bool OnlyAvail)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var pCodArticolo = new SqlParameter("@CodArticolo", CodArticolo);
+ var pOnlyAvail = new SqlParameter("@onlyAvail", OnlyAvail);
+
+ return await dbCtx
+ .DbSetPODLExp
+ .FromSqlRaw("EXEC stp_PODL_getByCodArt @CodArticolo, @onlyAvail", pCodArticolo, pOnlyAvail)
+ .AsNoTracking()
+ .ToListAsync();
+ }
+
+ ///
+ public async Task> ListPODL_ByKitParentAsync(int IdxPodlParent)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var pIdxPodlParent = new SqlParameter("@IdxPodlParent", IdxPodlParent);
+
+ return await dbCtx
+ .DbSetPODLExp
+ .FromSqlRaw("EXEC stp_PODL_getByParentKitIdx @IdxPodlParent", pIdxPodlParent)
+ .AsNoTracking()
+ .ToListAsync();
+ }
+
+ ///
+ public async Task> ListPODL_KitFiltAsync(bool lanciato, string keyRichPart, string idxMacchina, string codGruppo, DateTime startDate, DateTime endDate)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var Lanc = new SqlParameter("@Lanciato", lanciato);
+ var KeyRich = new SqlParameter("@KeyRich", keyRichPart);
+ var CodGrp = new SqlParameter("@CodGruppo", codGruppo);
+ var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina);
+ var DateFrom = new SqlParameter("@DtInizio", startDate);
+ var DateTo = new SqlParameter("@DtFine", endDate);
+
+ return await dbCtx
+ .DbSetPODLExp
+ .FromSqlRaw("EXEC stp_PODL_getByFiltSpecKit @Lanciato, @KeyRich, @CodGruppo, @IdxMacchina, @DtInizio, @DtFine", Lanc, KeyRich, CodGrp, IdxMacc, DateFrom, DateTo)
+ .AsNoTracking()
+ .ToListAsync();
+ }
+
+ ///
+ public async Task PODL_getByKeyAsync(int idxPODL)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ return await dbCtx
+ .DbSetPODL
+ .AsNoTracking()
+ .Where(x => x.IdxPromessa == idxPODL)
+ .Include(a => a.ArticoloNav)
+ .FirstOrDefaultAsync() ?? new();
+ }
+
+ ///
+ public async Task PODL_getByOdlAsync(int idxODL)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ return await dbCtx
+ .DbSetPODL
+ .AsNoTracking()
+ .Where(x => x.IdxOdl == idxODL)
+ .FirstOrDefaultAsync() ?? new();
+ }
+
+ ///
+ public async Task> PODL_getDictOdlPodlAsync(List missingIds)
+ {
+ if (missingIds == null || !missingIds.Any())
+ return new Dictionary();
+
+ await using var dbCtx = await GetMoonProContextAsync();
+ return await dbCtx
+ .DbSetPODL
+ .AsNoTracking()
+ .Where(x => missingIds.Contains(x.IdxOdl))
+ .ToDictionaryAsync(x => x.IdxOdl, x => x.IdxPromessa);
+ }
+
+ ///
+ public async Task PODL_startSetup(PODLExpModel editRec, int matrOpr, double tcRich, int pzPallet, string note, DateTime dtEvent)
+ {
+ bool answ = false;
+ PODLModel recPODL = new PODLModel()
+ {
+ IdxPromessa = editRec.IdxPromessa,
+ KeyRichiesta = editRec.KeyRichiesta,
+ KeyBCode = editRec.KeyBCode,
+ IdxOdl = editRec.IdxOdl,
+ CodArticolo = editRec.CodArticolo,
+ CodGruppo = editRec.CodGruppo,
+ IdxMacchina = editRec.IdxMacchina,
+ NumPezzi = editRec.NumPezzi,
+ Tcassegnato = editRec.Tcassegnato,
+ DueDate = editRec.DueDate,
+ Priorita = editRec.Priorita,
+ PzPallet = editRec.PzPallet,
+ Note = editRec.Note,
+ CodCli = editRec.CodCli,
+ InsertDate = editRec.InsertDate
+ };
+
+ await using var dbCtx = await GetMoonProContextAsync();
+ var currRec = await dbCtx
+ .DbSetPODL
+ .AsNoTracking()
+ .Where(x => x.IdxPromessa == recPODL.IdxPromessa)
+ .FirstOrDefaultAsync();
+
+ if (currRec != null)
+ {
+ var IdxPromessa = new SqlParameter("@idxPromessa", recPODL.IdxPromessa);
+ var MatrOpr = new SqlParameter("@MatrOpr", matrOpr);
+ var IdxMacchina = new SqlParameter("@IdxMacchina", recPODL.IdxMacchina);
+ var TCRichAttr = new SqlParameter("@TCRichAttr", tcRich);
+ var PzPallet = new SqlParameter("@PzPallet", pzPallet);
+ var Note = new SqlParameter("@Note", note);
+ var DtEvento = new SqlParameter("@dtEvento", dtEvent);
+ await dbCtx
+ .Database
+ .ExecuteSqlRawAsync("EXEC stp_ODL_inizioSetupPromessa @idxPromessa, @MatrOpr, @IdxMacchina, @TCRichAttr, @PzPallet, @Note, @dtEvento", IdxPromessa, MatrOpr, IdxMacchina, TCRichAttr, PzPallet, Note, DtEvento);
+
+ answ = true;
+ }
+ return answ;
+ }
+
+ ///
+ public async Task PODL_updateRecipe(int idxPODL, string recipeName)
+ {
+ bool answ = false;
+ await using var dbCtx = await GetMoonProContextAsync();
+ var currRec = await dbCtx
+ .DbSetPODL
+ .Where(x => x.IdxPromessa == idxPODL)
+ .FirstOrDefaultAsync();
+
+ if (currRec != null)
+ {
+ currRec.Recipe = recipeName;
+ dbCtx.Entry(currRec).State = EntityState.Modified;
+ answ = await dbCtx.SaveChangesAsync() > 0;
+ }
+ return answ;
+ }
+
+ ///
+ public async Task PODLDeleteRecordAsync(PODLExpModel currRec)
+ {
+ PODLModel recPODL = new PODLModel()
+ {
+ IdxPromessa = currRec.IdxPromessa,
+ KeyRichiesta = currRec.KeyRichiesta,
+ KeyBCode = currRec.KeyBCode,
+ IdxOdl = currRec.IdxOdl,
+ CodArticolo = currRec.CodArticolo,
+ CodGruppo = currRec.CodGruppo,
+ IdxMacchina = currRec.IdxMacchina,
+ NumPezzi = currRec.NumPezzi,
+ Tcassegnato = currRec.Tcassegnato,
+ DueDate = currRec.DueDate,
+ Priorita = currRec.Priorita,
+ PzPallet = currRec.PzPallet,
+ Note = currRec.Note,
+ CodCli = currRec.CodCli,
+ InsertDate = currRec.InsertDate
+ };
+ await using var dbCtx = await GetMoonProContextAsync();
+ var currVal = await dbCtx
+ .DbSetPODL
+ .Where(x => x.IdxPromessa == recPODL.IdxPromessa)
+ .FirstOrDefaultAsync();
+ dbCtx
+ .DbSetPODL
+ .Remove(currVal);
+ return await dbCtx.SaveChangesAsync() > 0;
+ }
+
+ ///
+ public async Task PODLUpdateRecordAsync(PODLModel editRec)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var currRec = await dbCtx
+ .DbSetPODL
+ .Where(x => x.IdxPromessa == editRec.IdxPromessa)
+ .FirstOrDefaultAsync();
+ if (currRec != null)
+ {
+ currRec.CodGruppo = editRec.CodGruppo;
+ currRec.CodArticolo = editRec.CodArticolo;
+ currRec.IdxMacchina = editRec.IdxMacchina;
+ currRec.KeyBCode = editRec.KeyBCode;
+ currRec.KeyRichiesta = editRec.KeyRichiesta;
+ currRec.NumPezzi = editRec.NumPezzi;
+ currRec.Tcassegnato = editRec.Tcassegnato;
+ currRec.Attivabile = editRec.Attivabile;
+ currRec.Note = editRec.Note;
+ dbCtx.Entry(currRec).State = EntityState.Modified;
+ }
+ else
+ {
+ await dbCtx
+ .DbSetPODL
+ .AddAsync(editRec);
+ }
+ return await dbCtx.SaveChangesAsync() > 0;
+ }
+
+ ///
+ public async Task PodlIstKitDeleteAsync(int IdxPODL)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var pIdxPODL = new SqlParameter("@IdxPODL", IdxPODL);
+
+ var dbResult = await dbCtx
+ .Database
+ .ExecuteSqlRawAsync("EXEC dbo.stp_PodlIstKit_delete @IdxPODL", pIdxPODL);
+ return dbResult != 0;
+ }
+
+ #endregion
+
+ #region Public Methods - Kit
+
+ ///
+ public async Task IstKitDeleteAsync(IstanzeKitModel rec2del)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var actRec = await dbCtx
+ .DbSetInstKit
+ .Where(x => x.KeyKit == rec2del.KeyKit && x.KeyExtOrd == rec2del.KeyExtOrd)
+ .FirstOrDefaultAsync();
+ if (actRec != null)
+ {
+ dbCtx
+ .DbSetInstKit
+ .Remove(actRec);
+ }
+ return await dbCtx.SaveChangesAsync() > 0;
+ }
+
+ ///
+ public async Task> IstKitFiltAsync(string keyKit, string keyExtOrd)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ return await dbCtx
+ .DbSetInstKit
+ .Where(x => (string.IsNullOrEmpty(keyKit) && string.IsNullOrEmpty(keyExtOrd)) || (x.KeyKit.Contains(keyKit) && !string.IsNullOrEmpty(keyKit)) || (x.KeyExtOrd.Contains(keyExtOrd) && !string.IsNullOrEmpty(keyExtOrd)))
+ .AsNoTracking()
+ .ToListAsync() ?? new();
+ }
+
+ ///
+ public async Task IstKitInsertByWKSAsync(string CodArtParent, string KeyFilt)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+
+ var pCodArtParent = new SqlParameter("@CodArtParent", CodArtParent);
+ var pKeyFilt = new SqlParameter("@KeyFilt", KeyFilt);
+
+ var dbResult = await dbCtx
+ .Database
+ .ExecuteSqlRawAsync("EXEC dbo.stp_IstKit_insertByWKS @CodArtParent,@KeyFilt", pCodArtParent, pKeyFilt);
+ return dbResult != 0;
+ }
+
+ ///
+ public async Task IstKitUpsertAsync(IstanzeKitModel editRec)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var actRec = await dbCtx
+ .DbSetInstKit
+ .Where(x => x.KeyKit == editRec.KeyKit && x.KeyExtOrd == editRec.KeyExtOrd)
+ .FirstOrDefaultAsync();
+
+ if (actRec == null)
+ {
+ await dbCtx
+ .DbSetInstKit
+ .AddAsync(editRec);
+ }
+ else
+ {
+ actRec.CodArtParent = editRec.CodArtParent;
+ actRec.CodArtChild = editRec.CodArtChild;
+ actRec.QtyART = editRec.QtyART;
+ actRec.QtyKIT = editRec.QtyKIT;
+ dbCtx.Entry(actRec).State = EntityState.Modified;
+ }
+ return await dbCtx.SaveChangesAsync() > 0;
+ }
+
+ ///
+ public async Task TemplateKitDeleteAsync(TemplateKitModel rec2del)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var actRec = await dbCtx
+ .DbSetTempKit
+ .Where(x => x.CodArtParent == rec2del.CodArtParent && x.CodArtChild == rec2del.CodArtChild)
+ .FirstOrDefaultAsync();
+ if (actRec != null)
+ {
+ dbCtx
+ .DbSetTempKit
+ .Remove(actRec);
+ }
+ return await dbCtx.SaveChangesAsync() > 0;
+ }
+
+ ///
+ public async Task> TemplateKitFiltAsync(string KitCode, string codChild)
+ {
+ List dbResult = new List();
+ await using var dbCtx = await GetMoonProContextAsync();
+ dbResult = await dbCtx
+ .DbSetTempKit
+ .Where(x => (string.IsNullOrEmpty(KitCode) && string.IsNullOrEmpty(codChild)) || (x.CodArtParent.Contains(KitCode) && !string.IsNullOrEmpty(KitCode)) || (x.CodArtChild.Contains(codChild) && !string.IsNullOrEmpty(codChild)))
+ .AsNoTracking()
+ .ToListAsync();
+ return dbResult;
+ }
+
+ ///
+ public async Task TemplateKitUpsertAsync(TemplateKitModel editRec, string codAzienda)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var recArt = await dbCtx
+ .DbSetArticoli
+ .FirstOrDefaultAsync(x => x.CodArticolo == editRec.CodArtParent);
+ if (recArt == null)
+ {
+ AnagArticoliModel newRecArt = new AnagArticoliModel()
+ {
+ CodArticolo = editRec.CodArtParent,
+ Tipo = "KIT",
+ DescArticolo = $"Articolo KIT - {DateTime.Now:yyyy-MM-dd HH:mm:ss}",
+ Disegno = "",
+ Azienda = codAzienda,
+ CurrRev = "",
+ ProdRev = ""
+ };
+ dbCtx
+ .DbSetArticoli
+ .Add(newRecArt);
+ }
+
+ var actRec = await dbCtx
+ .DbSetTempKit
+ .Where(x => x.CodArtParent == editRec.CodArtParent && x.CodArtChild == editRec.CodArtChild)
+ .FirstOrDefaultAsync();
+
+ if (actRec == null)
+ {
+ await dbCtx
+ .DbSetTempKit
+ .AddAsync(editRec);
+ }
+ else
+ {
+ actRec.Qty = editRec.Qty;
+ dbCtx.Entry(actRec).State = EntityState.Modified;
+ }
+ return await dbCtx.SaveChangesAsync() > 0;
+ }
+
+ ///
+ public async Task WipKitDeleteAsync(WipSetupKitModel rec2del)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var actRec = await dbCtx
+ .DbSetWipKit
+ .Where(x => x.KeyFilt == rec2del.KeyFilt && x.CodOrd == rec2del.CodOrd)
+ .FirstOrDefaultAsync();
+ if (actRec != null)
+ {
+ dbCtx
+ .DbSetWipKit
+ .Remove(actRec);
+ }
+ return await dbCtx.SaveChangesAsync() > 0;
+ }
+
+ ///
+ public async Task WipKitDeleteOlderAsync(DateTime dateLimit)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var actRec = await dbCtx
+ .DbSetWipKit
+ .Where(x => x.DataIns < dateLimit)
+ .ToListAsync();
+ if (actRec != null && actRec.Any())
+ {
+ dbCtx
+ .DbSetWipKit
+ .RemoveRange(actRec);
+ }
+ return await dbCtx.SaveChangesAsync() > 0;
+ }
+
+ ///
+ public async Task> WipKitFiltAsync(string KeyFilt)
+ {
+ List dbResult = new List();
+ if (!string.IsNullOrEmpty(KeyFilt))
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ dbResult = await dbCtx
+ .DbSetWipKit
+ .Where(x => x.KeyFilt.Contains(KeyFilt))
+ .AsNoTracking()
+ .ToListAsync();
+ }
+ return dbResult;
+ }
+
+ ///
+ public async Task WipKitUpsertAsync(WipSetupKitModel editRec)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var actRec = await dbCtx
+ .DbSetWipKit
+ .Where(x => x.KeyFilt == editRec.KeyFilt && x.CodOrd == editRec.CodOrd)
+ .FirstOrDefaultAsync();
+
+ if (actRec == null)
+ {
+ dbCtx
+ .DbSetWipKit
+ .Add(editRec);
+ }
+ else
+ {
+ actRec.CodArt = editRec.CodArt;
+ actRec.DescArt = editRec.DescArt;
+ actRec.Qta = editRec.Qta;
+ actRec.DataIns = editRec.DataIns;
+ dbCtx.Entry(actRec).State = EntityState.Modified;
+ }
+ return await dbCtx.SaveChangesAsync() > 0;
+ }
+
+ ///
+ public async Task> TksScoreAsync(string KeyFilt, int MaxResult)
+ {
+ List dbResult = new List();
+ if (!string.IsNullOrEmpty(KeyFilt))
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ var pKeyFilt = new SqlParameter("@KeyFilt", KeyFilt);
+ var pMaxRes = new SqlParameter("@maxResult", MaxResult);
+ dbResult = await dbCtx
+ .DbSetTksScore
+ .FromSqlRaw("EXEC stp_TKS_Search @KeyFilt, @maxResult", pKeyFilt, pMaxRes)
+ .AsNoTracking()
+ .ToListAsync();
+ }
+ return dbResult;
+ }
+
+ #endregion
+
+ #region Public Methods - Macchine/Gruppi
+
+ ///
+ public async Task> MacchineGetFiltAsync(string codGruppo)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ if (codGruppo == "*")
+ {
+ return await dbCtx
+ .DbSetMacchine
+ .AsNoTracking()
+ .OrderBy(x => x.IdxMacchina)
+ .ToListAsync();
+ }
+ else
+ {
+ return await dbCtx
+ .DbSetGrp2Macc
+ .Where(g => g.CodGruppo == codGruppo)
+ .Join(dbCtx.DbSetMacchine,
+ g => g.IdxMacchina,
+ m => m.IdxMacchina,
+ (g, m) => m
+ )
+ .AsNoTracking()
+ .OrderBy(x => x.IdxMacchina)
+ .ToListAsync();
+ }
+ }
+
+ ///
+ public async Task> MacchineByMatrOperAsync(int MatrOpr)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ if (MatrOpr == 0)
+ {
+ return await dbCtx
+ .DbSetMacchine
+ .AsNoTracking()
+ .OrderBy(x => x.IdxMacchina)
+ .ToListAsync();
+ }
+ else
+ {
+ return await dbCtx
+ .DbSetGrp2Oper
+ .Where(g => g.MatrOpr == MatrOpr)
+ .Join(dbCtx.DbSetGrp2Macc,
+ g => g.CodGruppo,
+ m => m.CodGruppo,
+ (g, m) => m
+ )
+ .Distinct()
+ .Join(dbCtx.DbSetMacchine,
+ g => g.IdxMacchina,
+ m => m.IdxMacchina,
+ (g, m) => m
+ )
+ .Distinct()
+ .AsNoTracking()
+ .OrderBy(x => x.IdxMacchina)
+ .ToListAsync();
+ }
+ }
+
+ ///
+ public async Task> MacchineWithFluxAsync(DateTime dtStart, DateTime dtEnd)
+ {
+ await using var dbCtx = new MoonPro_FluxContext(_configuration);
+ return await dbCtx
+ .DbSetFluxLog
+ .AsNoTracking()
+ .Where(x => x.dtEvento >= dtStart && x.dtEvento <= dtEnd)
+ .Select(i => i.IdxMacchina)
+ .Distinct()
+ .ToListAsync() ?? new();
+ }
+
+ ///
+ public async Task Grp2MaccDeleteAsync(Gruppi2MaccModel rec2del)
+ {
+ bool answ = false;
+ await using var dbCtx = await GetMoonProContextAsync();
+ var dbRec = await dbCtx
+ .DbSetGrp2Macc
+ .Where(x => x.CodGruppo == rec2del.CodGruppo && x.IdxMacchina == rec2del.IdxMacchina)
+ .FirstOrDefaultAsync();
+ if (dbRec != null)
+ {
+ dbCtx.DbSetGrp2Macc.Remove(dbRec);
+ int numDone = await dbCtx.SaveChangesAsync();
+ answ = numDone != 0;
+ }
+ return answ;
+ }
+
+ ///
+ public async Task Grp2MaccInsertAsync(Gruppi2MaccModel upsRec)
+ {
+ bool answ = false;
+ await using var dbCtx = await GetMoonProContextAsync();
+ var dbRec = await dbCtx
+ .DbSetGrp2Macc
+ .Where(x => x.CodGruppo == upsRec.CodGruppo && x.IdxMacchina == upsRec.IdxMacchina)
+ .FirstOrDefaultAsync();
+ if (dbRec == null)
+ {
+ await dbCtx.DbSetGrp2Macc.AddAsync(upsRec);
+ int numDone = await dbCtx.SaveChangesAsync();
+ answ = numDone != 0;
+ }
+ return answ;
+ }
+
+ ///
+ public async Task Grp2OperDeleteAsync(Gruppi2OperModel rec2del)
+ {
+ bool answ = false;
+ await using var dbCtx = await GetMoonProContextAsync();
+ var dbRec = await dbCtx
+ .DbSetGrp2Oper
+ .Where(x => x.CodGruppo == rec2del.CodGruppo && x.MatrOpr == rec2del.MatrOpr)
+ .FirstOrDefaultAsync();
+ if (dbRec != null)
+ {
+ dbCtx.DbSetGrp2Oper.Remove(dbRec);
+ int numDone = await dbCtx.SaveChangesAsync();
+ answ = numDone != 0;
+ }
+ return answ;
+ }
+
+ ///
+ public async Task Grp2OperInsertAsync(Gruppi2OperModel upsRec)
+ {
+ bool answ = false;
+ await using var dbCtx = await GetMoonProContextAsync();
+ var dbRec = await dbCtx
+ .DbSetGrp2Oper
+ .Where(x => x.CodGruppo == upsRec.CodGruppo && x.MatrOpr == upsRec.MatrOpr)
+ .FirstOrDefaultAsync();
+ if (dbRec == null)
+ {
+ await dbCtx.DbSetGrp2Oper.AddAsync(upsRec);
+ int numDone = await dbCtx.SaveChangesAsync();
+ answ = numDone != 0;
+ }
+ return answ;
+ }
+
+ ///
+ public async Task> StatoMacchinaAsync(string idxMacchina)
+ {
+ await using var dbCtx = await GetMoonProContextAsync();
+ return (await dbCtx
+ .DbSetStatoMacc
+ .AsNoTracking()
+ .Where(x => x.IdxMacchina == idxMacchina)
+ .ToListAsync())
+ .AsReadOnly();
+ }
+
+ #endregion
+
+ #region Public Methods - Misc
+
+ ///
+ public async Task> MseGetAllAsync(int maxAge = 2000)
+ {
+ List dbResult = new List();
+ await using var dbCtx = await GetMoonProContextAsync();
+
+ var maxAgeSec = new SqlParameter("@maxAgeSec", maxAge);
+
+ dbResult = await dbCtx
+ .DbSetMSE
+ .FromSqlRaw("EXEC stp_MSE_getData @maxAgeSec", maxAgeSec)
+ .AsNoTracking()
+ .ToListAsync();
+
+ return dbResult;
+ }
+
+ ///
+ public async Task> ListGiacenzeAsync(int IdxOdl)
+ {
+ await using var dbCtx = new MoonPro_InveContext(_configuration);
+ return await dbCtx
+ .DbGiacenzeData
+ .Where(x => x.IdxOdl == IdxOdl)
+ .AsNoTracking()
+ .ToListAsync() ?? new();
+ }
+
+ ///
+ public async Task> OperatoriGetFiltAsync(string codGruppo)
+ {
+ List dbResult = new List();
+ await using var dbCtx = await GetMoonProContextAsync();
+ if (codGruppo == "*")
+ {
+ dbResult = await dbCtx
+ .DbOperatori
+ .AsNoTracking()
+ .OrderBy(x => x.MatrOpr)
+ .ToListAsync();
+ }
+ else
+ {
+ dbResult = await dbCtx
+ .DbSetGrp2Oper
+ .Where(g => g.CodGruppo == codGruppo)
+ .Join(dbCtx.DbOperatori,
+ g => g.MatrOpr,
+ m => m.MatrOpr,
+ (g, m) => m
+ )
+ .AsNoTracking()
+ .OrderBy(x => x.MatrOpr)
+ .ToListAsync();
+ }
+ return dbResult;
+ }
+
+ ///
+ public async Task> ParametriGetFiltAsync(string IdxMacchina)
+ {
+ await using var dbCtx = new MoonPro_FluxContext(_configuration);
+ return await dbCtx
+ .DbSetFluxLog
+ .AsNoTracking()
+ .Where(x => (IdxMacchina == "*" || x.IdxMacchina == IdxMacchina))
+ .Take(1000)
+ .Select(i => i.CodFlux)
+ .Distinct()
+ .OrderBy(x => x)
+ .ToListAsync();
+ }
+
+ #endregion
+ }
+}
diff --git a/MP.Data/Repository/System/ISystemRepository.cs b/MP.Data/Repository/System/ISystemRepository.cs
new file mode 100644
index 00000000..055c3a87
--- /dev/null
+++ b/MP.Data/Repository/System/ISystemRepository.cs
@@ -0,0 +1,24 @@
+using MP.Data.DbModels;
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+
+namespace MP.Data.Repository.System
+{
+ public interface ISystemRepository
+ {
+ Task> ConfigGetAllAsync();
+
+ Task ConfigUpdateAsync(ConfigModel updRec);
+
+ Task EvListInsertAsync(EventListModel newRec);
+
+ Task ForceDbMaintAsync(bool doExec, bool doUpdStat, bool doSave, int minPgCnt, int minAvgFrag, int maxAvgFragReb);
+
+ Task> ListLinkAllAsync();
+
+ Task> ListLinkFiltAsync(string tipoLink);
+
+ Task> ElencoLinkAsync();
+ }
+}
diff --git a/MP.Data/Repository/System/SystemRepository.cs b/MP.Data/Repository/System/SystemRepository.cs
new file mode 100644
index 00000000..1f078364
--- /dev/null
+++ b/MP.Data/Repository/System/SystemRepository.cs
@@ -0,0 +1,114 @@
+using Microsoft.Data.SqlClient;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Configuration;
+using MP.Data.DbModels;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace MP.Data.Repository.System
+{
+ public class SystemRepository : ISystemRepository
+ {
+ #region Private Fields
+
+ private readonly IDbContextFactory _ctxFactory;
+ private readonly IConfiguration _configuration;
+
+ #endregion
+
+ #region Public Constructors
+
+ public SystemRepository(IDbContextFactory ctxFactory, IConfiguration configuration)
+ {
+ _ctxFactory = ctxFactory;
+ _configuration = configuration;
+ }
+
+ #endregion
+
+ #region Public Methods
+
+ ///
+ public async Task> ConfigGetAllAsync()
+ {
+ await using var dbCtx = await _ctxFactory.CreateDbContextAsync();
+ return await dbCtx
+ .DbSetConfig
+ .AsNoTracking()
+ .OrderBy(x => x.Chiave)
+ .ToListAsync() ?? new();
+ }
+
+ ///
+ public async Task ConfigUpdateAsync(ConfigModel updRec)
+ {
+ bool fatto = false;
+ ConfigModel dbResult = new();
+ await using var dbCtx = await _ctxFactory.CreateDbContextAsync();
+ dbResult = await dbCtx
+ .DbSetConfig
+ .Where(x => x.Chiave == updRec.Chiave)
+ .FirstOrDefaultAsync();
+ if (dbResult != null)
+ {
+ dbResult.Valore = updRec.Valore;
+ fatto = await dbCtx.SaveChangesAsync() > 0;
+ }
+ return fatto;
+ }
+
+ ///
+ public async Task EvListInsertAsync(EventListModel newRec)
+ {
+ await using var dbCtx = await _ctxFactory.CreateDbContextAsync();
+ _ = await dbCtx
+ .DbSetEvList
+ .AddAsync(newRec);
+ return await dbCtx.SaveChangesAsync() > 0;
+ }
+
+ ///
+ public async Task ForceDbMaintAsync(bool doExec, bool doUpdStat, bool doSave, int minPgCnt, int minAvgFrag, int maxAvgFragReb)
+ {
+ await using var dbCtx = new MoonProAdminContext(_configuration);
+
+ _ = await dbCtx
+ .Database
+ .ExecuteSqlRawAsync("EXEC man.stp_Utility_Maintanance");
+ return true;
+ }
+
+ ///
+ public async Task> ListLinkAllAsync()
+ {
+ await using var dbCtx = await _ctxFactory.CreateDbContextAsync();
+ return await dbCtx
+ .DbSetLinkMenu
+ .AsNoTracking()
+ .OrderBy(x => x.Ordine)
+ .ToListAsync();
+ }
+
+ ///
+ public async Task> ListLinkFiltAsync(string tipoLink)
+ {
+ await using var dbCtx = await _ctxFactory.CreateDbContextAsync();
+ return await dbCtx
+ .DbSetLinkMenu
+ .Where(x => x.TipoLink == tipoLink)
+ .AsNoTracking()
+ .OrderBy(x => x.Ordine)
+ .ToListAsync();
+ }
+
+ ///
+ public Task> ElencoLinkAsync()
+ {
+ return ListLinkFiltAsync("SpecLink");
+ }
+
+ #endregion
+ }
+}
diff --git a/MP.SPEC/refactor_repository.md b/MP.SPEC/refactor_repository.md
index e804649b..3dca7787 100644
--- a/MP.SPEC/refactor_repository.md
+++ b/MP.SPEC/refactor_repository.md
@@ -29,19 +29,27 @@
## 4. Checklist Avanzamento Modifiche
### Fase 1: Preparazione (Infrastruttura)
-- [ ] Creazione file
-efactor_repository.md.
-- [ ] Analisi delle interfacce necessarie per ogni dominio.
-- [ ] Rinomina MpSpecController $\rightarrow$ MpSpecRepository (per allineamento naming).
+- [x] Creazione file refactor_repository.md.
+- [x] Analisi delle interfacce necessarie per ogni dominio.
+- [x] Rinomina MpSpecController $\rightarrow$ MpSpecRepository (per allineamento naming).
### Fase 2: Estrazione Modulare (Iterativa)
-- [ ] **Modulo Anagrafica**: Creazione AnagRepository $\rightarrow$ Spostamento metodi $\rightarrow$ Aggiornamento MpDataService.
-- [ ] **Modulo Produzione**: Creazione ProductionRepository $\rightarrow$ Spostamento metodi $\rightarrow$ Aggiornamento MpDataService.
-- [ ] **Modulo Dossier**: Creazione DossierRepository $\rightarrow$ Spostamento metodi $\rightarrow$ Aggiornamento MpDataService.
-- [ ] **Modulo FluxLog**: Creazione FluxLogRepository $\rightarrow$ Spostamento metodi $\rightarrow$ Aggiornamento MpDataService.
-- [ ] **Modulo Sistema**: Creazione SystemRepository $\rightarrow$ Spostamento metodi $\rightarrow$ Aggiornamento MpDataService.
+- [x] **Modulo Anagrafica**: Creazione AnagRepository + IAnagRepository (26 metodi migrati, codice sorgente in `#if false` in MpSpecRepository). File: `MP.Data\Repository\Anag\`
+- [x] **Modulo Produzione**: Creazione ProductionRepository + IProductionRepository (32 metodi migrati). File: `MP.Data\Repository\Production\`
+ - ODL: ListODLFiltAsync, OdlByKeyAsync, ODLCloseAsync, OdlGetCurrentAsync, OdlGetStatAsync, OdlByBatchAsync
+ - PODL: ListPODLFiltAsync, ListPODL_ByCodArtAsync, ListPODL_ByKitParentAsync, ListPODL_KitFiltAsync, PODL_getByKeyAsync, PODL_getByOdlAsync, PODL_getDictOdlPodlAsync, PODL_startSetup, PODL_updateRecipe, PODLDeleteRecordAsync, PODLUpdateRecordAsync, PodlIstKitDeleteAsync
+ - Kit: IstKitDeleteAsync, IstKitFiltAsync, IstKitInsertByWKSAsync, IstKitUpsertAsync, TemplateKitDeleteAsync, TemplateKitFiltAsync, TemplateKitUpsertAsync, WipKitDeleteAsync, WipKitDeleteOlderAsync, WipKitFiltAsync, WipKitUpsertAsync, TksScoreAsync
+ - Macchine/Gruppi: MacchineGetFiltAsync, MacchineByMatrOperAsync, MacchineWithFluxAsync, Grp2MaccDeleteAsync, Grp2MaccInsertAsync, Grp2OperDeleteAsync, Grp2OperInsertAsync
+ - Misc: MseGetAllAsync, ListGiacenzeAsync, OperatoriGetFiltAsync, ParametriGetFiltAsync
+- [x] **Modulo Dossier**: Creazione DossierRepository + IDossierRepository (5 metodi migrati). File: `MP.Data\Repository\Dossier\`
+ - DossiersDeleteRecordAsync, DossiersGetLastFiltAsync, DossiersInsertAsync, DossiersTakeParamsSnapshotLastAsync, DossiersUpdateValoreAsync
+- [x] **Modulo FluxLog**: Creazione FluxLogRepository + IFluxLogRepository (3 metodi migrati). File: `MP.Data\Repository\FluxLog\`
+ - FluxLogDataReduxAsync, FluxLogGetLastFiltAsync, FluxLogParetoAsync
+- [x] **Modulo Sistema**: Creazione SystemRepository + ISystemRepository (7 metodi migrati). File: `MP.Data\Repository\System\`
+ - ConfigGetAllAsync, ConfigUpdateAsync, EvListInsertAsync, ForceDbMaintAsync, ListLinkAllAsync, ListLinkFiltAsync, ElencoLinkAsync
### Fase 3: Pulizia e Verifica
-- [ ] Rimozione del codice residuo da MpSpecRepository.
-- [ ] Verifica della compilazione della soluzione (./build_all_par.ps1 --agent).
-- [ ] Verifica della coerenza dei log e della persistenza dei dati.
+- [ ] **Passo 3.1**: Spostamento a `#if false` dei metodi migrati in MpSpecRepository (33 metodi originali ancora attivi).
+- [ ] **Passo 3.2**: Verifica compilazione (dotnet build MP.Data\MP.Data.csproj).
+- [ ] **Passo 3.3**: Aggiornamento MpDataService per iniettare i 5 nuovi repository invece di MpSpecRepository.
+- [ ] **Passo 3.4**: Refactoring MpSpecRepository (solo metodi residui non migrati).
diff --git a/build_all_par.ps1 b/build_all_par.ps1
index 9cd56ce2..8d845d01 100644
--- a/build_all_par.ps1
+++ b/build_all_par.ps1
@@ -1,3 +1,9 @@
+# build_all_par.ps1
+# script per compilazione (parallela) progetti MAPO-CORE
+# nb: prerequisito powershell >= 7, install con
+# winget install Microsoft.PowerShell
+
+
# --- CONFIGURAZIONE ---
$pattern = "MP-*.sln"
$sharedProjectPath = ".\MP.Data\MP.Data.csproj"
@@ -9,7 +15,9 @@ $agentMode = $args -contains "--agent"
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
# 1. Trova l'MSBuild ufficiale di Visual Studio 2022
-$vsPaths = & "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -version "[17.0,18.0)" -products * -requires Microsoft.Component.MSBuild -property installationPath
+$vsPaths = @(& "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -version "[17.0,19.0)" -products * -requires Microsoft.Component.MSBuild -property installationPath)
+# versione WKS-R9-SAM
+# $vsPaths = & "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -version "[17.0,18.0)" -products * -requires Microsoft.Component.MSBuild -property installationPath
if (-not $vsPaths) {
if ($agentMode) { exit 1 }
Write-Host "❌ Impossibile trovare Visual Studio 2022!" -ForegroundColor Red