Files
EFC-DESK-V2/Classes/ConfiguracionExpediente.cs
2026-02-09 10:55:45 -07:00

114 lines
6.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EFCDesk.Classes
{
public class ConfiguracionExpediente
{
public bool IniciarExpedienteWindows { get; set; }
public string? FolderParaGenerarExpediente { get; set; }
public bool DepurarExpediente { get; set; }
public string? FolderParaDepurarExpediente { get; set; }
public bool UsarEstructuraPedimentosWinsaai { get; set; }
public bool UsarExpedienteLogistico { get; set; }
public string ServidorFTP { get; set; }
public string UsuarioFTP { get; set; }
public string PasswordFTP { get; set; }
public bool ModoPasivoFTP { get; set; }
public int PuertoFTP { get; set; }
public ConfiguracionExpediente() {
ServidorFTP = "efc.aduanasoft.com";
UsuarioFTP = "expElec";
PasswordFTP = "8lackSta8";
ModoPasivoFTP = true;
PuertoFTP = 20;
}
// Método para guardar la configuración en la base de datos
public void GuardarEnBaseDeDatos(SQLiteHelper sqliteHelper)
{
try
{
using (var connection = sqliteHelper.GetConnection())
{
connection.Open();
string insertOrUpdateQuery = @"
INSERT OR REPLACE INTO Configuracion (
Id, IniciarExpedienteWindows, FolderParaGenerarExpediente, DepurarExpediente,
FolderParaDepurarExpediente, UsarEstructuraPedimentosWinsaai, UsarExpedienteLogistico, ServidorFTP, UsuarioFTP, PasswordFTP, PuertoFTP , ModoPasivoFTP
) VALUES (
1, @IniciarExpedienteWindows, @FolderParaGenerarExpediente, @DepurarExpediente,
@FolderParaDepurarExpediente, @UsarEstructuraPedimentosWinsaai, @UsarExpedienteLogistico, @ServidorFTP, @UsuarioFTP, @PasswordFTP, @PuertoFTP, @ModoPasivoFTP
);";
using (var command = new SQLiteCommand(insertOrUpdateQuery, connection))
{
command.Parameters.AddWithValue("@IniciarExpedienteWindows", IniciarExpedienteWindows ? 1 : 0);
command.Parameters.AddWithValue("@FolderParaGenerarExpediente", FolderParaGenerarExpediente ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@DepurarExpediente", DepurarExpediente ? 1 : 0);
command.Parameters.AddWithValue("@FolderParaDepurarExpediente", FolderParaDepurarExpediente ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@UsarEstructuraPedimentosWinsaai", UsarEstructuraPedimentosWinsaai ? 1 : 0);
command.Parameters.AddWithValue("@UsarExpedienteLogistico", UsarExpedienteLogistico ? 1 : 0);
command.Parameters.AddWithValue("@ServidorFTP", ServidorFTP ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@UsuarioFTP", UsuarioFTP ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@PasswordFTP", PasswordFTP ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@PuertoFTP", PuertoFTP == 0 ? (object)DBNull.Value : PuertoFTP);
command.Parameters.AddWithValue("@ModoPasivoFTP", ModoPasivoFTP ? 1 : 0);
command.ExecuteNonQuery();
}
}
} catch(Exception ex) {
MessageBox.Show(ex.Message);
}
}
// Método para obtener la configuración desde la base de datos
public static ConfiguracionExpediente ObtenerDesdeBaseDeDatos(SQLiteHelper sqliteHelper)
{
using (var connection = sqliteHelper.GetConnection())
{
connection.Open();
string selectQuery = "SELECT * FROM Configuracion WHERE Id = 1;";
using (var command = new SQLiteCommand(selectQuery, connection))
{
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
return new ConfiguracionExpediente
{
IniciarExpedienteWindows = reader.GetInt32(reader.GetOrdinal("IniciarExpedienteWindows")) == 1,
FolderParaGenerarExpediente = reader["FolderParaGenerarExpediente"] as string,
DepurarExpediente = reader.IsDBNull(reader.GetOrdinal("DepurarExpediente")) ? false : (reader.GetInt32(reader.GetOrdinal("DepurarExpediente")) == 1),
FolderParaDepurarExpediente = reader["FolderParaDepurarExpediente"] as string,
UsarEstructuraPedimentosWinsaai = reader.IsDBNull(reader.GetOrdinal("UsarEstructuraPedimentosWinsaai")) ? false : (reader.GetInt32(reader.GetOrdinal("UsarEstructuraPedimentosWinsaai")) == 1),
UsarExpedienteLogistico = reader.IsDBNull(reader.GetOrdinal("UsarExpedienteLogistico")) ? false : (reader.GetInt32(reader.GetOrdinal("UsarExpedienteLogistico")) == 1),
ServidorFTP = reader["ServidorFTP"] as string,
UsuarioFTP = reader["UsuarioFTP"] as string,
PasswordFTP = reader["PasswordFTP"] as string,
ModoPasivoFTP = reader.IsDBNull(reader.GetOrdinal("ModoPasivoFTP")) ? false : (reader.GetInt32(reader.GetOrdinal("ModoPasivoFTP")) == 1),
PuertoFTP = reader.IsDBNull(reader.GetOrdinal("PuertoFTP")) ? 21 : reader.GetInt32(reader.GetOrdinal("PuertoFTP"))
};
}
}
}
}
// Si no hay registro, devolver una nueva instancia con valores predeterminados
return new ConfiguracionExpediente();
}
}
}