using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using MP.Data.DbModels; using NLog; using System; using System.Data; using System.Linq; using System.Threading.Tasks; using static MP.Core.Objects.Enums; namespace MP.Data.Repository.IOC { public class IocRepository : BaseRepository, IIocRepository { #region Public Constructors public IocRepository(IDbContextFactory ctxFactory) : base(ctxFactory) { } #endregion Public Constructors #region Public Methods /// public async Task CheckCambiaStatoBatchAsync(tipoInputEvento tipoInput, string IdxMacchina, DateTime InizioStato, int IdxTipo, string CodArt, string Value, int MatrOpr, string pallet) { await using var dbCtx = await CreateContextAsync(); //await using var tx = await dbCtx.Database.BeginTransactionAsync(IsolationLevel.ReadCommitted); try { var pIdxMacchina = new SqlParameter("@IdxMacchina", IdxMacchina); var pIdxTipo = new SqlParameter("@IdxTipo", IdxTipo); var pInizioStato = new SqlParameter("@InizioStato", InizioStato); var pCodArticolo = new SqlParameter("@codArticolo", CodArt); var pValue = new SqlParameter("@Value", Value); var pMatrOpr = new SqlParameter("@MatrOpr", MatrOpr); var pPallet = new SqlParameter("@pallet", pallet); string opType = tipoInput switch { tipoInputEvento.barcode => "BARCODE", tipoInputEvento.hw => "HW", _ => "ND" }; TransizioneStatiModel? rigaTrans = null; if (opType != "ND") { string sql = tipoInput switch { tipoInputEvento.barcode => "EXEC dbo.stp_TS_getUserForcedTrans @IdxMacchina, @IdxTipo", tipoInputEvento.hw => "EXEC dbo.stp_TS_getByIdxMacchIdxTipoEv @IdxMacchina, @IdxTipo", _ => string.Empty }; // ✅ prima chiamata ASYNC rigaTrans = (await dbCtx.DbSetSMES .FromSqlRaw(sql, pIdxMacchina, pIdxTipo) .AsNoTracking() .ToListAsync() ).FirstOrDefault(); } if (rigaTrans != null && rigaTrans.IdxStato != rigaTrans.next_IdxStato) { var pIdxStato = new SqlParameter("@IdxStato", rigaTrans.next_IdxStato); await dbCtx.Database.ExecuteSqlRawAsync( "EXEC dbo.stp_DDB_InsStatoBatch @IdxMacchina, @InizioStato, @IdxStato, @codArticolo, @Value, @MatrOpr, @pallet", pIdxMacchina, pInizioStato, pIdxStato, pCodArticolo, pValue, pMatrOpr, pPallet); // eseguo solo se evento manuale/barcode if (tipoInput == tipoInputEvento.barcode) { var pMaxAgeSec = new SqlParameter("@maxAgeSec", 0); await dbCtx.Database.ExecuteSqlRawAsync( "EXEC stp_MSE_recalc @maxAgeSec, @idxMacchina", pMaxAgeSec, pIdxMacchina); } } else { Log.Debug($"Nessun cambio stato richiesto | {opType} | {IdxMacchina} | {IdxTipo}"); } //// Nessuna eccezione = successo //await tx.CommitAsync(); return true; } catch (Exception ex) { //await tx.RollbackAsync(); // Log dettagliato errore Log.Error(ex, $"Errore in CheckCambiaStatoBatchAsync: {IdxMacchina} | {tipoInput}"); throw; // O return false; se il chiamante gestisce fallimenti } } /// public async Task EvListMicroStatoInsertAsync(MicroStatoMacchinaModel newRecMsm, EventListModel newRecEv) { await using var dbCtx = await CreateContextAsync(); //// eseguo in transazione... //await using var tx = await dbCtx.Database.BeginTransactionAsync(IsolationLevel.ReadCommitted); try { // inizio con record microstato... var actRec = await dbCtx .DbSetMicroStatoMacc .FindAsync(newRecMsm.IdxMacchina); //.SingleOrDefaultAsync(); if (actRec == null) { dbCtx.DbSetMicroStatoMacc.Add(newRecMsm); } else { actRec.IdxMicroStato = newRecMsm.IdxMicroStato; actRec.InizioStato = newRecMsm.InizioStato; actRec.Value = newRecMsm.Value; // Update() allega l'entità e segna tutti i campi come Modified dbCtx.DbSetMicroStatoMacc.Update(actRec); } // ora record EVList dbCtx.DbSetEvList.Add(newRecEv); // EF Core 8 raggruppa automaticamente INSERT/UPDATE in un batch var rowsAffected = await dbCtx.SaveChangesAsync(); //await tx.CommitAsync(); return rowsAffected > 0; } catch (Exception ex) { //await tx.RollbackAsync(); // Log dettagliato errore Log.Error(ex, $"Errore in EvListMicroStatoInsertAsync: {newRecEv.IdxMacchina}"); throw; } } public async Task MicroStatoMacchinaUpsertAsync(MicroStatoMacchinaModel newRec) { bool fatto = false; using var dbCtx = new MoonProContext(options); var actRec = await dbCtx .DbSetMicroStatoMacc .Where(x => x.IdxMacchina == newRec.IdxMacchina) .AsNoTracking() .FirstOrDefaultAsync(); if (actRec == null) { dbCtx .DbSetMicroStatoMacc .Add(newRec); } else { actRec.IdxMicroStato = newRec.IdxMicroStato; actRec.InizioStato = newRec.InizioStato; actRec.Value = newRec.Value; dbCtx.Entry(actRec).State = EntityState.Modified; } fatto = await dbCtx.SaveChangesAsync() > 0; return fatto; } #endregion Public Methods #region Protected Fields protected static NLog.Logger Log = LogManager.GetCurrentClassLogger(); #endregion Protected Fields } }