Prima parte di migliorie per la sincronizzazione dei dati

This commit is contained in:
2025-09-03 15:46:49 +02:00
parent 8be3fa9f9e
commit 374b99501e
32 changed files with 326 additions and 166 deletions

View File

@@ -1,21 +1,133 @@
using AutoMapper;
using System.Linq.Expressions;
using salesbook.Shared.Core.Dto;
using salesbook.Shared.Core.Dto.Activity;
using salesbook.Shared.Core.Dto.Contact;
using salesbook.Shared.Core.Entity;
using salesbook.Shared.Core.Helpers.Enum;
using salesbook.Shared.Core.Interface;
using Sentry.Protocol;
using System.Linq.Expressions;
namespace salesbook.Maui.Core.Services;
public class ManageDataService(LocalDbService localDb, IMapper mapper) : IManageDataService
public class ManageDataService(
LocalDbService localDb,
IMapper mapper,
IIntegryApiService integryApiService,
INetworkService networkService
) : IManageDataService
{
public Task<List<T>> GetTable<T>(Expression<Func<T, bool>>? whereCond = null) where T : new() =>
localDb.Get(whereCond);
public async Task<List<ContactDTO>> GetContact()
public async Task<List<AnagClie>> GetClienti(WhereCondContact? whereCond)
{
var contactList = await localDb.Get<AnagClie>(x => x.FlagStato.Equals("A"));
var prospectList = await localDb.Get<PtbPros>();
List<AnagClie> clienti = [];
whereCond ??= new WhereCondContact();
whereCond.OnlyContact = true;
if (networkService.IsNetworkAvailable())
{
var response = await integryApiService.RetrieveAnagClie(
new CRMAnagRequestDTO
{
CodAnag = whereCond.CodAnag,
FlagStato = whereCond.FlagStato,
PartIva = whereCond.PartIva,
ReturnPersRif = !whereCond.OnlyContact
}
);
_ = UpdateDbUsers(response);
clienti = response.AnagClie ?? [];
}
else
{
clienti = await localDb.Get<AnagClie>(x =>
(whereCond.FlagStato != null && x.FlagStato.Equals(whereCond.FlagStato)) ||
(whereCond.PartIva != null && x.PartIva.Equals(whereCond.PartIva)) ||
(whereCond.PartIva == null && whereCond.FlagStato == null)
);
}
return clienti;
}
public async Task<List<PtbPros>> GetProspect(WhereCondContact? whereCond)
{
List<PtbPros> prospect = [];
whereCond ??= new WhereCondContact();
whereCond.OnlyContact = true;
if (networkService.IsNetworkAvailable())
{
var response = await integryApiService.RetrieveProspect(
new CRMProspectRequestDTO
{
CodPpro = whereCond.CodAnag,
PartIva = whereCond.PartIva,
ReturnPersRif = !whereCond.OnlyContact
}
);
_ = UpdateDbUsers(response);
prospect = response.PtbPros ?? [];
}
else
{
prospect = await localDb.Get<PtbPros>(x =>
(whereCond.PartIva != null && x.PartIva.Equals(whereCond.PartIva)) ||
(whereCond.PartIva == null)
);
}
return prospect;
}
public async Task<List<ContactDTO>> GetContact(WhereCondContact? whereCond)
{
List<AnagClie>? contactList;
List<PtbPros>? prospectList;
whereCond ??= new WhereCondContact();
if (networkService.IsNetworkAvailable())
{
var clienti = await integryApiService.RetrieveAnagClie(
new CRMAnagRequestDTO
{
CodAnag = whereCond.CodAnag,
FlagStato = whereCond.FlagStato,
PartIva = whereCond.PartIva,
ReturnPersRif = !whereCond.OnlyContact
}
);
_ = UpdateDbUsers(clienti);
var prospect = await integryApiService.RetrieveProspect(
new CRMProspectRequestDTO
{
CodPpro = whereCond.CodAnag,
PartIva = whereCond.PartIva,
ReturnPersRif = !whereCond.OnlyContact
}
);
_ = UpdateDbUsers(prospect);
contactList = clienti.AnagClie;
prospectList = prospect.PtbPros;
}
else
{
contactList = await localDb.Get<AnagClie>(x =>
(whereCond.FlagStato != null && x.FlagStato.Equals(whereCond.FlagStato)) ||
(whereCond.PartIva != null && x.PartIva.Equals(whereCond.PartIva)) ||
(whereCond.PartIva == null && whereCond.FlagStato == null)
);
prospectList = await localDb.Get<PtbPros>(x =>
(whereCond.PartIva != null && x.PartIva.Equals(whereCond.PartIva)) ||
(whereCond.PartIva == null)
);
}
// Mappa i contatti
var contactMapper = mapper.Map<List<ContactDTO>>(contactList);
@@ -46,9 +158,35 @@ public class ManageDataService(LocalDbService localDb, IMapper mapper) : IManage
}
}
public async Task<List<ActivityDTO>> GetActivity(Expression<Func<StbActivity, bool>>? whereCond = null)
public async Task<List<ActivityDTO>> GetActivity(WhereCondActivity whereCond, bool useLocalDb)
{
var activities = await localDb.Get(whereCond);
List<StbActivity>? activities;
if (networkService.IsNetworkAvailable() && !useLocalDb)
{
activities = await integryApiService.RetrieveActivity(
new CRMRetrieveActivityRequestDTO
{
StarDate = whereCond.Start,
EndDate = whereCond.End,
ActivityId = whereCond.ActivityId
}
);
_ = UpdateDb(activities);
}
else
{
activities = await localDb.Get<StbActivity>(x =>
(whereCond.ActivityId != null && x.ActivityId != null && whereCond.ActivityId.Equals(x.ActivityId)) ||
(whereCond.Start != null && whereCond.End != null && x.EffectiveDate == null &&
x.EstimatedDate >= whereCond.Start && x.EstimatedDate <= whereCond.End) ||
(x.EffectiveDate >= whereCond.Start && x.EffectiveDate <= whereCond.End) ||
(whereCond.ActivityId == null && (whereCond.Start == null || whereCond.End == null))
);
}
if (activities == null) return [];
var codJcomList = activities
.Select(x => x.CodJcom)
@@ -103,7 +241,37 @@ public class ManageDataService(LocalDbService localDb, IMapper mapper) : IManage
return returnDto;
}
public Task InsertOrUpdate<T>(List<T> listToSave) =>
private Task UpdateDbUsers(UsersSyncResponseDTO response)
{
return Task.Run(async () =>
{
if (response.AnagClie != null)
{
await localDb.InsertOrUpdate(response.AnagClie);
if (response.VtbDest != null) await localDb.InsertOrUpdate(response.VtbDest);
if (response.VtbCliePersRif != null) await localDb.InsertOrUpdate(response.VtbCliePersRif);
}
if (response.PtbPros != null)
{
await localDb.InsertOrUpdate(response.PtbPros);
if (response.PtbProsRif != null) await localDb.InsertOrUpdate(response.PtbProsRif);
}
});
}
private Task UpdateDb<T>(List<T>? entityList)
{
return Task.Run(() =>
{
if (entityList == null) return;
_ = localDb.InsertOrUpdate(entityList);
});
}
public Task InsertOrUpdate<T>(List<T> listToSave) =>
localDb.InsertOrUpdate(listToSave);
public Task InsertOrUpdate<T>(T objectToSave) =>