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