diff --git a/Controllers/LinesController.cs b/Controllers/LinesController.cs index f75f317..cdfa4ea 100644 --- a/Controllers/LinesController.cs +++ b/Controllers/LinesController.cs @@ -1177,15 +1177,12 @@ namespace line_gestao_api.Controllers var lastRow = ws.LastRowUsed()?.RowNumber() ?? startRow; var tenantId = GetTenantIdFromClaims(); - var notificationsQuery = _db.Notifications - .IgnoreQueryFilters() - .Where(n => n.VigenciaLineId != null); + var vigenciaQuery = _db.VigenciaLines.IgnoreQueryFilters(); if (tenantId.HasValue) { - notificationsQuery = notificationsQuery.Where(n => n.TenantId == tenantId.Value); + vigenciaQuery = vigenciaQuery.Where(v => v.TenantId == tenantId.Value); } - await notificationsQuery.ExecuteDeleteAsync(); - await _db.VigenciaLines.ExecuteDeleteAsync(); + await vigenciaQuery.ExecuteDeleteAsync(); var buffer = new List(600); @@ -1398,10 +1395,19 @@ namespace line_gestao_api.Controllers { await _db.ControleRecebidoLines.ExecuteDeleteAsync(); + var years = new[] { 2022, 2023, 2024, 2025 }; + var importedYears = new HashSet(); + foreach (var info in GetControleRecebidosWorksheets(wb)) + { await ImportControleRecebidosSheet(info.Sheet, info.Year); + importedYears.Add(info.Year); + } + foreach (var year in years) { + if (importedYears.Contains(year)) continue; + var ws = FindControleRecebidosWorksheet(wb, year); if (ws == null) continue; @@ -1476,29 +1482,14 @@ namespace line_gestao_api.Controllers ConteudoDaNf = string.IsNullOrWhiteSpace(conteudo) ? null : conteudo.Trim(), NumeroDaLinha = numeroLinha, ValorUnit = valorUnit, - private sealed class ControleRecebidosWorksheetInfo - { - public ControleRecebidosWorksheetInfo(IXLWorksheet sheet, int year) - { - Sheet = sheet; - Year = year; - } - - public IXLWorksheet Sheet { get; } - public int Year { get; } - } - - private static IEnumerable GetControleRecebidosWorksheets(XLWorkbook wb) - yield return new ControleRecebidosWorksheetInfo(ws, year); + ValorDaNf = valorDaNf, + DataDaNf = dataDaNf, DataDoRecebimento = dataReceb, Quantidade = qtd, IsResumo = isResumo, CreatedAt = now, UpdatedAt = now - var isControleRecebidos = name.Contains("CONTROLE") && name.Contains("RECEBIDOS"); - var isRomaneio = name.Contains("ROMANEIO"); - - if (!isControleRecebidos && !isRomaneio) + }; buffer.Add(e); @@ -1520,6 +1511,37 @@ namespace line_gestao_api.Controllers } } + private sealed class ControleRecebidosWorksheetInfo + { + public ControleRecebidosWorksheetInfo(IXLWorksheet sheet, int year) + { + Sheet = sheet; + Year = year; + } + + public IXLWorksheet Sheet { get; } + public int Year { get; } + } + + private static IEnumerable GetControleRecebidosWorksheets(XLWorkbook wb) + { + var years = new[] { 2022, 2023, 2024, 2025 }; + + foreach (var ws in wb.Worksheets) + { + var name = NormalizeHeader(ws.Name); + var isControleRecebidos = name.Contains("CONTROLE") && name.Contains("RECEBIDOS"); + var isRomaneio = name.Contains("ROMANEIO"); + + if (!isControleRecebidos && !isRomaneio) continue; + + var year = years.FirstOrDefault(y => name.Contains(y.ToString())); + if (year == 0) continue; + + yield return new ControleRecebidosWorksheetInfo(ws, year); + } + } + private static IXLWorksheet? FindControleRecebidosWorksheet(XLWorkbook wb, int year) { var normalizedName = NormalizeHeader($"CONTROLE DE RECEBIDOS {year}"); diff --git a/Services/VigenciaNotificationBackgroundService.cs b/Services/VigenciaNotificationBackgroundService.cs index 9b22f1d..d97ff91 100644 --- a/Services/VigenciaNotificationBackgroundService.cs +++ b/Services/VigenciaNotificationBackgroundService.cs @@ -190,13 +190,26 @@ public class VigenciaNotificationBackgroundService : BackgroundService return; } - var dedupKeys = candidates.Select(c => c.DedupKey).Distinct().ToList(); - var existingKeys = await db.Notifications.AsNoTracking() - .Where(n => dedupKeys.Contains(n.DedupKey)) - .Select(n => n.DedupKey) + var candidateTipos = candidates.Select(c => c.Tipo).Distinct().ToList(); + var candidateDates = candidates + .Where(c => c.ReferenciaData.HasValue) + .Select(c => c.ReferenciaData!.Value.Date) + .Distinct() + .ToList(); + var existingNotifications = await db.Notifications.AsNoTracking() + .Where(n => n.TenantId == tenantId) + .Where(n => candidateTipos.Contains(n.Tipo)) + .Where(n => n.ReferenciaData != null && candidateDates.Contains(n.ReferenciaData.Value.Date)) .ToListAsync(stoppingToken); - var existingSet = new HashSet(existingKeys); + var existingSet = new HashSet(existingNotifications.Select(n => + BuildDedupKey( + n.Tipo, + n.ReferenciaData!.Value, + n.DiasParaVencer ?? 0, + n.Usuario, + n.Cliente, + n.Linha))); var toInsert = candidates .Where(c => !existingSet.Contains(c.DedupKey)) .ToList(); @@ -232,7 +245,7 @@ public class VigenciaNotificationBackgroundService : BackgroundService ReferenciaData = referenciaData, DiasParaVencer = diasParaVencer, Lida = false, - DedupKey = BuildDedupKey(tipo, vigenciaLineId, referenciaData, diasParaVencer, usuario, cliente, linha), + DedupKey = BuildDedupKey(tipo, referenciaData, diasParaVencer, usuario, cliente, linha), UserId = userId, Usuario = usuario, Cliente = cliente, @@ -244,7 +257,6 @@ public class VigenciaNotificationBackgroundService : BackgroundService private static string BuildDedupKey( string tipo, - Guid vigenciaLineId, DateTime referenciaData, int diasParaVencer, string? usuario, @@ -254,7 +266,6 @@ public class VigenciaNotificationBackgroundService : BackgroundService var parts = new[] { tipo.Trim().ToLowerInvariant(), - vigenciaLineId.ToString(), referenciaData.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture), diasParaVencer.ToString(CultureInfo.InvariantCulture), (usuario ?? string.Empty).Trim().ToLowerInvariant(),