Data inicial cálculada conforme calendário CCEE. Caso ainda esteja dentro do período de coleta ainda é feito as busca dos dados do mês anterior (M-1).

This commit is contained in:
Adriano Serighelli 2025-11-19 16:10:35 -03:00
parent 41358ad446
commit ae5be9853b
3 changed files with 45 additions and 30 deletions

View File

@ -2,6 +2,6 @@ namespace Application
{
public interface ICalendarioService
{
Task<string?> ObterEventoLimiteColetaAsync(DateTime dataReferencia, CancellationToken ct = default);
Task<double?> ObterEventoLimiteColetaAsync(DateTime dataReferencia, CancellationToken ct = default);
}
}

View File

@ -8,7 +8,7 @@ namespace Infrastructure
{
private const string Url = "https://www.ccee.org.br/en/web/guest/calendario?p_p_id=com_liferay_calendar_web_portlet_CalendarPortlet_INSTANCE_urjn&p_p_lifecycle=2&p_p_cacheability=cacheLevelPage&doAsUserId=&p_p_resource_id=calendarBookings";
public async Task<string?> ObterEventoLimiteColetaAsync(DateTime dataReferencia, CancellationToken ct = default)
public async Task<double?> ObterEventoLimiteColetaAsync(DateTime dataReferencia, CancellationToken ct = default)
{
var year = dataReferencia.Year;
var month = dataReferencia.Month - 1;
@ -42,37 +42,49 @@ namespace Infrastructure
try
{
using var doc = JsonDocument.Parse(resposta);
string? found = null;
double? encontrado = null;
double? startTimeDay = null;
void SearchJson(JsonElement el)
{
if (found != null) return;
switch (el.ValueKind)
if (encontrado != null) return;
if (el.ValueKind == JsonValueKind.Object)
{
case JsonValueKind.String:
var s = el.GetString() ?? "";
if (s.Contains(alvo, StringComparison.OrdinalIgnoreCase)) found = s;
break;
case JsonValueKind.Object:
foreach (var prop in el.EnumerateObject()) SearchJson(prop.Value);
break;
case JsonValueKind.Array:
foreach (var item in el.EnumerateArray()) SearchJson(item);
break;
// Verifica se alguma propriedade contém o texto alvo
foreach (var prop in el.EnumerateObject())
{
if (prop.Value.ValueKind == JsonValueKind.String)
{
var s = prop.Value.GetString() ?? "";
if (s.Contains(alvo, StringComparison.OrdinalIgnoreCase))
{
// Encontrou o evento, extrair dados do mesmo objeto
if (el.TryGetProperty("startTimeDay", out var day))
startTimeDay = day.GetInt32();
if (startTimeDay != null)
encontrado = startTimeDay;
return;
}
}
}
foreach (var prop in el.EnumerateObject())
SearchJson(prop.Value);
}
else if (el.ValueKind == JsonValueKind.Array)
{
foreach (var item in el.EnumerateArray())
SearchJson(item);
}
}
SearchJson(doc.RootElement);
if (found != null) return found;
if (encontrado != null) return encontrado;
}
catch { }
var idx = resposta.IndexOf(alvo, StringComparison.OrdinalIgnoreCase);
if (idx >= 0)
{
var start = Math.Max(0, idx - 60);
var len = Math.Min(400, resposta.Length - start);
return resposta.Substring(start, len).Replace("\r", " ").Replace("\n", " ");
}
return null;
}
catch

View File

@ -11,14 +11,17 @@ class Program
string ACCESS_CONN_STRING = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\srv-dados\documentos\Middle\Informativo Setorial\Modelo Word\BD1_dados cadastrais e faturas.accdb;Jet OLEDB:Database Password=gds21";
string caminhoLog = $@"\\srv-dados\documentos\Back\Carteira x.x\Codigo\Erros\log_erros_{inicio:MM_dd_HH_mm}.csv";
//agosto finalizado
DateTime dataIni = new DateTime(inicio.Year, 11, 01);
// var a = dataFim == DateTime.Today;
DateTime dataFim = inicio.Date;
var calendarioService = new CalendarioService();
var evento = await calendarioService.ObterEventoLimiteColetaAsync(inicio, CancellationToken.None);
Console.WriteLine(evento != null ? $"Evento encontrado: {evento}" : "Evento não encontrado no calendário.");
//agosto finalizado
DateTime dataIni = new DateTime(inicio.Year, inicio.Month, 01);
DateTime dataFim = inicio.Date;
if (dataFim.Day <= evento + 1)
{
dataIni = dataIni.AddMonths(-1);
}
// Configuração de dependências (pode usar um container DI depois)
var postgresRepo = new PostgresRepository(PG_CONN_STRING_PROD);