generated from Integry/Template_NetMauiBlazorHybrid
300 lines
9.9 KiB
C#
300 lines
9.9 KiB
C#
using ConSegna.Shared.Core.Dto;
|
|
using Microsoft.Extensions.Logging;
|
|
using SQLite;
|
|
|
|
namespace ConSegna.Maui.Core.Services;
|
|
|
|
public class LocalDbService
|
|
{
|
|
private const string DB_NAME = "consegna_db.db3";
|
|
private const string HISTORY_DB_NAME = "consegna_history_db.db3";
|
|
private readonly SQLiteAsyncConnection _connection;
|
|
private readonly SQLiteAsyncConnection _connectionHistory;
|
|
|
|
private readonly ILogger<LocalDbService> _logger;
|
|
|
|
public LocalDbService(ILogger<LocalDbService> logger)
|
|
{
|
|
_connection = new SQLiteAsyncConnection(Path.Combine(FileSystem.AppDataDirectory, DB_NAME));
|
|
_connectionHistory = new SQLiteAsyncConnection(Path.Combine(FileSystem.AppDataDirectory, HISTORY_DB_NAME));
|
|
|
|
// Creazione tabelle database principale
|
|
_connection.CreateTableAsync<DatiClientiDTO>();
|
|
_connection.CreateTableAsync<DatiConsegneDTO>();
|
|
_connection.CreateTableAsync<SospesiClienteDTO>();
|
|
_connection.CreateTableAsync<DettaglioRigheDTO>();
|
|
_connection.CreateTableAsync<PaymentDataDTO>();
|
|
|
|
// Creazione tabelle database storico
|
|
_connectionHistory.CreateTableAsync<DatiClientiDTO>();
|
|
_connectionHistory.CreateTableAsync<DatiConsegneDTO>();
|
|
_connectionHistory.CreateTableAsync<SospesiClienteDTO>();
|
|
_connectionHistory.CreateTableAsync<DettaglioRigheDTO>();
|
|
_connectionHistory.CreateTableAsync<PaymentDataDTO>();
|
|
|
|
_logger = logger;
|
|
}
|
|
|
|
private async Task ResetDatabase()
|
|
{
|
|
try
|
|
{
|
|
var timestamp = DateTime.Now;
|
|
|
|
// Recupera tutti i dati prima del reset
|
|
var clienti = await _connection.Table<DatiClientiDTO>().ToListAsync();
|
|
var consegne = await _connection.Table<DatiConsegneDTO>().ToListAsync();
|
|
var sospesi = await _connection.Table<SospesiClienteDTO>().ToListAsync();
|
|
var paymentData = await _connection.Table<PaymentDataDTO>().ToListAsync();
|
|
var dettagli = await _connection.Table<DettaglioRigheDTO>().ToListAsync();
|
|
|
|
// Imposta LastUpdate e resetta gli ID per l'inserimento nel database storico
|
|
clienti.ForEach(c =>
|
|
{
|
|
c.LastUpdate = timestamp;
|
|
c.Id = 0; // Resetta l'ID per permettere l'auto-incremento
|
|
});
|
|
consegne.ForEach(c =>
|
|
{
|
|
c.LastUpdate = timestamp;
|
|
c.Id = 0;
|
|
});
|
|
sospesi.ForEach(s =>
|
|
{
|
|
s.LastUpdate = timestamp;
|
|
s.Id = 0;
|
|
});
|
|
dettagli.ForEach(d =>
|
|
{
|
|
d.LastUpdate = timestamp;
|
|
d.Id = 0;
|
|
});
|
|
paymentData.ForEach(d =>
|
|
{
|
|
d.LastUpdate = timestamp;
|
|
d.Id = 0;
|
|
});
|
|
|
|
// Salva i dati nel database storico
|
|
foreach (var cliente in clienti)
|
|
{
|
|
await _connectionHistory.InsertAsync(cliente);
|
|
}
|
|
|
|
foreach (var consegna in consegne)
|
|
{
|
|
await _connectionHistory.InsertAsync(consegna);
|
|
}
|
|
|
|
foreach (var sospeso in sospesi)
|
|
{
|
|
await _connectionHistory.InsertAsync(sospeso);
|
|
}
|
|
|
|
foreach (var dettaglio in dettagli)
|
|
{
|
|
await _connectionHistory.InsertAsync(dettaglio);
|
|
}
|
|
|
|
foreach (var data in paymentData)
|
|
{
|
|
await _connectionHistory.InsertAsync(data);
|
|
}
|
|
|
|
// Elimina le tabelle del database principale
|
|
await _connection.ExecuteAsync("DROP TABLE IF EXISTS DatiClienti;");
|
|
await _connection.ExecuteAsync("DROP TABLE IF EXISTS DatiConsegne;");
|
|
await _connection.ExecuteAsync("DROP TABLE IF EXISTS SospesiCliente;");
|
|
await _connection.ExecuteAsync("DROP TABLE IF EXISTS DettaglioRighe;");
|
|
await _connection.ExecuteAsync("DROP TABLE IF EXISTS PaymentData;");
|
|
|
|
// Ricrea le tabelle nel database principale
|
|
await _connection.CreateTableAsync<DatiClientiDTO>();
|
|
await _connection.CreateTableAsync<DatiConsegneDTO>();
|
|
await _connection.CreateTableAsync<SospesiClienteDTO>();
|
|
await _connection.CreateTableAsync<DettaglioRigheDTO>();
|
|
await _connection.CreateTableAsync<PaymentDataDTO>();
|
|
|
|
Console.WriteLine("Database resettato con successo e backup storico creato.");
|
|
_logger.LogInformation("Database resettato con successo e backup storico creato.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Errore durante il reset del database: {ex.Message}");
|
|
_logger.LogError(ex, $"Errore durante il reset del database: {ex.Message}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<DatiClientiDTO>> RetrieveDatiClienti()
|
|
{
|
|
var clienti = await _connection.Table<DatiClientiDTO>().ToListAsync();
|
|
|
|
foreach (var cliente in clienti)
|
|
{
|
|
cliente.DatiConsegne = await _connection.Table<DatiConsegneDTO>()
|
|
.Where(c => c.CodVdes == cliente.CodVdes)
|
|
.ToListAsync();
|
|
|
|
foreach (var consegna in cliente.DatiConsegne)
|
|
{
|
|
try
|
|
{
|
|
consegna.DettaglioRighe = await _connection.Table<DettaglioRigheDTO>()
|
|
.Where(x =>
|
|
x.NumDoc == consegna.NumDoc &&
|
|
x.SerDoc == consegna.SerDoc &&
|
|
x.DataDoc == consegna.DataDoc &&
|
|
x.CodAnag == consegna.CodAnag &&
|
|
x.CodDtip == consegna.CodDtip
|
|
)
|
|
.ToListAsync();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
|
|
consegna.PaymentData = await _connection.Table<PaymentDataDTO>()
|
|
.Where(x =>
|
|
x.NumDoc == consegna.NumDoc &&
|
|
x.SerDoc == consegna.SerDoc &&
|
|
x.DataDoc == consegna.DataDoc
|
|
)
|
|
.ToListAsync();
|
|
}
|
|
|
|
cliente.SospesiCliente = await RetrieveSospesi(cliente.CodVdes);
|
|
}
|
|
|
|
return clienti;
|
|
}
|
|
|
|
public async Task<List<DatiConsegneDTO>> RetrieveDatiConsegne()
|
|
{
|
|
var datiConsegne = await _connection.Table<DatiConsegneDTO>().ToListAsync();
|
|
|
|
foreach (var consegna in datiConsegne)
|
|
{
|
|
consegna.PaymentData = await _connection.Table<PaymentDataDTO>()
|
|
.Where(x =>
|
|
x.NumDoc == consegna.NumDoc &&
|
|
x.SerDoc == consegna.SerDoc &&
|
|
x.DataDoc == consegna.DataDoc
|
|
)
|
|
.ToListAsync();
|
|
}
|
|
|
|
return datiConsegne;
|
|
}
|
|
|
|
public async Task<List<SospesiClienteDTO>> RetrieveSospesi(string codVdes) =>
|
|
await _connection.Table<SospesiClienteDTO>()
|
|
.Where(s => s.CodVdes == codVdes)
|
|
.ToListAsync();
|
|
|
|
public async Task<List<SospesiClienteDTO>?> RetrieveAllSospesi() =>
|
|
await _connection.Table<SospesiClienteDTO>().ToListAsync();
|
|
|
|
public async Task<List<PaymentDataDTO>?> RetrieveAllPaymentData() =>
|
|
await _connection.Table<PaymentDataDTO>().ToListAsync();
|
|
|
|
public async Task SaveDatiCliente(List<DatiClientiDTO> clienti)
|
|
{
|
|
await ResetDatabase();
|
|
|
|
foreach (var cliente in clienti)
|
|
{
|
|
await SaveDatiCliente(cliente);
|
|
}
|
|
}
|
|
|
|
private async Task SaveDatiCliente(DatiClientiDTO cliente)
|
|
{
|
|
await _connection.InsertAsync(cliente);
|
|
|
|
foreach (var consegna in cliente.DatiConsegne)
|
|
{
|
|
await _connection.InsertAsync(consegna);
|
|
|
|
foreach (var riga in consegna.DettaglioRighe)
|
|
{
|
|
riga.CodAnag = consegna.CodAnag;
|
|
riga.SerDoc = consegna.SerDoc;
|
|
riga.DataDoc = consegna.DataDoc;
|
|
riga.CodDtip = consegna.CodDtip;
|
|
|
|
await _connection.InsertAsync(riga);
|
|
}
|
|
}
|
|
|
|
if (cliente.SospesiCliente != null)
|
|
{
|
|
foreach (var sospeso in cliente.SospesiCliente)
|
|
{
|
|
await _connection.InsertAsync(sospeso);
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task UpdateDatiCliente(List<DatiClientiDTO> clienti)
|
|
{
|
|
foreach (var cliente in clienti)
|
|
{
|
|
await UpdateDatiCliente(cliente);
|
|
}
|
|
}
|
|
|
|
public async Task UpdateDatiCliente(DatiClientiDTO cliente)
|
|
{
|
|
await _connection.UpdateAsync(cliente);
|
|
|
|
foreach (var consegna in cliente.DatiConsegne)
|
|
{
|
|
await _connection.UpdateAsync(consegna);
|
|
|
|
foreach (var riga in consegna.DettaglioRighe)
|
|
{
|
|
await _connection.UpdateAsync(riga);
|
|
}
|
|
}
|
|
|
|
if (cliente.SospesiCliente != null)
|
|
{
|
|
foreach (var sospeso in cliente.SospesiCliente)
|
|
{
|
|
await _connection.UpdateAsync(sospeso);
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task InsertPaymantData(List<PaymentDataDTO> paymentData)
|
|
{
|
|
foreach (var data in paymentData)
|
|
{
|
|
await _connection.InsertAsync(data);
|
|
}
|
|
}
|
|
|
|
public async Task InsertSospesi(List<SospesiClienteDTO> sospesi)
|
|
{
|
|
foreach (var sospeso in sospesi)
|
|
{
|
|
await _connection.InsertAsync(sospeso);
|
|
}
|
|
}
|
|
|
|
public async Task UpdateSospesi(List<SospesiClienteDTO> sospesi)
|
|
{
|
|
foreach (var sospeso in sospesi)
|
|
{
|
|
await _connection.UpdateAsync(sospeso);
|
|
}
|
|
}
|
|
|
|
public async Task DeleteSospesi()
|
|
{
|
|
await _connection.ExecuteAsync("DROP TABLE IF EXISTS SospesiCliente;");
|
|
await _connection.CreateTableAsync<SospesiClienteDTO>();
|
|
}
|
|
} |