using AutoMapper; 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, IIntegryApiService integryApiService, INetworkService networkService ) : IManageDataService { public Task> GetTable(Expression>? whereCond = null) where T : new() => localDb.Get(whereCond); public async Task> GetClienti(WhereCondContact? whereCond) { List 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 } ); clienti = response.AnagClie ?? []; } else { clienti = await localDb.Get(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> GetProspect(WhereCondContact? whereCond) { List 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 } ); prospect = response.PtbPros ?? []; } else { prospect = await localDb.Get(x => (whereCond.PartIva != null && x.PartIva.Equals(whereCond.PartIva)) || (whereCond.PartIva == null) ); } return prospect; } public async Task> GetContact(WhereCondContact? whereCond) { List? contactList; List? 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(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(x => (whereCond.PartIva != null && x.PartIva.Equals(whereCond.PartIva)) || (whereCond.PartIva == null) ); } // Mappa i contatti var contactMapper = mapper.Map>(contactList); // Mappa i prospects var prospectMapper = mapper.Map>(prospectList); contactMapper.AddRange(prospectMapper); return contactMapper; } public async Task GetSpecificContact(string codAnag, bool isContact) { if (isContact) { var contact = (await localDb.Get(x => x.CodAnag != null && x.CodAnag.Equals(codAnag))) .LastOrDefault(); return contact == null ? null : mapper.Map(contact); } else { var contact = (await localDb.Get(x => x.CodPpro != null && x.CodPpro.Equals(codAnag))) .LastOrDefault(); return contact == null ? null : mapper.Map(contact); } } public async Task> GetActivity(WhereCondActivity whereCond, bool useLocalDb) { List? 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(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) .Where(x => !string.IsNullOrEmpty(x)) .Distinct().ToList(); var jtbComtList = await localDb.Get(x => codJcomList.Contains(x.CodJcom)); var commesseDict = jtbComtList.ToDictionary(x => x.CodJcom, x => x.Descrizione); var codAnagList = activities .Select(x => x.CodAnag) .Where(x => !string.IsNullOrEmpty(x)) .Distinct().ToList(); var clientList = await localDb.Get(x => codAnagList.Contains(x.CodAnag)); var distinctClient = clientList.ToDictionary(x => x.CodAnag, x => x.RagSoc); var prospectList = await localDb.Get(x => codAnagList.Contains(x.CodPpro)); var distinctProspect = prospectList.ToDictionary(x => x.CodPpro, x => x.RagSoc); var returnDto = activities .Select(activity => { var dto = mapper.Map(activity); if (activity.CodJcom != null) { dto.Category = ActivityCategoryEnum.Commessa; } else { dto.Category = activity.CodAnag != null ? ActivityCategoryEnum.Interna : ActivityCategoryEnum.Memo; } if (dto.Category != ActivityCategoryEnum.Memo && activity.CodAnag != null) { string? ragSoc; if (distinctClient.TryGetValue(activity.CodAnag, out ragSoc) || distinctProspect.TryGetValue(activity.CodAnag, out ragSoc)) { dto.Cliente = ragSoc; } } dto.Commessa = activity.CodJcom != null && commesseDict.TryGetValue(activity.CodJcom, out var descr) ? descr : null; return dto; }) .ToList(); return returnDto; } 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(List? entityList) { return Task.Run(() => { if (entityList == null) return; _ = localDb.InsertOrUpdate(entityList); }); } public Task InsertOrUpdate(List listToSave) => localDb.InsertOrUpdate(listToSave); public Task InsertOrUpdate(T objectToSave) => localDb.InsertOrUpdate([objectToSave]); public Task Delete(T objectToDelete) => localDb.Delete(objectToDelete); public async Task DeleteActivity(ActivityDTO activity) { await localDb.Delete( (await GetTable(x => x.ActivityId.Equals(activity.ActivityId))).Last() ); } public Task ClearDb() => localDb.ResetDb(); }