Otimização da função para upload de dados estimados.
Correção das demais função para realizar o upload de dados para unidades gerenciadas. Custo_unitário para períodos anteriores à migração = null. Unificação para realizar apenas um build.
This commit is contained in:
parent
7f51f02e26
commit
e8a86dc6ec
678
Program.cs
678
Program.cs
@ -20,7 +20,8 @@ using System.Threading;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
|
|
||||||
internal class Plat_integ {
|
internal class Plat_integ
|
||||||
|
{
|
||||||
//tempo medio de execucao para processamente sincrono: 15 dias: 5,4s / 30 dias:10,0s / 60 dias:16,2s
|
//tempo medio de execucao para processamente sincrono: 15 dias: 5,4s / 30 dias:10,0s / 60 dias:16,2s
|
||||||
|
|
||||||
static string PG_CONN_STRING_DEV = "Server = smart-energia-dev-pgsql.cykff7tj7mik.us-east-1.rds.amazonaws.com; Port = 5432; Database = smartimptest; User Id = postgres; Password = VfHml#Z78!%kvvNM;";
|
static string PG_CONN_STRING_DEV = "Server = smart-energia-dev-pgsql.cykff7tj7mik.us-east-1.rds.amazonaws.com; Port = 5432; Database = smartimptest; User Id = postgres; Password = VfHml#Z78!%kvvNM;";
|
||||||
@ -31,21 +32,15 @@ internal class Plat_integ {
|
|||||||
|
|
||||||
static string ACCESS_DB_LOCATION_APP = @"X:\Middle\Informativo Setorial\Modelo Word\BD_APP.accdb";
|
static string ACCESS_DB_LOCATION_APP = @"X:\Middle\Informativo Setorial\Modelo Word\BD_APP.accdb";
|
||||||
|
|
||||||
static void Main(string[] args) {
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
bool Consolidada = true; //True = econ consolidada; False = econ estimada
|
DateTime searchFrom = new DateTime((DateTime.Now.Year) - 2, 1, 1);
|
||||||
|
|
||||||
DateTime searchFromMonth = DateTime.Now.AddMonths(-24);
|
|
||||||
DateTime searchFrom = new DateTime(searchFromMonth.Year, searchFromMonth.Month, 1);
|
|
||||||
string searchFromString = searchFrom.ToString("yMM");
|
string searchFromString = searchFrom.ToString("yMM");
|
||||||
|
|
||||||
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
|
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
|
||||||
//transfere_PLD();
|
|
||||||
|
|
||||||
if (Consolidada)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Iniciando importação dos dados cadastrais");
|
Console.WriteLine("Iniciando importação dos dados cadastrais");
|
||||||
transfere_DC();
|
TransfereDC();
|
||||||
Console.WriteLine("Finalizando importação dos dados cadastrais");
|
Console.WriteLine("Finalizando importação dos dados cadastrais");
|
||||||
|
|
||||||
Console.WriteLine("Iniciando importação dos dados TE");
|
Console.WriteLine("Iniciando importação dos dados TE");
|
||||||
@ -55,82 +50,83 @@ internal class Plat_integ {
|
|||||||
Console.WriteLine("Iniciando importação dos dados de economia");
|
Console.WriteLine("Iniciando importação dos dados de economia");
|
||||||
transfere_econ(searchFromString);
|
transfere_econ(searchFromString);
|
||||||
Console.WriteLine("Finalizando importação dos dados de economia");
|
Console.WriteLine("Finalizando importação dos dados de economia");
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//args -> limit & offset
|
|
||||||
string limit = "1000"; //args[0];
|
|
||||||
string offset = "0"; // args[1];
|
|
||||||
int limitInt = Int32.Parse(limit);
|
|
||||||
int offsetInt = Int32.Parse(offset);
|
|
||||||
Console.WriteLine("Iniciando importação dos dados estimados de economia");
|
Console.WriteLine("Iniciando importação dos dados estimados de economia");
|
||||||
transfere_econ_estimado(searchFromString, limitInt, offsetInt);
|
transfere_econ_estimado(searchFromString);
|
||||||
Console.WriteLine("Finalizando importação dos dados estimados de economia");
|
Console.WriteLine("Finalizando importação dos dados estimados de economia");
|
||||||
}
|
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void transfere_econ(string searchFromString) {
|
private static void transfere_econ(string searchFromString)
|
||||||
|
{
|
||||||
string strCOM_ac, strCOM_ac_list;
|
string strCOM_ac, strCOM_ac_list;
|
||||||
|
|
||||||
long cod_econ, cod_smart_unidade;
|
long cod_econ, cod_smart_unidade;
|
||||||
string mes;
|
string mes;
|
||||||
double custo_cativo, custo_livre, economia_mensal, economia_acumulada, custo_unit;
|
double custo_cativo, custo_livre, economia_mensal, economia_acumulada, custo_unit_temp;
|
||||||
|
double? custo_unit;
|
||||||
|
|
||||||
//abre conexao com o Accesss
|
//abre conexao com o Accesss
|
||||||
OleDbConnection conn_access = new("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ACCESS_DB_LOCATION + ";Jet OLEDB:Database Password=gds21");
|
OleDbConnection conn_access = new("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ACCESS_DB_LOCATION + ";Jet OLEDB:Database Password=gds21");
|
||||||
conn_access.Open();
|
conn_access.Open();
|
||||||
|
|
||||||
//dados >= 2021-01-01
|
strCOM_ac_list = "SELECT DISTINCT dados_cadastrais.cod_smart_unidade " +
|
||||||
strCOM_ac_list = "SELECT DISTINCT cod_smart_unidade FROM Economia WHERE mes >= " + searchFromString;
|
"FROM dados_cadastrais RIGHT JOIN Economia ON dados_cadastrais.Cod_Smart_unidade = Economia.Cod_Smart_unidade " +
|
||||||
|
"WHERE unidade_gerenciada AND mes >= " + searchFromString;
|
||||||
|
|
||||||
OleDbCommand tbList = new(strCOM_ac_list, conn_access);
|
OleDbCommand tbList = new(strCOM_ac_list, conn_access);
|
||||||
OleDbDataReader reader_access_list = tbList.ExecuteReader();
|
OleDbDataReader reader_access_list = tbList.ExecuteReader();
|
||||||
|
|
||||||
//abre a conexao com o PG
|
//abre a conexao com o PG
|
||||||
NpgsqlConnection conn_pg;
|
NpgsqlConnection conn_pg;
|
||||||
if (ENVIRONMENT is "dev") {
|
if (ENVIRONMENT is "dev")
|
||||||
|
{
|
||||||
conn_pg = new(PG_CONN_STRING_DEV);
|
conn_pg = new(PG_CONN_STRING_DEV);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
conn_pg = new(PG_CONN_STRING_PROD);
|
conn_pg = new(PG_CONN_STRING_PROD);
|
||||||
}
|
}
|
||||||
conn_pg.Open();
|
conn_pg.Open();
|
||||||
|
|
||||||
Console.WriteLine("Excluindo dados existentes de economia");
|
Console.WriteLine("Excluindo dados existentes de economia");
|
||||||
string pg_strCOM = $"DELETE FROM economia WHERE mes::INTEGER >= {searchFromString}"; //TODO: ESTUDAR TROCA PARA TRUNCATE
|
string pg_strCOM = $"DELETE FROM economia"; //TODO: ESTUDAR TROCA PARA TRUNCATE
|
||||||
NpgsqlCommand pg_tcSCDE = new(pg_strCOM, conn_pg);
|
NpgsqlCommand pg_tcSCDE = new(pg_strCOM, conn_pg);
|
||||||
NpgsqlDataReader pg_reader = pg_tcSCDE.ExecuteReader();
|
NpgsqlDataReader pg_reader = pg_tcSCDE.ExecuteReader();
|
||||||
pg_reader.Close();
|
pg_reader.Close();
|
||||||
Console.WriteLine("Dados de economia excluídos");
|
Console.WriteLine("Dados de economia excluídos");
|
||||||
|
|
||||||
using (var pg_writer = conn_pg.BeginBinaryImport("COPY economia (cod_econ, cod_smart_unidade, mes, custo_cativo, custo_livre, economia_mensal, economia_acumulada, custo_unit, dad_estimado) FROM STDIN (FORMAT BINARY)")) {
|
using (var pg_writer = conn_pg.BeginBinaryImport("COPY economia (cod_econ, cod_smart_unidade, mes, custo_cativo, custo_livre, economia_mensal, economia_acumulada, custo_unit, dad_estimado) FROM STDIN (FORMAT BINARY)"))
|
||||||
|
{
|
||||||
while (reader_access_list.Read())
|
while (reader_access_list.Read())
|
||||||
{
|
{
|
||||||
string idUni = reader_access_list["cod_smart_unidade"].ToString()!;
|
string idUni = reader_access_list["cod_smart_unidade"].ToString()!;
|
||||||
strCOM_ac = $"SELECT calendario_aamm.data, IIf(a.custo_cativo IS NULL, 0.001, a.custo_cativo) AS custo_cativo, IIf(a.custo_livre IS NULL, 0.001, a.custo_livre) AS custo_livre, IIf(a.economia_mensal IS NULL, 0, a.economia_mensal) AS economia_mensal, IIf(a.economia_acumul IS NULL, 0, a.economia_acumul) AS economia_acumul, IIf(a.Custo_unit IS NULL, 0, a.Custo_unit) AS Custo_unit FROM calendario_aamm LEFT JOIN (SELECT * FROM economia WHERE cod_smart_unidade = {idUni} ORDER BY mes) AS a ON calendario_aamm.data = a.mes WHERE calendario_aamm.data >= {searchFromString} And calendario_aamm.data <= (SELECT MAX(mes) FROM economia WHERE cod_smart_unidade = {idUni})";
|
strCOM_ac = $"SELECT calendario_aamm.data, IIf(a.custo_cativo IS NULL, 0.001, a.custo_cativo) AS custo_cativo, IIf(a.custo_livre IS NULL, 0.001, a.custo_livre) AS custo_livre, IIf(a.economia_mensal IS NULL, 0, a.economia_mensal) AS economia_mensal, IIf(a.economia_acumul IS NULL, 0, a.economia_acumul) AS economia_acumul, a.Custo_unit AS Custo_unit FROM calendario_aamm LEFT JOIN (SELECT * FROM economia WHERE cod_smart_unidade = {idUni} ORDER BY mes) AS a ON calendario_aamm.data = a.mes WHERE calendario_aamm.data >= {searchFromString} And calendario_aamm.data <= (SELECT MAX(mes) FROM economia WHERE cod_smart_unidade = {idUni})";
|
||||||
|
|
||||||
OleDbCommand tbEC = new(strCOM_ac, conn_access);
|
OleDbCommand tbEC = new(strCOM_ac, conn_access);
|
||||||
OleDbDataReader reader_access = tbEC.ExecuteReader();
|
OleDbDataReader reader_access = tbEC.ExecuteReader();
|
||||||
while (reader_access.Read())
|
while (reader_access.Read())
|
||||||
{
|
{
|
||||||
//Console.WriteLine(reader_access["Data"]);
|
|
||||||
//Console.ReadKey();
|
|
||||||
mes = reader_access["data"].ToString()!;
|
mes = reader_access["data"].ToString()!;
|
||||||
|
|
||||||
double.TryParse(reader_access["Custo_cativo"].ToString(), out custo_cativo);
|
double.TryParse(reader_access["Custo_cativo"].ToString(), out custo_cativo);
|
||||||
double.TryParse(reader_access["Custo_livre"].ToString(), out custo_livre);
|
double.TryParse(reader_access["Custo_livre"].ToString(), out custo_livre);
|
||||||
double.TryParse(reader_access["Economia_mensal"].ToString(), out economia_mensal);
|
double.TryParse(reader_access["Economia_mensal"].ToString(), out economia_mensal);
|
||||||
double.TryParse(reader_access["Economia_acumul"].ToString(), out economia_acumulada);
|
double.TryParse(reader_access["Economia_acumul"].ToString(), out economia_acumulada);
|
||||||
double.TryParse(reader_access["Custo_unit"].ToString(), out custo_unit);
|
if (!double.TryParse(reader_access["Custo_unit"].ToString(), out custo_unit_temp))
|
||||||
|
{
|
||||||
|
custo_unit = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
custo_unit = custo_unit_temp;
|
||||||
|
}
|
||||||
|
|
||||||
long.TryParse(String.Concat(idUni, mes), out cod_econ);
|
long.TryParse(String.Concat(idUni, mes), out cod_econ);
|
||||||
long.TryParse(idUni, out cod_smart_unidade);
|
long.TryParse(idUni, out cod_smart_unidade);
|
||||||
|
|
||||||
//COMENTADO SO DEV -> Random rnd = new Random();
|
|
||||||
//COMENTADO SO DEV -> custo_unit = 500 + rnd.NextDouble()*200;
|
|
||||||
|
|
||||||
//strCOM_pg = "INSERT INTO economia (cod_econ, cod_smart_unidade, mes, custo_cativo, custo_livre, economia_mensal, economia_acumulada, custo_unit, dad_estimado)";
|
|
||||||
//strCOM_pg += $" VALUES ({cod_econ}, {cod_smart_unidade},'{mes}', {custo_cativo}, {custo_livre}, {economia_mensal}, {economia_acumulada}, {custo_unit}, {false});";
|
|
||||||
pg_writer.StartRow();
|
pg_writer.StartRow();
|
||||||
pg_writer.Write(cod_econ, NpgsqlTypes.NpgsqlDbType.Bigint);
|
pg_writer.Write(cod_econ, NpgsqlTypes.NpgsqlDbType.Bigint);
|
||||||
pg_writer.Write(cod_smart_unidade, NpgsqlTypes.NpgsqlDbType.Bigint);
|
pg_writer.Write(cod_smart_unidade, NpgsqlTypes.NpgsqlDbType.Bigint);
|
||||||
@ -139,18 +135,15 @@ internal class Plat_integ {
|
|||||||
pg_writer.Write(custo_livre, NpgsqlTypes.NpgsqlDbType.Numeric);
|
pg_writer.Write(custo_livre, NpgsqlTypes.NpgsqlDbType.Numeric);
|
||||||
pg_writer.Write(economia_mensal, NpgsqlTypes.NpgsqlDbType.Numeric);
|
pg_writer.Write(economia_mensal, NpgsqlTypes.NpgsqlDbType.Numeric);
|
||||||
pg_writer.Write(economia_acumulada, NpgsqlTypes.NpgsqlDbType.Numeric);
|
pg_writer.Write(economia_acumulada, NpgsqlTypes.NpgsqlDbType.Numeric);
|
||||||
pg_writer.Write(custo_unit, NpgsqlTypes.NpgsqlDbType.Numeric);
|
if (custo_unit is null)
|
||||||
|
{
|
||||||
|
pg_writer.WriteNull();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pg_writer.Write((double)custo_unit, NpgsqlTypes.NpgsqlDbType.Numeric);
|
||||||
|
}
|
||||||
pg_writer.Write(false, NpgsqlTypes.NpgsqlDbType.Boolean);
|
pg_writer.Write(false, NpgsqlTypes.NpgsqlDbType.Boolean);
|
||||||
|
|
||||||
//try {
|
|
||||||
// NpgsqlCommand tcDC = new(strCOM_pg, conn_pg);
|
|
||||||
// using (NpgsqlDataReader reader = tcDC.ExecuteReader()) {
|
|
||||||
// };
|
|
||||||
//} catch (Exception ex) {
|
|
||||||
// Console.WriteLine(ex.Message);
|
|
||||||
// Console.WriteLine(strCOM_pg);
|
|
||||||
// //Console.ReadKey();
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
reader_access.Close();
|
reader_access.Close();
|
||||||
}
|
}
|
||||||
@ -159,10 +152,10 @@ internal class Plat_integ {
|
|||||||
|
|
||||||
conn_access.Close();
|
conn_access.Close();
|
||||||
conn_pg.Close();
|
conn_pg.Close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void transfere_TE(string searchFromString) {
|
private static void transfere_TE(string searchFromString)
|
||||||
|
{
|
||||||
string strCOM_ac, strCOM_pg;
|
string strCOM_ac, strCOM_pg;
|
||||||
|
|
||||||
long cod_TE, cod_smart_unidade;
|
long cod_TE, cod_smart_unidade;
|
||||||
@ -173,31 +166,36 @@ internal class Plat_integ {
|
|||||||
OleDbConnection conn_access = new("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ACCESS_DB_LOCATION + ";Jet OLEDB:Database Password=gds21");
|
OleDbConnection conn_access = new("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ACCESS_DB_LOCATION + ";Jet OLEDB:Database Password=gds21");
|
||||||
conn_access.Open();
|
conn_access.Open();
|
||||||
|
|
||||||
//dados >= 2022-01-01
|
strCOM_ac = "SELECT Dados_TE.* " +
|
||||||
strCOM_ac = "SELECT * FROM Dados_TE WHERE mes >= " + searchFromString;
|
"FROM Dados_cadastrais RIGHT JOIN Dados_TE ON Dados_cadastrais.Cod_Smart_unidade = Dados_TE.Cod_Smart_unidade " +
|
||||||
|
"WHERE unidade_gerenciada AND mes >= " + searchFromString;
|
||||||
|
|
||||||
OleDbCommand tbTE = new(strCOM_ac, conn_access);
|
OleDbCommand tbTE = new(strCOM_ac, conn_access);
|
||||||
OleDbDataReader reader_access = tbTE.ExecuteReader();
|
OleDbDataReader reader_access = tbTE.ExecuteReader();
|
||||||
|
|
||||||
//abre a conexao com o PG
|
//abre a conexao com o PG
|
||||||
NpgsqlConnection conn_pg;
|
NpgsqlConnection conn_pg;
|
||||||
if (ENVIRONMENT is "dev") {
|
if (ENVIRONMENT is "dev")
|
||||||
|
{
|
||||||
conn_pg = new(PG_CONN_STRING_DEV);
|
conn_pg = new(PG_CONN_STRING_DEV);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
conn_pg = new(PG_CONN_STRING_PROD);
|
conn_pg = new(PG_CONN_STRING_PROD);
|
||||||
}
|
}
|
||||||
conn_pg.Open();
|
conn_pg.Open();
|
||||||
|
|
||||||
Console.WriteLine("Excluindo dados existentes de TE");
|
Console.WriteLine("Excluindo dados existentes de TE");
|
||||||
string pg_strCOM = $"DELETE FROM dados_te WHERE mes::INTEGER >= {searchFromString}"; //TODO: ESTUDAR TROCA PARA TRUNCATE
|
string pg_strCOM = $"DELETE FROM dados_te"; //TODO: ESTUDAR TROCA PARA TRUNCATE
|
||||||
NpgsqlCommand pg_tcSCDE = new(pg_strCOM, conn_pg);
|
NpgsqlCommand pg_tcSCDE = new(pg_strCOM, conn_pg);
|
||||||
NpgsqlDataReader pg_reader = pg_tcSCDE.ExecuteReader();
|
NpgsqlDataReader pg_reader = pg_tcSCDE.ExecuteReader();
|
||||||
pg_reader.Close();
|
pg_reader.Close();
|
||||||
Console.WriteLine("Dados de TE excluídos");
|
Console.WriteLine("Dados de TE excluídos");
|
||||||
|
|
||||||
using (var pg_writer = conn_pg.BeginBinaryImport("COPY dados_te (cod_te, cod_smart_unidade, mes, operacao, tipo, montante_nf, preco_nf, nf_c_icms, perfil_contr) FROM STDIN (FORMAT BINARY)")) {
|
using (var pg_writer = conn_pg.BeginBinaryImport("COPY dados_te (cod_te, cod_smart_unidade, mes, operacao, tipo, montante_nf, preco_nf, nf_c_icms, perfil_contr) FROM STDIN (FORMAT BINARY)"))
|
||||||
while (reader_access.Read()) {
|
{
|
||||||
//Console.WriteLine(reader_access["Data"]);
|
while (reader_access.Read())
|
||||||
//Console.ReadKey();
|
{
|
||||||
mes = reader_access["Mes"].ToString();
|
mes = reader_access["Mes"].ToString();
|
||||||
operacao = reader_access["Operacao"].ToString();
|
operacao = reader_access["Operacao"].ToString();
|
||||||
tipo = reader_access["Tipo"].ToString();
|
tipo = reader_access["Tipo"].ToString();
|
||||||
@ -209,9 +207,6 @@ internal class Plat_integ {
|
|||||||
long.TryParse(reader_access["Cod_TE"].ToString(), out cod_TE);
|
long.TryParse(reader_access["Cod_TE"].ToString(), out cod_TE);
|
||||||
long.TryParse(reader_access["Cod_Smart_unidade"].ToString(), out cod_smart_unidade);
|
long.TryParse(reader_access["Cod_Smart_unidade"].ToString(), out cod_smart_unidade);
|
||||||
|
|
||||||
//strCOM_pg = "INSERT INTO dados_te (cod_te, cod_smart_unidade, mes, operacao, tipo, montante_nf, preco_nf, nf_c_icms, perfil_contr)";
|
|
||||||
//strCOM_pg += $" VALUES ({cod_TE}, {cod_smart_unidade},'{mes}', '{operacao}','{tipo}',{montante_nf},{preco_nf},{nf_c_icms},'{perfil_contr}');";
|
|
||||||
|
|
||||||
pg_writer.StartRow();
|
pg_writer.StartRow();
|
||||||
pg_writer.Write(cod_TE, NpgsqlTypes.NpgsqlDbType.Bigint);
|
pg_writer.Write(cod_TE, NpgsqlTypes.NpgsqlDbType.Bigint);
|
||||||
pg_writer.Write(cod_smart_unidade, NpgsqlTypes.NpgsqlDbType.Bigint);
|
pg_writer.Write(cod_smart_unidade, NpgsqlTypes.NpgsqlDbType.Bigint);
|
||||||
@ -223,16 +218,6 @@ internal class Plat_integ {
|
|||||||
pg_writer.Write(nf_c_icms, NpgsqlTypes.NpgsqlDbType.Numeric);
|
pg_writer.Write(nf_c_icms, NpgsqlTypes.NpgsqlDbType.Numeric);
|
||||||
pg_writer.Write(perfil_contr);
|
pg_writer.Write(perfil_contr);
|
||||||
|
|
||||||
//try {
|
|
||||||
// NpgsqlCommand tcDC = new(strCOM_pg, conn_pg);
|
|
||||||
// using (NpgsqlDataReader reader = tcDC.ExecuteReader()) {
|
|
||||||
// };
|
|
||||||
//} catch (Exception ex) {
|
|
||||||
// Console.WriteLine(ex.Message);
|
|
||||||
// Console.WriteLine(strCOM_pg);
|
|
||||||
// //Console.ReadKey();
|
|
||||||
// //Console.WriteLine("Codigo unidade: {0}", cod_smart_unidade);
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
pg_writer.Complete();
|
pg_writer.Complete();
|
||||||
}
|
}
|
||||||
@ -242,94 +227,139 @@ internal class Plat_integ {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void transfere_DC() {
|
private static void TransfereDC()
|
||||||
string strCOM_ac, strCOM_pg;
|
{
|
||||||
long cod_smart_unidade, cod_smart_cliente;
|
string accessConnectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={ACCESS_DB_LOCATION};Jet OLEDB:Database Password=gds21";
|
||||||
string cliente, unidade, codigo_scde, status_empresa, status_unidade;
|
string pgConnectionString = ENVIRONMENT == "dev" ? PG_CONN_STRING_DEV : PG_CONN_STRING_PROD;
|
||||||
double demanda_p, demanda_fp, data_de_migracao;
|
|
||||||
bool isInsert;
|
|
||||||
NpgsqlCommand tcDC;
|
|
||||||
|
|
||||||
//abre conexao com o Accesss
|
try
|
||||||
OleDbConnection conn_access = new("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ACCESS_DB_LOCATION + ";Jet OLEDB:Database Password=gds21");
|
{
|
||||||
conn_access.Open();
|
using (OleDbConnection accessConn = new(accessConnectionString))
|
||||||
|
using (NpgsqlConnection pgConn = new(pgConnectionString))
|
||||||
|
{
|
||||||
|
accessConn.Open();
|
||||||
|
pgConn.Open();
|
||||||
|
|
||||||
strCOM_ac = "SELECT * FROM Dados_cadastrais WHERE Unidade_gerenciada = True";
|
string selectAccessQuery = "SELECT * FROM Dados_cadastrais";
|
||||||
OleDbCommand tbDC = new(strCOM_ac, conn_access);
|
OleDbCommand accessCommand = new(selectAccessQuery, accessConn);
|
||||||
OleDbDataReader reader_access = tbDC.ExecuteReader();
|
|
||||||
|
|
||||||
//abre a conexao com o PG
|
using (OleDbDataReader accessReader = accessCommand.ExecuteReader())
|
||||||
NpgsqlConnection conn_pg;
|
{
|
||||||
if (ENVIRONMENT is "dev") {
|
while (accessReader.Read())
|
||||||
conn_pg = new(PG_CONN_STRING_DEV);
|
{
|
||||||
} else {
|
try
|
||||||
conn_pg = new(PG_CONN_STRING_PROD);
|
{
|
||||||
}
|
// Leitura dos dados do Access
|
||||||
conn_pg.Open();
|
string cliente = accessReader["Cliente"].ToString() ?? string.Empty;
|
||||||
|
string unidade = accessReader["Unidade"].ToString() ?? string.Empty;
|
||||||
|
string codigoScde = (accessReader["Codigo_SCDE"].ToString() ?? string.Empty) + "P";
|
||||||
|
string statusEmpresa = accessReader["Status_empresa"].ToString() ?? string.Empty;
|
||||||
|
string statusUnidade = accessReader["Status_unidade"].ToString() ?? string.Empty;
|
||||||
|
bool unidadeGerenciada = Convert.ToBoolean(accessReader["Unidade_gerenciada"]);
|
||||||
|
|
||||||
//using (var pg_writer = conn_pg.BeginBinaryImport("COPY dados_cadastrais (cod_smart_unidade, cod_smart_cliente, cliente, unidade, codigo_scde, status_empresa, status_unidade,demanda_p, demanda_fp, data_de_migracao) FROM STDIN (FORMAT BINARY)")) {
|
long.TryParse(accessReader["Cod_Smart_unidade"].ToString(), out long codSmartUnidade);
|
||||||
while (reader_access.Read()) {
|
long.TryParse(accessReader["Cod_Smart_cliente"].ToString(), out long codSmartCliente);
|
||||||
//Console.WriteLine(reader_access["Data"]);
|
double.TryParse(accessReader["Demanda_P"].ToString(), out double demandaP);
|
||||||
//Console.ReadKey();
|
double.TryParse(accessReader["Demanda_FP"].ToString(), out double demandaFp);
|
||||||
cliente = reader_access["Cliente"].ToString();
|
|
||||||
unidade = reader_access["Unidade"].ToString();
|
|
||||||
//DEV ONLY -> cliente = "Cliente " + reader_access["Cod_Smart_cliente"].ToString();
|
|
||||||
//DEV ONLY -> unidade = "Unidade " + reader_access["Cod_Smart_unidade"].ToString();
|
|
||||||
codigo_scde = reader_access["Codigo_SCDE"].ToString() + "P";
|
|
||||||
status_empresa = reader_access["Status_empresa"].ToString();
|
|
||||||
status_unidade = reader_access["Status_unidade"].ToString();
|
|
||||||
|
|
||||||
double.TryParse(reader_access["Demanda_P"].ToString(), out demanda_p);
|
DateTime.TryParse(accessReader["Data_de_migracao"].ToString(), out DateTime dataMigracao);
|
||||||
double.TryParse(reader_access["Demanda_FP"].ToString(), out demanda_fp);
|
|
||||||
double.TryParse(reader_access["Data_de_migracao"].ToString(), out data_de_migracao);
|
|
||||||
|
|
||||||
long.TryParse(reader_access["Cod_Smart_unidade"].ToString(), out cod_smart_unidade);
|
if (unidadeGerenciada)
|
||||||
long.TryParse(reader_access["Cod_Smart_cliente"].ToString(), out cod_smart_cliente);
|
{
|
||||||
|
// UPSERT
|
||||||
|
string upsertQuery = @" INSERT INTO dados_cadastrais
|
||||||
|
(cod_smart_unidade, cod_smart_cliente, cliente, unidade, codigo_scde,
|
||||||
|
status_empresa, status_unidade, demanda_p, demanda_fp, data_de_migracao)
|
||||||
|
VALUES
|
||||||
|
(@CodSmartUnidade, @CodSmartCliente, @Cliente, @Unidade, @CodigoScde,
|
||||||
|
@StatusEmpresa, @StatusUnidade, @DemandaP, @DemandaFp, @DataMigracao)
|
||||||
|
ON CONFLICT (cod_smart_unidade)
|
||||||
|
DO UPDATE SET
|
||||||
|
cod_smart_cliente = EXCLUDED.cod_smart_cliente,
|
||||||
|
cliente = EXCLUDED.cliente,
|
||||||
|
unidade = EXCLUDED.unidade,
|
||||||
|
codigo_scde = EXCLUDED.codigo_scde,
|
||||||
|
status_empresa = EXCLUDED.status_empresa,
|
||||||
|
status_unidade = EXCLUDED.status_unidade,
|
||||||
|
demanda_p = EXCLUDED.demanda_p,
|
||||||
|
demanda_fp = EXCLUDED.demanda_fp,
|
||||||
|
data_de_migracao = EXCLUDED.data_de_migracao;";
|
||||||
|
|
||||||
//VERIFICAR SE É NECESSÁRIO CADASTRAR OU ATUALIZAR
|
using (NpgsqlCommand upsertCommand = new(upsertQuery, pgConn))
|
||||||
string SQL_selectDCExists = $"SELECT * FROM dados_cadastrais WHERE cod_smart_unidade = {cod_smart_unidade}";
|
{
|
||||||
isInsert = true;
|
AddParameters(upsertCommand, codSmartUnidade, codSmartCliente, cliente, unidade, codigoScde, statusEmpresa, statusUnidade, demandaP, demandaFp, dataMigracao);
|
||||||
tcDC = new(SQL_selectDCExists, conn_pg);
|
upsertCommand.ExecuteNonQuery();
|
||||||
using (NpgsqlDataReader reader = tcDC.ExecuteReader()) {
|
Console.WriteLine($"Unidade {codSmartUnidade} processada (inserida ou atualizada).");
|
||||||
while (reader.Read()) {
|
|
||||||
isInsert = false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if (isInsert) {
|
|
||||||
strCOM_pg = "INSERT INTO dados_cadastrais (cod_smart_unidade, cod_smart_cliente, cliente, unidade, codigo_scde, status_empresa, status_unidade,demanda_p, demanda_fp, data_de_migracao)";
|
|
||||||
strCOM_pg += $" VALUES ({cod_smart_unidade}, {cod_smart_cliente} ,'{cliente}', '{unidade}','{codigo_scde}', '{status_empresa}', '{status_unidade}', {demanda_p}, {demanda_fp}, {data_de_migracao});";
|
|
||||||
Console.WriteLine("Unidade {0} inserida", cod_smart_unidade);
|
|
||||||
} else {
|
|
||||||
strCOM_pg = $"UPDATE dados_cadastrais SET cod_smart_cliente = {cod_smart_cliente}, cliente = '{cliente}', unidade = '{unidade}', codigo_scde = '{codigo_scde}', status_empresa = '{status_empresa}', status_unidade = '{status_unidade}', demanda_p = {demanda_p}, demanda_fp = {demanda_fp}, data_de_migracao = {data_de_migracao} WHERE cod_smart_unidade = {cod_smart_unidade}";
|
|
||||||
Console.WriteLine("Unidade {0} atualizada", cod_smart_unidade);
|
|
||||||
}
|
|
||||||
//pg_writer.StartRow();
|
|
||||||
//pg_writer.Write(cod_smart_unidade, NpgsqlTypes.NpgsqlDbType.Bigint);
|
|
||||||
//pg_writer.Write(cod_smart_cliente, NpgsqlTypes.NpgsqlDbType.Bigint);
|
|
||||||
//pg_writer.Write(cliente);
|
|
||||||
//pg_writer.Write(unidade);
|
|
||||||
//pg_writer.Write(codigo_scde);
|
|
||||||
//pg_writer.Write(status_empresa);
|
|
||||||
//pg_writer.Write(status_unidade);
|
|
||||||
//pg_writer.Write(demanda_p, NpgsqlTypes.NpgsqlDbType.Numeric);
|
|
||||||
//pg_writer.Write(demanda_fp, NpgsqlTypes.NpgsqlDbType.Numeric);
|
|
||||||
//pg_writer.Write(data_de_migracao, NpgsqlTypes.NpgsqlDbType.Numeric);
|
|
||||||
|
|
||||||
try {
|
|
||||||
tcDC = new(strCOM_pg, conn_pg);
|
|
||||||
using (NpgsqlDataReader reader = tcDC.ExecuteReader()) {
|
|
||||||
};
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Console.WriteLine(ex.Message);
|
|
||||||
Console.WriteLine(strCOM_pg);
|
|
||||||
//Console.ReadKey();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//pg_writer.Complete();
|
else
|
||||||
//}
|
{
|
||||||
conn_access.Close();
|
// DELETE
|
||||||
conn_pg.Close();
|
string deleteQuery = "DELETE FROM dados_cadastrais WHERE cod_smart_unidade = @CodSmartUnidade";
|
||||||
|
|
||||||
|
using (NpgsqlCommand deleteCommand = new(deleteQuery, pgConn))
|
||||||
|
{
|
||||||
|
deleteCommand.Parameters.AddWithValue("@CodSmartUnidade", codSmartUnidade);
|
||||||
|
deleteCommand.ExecuteNonQuery();
|
||||||
|
Console.WriteLine($"Unidade {codSmartUnidade} deletada.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Erro ao processar registro: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
selectAccessQuery = "SELECT public_dados_cadastrais.cod_smart_unidade\r\nFROM Dados_cadastrais \r\nRIGHT JOIN public_dados_cadastrais ON Dados_cadastrais.Cod_Smart_unidade = public_dados_cadastrais.cod_smart_unidade\r\nWHERE Dados_cadastrais.Cod_Smart_unidade Is Null;";
|
||||||
|
accessCommand = new(selectAccessQuery, accessConn);
|
||||||
|
|
||||||
|
using (OleDbDataReader accessReader = accessCommand.ExecuteReader())
|
||||||
|
{
|
||||||
|
while (accessReader.Read())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Leitura dos dados do Access
|
||||||
|
long.TryParse(accessReader["Cod_Smart_unidade"].ToString(), out long codSmartUnidade);
|
||||||
|
|
||||||
|
// DELETE
|
||||||
|
string deleteQuery = "DELETE FROM dados_cadastrais WHERE cod_smart_unidade = @CodSmartUnidade";
|
||||||
|
|
||||||
|
using (NpgsqlCommand deleteCommand = new(deleteQuery, pgConn))
|
||||||
|
{
|
||||||
|
deleteCommand.Parameters.AddWithValue("@CodSmartUnidade", codSmartUnidade);
|
||||||
|
deleteCommand.ExecuteNonQuery();
|
||||||
|
Console.WriteLine($"Unidade {codSmartUnidade} deletada.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Erro ao processar registro: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Erro na operação geral: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AddParameters(NpgsqlCommand command, long codSmartUnidade, long codSmartCliente, string cliente, string unidade, string codigoScde, string statusEmpresa, string statusUnidade, double demandaP, double demandaFp, DateTime dataMigracao)
|
||||||
|
{
|
||||||
|
command.Parameters.AddWithValue("@CodSmartUnidade", codSmartUnidade);
|
||||||
|
command.Parameters.AddWithValue("@CodSmartCliente", codSmartCliente);
|
||||||
|
command.Parameters.AddWithValue("@Cliente", cliente ?? (object)DBNull.Value);
|
||||||
|
command.Parameters.AddWithValue("@Unidade", unidade ?? (object)DBNull.Value);
|
||||||
|
command.Parameters.AddWithValue("@CodigoScde", codigoScde ?? (object)DBNull.Value);
|
||||||
|
command.Parameters.AddWithValue("@StatusEmpresa", statusEmpresa ?? (object)DBNull.Value);
|
||||||
|
command.Parameters.AddWithValue("@StatusUnidade", statusUnidade ?? (object)DBNull.Value);
|
||||||
|
command.Parameters.AddWithValue("@DemandaP", demandaP);
|
||||||
|
command.Parameters.AddWithValue("@DemandaFp", demandaFp);
|
||||||
|
command.Parameters.AddWithValue("@DataMigracao", dataMigracao != default ? dataMigracao : DBNull.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
//private static void transfere_PLD() {
|
//private static void transfere_PLD() {
|
||||||
@ -525,74 +555,35 @@ internal class Plat_integ {
|
|||||||
-> -> -> dad_estimado = true
|
-> -> -> dad_estimado = true
|
||||||
*/
|
*/
|
||||||
//IMPORTANTE, DEVE RODAR APOS IMPORTACAO DE ECONOMIA CONSOLIDADA
|
//IMPORTANTE, DEVE RODAR APOS IMPORTACAO DE ECONOMIA CONSOLIDADA
|
||||||
private static void transfere_econ_estimado(string searchFromString, int limitInt, int offsetInt) {
|
private static void transfere_econ_estimado(string searchFromString)
|
||||||
|
{
|
||||||
//abre conexao com o Accesss
|
//abre conexao com o Accesss
|
||||||
OleDbConnection conn_access = new("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ACCESS_DB_LOCATION_APP + ";Jet OLEDB:Database Password=gds21");
|
OleDbConnection conn_access = new("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ACCESS_DB_LOCATION_APP + ";Jet OLEDB:Database Password=gds21");
|
||||||
conn_access.Open();
|
conn_access.Open();
|
||||||
|
|
||||||
List<string> acc_em_data_types = new List<string>();
|
List<string> acc_months = new List<string>();
|
||||||
acc_em_data_types.Add("EM_Jan");
|
acc_months.Add("Jan");
|
||||||
acc_em_data_types.Add("EM_Fev");
|
acc_months.Add("Fev");
|
||||||
acc_em_data_types.Add("EM_Mar");
|
acc_months.Add("Mar");
|
||||||
acc_em_data_types.Add("EM_Abr");
|
acc_months.Add("Abr");
|
||||||
acc_em_data_types.Add("EM_Mai");
|
acc_months.Add("Mai");
|
||||||
acc_em_data_types.Add("EM_Jun");
|
acc_months.Add("Jun");
|
||||||
acc_em_data_types.Add("EM_Jul");
|
acc_months.Add("Jul");
|
||||||
acc_em_data_types.Add("EM_Ago");
|
acc_months.Add("Ago");
|
||||||
acc_em_data_types.Add("EM_Set");
|
acc_months.Add("Set");
|
||||||
acc_em_data_types.Add("EM_Out");
|
acc_months.Add("Out");
|
||||||
acc_em_data_types.Add("EM_Nov");
|
acc_months.Add("Nov");
|
||||||
acc_em_data_types.Add("EM_Dez");
|
acc_months.Add("Dez");
|
||||||
|
|
||||||
List<string> acc_ea_data_types = new List<string>();
|
|
||||||
acc_ea_data_types.Add("EE_Jan");
|
|
||||||
acc_ea_data_types.Add("EE_Fev");
|
|
||||||
acc_ea_data_types.Add("EE_Mar");
|
|
||||||
acc_ea_data_types.Add("EE_Abr");
|
|
||||||
acc_ea_data_types.Add("EE_Mai");
|
|
||||||
acc_ea_data_types.Add("EE_Jun");
|
|
||||||
acc_ea_data_types.Add("EE_Jul");
|
|
||||||
acc_ea_data_types.Add("EE_Ago");
|
|
||||||
acc_ea_data_types.Add("EE_Set");
|
|
||||||
acc_ea_data_types.Add("EE_Out");
|
|
||||||
acc_ea_data_types.Add("EE_Nov");
|
|
||||||
acc_ea_data_types.Add("EE_Dez");
|
|
||||||
|
|
||||||
List<string> acc_cce_data_types = new List<string>();
|
|
||||||
acc_cce_data_types.Add("CCE_Jan");
|
|
||||||
acc_cce_data_types.Add("CCE_Fev");
|
|
||||||
acc_cce_data_types.Add("CCE_Mar");
|
|
||||||
acc_cce_data_types.Add("CCE_Abr");
|
|
||||||
acc_cce_data_types.Add("CCE_Mai");
|
|
||||||
acc_cce_data_types.Add("CCE_Jun");
|
|
||||||
acc_cce_data_types.Add("CCE_Jul");
|
|
||||||
acc_cce_data_types.Add("CCE_Ago");
|
|
||||||
acc_cce_data_types.Add("CCE_Set");
|
|
||||||
acc_cce_data_types.Add("CCE_Out");
|
|
||||||
acc_cce_data_types.Add("CCE_Nov");
|
|
||||||
acc_cce_data_types.Add("CCE_Dez");
|
|
||||||
|
|
||||||
List<string> acc_cle_data_types = new List<string>();
|
|
||||||
acc_cle_data_types.Add("CLE_Jan");
|
|
||||||
acc_cle_data_types.Add("CLE_Fev");
|
|
||||||
acc_cle_data_types.Add("CLE_Mar");
|
|
||||||
acc_cle_data_types.Add("CLE_Abr");
|
|
||||||
acc_cle_data_types.Add("CLE_Mai");
|
|
||||||
acc_cle_data_types.Add("CLE_Jun");
|
|
||||||
acc_cle_data_types.Add("CLE_Jul");
|
|
||||||
acc_cle_data_types.Add("CLE_Ago");
|
|
||||||
acc_cle_data_types.Add("CLE_Set");
|
|
||||||
acc_cle_data_types.Add("CLE_Out");
|
|
||||||
acc_cle_data_types.Add("CLE_Nov");
|
|
||||||
acc_cle_data_types.Add("CLE_Dez");
|
|
||||||
|
|
||||||
//abre a conexao com o PG
|
|
||||||
NpgsqlConnection conn_pg, conn_writer, conn_check_last;
|
NpgsqlConnection conn_pg, conn_writer, conn_check_last;
|
||||||
if (ENVIRONMENT is "dev") {
|
if (ENVIRONMENT is "dev")
|
||||||
|
{
|
||||||
conn_pg = new(PG_CONN_STRING_DEV);
|
conn_pg = new(PG_CONN_STRING_DEV);
|
||||||
conn_writer = new(PG_CONN_STRING_DEV);
|
conn_writer = new(PG_CONN_STRING_DEV);
|
||||||
conn_check_last = new(PG_CONN_STRING_DEV);
|
conn_check_last = new(PG_CONN_STRING_DEV);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
conn_pg = new(PG_CONN_STRING_PROD);
|
conn_pg = new(PG_CONN_STRING_PROD);
|
||||||
conn_writer = new(PG_CONN_STRING_PROD);
|
conn_writer = new(PG_CONN_STRING_PROD);
|
||||||
conn_check_last = new(PG_CONN_STRING_PROD);
|
conn_check_last = new(PG_CONN_STRING_PROD);
|
||||||
@ -601,149 +592,78 @@ internal class Plat_integ {
|
|||||||
conn_writer.Open();
|
conn_writer.Open();
|
||||||
conn_check_last.Open();
|
conn_check_last.Open();
|
||||||
|
|
||||||
//só executa na leitura inicial
|
|
||||||
if (offsetInt == 0) {
|
|
||||||
Console.WriteLine("Excluindo dados existentes de economia estimada");
|
Console.WriteLine("Excluindo dados existentes de economia estimada");
|
||||||
string pg_strCOM_delete = $"DELETE FROM economia WHERE dad_estimado = true AND mes::INTEGER >= {searchFromString}"; //TODO: ESTUDAR TROCA PARA TRUNCATE
|
string pg_strCOM_delete = $"DELETE FROM economia WHERE dad_estimado = true AND mes::INTEGER >= {searchFromString}";
|
||||||
NpgsqlCommand pg_tcSCDE_delete = new(pg_strCOM_delete, conn_pg);
|
NpgsqlCommand pg_tcSCDE_delete = new(pg_strCOM_delete, conn_pg);
|
||||||
NpgsqlDataReader pg_reader_delete = pg_tcSCDE_delete.ExecuteReader();
|
NpgsqlDataReader pg_reader_delete = pg_tcSCDE_delete.ExecuteReader();
|
||||||
pg_reader_delete.Close();
|
pg_reader_delete.Close();
|
||||||
Console.WriteLine("Dados de economia estimada excluídos");
|
Console.WriteLine("Dados de economia estimada excluídos");
|
||||||
}
|
|
||||||
|
|
||||||
string pgsql_select_for_each_client_unity = "SELECT cod_smart_cliente, cod_smart_unidade, unidade FROM dados_cadastrais;";
|
string pgsql_select_units = "SELECT dc.cod_smart_cliente, dc.cod_smart_unidade, dc.unidade, e.max_month " +
|
||||||
NpgsqlCommand pg_tcSCDE = new(pgsql_select_for_each_client_unity, conn_pg);
|
"FROM dados_cadastrais dc " +
|
||||||
NpgsqlDataReader pg_reader_unities = pg_tcSCDE.ExecuteReader();
|
"LEFT JOIN ( " +
|
||||||
//Para cada unidade
|
" SELECT cod_smart_unidade, TO_CHAR(MAX(TO_DATE(mes, 'YYMM')), 'MM')::INTEGER AS max_month " +
|
||||||
|
" FROM economia" +
|
||||||
|
" WHERE dad_estimado = false " +
|
||||||
|
" GROUP BY cod_smart_unidade" +
|
||||||
|
") e ON dc.cod_smart_unidade = e.cod_smart_unidade;";
|
||||||
|
NpgsqlCommand pg_tc_units = new(pgsql_select_units, conn_pg);
|
||||||
|
NpgsqlDataReader pg_reader_units = pg_tc_units.ExecuteReader();
|
||||||
|
|
||||||
string unidade, cod_smart_unidade, cod_smart_cliente;
|
string unidade, cod_smart_unidade, cod_smart_cliente;
|
||||||
|
int last_month;
|
||||||
int from, to, i;
|
int i = 0;
|
||||||
i = 0;
|
|
||||||
from = offsetInt;
|
|
||||||
to = offsetInt + limitInt - 1;
|
|
||||||
// limit 100, offset 0 -> registros de 0 a 99
|
|
||||||
// limit 100, offset 100 -> registros de 100 a 199
|
|
||||||
// limit 100, offset 200 -> registros de 200 a 299
|
|
||||||
// limit 100, offset 300 -> registros de 300 a 399
|
|
||||||
// limit 100, offset 400 -> registros de 400 a 499
|
|
||||||
// limit 100, offset 500 -> registros de 500 a 599
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var pg_writer = conn_writer.BeginBinaryImport("COPY economia (cod_econ, cod_smart_unidade, mes, custo_cativo, custo_livre, economia_mensal, economia_acumulada, custo_unit, dad_estimado) FROM STDIN (FORMAT BINARY)"))
|
using (var pg_writer = conn_writer.BeginBinaryImport("COPY economia (cod_econ, cod_smart_unidade, mes, custo_cativo, custo_livre, economia_mensal, economia_acumulada, custo_unit, dad_estimado) FROM STDIN (FORMAT BINARY)"))
|
||||||
{
|
{
|
||||||
while (pg_reader_unities.Read())
|
while (pg_reader_units.Read())
|
||||||
{
|
{
|
||||||
|
unidade = pg_reader_units["unidade"].ToString();
|
||||||
|
cod_smart_unidade = pg_reader_units["cod_smart_unidade"].ToString();
|
||||||
|
cod_smart_cliente = pg_reader_units["cod_smart_cliente"].ToString();
|
||||||
|
if (!Int32.TryParse(pg_reader_units["max_month"].ToString(), out last_month))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
if (i < (from + 1) || i > (to + 1))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
unidade = pg_reader_unities["unidade"].ToString();
|
|
||||||
cod_smart_unidade = pg_reader_unities["cod_smart_unidade"].ToString();
|
|
||||||
cod_smart_cliente = pg_reader_unities["cod_smart_cliente"].ToString();
|
|
||||||
|
|
||||||
//Carrega último mês que os dados consolidados estão disponíveis atualmente
|
|
||||||
string pgsql_select_max_year_month_unity_real_data = $"SELECT TO_CHAR(max(TO_DATE(mes, 'YYMM')), 'MM')::INTEGER AS max_month FROM economia WHERE cod_smart_unidade = {cod_smart_unidade} AND dad_estimado = false;";
|
|
||||||
NpgsqlCommand pg_command_pgsql_select_max_year_month_unity_real_data = new(pgsql_select_max_year_month_unity_real_data, conn_check_last);
|
|
||||||
NpgsqlDataReader pg_reader_month_unity = pg_command_pgsql_select_max_year_month_unity_real_data.ExecuteReader();
|
|
||||||
int last_month = 1;
|
|
||||||
string last_month_string = "";
|
|
||||||
while (pg_reader_month_unity.Read())
|
|
||||||
{
|
|
||||||
if (pg_reader_month_unity["max_month"] != null)
|
|
||||||
{
|
|
||||||
last_month_string = pg_reader_month_unity["max_month"].ToString();
|
|
||||||
if (last_month_string == "")
|
|
||||||
{
|
|
||||||
last_month = -1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
last_month = Int32.Parse(last_month_string);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pg_reader_month_unity.Close();
|
|
||||||
|
|
||||||
if (last_month == -1)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (last_month == null)
|
|
||||||
{
|
|
||||||
last_month = 1;
|
|
||||||
}
|
|
||||||
if (last_month < 12)
|
if (last_month < 12)
|
||||||
{
|
{
|
||||||
//BASE SELECT
|
//BASE SELECT
|
||||||
Console.WriteLine("Lendo dados do cliente {0} unidade {1} - Operação {2}", cod_smart_cliente, cod_smart_unidade, i);
|
Console.WriteLine("Lendo dados do cliente {0} unidade {1} - Operação {2}", cod_smart_cliente, cod_smart_unidade, i);
|
||||||
string accessdb_select_max_year_month_unity_real_data_base = $"SELECT Valor FROM Dados_APP WHERE Ult_ver <> 0 AND Cod_Smart_cliente = {cod_smart_cliente} AND Unidade = '{unidade}' AND Tipo_dado = 'REPLACEDATATYPE'";
|
string accessdb_select_last_data_month = $"TRANSFORM SUM(Valor) AS SomaDeValor " +
|
||||||
|
$"SELECT Cod_Smart_cliente, Unidade, Mes " +
|
||||||
|
$"FROM Dados_APP " +
|
||||||
|
$"WHERE Mes = (SELECT MAX(Mes)" +
|
||||||
|
$" FROM Dados_APP" +
|
||||||
|
$" WHERE Cod_Smart_cliente = {cod_smart_cliente} AND Unidade = '{unidade}')" +
|
||||||
|
$" AND Cod_Smart_cliente = {cod_smart_cliente} AND Unidade = '{unidade}' AND (Tipo_dado = 'EM_REPLACEMONTH' OR Tipo_dado = 'EE_REPLACEMONTH' OR Tipo_dado = 'CCE_REPLACEMONTH' OR Tipo_dado = 'CLE_REPLACEMONTH') " +
|
||||||
|
$"GROUP BY Cod_Smart_cliente, Unidade, Mes " +
|
||||||
|
$"PIVOT Tipo_dado";
|
||||||
|
|
||||||
//PARA CADA MÊS ATÉ O FINAL DO ANO
|
//PARA CADA MÊS ATÉ O FINAL DO ANO
|
||||||
for (int month = last_month; month < 12; month++)
|
for (int month = last_month; month < 12; month++)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Buscando dados de economia a partir do mês {0}", month + 1);
|
Console.WriteLine("Buscando dados de economia a partir do mês {0}", month + 1);
|
||||||
//CARREGA ECONOMIA ACUMULADA
|
|
||||||
//Console.WriteLine("Iniciando EA do mês {0}", month + 1);
|
string accessdb_select = accessdb_select_last_data_month.Replace("REPLACEMONTH", acc_months[month]);
|
||||||
string accessdb_select_ea = accessdb_select_max_year_month_unity_real_data_base.Replace("REPLACEDATATYPE", acc_ea_data_types[month]);
|
OleDbCommand access_select = new(accessdb_select, conn_access);
|
||||||
OleDbCommand access_select_ea = new(accessdb_select_ea, conn_access);
|
OleDbDataReader reader_access = access_select.ExecuteReader();
|
||||||
OleDbDataReader reader_access_ea = access_select_ea.ExecuteReader();
|
|
||||||
string value_ea = "0";
|
string value_ea = "0";
|
||||||
while (reader_access_ea.Read())
|
|
||||||
{
|
|
||||||
if (reader_access_ea["Valor"] != null)
|
|
||||||
{
|
|
||||||
value_ea = reader_access_ea["Valor"].ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
reader_access_ea.Close();
|
|
||||||
//Console.WriteLine("Finalizando EA do mês {0}", month + 1);
|
|
||||||
//CARREGA ECONOMIA MENSAL
|
|
||||||
//Console.WriteLine("Iniciando EM do mês {0}", month + 1);
|
|
||||||
string accessdb_select_em = accessdb_select_max_year_month_unity_real_data_base.Replace("REPLACEDATATYPE", acc_em_data_types[month]);
|
|
||||||
OleDbCommand access_select_em = new(accessdb_select_em, conn_access);
|
|
||||||
OleDbDataReader reader_access_em = access_select_em.ExecuteReader();
|
|
||||||
string value_em = "0";
|
string value_em = "0";
|
||||||
while (reader_access_em.Read())
|
|
||||||
{
|
|
||||||
if (reader_access_em["Valor"] != null)
|
|
||||||
{
|
|
||||||
value_em = reader_access_em["Valor"].ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
reader_access_em.Close();
|
|
||||||
//Console.WriteLine("Finalizando EM do mês {0}", month + 1);
|
|
||||||
//CARREGA CUSTO LIVRE ESTIMADO
|
|
||||||
//Console.WriteLine("Iniciando CLE do mês {0}", month + 1);
|
|
||||||
string accessdb_select_cle = accessdb_select_max_year_month_unity_real_data_base.Replace("REPLACEDATATYPE", acc_cle_data_types[month]);
|
|
||||||
OleDbCommand access_select_cle = new(accessdb_select_cle, conn_access);
|
|
||||||
OleDbDataReader reader_access_cle = access_select_cle.ExecuteReader();
|
|
||||||
string value_cle = "0";
|
string value_cle = "0";
|
||||||
while (reader_access_cle.Read())
|
|
||||||
{
|
|
||||||
if (reader_access_cle["Valor"] != null)
|
|
||||||
{
|
|
||||||
value_cle = reader_access_cle["Valor"].ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
reader_access_cle.Close();
|
|
||||||
//Console.WriteLine("Finalizando CLE do mês {0}", month + 1);
|
|
||||||
//CARREGA CUSTO CATIVO ESTIMADO
|
|
||||||
//Console.WriteLine("Iniciando CCE do mês {0}", month + 1);
|
|
||||||
string accessdb_select_cce = accessdb_select_max_year_month_unity_real_data_base.Replace("REPLACEDATATYPE", acc_cce_data_types[month]);
|
|
||||||
OleDbCommand access_select_cce = new(accessdb_select_cce, conn_access);
|
|
||||||
OleDbDataReader reader_access_cce = access_select_cce.ExecuteReader();
|
|
||||||
string value_cce = "0";
|
string value_cce = "0";
|
||||||
while (reader_access_cce.Read())
|
|
||||||
|
while (reader_access.Read())
|
||||||
{
|
{
|
||||||
if (reader_access_cce["Valor"] != null)
|
value_ea = reader_access["EE_" + acc_months[month]].ToString();
|
||||||
{
|
value_em = reader_access["EM_" + acc_months[month]].ToString();
|
||||||
value_cce = reader_access_cce["Valor"].ToString();
|
value_cle = reader_access["CLE_" + acc_months[month]].ToString();
|
||||||
|
value_cce = reader_access["CCE_" + acc_months[month]].ToString();
|
||||||
}
|
}
|
||||||
}
|
reader_access.Close();
|
||||||
reader_access_cce.Close();
|
|
||||||
//Console.WriteLine("Finalizando CCE do mês {0}", month + 1);
|
|
||||||
|
|
||||||
DateTime currentMonthInserted = new DateTime(DateTime.Now.Year, month + 1, 1);
|
DateTime currentMonthInserted = new DateTime(DateTime.Now.Year, month + 1, 1);
|
||||||
string currentMonthString = currentMonthInserted.ToString("yMM");
|
string currentMonthString = currentMonthInserted.ToString("yMM");
|
||||||
@ -764,103 +684,41 @@ internal class Plat_integ {
|
|||||||
pg_writer.Write(ea, NpgsqlTypes.NpgsqlDbType.Numeric);
|
pg_writer.Write(ea, NpgsqlTypes.NpgsqlDbType.Numeric);
|
||||||
pg_writer.Write(0, NpgsqlTypes.NpgsqlDbType.Numeric);
|
pg_writer.Write(0, NpgsqlTypes.NpgsqlDbType.Numeric);
|
||||||
pg_writer.Write(true, NpgsqlTypes.NpgsqlDbType.Boolean);
|
pg_writer.Write(true, NpgsqlTypes.NpgsqlDbType.Boolean);
|
||||||
//Console.WriteLine("Dados do mês {0} adicionados para o Writer", month + 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//ECONOMIA PROXIMOS 5 ANOS
|
//ECONOMIA PROXIMOS 5 ANOS
|
||||||
//Carrega último ano que os dados consolidados estão disponíveis atualmente
|
string accessdb_select_last_data_year = $"TRANSFORM SUM(Valor) AS SomaDeValor " +
|
||||||
string pgsql_select_max_year_unity_real_data = $"SELECT TO_CHAR(max(TO_DATE(mes, 'YYMM')), 'yy')::INTEGER AS max_year FROM economia WHERE cod_smart_unidade = {cod_smart_unidade} AND dad_estimado = false;";
|
$"SELECT Cod_Smart_cliente, Unidade, Mes " +
|
||||||
NpgsqlCommand pg_command_pgsql_select_max_year_unity_real_data = new(pgsql_select_max_year_unity_real_data, conn_check_last);
|
$"FROM Dados_APP " +
|
||||||
NpgsqlDataReader pg_reader_year_unity = pg_command_pgsql_select_max_year_unity_real_data.ExecuteReader();
|
$"WHERE Mes = (SELECT MAX(Mes)" +
|
||||||
int last_year = 1;
|
$" FROM Dados_APP" +
|
||||||
string last_year_string = "";
|
$" WHERE Cod_Smart_cliente = {cod_smart_cliente} AND Unidade = '{unidade}')" +
|
||||||
while (pg_reader_year_unity.Read())
|
$" AND Cod_Smart_cliente = {cod_smart_cliente} AND Unidade = '{unidade}' AND (Tipo_dado = 'EM_REPLACEYEAR' OR Tipo_dado = 'EE_REPLACEYEAR' OR Tipo_dado = 'CCE_REPLACEYEAR' OR Tipo_dado = 'CLE_REPLACEYEAR') " +
|
||||||
{
|
$"GROUP BY Cod_Smart_cliente, Unidade, Mes " +
|
||||||
if (pg_reader_year_unity["max_year"] != null)
|
$"PIVOT Tipo_dado";
|
||||||
{
|
|
||||||
last_year_string = pg_reader_year_unity["max_year"].ToString();
|
|
||||||
if (last_year_string == "")
|
|
||||||
{
|
|
||||||
last_year = -1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
last_year = Int32.Parse(last_year_string);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pg_reader_year_unity.Close();
|
|
||||||
Console.WriteLine("Lendo dados do cliente {0} unidade {1} - Operação {2}", cod_smart_cliente, cod_smart_unidade, i);
|
|
||||||
string accessdb_select_max_year_unity_real_data_base = $"SELECT Valor FROM Dados_APP WHERE Ult_ver <> 0 AND Cod_Smart_cliente = {cod_smart_cliente} AND Unidade = '{unidade}' AND Tipo_dado = 'REPLACEDATATYPE'";
|
|
||||||
for (int year = 1; year < 6; year++)
|
for (int year = 1; year < 6; year++)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Buscando dados de economia a partir do ano {0}", year);
|
Console.WriteLine("Buscando dados de economia a partir do ano {0}", year);
|
||||||
//CARREGA ECONOMIA ACUMULADA
|
//CARREGA ECONOMIA ACUMULADA
|
||||||
//Console.WriteLine("Iniciando EA do mês {0}", month + 1);
|
//Console.WriteLine("Iniciando EA do mês {0}", month + 1);
|
||||||
string accessdb_select_ea = accessdb_select_max_year_unity_real_data_base.Replace("REPLACEDATATYPE", "EE_Ano" + year);
|
string accessdb_select = accessdb_select_last_data_year.Replace("REPLACEYEAR", "Ano" + year);
|
||||||
OleDbCommand access_select_ea = new(accessdb_select_ea, conn_access);
|
OleDbCommand access_select = new(accessdb_select, conn_access);
|
||||||
OleDbDataReader reader_access_ea = access_select_ea.ExecuteReader();
|
OleDbDataReader reader_access = access_select.ExecuteReader();
|
||||||
|
|
||||||
string value_ea = "0";
|
string value_ea = "0";
|
||||||
while (reader_access_ea.Read())
|
|
||||||
{
|
|
||||||
if (reader_access_ea["Valor"] != null)
|
|
||||||
{
|
|
||||||
value_ea = reader_access_ea["Valor"].ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
reader_access_ea.Close();
|
|
||||||
//Console.WriteLine("Finalizando EA do mês {0}", month + 1);
|
|
||||||
//CARREGA ECONOMIA MENSAL
|
|
||||||
//Console.WriteLine("Iniciando EM do mês {0}", month + 1);
|
|
||||||
string accessdb_select_em = accessdb_select_max_year_unity_real_data_base.Replace("REPLACEDATATYPE", "EM_Ano" + year);
|
|
||||||
OleDbCommand access_select_em = new(accessdb_select_em, conn_access);
|
|
||||||
OleDbDataReader reader_access_em = access_select_em.ExecuteReader();
|
|
||||||
string value_em = "0";
|
string value_em = "0";
|
||||||
while (reader_access_em.Read())
|
|
||||||
{
|
|
||||||
if (reader_access_em["Valor"] != null)
|
|
||||||
{
|
|
||||||
value_em = reader_access_em["Valor"].ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
reader_access_em.Close();
|
|
||||||
//Console.WriteLine("Finalizando EM do mês {0}", month + 1);
|
|
||||||
//CARREGA CUSTO LIVRE ESTIMADO
|
|
||||||
//Console.WriteLine("Iniciando CLE do mês {0}", month + 1);
|
|
||||||
string accessdb_select_cle = accessdb_select_max_year_unity_real_data_base.Replace("REPLACEDATATYPE", "CLE_Ano" + year);
|
|
||||||
OleDbCommand access_select_cle = new(accessdb_select_cle, conn_access);
|
|
||||||
OleDbDataReader reader_access_cle = access_select_cle.ExecuteReader();
|
|
||||||
string value_cle = "0";
|
string value_cle = "0";
|
||||||
while (reader_access_cle.Read())
|
|
||||||
{
|
|
||||||
if (reader_access_cle["Valor"] != null)
|
|
||||||
{
|
|
||||||
value_cle = reader_access_cle["Valor"].ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
reader_access_cle.Close();
|
|
||||||
//Console.WriteLine("Finalizando CLE do mês {0}", month + 1);
|
|
||||||
//CARREGA CUSTO CATIVO ESTIMADO
|
|
||||||
//Console.WriteLine("Iniciando CCE do mês {0}", month + 1);
|
|
||||||
string replaceCCE = "CCE_Ano" + year;
|
|
||||||
if (year == 1)
|
|
||||||
{
|
|
||||||
//replaceCCE = "CCE_2301";
|
|
||||||
replaceCCE = "CCE_Ano" + year;
|
|
||||||
}
|
|
||||||
string accessdb_select_cce = accessdb_select_max_year_unity_real_data_base.Replace("REPLACEDATATYPE", replaceCCE);
|
|
||||||
OleDbCommand access_select_cce = new(accessdb_select_cce, conn_access);
|
|
||||||
OleDbDataReader reader_access_cce = access_select_cce.ExecuteReader();
|
|
||||||
string value_cce = "0";
|
string value_cce = "0";
|
||||||
while (reader_access_cce.Read())
|
|
||||||
|
while (reader_access.Read())
|
||||||
{
|
{
|
||||||
if (reader_access_cce["Valor"] != null)
|
value_ea = reader_access["EE_" + "Ano" + year].ToString();
|
||||||
{
|
value_em = reader_access["EM_" + "Ano" + year].ToString();
|
||||||
value_cce = reader_access_cce["Valor"].ToString();
|
value_cle = reader_access["CLE_" + "Ano" + year].ToString();
|
||||||
|
value_cce = reader_access["CCE_" + "Ano" + year].ToString();
|
||||||
}
|
}
|
||||||
}
|
reader_access.Close();
|
||||||
reader_access_cce.Close();
|
|
||||||
//Console.WriteLine("Finalizando CCE do mês {0}", month + 1);
|
|
||||||
|
|
||||||
DateTime currentMonthInserted = new DateTime((DateTime.Now.Year + year), 12, 1);
|
DateTime currentMonthInserted = new DateTime((DateTime.Now.Year + year), 12, 1);
|
||||||
string currentMonthString = currentMonthInserted.ToString("yMM");
|
string currentMonthString = currentMonthInserted.ToString("yMM");
|
||||||
@ -883,7 +741,6 @@ internal class Plat_integ {
|
|||||||
pg_writer.Write(true, NpgsqlTypes.NpgsqlDbType.Boolean);
|
pg_writer.Write(true, NpgsqlTypes.NpgsqlDbType.Boolean);
|
||||||
//Console.WriteLine("Dados do mês {0} adicionados para o Writer", month + 1);
|
//Console.WriteLine("Dados do mês {0} adicionados para o Writer", month + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
var a = pg_writer.Complete();
|
var a = pg_writer.Complete();
|
||||||
}
|
}
|
||||||
@ -892,12 +749,11 @@ internal class Plat_integ {
|
|||||||
{
|
{
|
||||||
Console.WriteLine(e.Message);
|
Console.WriteLine(e.Message);
|
||||||
}
|
}
|
||||||
pg_reader_unities.Close();
|
pg_reader_units.Close();
|
||||||
|
|
||||||
conn_access.Close();
|
conn_access.Close();
|
||||||
conn_pg.Close();
|
conn_pg.Close();
|
||||||
conn_writer.Close();
|
conn_writer.Close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user