From c1740335832aa8784e38d1a2cae371e78f6288bb Mon Sep 17 00:00:00 2001 From: Eduardo Lopes <155753879+eduardolopesx03@users.noreply.github.com> Date: Tue, 3 Feb 2026 14:33:37 -0300 Subject: [PATCH] Fix resumo reserva DDD forward fill --- Controllers/LinesController.cs | 116 +++++++++++++++++++++++++++++---- 1 file changed, 104 insertions(+), 12 deletions(-) diff --git a/Controllers/LinesController.cs b/Controllers/LinesController.cs index d9269d1..36dcf60 100644 --- a/Controllers/LinesController.cs +++ b/Controllers/LinesController.cs @@ -1814,34 +1814,78 @@ namespace line_gestao_api.Controllers private async Task ImportResumoTabela6(IXLWorksheet ws, DateTime now) { - const int headerRow = 91; - const int totalRow = 139; - var lastRow = Math.Min(totalRow - 1, ws.LastRowUsed()?.RowNumber() ?? totalRow - 1); + var lastRowUsed = ws.LastRowUsed()?.RowNumber() ?? 1; + var sectionRow = FindSectionRow(ws, "LINHAS NA RESERVA"); + if (sectionRow == 0) return; + + var headerRow = FindHeaderRowForReserva(ws, sectionRow + 1, lastRowUsed); + if (headerRow == 0) return; var map = BuildHeaderMap(ws.Row(headerRow)); var colDdd = GetCol(map, "DDD"); var colFranquiaGb = GetColAny(map, "FRANQUIA GB", "FRAQUIA GB"); - var colQtdLinhas = GetColAny(map, "QTD. DE LINHAS", "QTD DE LINHAS", "QTD. LINHAS"); + var colQtdLinhas = GetColAny(map, "QTD. DE LINHAS", "QTD DE LINHAS", "QTD. LINHAS", "QTDLINHAS"); var colTotal = GetCol(map, "TOTAL"); var buffer = new List(200); decimal? lastTotal = null; + string? lastDddValid = null; + var dataStarted = false; + var emptyRowStreak = 0; + int? totalRowIndex = null; - for (int r = headerRow + 1; r <= lastRow; r++) + for (int r = headerRow + 1; r <= lastRowUsed; r++) { var ddd = GetCellString(ws, r, colDdd); var franquia = GetCellString(ws, r, colFranquiaGb); var qtdLinhas = GetCellString(ws, r, colQtdLinhas); var total = GetCellString(ws, r, colTotal); - if (string.IsNullOrWhiteSpace(ddd) + var hasAnyValue = !(string.IsNullOrWhiteSpace(ddd) && string.IsNullOrWhiteSpace(franquia) && string.IsNullOrWhiteSpace(qtdLinhas) - && string.IsNullOrWhiteSpace(total)) + && string.IsNullOrWhiteSpace(total)); + + if (!hasAnyValue) { + if (dataStarted) + { + emptyRowStreak++; + if (emptyRowStreak >= 2) break; + } continue; } + emptyRowStreak = 0; + + var franquiaValue = TryDecimal(franquia); + var qtdValue = TryNullableInt(qtdLinhas); + var isDataRow = franquiaValue.HasValue || qtdValue.HasValue; + var dddCandidate = NullIfEmptyDigits(ddd); + + if (!string.IsNullOrWhiteSpace(dddCandidate)) + { + lastDddValid = dddCandidate; + } + + var isTotalRow = !isDataRow && !string.IsNullOrWhiteSpace(total); + if (isTotalRow) + { + totalRowIndex = r; + break; + } + + if (!isDataRow && dataStarted) + { + break; + } + + if (isDataRow) dataStarted = true; + + var resolvedDdd = isDataRow + ? (dddCandidate ?? lastDddValid) + : dddCandidate; + var totalValue = TryDecimal(total); if (!totalValue.HasValue && lastTotal.HasValue) { @@ -1854,25 +1898,36 @@ namespace line_gestao_api.Controllers buffer.Add(new ResumoReservaLine { - Ddd = string.IsNullOrWhiteSpace(ddd) ? null : ddd.Trim(), - FranquiaGb = TryDecimal(franquia), - QtdLinhas = TryNullableInt(qtdLinhas), + Ddd = string.IsNullOrWhiteSpace(resolvedDdd) ? null : resolvedDdd, + FranquiaGb = franquiaValue, + QtdLinhas = qtdValue, Total = totalValue, CreatedAt = now, UpdatedAt = now }); } + var missingDddCount = buffer.Count(x => x.Ddd == null && (x.FranquiaGb.HasValue || x.QtdLinhas.HasValue)); + if (missingDddCount > 0) + { + throw new InvalidOperationException($"Import RESUMO/RESERVA: {missingDddCount} linhas de dados ficaram sem DDD."); + } + if (buffer.Count > 0) { await _db.ResumoReservaLines.AddRangeAsync(buffer); await _db.SaveChangesAsync(); } + if (totalRowIndex == null) + { + return; + } + var totalEntity = new ResumoReservaTotal { - QtdLinhasTotal = TryNullableInt(GetCellString(ws, totalRow, colQtdLinhas)), - Total = TryDecimal(GetCellString(ws, totalRow, colTotal)), + QtdLinhasTotal = TryNullableInt(GetCellString(ws, totalRowIndex.Value, colQtdLinhas)), + Total = TryDecimal(GetCellString(ws, totalRowIndex.Value, colTotal)), CreatedAt = now, UpdatedAt = now }; @@ -1881,6 +1936,43 @@ namespace line_gestao_api.Controllers await _db.SaveChangesAsync(); } + private static int FindSectionRow(IXLWorksheet ws, string sectionName) + { + var normalizedTarget = NormalizeHeader(sectionName); + foreach (var row in ws.RowsUsed()) + { + foreach (var cell in row.CellsUsed()) + { + var key = NormalizeHeader(cell.GetString()); + if (string.IsNullOrWhiteSpace(key)) continue; + if (key.Contains(normalizedTarget)) return row.RowNumber(); + } + } + + return 0; + } + + private static int FindHeaderRowForReserva(IXLWorksheet ws, int startRow, int lastRow) + { + for (int r = startRow; r <= lastRow; r++) + { + var row = ws.Row(r); + if (!row.CellsUsed().Any()) continue; + + var map = BuildHeaderMap(row); + var hasDdd = GetCol(map, "DDD") > 0; + var hasFranquia = GetColAny(map, "FRANQUIA GB", "FRAQUIA GB") > 0; + var hasQtd = GetColAny(map, "QTD. DE LINHAS", "QTD DE LINHAS", "QTD. LINHAS", "QTDLINHAS") > 0; + + if (hasDdd && hasFranquia && hasQtd) + { + return r; + } + } + + return 0; + } + private async Task ImportControleRecebidosSheet(IXLWorksheet ws, int year) { var buffer = new List(500);