- Added Microsoft.Extensions.Logging to various projects for enhanced logging capabilities. - Updated AccessDbRepository and AttachmentRepository to include logging for database operations. - Integrated logging into MailListener for better error handling and operational insights. - Modified tests to utilize mocks for ILogger to ensure logging behavior is tested. - Enhanced App.xaml.cs and MainWindow.xaml.cs to log application startup and initialization events. - Created LoggingBootstrapper to configure logging services in the WPF application. - Updated TODOs-and-Roadmap.md to reflect the addition of logging features.
195 lines
7.8 KiB
C#
195 lines
7.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.OleDb;
|
|
using System.Numerics;
|
|
using ComplianceNFs.Core.Entities;
|
|
using ComplianceNFs.Core.Ports;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace ComplianceNFs.Infrastructure.Repositories
|
|
{
|
|
// Placeholder: fill in actual SQL and mapping logic
|
|
public class AccessDbRepository(string connectionString, ILogger<AccessDbRepository>? logger = null) : IAccessDbRepository
|
|
{
|
|
private readonly string _connectionString = connectionString;
|
|
private readonly ILogger<AccessDbRepository>? _logger = logger;
|
|
|
|
private const string BuyingRecordColumns = @"
|
|
Dados_TE.Cod_TE,
|
|
Dados_TE.Cod_Smart_unidade,
|
|
Dados_TE.Mes,
|
|
Dados_TE.Hora_LO,
|
|
Dados_TE.Operacao,
|
|
Dados_TE.Tipo,
|
|
Dados_TE.Hora_NF,
|
|
Dados_TE.Tempo_NF,
|
|
Dados_TE.Contraparte_NF,
|
|
Dados_TE.Energia,
|
|
Dados_TE.Montante_NF,
|
|
Dados_TE.Preco_NF,
|
|
Dados_TE.Desconto_NF,
|
|
Dados_TE.NF_c_ICMS,
|
|
Dados_TE.NF_recebida,
|
|
Dados_TE.NF_Correta,
|
|
Dados_TE.Numero_NF,
|
|
Dados_TE.Chave_acesso,
|
|
Dados_TE.Lanc_autom,
|
|
Dados_TE.Revend_Mont,
|
|
Dados_TE.Revend_Prec,
|
|
Dados_TE.CNPJ_comp,
|
|
Dados_TE.CNPJ_vend,
|
|
Dados_TE.Mont_LO,
|
|
Dados_TE.Prec_LO,
|
|
Dados_TE.Contrato_CliqCCEE,
|
|
Dados_TE.Vig_ini_CliqCCEE,
|
|
Dados_TE.Vig_fim_CliqCCEE,
|
|
Dados_TE.Submercado,
|
|
Dados_TE.Consolidado,
|
|
Dados_TE.PerfilCliqCCEE,
|
|
Dados_TE.Perfil_Contr
|
|
";
|
|
|
|
public IEnumerable<BuyingRecord> GetByCnpj(string CNPJ_comp)
|
|
{
|
|
var results = new List<BuyingRecord>();
|
|
try
|
|
{
|
|
_logger?.LogInformation("Querying Access DB for CNPJ_comp={CNPJ_comp}", CNPJ_comp);
|
|
using (var conn = new OleDbConnection(_connectionString))
|
|
{
|
|
conn.Open();
|
|
var cmd = conn.CreateCommand();
|
|
cmd.CommandText = $@"SELECT {BuyingRecordColumns} FROM Dados_TE WHERE ((Dados_TE.CNPJ_comp)=@CNPJ_comp);";
|
|
cmd.Parameters.AddWithValue("@CNPJ_comp", CNPJ_comp);
|
|
using var reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
results.Add(MapBuyingRecord(reader));
|
|
}
|
|
}
|
|
_logger?.LogInformation("Query for CNPJ_comp={CNPJ_comp} returned {Count} records", CNPJ_comp, results.Count);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger?.LogError(ex, "Error querying Access DB for CNPJ_comp={CNPJ_comp}", CNPJ_comp);
|
|
throw;
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public IEnumerable<BuyingRecord> GetByCnpjAndMonth(string CNPJ_comp, int refMonth)
|
|
{
|
|
var results = new List<BuyingRecord>();
|
|
try
|
|
{
|
|
_logger?.LogInformation("Querying Access DB for CNPJ_comp={CNPJ_comp}, Mes={MesRef}", CNPJ_comp, refMonth);
|
|
using (var conn = new OleDbConnection(_connectionString))
|
|
{
|
|
conn.Open();
|
|
var cmd = conn.CreateCommand();
|
|
cmd.CommandText = $@"SELECT {BuyingRecordColumns} FROM Dados_TE WHERE ((Dados_TE.CNPJ_comp)=@CNPJ_comp) AND ((Dados_TE.Mes)=@MesRef);";
|
|
cmd.Parameters.AddWithValue("@CNPJ_comp", CNPJ_comp);
|
|
cmd.Parameters.AddWithValue("@MesRef", refMonth);
|
|
using var reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
results.Add(MapBuyingRecord(reader));
|
|
}
|
|
}
|
|
_logger?.LogInformation("Query for CNPJ_comp={CNPJ_comp}, Mes={MesRef} returned {Count} records", CNPJ_comp, refMonth, results.Count);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger?.LogError(ex, "Error querying Access DB for CNPJ_comp={CNPJ_comp}, Mes={MesRef}", CNPJ_comp, refMonth);
|
|
throw;
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private static BuyingRecord MapBuyingRecord(OleDbDataReader reader)
|
|
{
|
|
return new BuyingRecord
|
|
{
|
|
CodTE = ToBigInteger(reader, "Cod_TE"),
|
|
CodSmartUnidade = ToBigIntegerOrNull(reader, "Cod_Smart_unidade"),
|
|
Mes = ToInt(reader, "Mes"),
|
|
Hora_LO = ToDateTimeOrNull(reader, "Hora_LO"),
|
|
Operacao = ToStringOrNull(reader, "Operacao"),
|
|
Tipo = ToStringOrNull(reader, "Tipo"),
|
|
Hora_NF = ToDateTimeOrNull(reader, "Hora_NF"),
|
|
Tempo_NF = ToDecimalOrNull(reader, "Tempo_NF"),
|
|
Contraparte_NF = ToStringOrNull(reader, "Contraparte_NF"),
|
|
Energia = ToStringOrNull(reader, "Energia"),
|
|
Montante_NF = ToDecimalOrNull(reader, "Montante_NF"),
|
|
Preco_NF = ToDecimalOrNull(reader, "Preco_NF"),
|
|
Desconto_NF = ToDecimalOrNull(reader, "Desconto_NF"),
|
|
NF_c_ICMS = ToDecimalOrNull(reader, "NF_c_ICMS"),
|
|
NF_recebida = ToBool(reader, "NF_recebida"),
|
|
NF_Correta = ToBool(reader, "NF_Correta"),
|
|
Numero_NF = ToStringOrNull(reader, "Numero_NF"),
|
|
Chave_acesso = ToStringOrNull(reader, "Chave_acesso"),
|
|
Lanc_autom = ToBool(reader, "Lanc_autom"),
|
|
Revend_Mont = ToDecimalOrNull(reader, "Revend_Mont"),
|
|
Revend_Prec = ToDecimalOrNull(reader, "Revend_Prec"),
|
|
CnpjComp = ToStringOrNull(reader, "CNPJ_comp"),
|
|
CnpjVend = ToStringOrNull(reader, "CNPJ_vend"),
|
|
MontLO = ToDecimalOrNull(reader, "Mont_LO"),
|
|
PrecLO = ToDecimalOrNull(reader, "Prec_LO"),
|
|
Contrato_CliqCCEE = ToStringOrNull(reader, "Contrato_CliqCCEE"),
|
|
Vig_ini_CliqCCEE = ToStringOrNull(reader, "Vig_ini_CliqCCEE"),
|
|
Vig_fim_CliqCCEE = ToStringOrNull(reader, "Vig_fim_CliqCCEE"),
|
|
Submercado = ToStringOrNull(reader, "Submercado"),
|
|
Consolidado = ToStringOrNull(reader, "Consolidado"),
|
|
PerfilCliqCCEE = ToStringOrNull(reader, "PerfilCliqCCEE"),
|
|
Perfil_Contr = ToStringOrNull(reader, "Perfil_Contr"),
|
|
};
|
|
}
|
|
#region Helpers
|
|
private static BigInteger ToBigInteger(OleDbDataReader r, string col)
|
|
{
|
|
// Assumes non-null, integral
|
|
var v = r[col];
|
|
return v is BigInteger bi
|
|
? bi
|
|
: new BigInteger(Convert.ToInt64(v));
|
|
}
|
|
|
|
private static BigInteger? ToBigIntegerOrNull(OleDbDataReader r, string col)
|
|
{
|
|
if (r.IsDBNull(r.GetOrdinal(col))) return null;
|
|
return new BigInteger(Convert.ToInt64(r[col]));
|
|
}
|
|
|
|
private static int ToInt(OleDbDataReader r, string col)
|
|
{
|
|
return Convert.ToInt32(r[col]);
|
|
}
|
|
|
|
private static DateTime? ToDateTimeOrNull(OleDbDataReader r, string col)
|
|
{
|
|
if (r.IsDBNull(r.GetOrdinal(col))) return null;
|
|
return Convert.ToDateTime(r[col]);
|
|
}
|
|
|
|
private static decimal? ToDecimalOrNull(OleDbDataReader r, string col)
|
|
{
|
|
if (r.IsDBNull(r.GetOrdinal(col))) return null;
|
|
return Convert.ToDecimal(r[col]);
|
|
}
|
|
|
|
private static bool ToBool(OleDbDataReader r, string col)
|
|
{
|
|
return Convert.ToBoolean(r[col]);
|
|
}
|
|
|
|
private static string? ToStringOrNull(OleDbDataReader r, string col)
|
|
{
|
|
return r.IsDBNull(r.GetOrdinal(col))
|
|
? null
|
|
: r.GetString(r.GetOrdinal(col));
|
|
}
|
|
#endregion
|
|
}
|
|
}
|