generated from Integry/Template_NetMauiBlazorHybrid
279 lines
7.2 KiB
C#
279 lines
7.2 KiB
C#
using ConSegna.Maui.Core.Utility;
|
|
using ConSegna.Shared.Core.Dto;
|
|
using ConSegna.Shared.Core.Interfaces;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace ConSegna.Maui.Core.Services;
|
|
|
|
public class SyncStorage(
|
|
IIntegryApiService integryApiService,
|
|
IDataStorage dataStorage,
|
|
LocalDbService localDb,
|
|
ILogger<SyncStorage> logger,
|
|
IUtilityFile utilityFile
|
|
) : ISyncStorage
|
|
{
|
|
public async Task<bool> GetAndSaveDatiClienti()
|
|
{
|
|
#if DEBUG
|
|
var dataDoc = new DateTime(2025, 11, 14);
|
|
//var dataDoc = DateTime.Today;
|
|
#else
|
|
var dataDoc = DateTime.Today;
|
|
#endif
|
|
|
|
try
|
|
{
|
|
var dataToSave = await integryApiService.GetDatiConsegne();
|
|
foreach (var datiClienti in dataToSave)
|
|
{
|
|
datiClienti.Sync = true;
|
|
|
|
foreach (var bolla in datiClienti.DatiConsegne)
|
|
{
|
|
bolla.FileName = utilityFile.ComposeFileName(
|
|
bolla.CodDtip, bolla.DataDoc, bolla.SerDoc, bolla.NumDoc, bolla.CodAnag
|
|
);
|
|
|
|
var filePath = dataStorage.GetFilePath(bolla.FileName, true);
|
|
|
|
if (filePath != null)
|
|
bolla.Firmato = true;
|
|
}
|
|
}
|
|
|
|
await localDb.SaveDatiCliente(dataToSave);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
logger.LogError(e, e.Message);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> GetAndSaveFile(JasperDTO jasperDto, string fileName)
|
|
{
|
|
var appDataPath = FileSystem.AppDataDirectory;
|
|
var targetDirectory = Path.Combine(appDataPath, "documents", "original");
|
|
|
|
if (!Directory.Exists(targetDirectory))
|
|
{
|
|
Directory.CreateDirectory(targetDirectory);
|
|
}
|
|
|
|
var tempFilePath = Path.Combine(targetDirectory, fileName + ".temp");
|
|
var filePath = Path.Combine(targetDirectory, fileName);
|
|
|
|
if (File.Exists(filePath))
|
|
{
|
|
File.Delete(filePath);
|
|
}
|
|
|
|
try
|
|
{
|
|
var stream = await integryApiService.DownloadReport(jasperDto);
|
|
|
|
await using var fileStream =
|
|
new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
|
|
await stream.CopyToAsync(fileStream);
|
|
|
|
File.Move(tempFilePath, filePath);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine($"Errore durante il salvataggio dello stream: {e.Message}");
|
|
logger.LogError(e, $"Errore durante il salvataggio dello stream: {e.Message}");
|
|
return false;
|
|
}
|
|
finally
|
|
{
|
|
if (File.Exists(tempFilePath))
|
|
{
|
|
File.Delete(tempFilePath);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public async Task<string?> GetAndSaveCertificate()
|
|
{
|
|
var file = await integryApiService.GetCertificate();
|
|
|
|
var appDataPath = FileSystem.AppDataDirectory;
|
|
var targetDirectory = Path.Combine(appDataPath, "certificate");
|
|
|
|
if (!Directory.Exists(targetDirectory))
|
|
{
|
|
Directory.CreateDirectory(targetDirectory);
|
|
}
|
|
|
|
var tempFilePath = Path.Combine(targetDirectory, file.FileName + ".temp");
|
|
var filePath = Path.Combine(targetDirectory, file.FileName);
|
|
|
|
if (File.Exists(filePath))
|
|
{
|
|
File.Delete(filePath);
|
|
}
|
|
|
|
try
|
|
{
|
|
var stream = await integryApiService.DownloadCertificate(file);
|
|
|
|
await using var fileStream =
|
|
new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
|
|
await stream.CopyToAsync(fileStream);
|
|
|
|
File.Move(tempFilePath, filePath);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine($"Errore durante il salvataggio dello stream: {e.Message}");
|
|
logger.LogError(e, $"Errore durante il salvataggio dello stream: {e.Message}");
|
|
return null;
|
|
}
|
|
finally
|
|
{
|
|
if (File.Exists(tempFilePath))
|
|
{
|
|
File.Delete(tempFilePath);
|
|
}
|
|
}
|
|
|
|
return file.FileName;
|
|
}
|
|
|
|
public void OverwriteFile(string filePath, Stream newFile, bool signed, string? filename)
|
|
{
|
|
if (signed && filename != null)
|
|
filePath = Path.Combine(filePath, filename);
|
|
|
|
var tempFilePath = $"{filePath}.temp";
|
|
|
|
if (File.Exists(filePath))
|
|
File.Delete(filePath);
|
|
|
|
try
|
|
{
|
|
File.Move(tempFilePath, filePath);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine($"Errore durante il salvataggio dello stream: {e.Message}");
|
|
logger.LogError(e, $"Errore durante il salvataggio dello stream: {e.Message}");
|
|
}
|
|
finally
|
|
{
|
|
if (File.Exists(tempFilePath))
|
|
{
|
|
File.Delete(tempFilePath);
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task<bool> UpdateDatiClienti(List<DatiClientiDTO> dataToSave)
|
|
{
|
|
try
|
|
{
|
|
await localDb.UpdateDatiCliente(dataToSave);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
logger.LogError(e, e.Message);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> UpdateDatiClienti(DatiClientiDTO dataToSave)
|
|
{
|
|
try
|
|
{
|
|
await localDb.UpdateDatiCliente(dataToSave);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
logger.LogError(e, e.Message);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> InsertPaymantData(PaymentDataDTO paymentData)
|
|
{
|
|
List<PaymentDataDTO> listPaymentData =
|
|
[
|
|
paymentData
|
|
];
|
|
|
|
try
|
|
{
|
|
await InsertPaymantData(listPaymentData);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
logger.LogError(e, e.Message);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> InsertPaymantData(List<PaymentDataDTO> paymentData)
|
|
{
|
|
try
|
|
{
|
|
await localDb.InsertPaymantData(paymentData);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
logger.LogError(e, e.Message);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> InsertSospesi(List<SospesiClienteDTO> sospesi)
|
|
{
|
|
try
|
|
{
|
|
await localDb.InsertSospesi(sospesi);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
logger.LogError(e, e.Message);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> UpdateSospesi(List<SospesiClienteDTO> sospesi)
|
|
{
|
|
try
|
|
{
|
|
await localDb.UpdateSospesi(sospesi);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
logger.LogError(e, e.Message);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public Task DeleteSospesi() => localDb.DeleteSospesi();
|
|
} |