diff --git a/MP.Data/Controllers/MpIocController.cs b/MP.Data/Controllers/MpIocController.cs index cee027b3..4c3cb3be 100644 --- a/MP.Data/Controllers/MpIocController.cs +++ b/MP.Data/Controllers/MpIocController.cs @@ -139,7 +139,7 @@ namespace MP.Data.Controllers public async Task CheckCambiaStatoBatchAsync(tipoInputEvento tipoInput, string IdxMacchina, DateTime InizioStato, int IdxTipo, string CodArt, string Value, int MatrOpr, string pallet) { await using var dbCtx = new MoonProContext(options); - await using var tx = await dbCtx.Database.BeginTransactionAsync(); + //await using var tx = await dbCtx.Database.BeginTransactionAsync(); try { @@ -200,12 +200,12 @@ namespace MP.Data.Controllers } // Nessuna eccezione = successo - await tx.CommitAsync(); + //await tx.CommitAsync(); return true; } catch (Exception ex) { - await tx.RollbackAsync(); + //await tx.RollbackAsync(); // Log dettagliato errore Log.Error(ex, $"Errore in CheckCambiaStatoBatchAsync: {IdxMacchina} | {tipoInput}"); throw; // O return false; se il chiamante gestisce fallimenti diff --git a/MP.Data/Services/IOC/IIocService.cs b/MP.Data/Services/IOC/IIocService.cs index f33a9942..d723e812 100644 --- a/MP.Data/Services/IOC/IIocService.cs +++ b/MP.Data/Services/IOC/IIocService.cs @@ -7,6 +7,7 @@ namespace MP.Data.Services.IOC { public interface IIocService { + #region Public Methods /// /// Processing intera catena eventi verifica cambio stato in singola transazione e con unico DbContext @@ -29,5 +30,14 @@ namespace MP.Data.Services.IOC /// /// Task EvListMicroStatoInsertAsync(MicroStatoMacchinaModel newRecMsm, EventListModel newRecEv); + + /// + /// Aggiornamento record Microstato macchina + /// + /// + /// + Task MicroStatoMacchinaUpsertAsync(MicroStatoMacchinaModel newRec); + + #endregion Public Methods } -} +} \ No newline at end of file diff --git a/MP.Data/Services/IOC/IocService.cs b/MP.Data/Services/IOC/IocService.cs index 5ed6e26c..3427b3b2 100644 --- a/MP.Data/Services/IOC/IocService.cs +++ b/MP.Data/Services/IOC/IocService.cs @@ -10,9 +10,7 @@ namespace MP.Data.Services.IOC { public class IocService : BaseServ, IIocService { - - private readonly string _className; - private readonly IIocRepository _repo; + #region Public Constructors public IocService( IConfiguration config, @@ -23,6 +21,10 @@ namespace MP.Data.Services.IOC _repo = repo; } + #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) { @@ -39,7 +41,6 @@ namespace MP.Data.Services.IOC }); } - /// public async Task EvListMicroStatoInsertAsync(MicroStatoMacchinaModel newRecMsm, EventListModel newRecEv) { @@ -55,5 +56,30 @@ namespace MP.Data.Services.IOC return success; }); } + + /// + public async Task MicroStatoMacchinaUpsertAsync(MicroStatoMacchinaModel newRec) + { + return await TraceAsync($"{_className}.MicroStatoMacchinaUpsert", async (activity) => + { + // eseguo su repostory (DB) + string operation = "UPDATE"; + bool success = await _repo.MicroStatoMacchinaUpsertAsync(newRec); + + activity?.SetTag("db.operation", operation); + + await ClearCacheAsync($"{_redisBaseKey}:{_className}:*"); + return success; + }); + } + + #endregion Public Methods + + #region Private Fields + + private readonly string _className; + private readonly IIocRepository _repo; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/MP.IOC/Data/MpDataService.cs b/MP.IOC/Data/MpDataService.cs index d67d3f07..78a48d40 100644 --- a/MP.IOC/Data/MpDataService.cs +++ b/MP.IOC/Data/MpDataService.cs @@ -1033,8 +1033,18 @@ namespace MP.IOC.Data } else { - // solo microstato - await IocDbController.MicroStatoMacchinaUpsertAsync(newRecMsm); + if (useFactory) + { + await using var scope = _scopeFactory.CreateAsyncScope(); + var iocService = scope.ServiceProvider.GetRequiredService(); + // solo microstato + await iocService.MicroStatoMacchinaUpsertAsync(newRecMsm); + } + else + { + // solo microstato + await IocDbController.MicroStatoMacchinaUpsertAsync(newRecMsm); + } } return answ; }