Bozza detail upsert da testare
This commit is contained in:
@@ -68,8 +68,102 @@ namespace MP.Data.Repository.Utils
|
||||
return answ;
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<int> UpsertManyAsync(List<StatsDetailModel> listRecords, bool removeOld)
|
||||
{
|
||||
if (listRecords == null || !listRecords.Any()) return 0;
|
||||
|
||||
int answ = 0;
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
await using var tx = await dbCtx.Database.BeginTransactionAsync();
|
||||
|
||||
try
|
||||
{
|
||||
// 1. Calcolo del range temporale basato sulla lista in arrivo per ottimizzare la query SQL
|
||||
var minHour = listRecords.Min(x => x.Hour);
|
||||
var maxHour = listRecords.Max(x => x.Hour);
|
||||
|
||||
// 2. Gestione eliminazione distruttiva (se richiesto)
|
||||
if (removeOld)
|
||||
{
|
||||
// uso direttamente ExecuteDelete quando in EFCore8...
|
||||
#if false
|
||||
await dbCtx
|
||||
.DbSetStatsDet
|
||||
.Where(x => x.Hour >= startDate && x.Hour <= endDate)
|
||||
.ExecuteDeleteAsync();
|
||||
#endif
|
||||
|
||||
var itemsToRemove = await dbCtx.DbSetStatsDet
|
||||
.Where(x => x.Hour >= minHour && x.Hour <= maxHour)
|
||||
.ToListAsync();
|
||||
|
||||
if (itemsToRemove.Any())
|
||||
{
|
||||
dbCtx.DbSetStatsDet.RemoveRange(itemsToRemove);
|
||||
await dbCtx.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
// 3. LOGICA DI UPSERT (Merge basato su Destination + Type + Hour)
|
||||
|
||||
// Recuperiamo dal DB solo i record che rientrano nel range temporale della lista in arrivo
|
||||
var existingRecords = await dbCtx.DbSetStatsDet
|
||||
.Where(x => x.Hour >= minHour && x.Hour <= maxHour)
|
||||
.ToListAsync();
|
||||
|
||||
// Creiamo il dizionario di lookup con la chiave composta (Dest, Type, Hour)
|
||||
var lookup = existingRecords.ToDictionary(
|
||||
x => (x.Destination, x.Type, x.Hour),
|
||||
x => x
|
||||
);
|
||||
|
||||
foreach (var incoming in listRecords)
|
||||
{
|
||||
// Creiamo la chiave di ricerca basata sul record in arrivo
|
||||
var key = (incoming.Destination, incoming.Type, incoming.Hour);
|
||||
|
||||
if (lookup.TryGetValue(key, out var existing))
|
||||
{
|
||||
// --- CASO: UPDATE ---
|
||||
// Aggiorniamo i valori del record esistente con quelli nuovi
|
||||
existing.RequestCount = incoming.RequestCount;
|
||||
existing.AvgDuration = incoming.AvgDuration;
|
||||
existing.MinDuration = incoming.MinDuration;
|
||||
existing.MaxDuration = incoming.MaxDuration;
|
||||
existing.NoReply = incoming.NoReply;
|
||||
}
|
||||
else
|
||||
{
|
||||
// --- CASO: INSERT ---
|
||||
// Il record non esiste per questa combinazione, lo aggiungiamo
|
||||
await dbCtx.DbSetStatsDet.AddAsync(incoming);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Salvataggio finale delle modifiche (Insert + Update)
|
||||
answ = await dbCtx.SaveChangesAsync();
|
||||
|
||||
// Commit della transazione
|
||||
await tx.CommitAsync();
|
||||
|
||||
// Pulizia memoria del ChangeTracker per evitare accumuli durante sessioni lunghe
|
||||
dbCtx.ChangeTracker.Clear();
|
||||
|
||||
return answ;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await tx.RollbackAsync();
|
||||
Log.Error(ex, "Error during StatsDetail UpsertManyAsync");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
#if false
|
||||
/// <inheritdoc />
|
||||
public async Task<int> UpsertManyAsyncOrig(List<StatsDetailModel> listRecords, bool removeOld)
|
||||
{
|
||||
int answ = 0;
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
@@ -87,13 +181,7 @@ namespace MP.Data.Repository.Utils
|
||||
DateTime startDate = firstRec.Hour;
|
||||
DateTime endDate = lastRec.Hour;
|
||||
|
||||
// uso direttamente ExecuteDelete quando in EFCore8...
|
||||
#if false
|
||||
await dbCtx
|
||||
.DbSetStatsDet
|
||||
.Where(x => x.Hour >= startDate && x.Hour <= endDate)
|
||||
.ExecuteDeleteAsync();
|
||||
#endif
|
||||
|
||||
var items = await dbCtx.DbSetStatsDet
|
||||
.Where(x => x.Hour >= startDate && x.Hour <= endDate)
|
||||
.ToListAsync();
|
||||
@@ -121,7 +209,8 @@ namespace MP.Data.Repository.Utils
|
||||
await tx.RollbackAsync();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endregion Public Methods
|
||||
|
||||
|
||||
Reference in New Issue
Block a user