public async Task <List <DataLoadResult> > SaveItems <T, V>(List <T> items, Func <List <V>, Task <BaseReply> > loader) where T : Entity, new() where V : HuntersWP.ServiceReference.Entity { var result = new List <DataLoadResult>(); if (!items.Any()) { return(result); } //foreach (var item in items) //{ // await new DbService().Save(item, ESyncStatus.InProcess, null, true); //} var serviceItems = Convert <V, T>(items); int counter = 0; foreach (var batch in serviceItems.Batch(MyServiceClient.SAVE_ITEMS_PER_PAGE)) { var batchList = batch.ToList(); var reply = await loader(batchList); if (!reply.IsSuccess) { foreach (var v in batchList) { await new DbService().Save(items.First(x => x.Id == v.Id), ESyncStatus.NotSynced, new Exception(reply.Data ?? ""), true); result.Add(new DataLoadResult() { EntityId = v.Id, IsSuccess = false }); } // throw new DataLoadException(reply.Data); } else { foreach (var v in batchList) { var i = items.First(x => x.Id == v.Id); i.IsCreatedOnClient = false; await new DbService().Save(i, ESyncStatus.Success, null, true); result.Add(new DataLoadResult() { EntityId = v.Id, IsSuccess = true }); } } counter += batchList.Count(); Helpers.DebugMessage(string.Format("{0},Count - {1}-{3},Success={2}", typeof(T), counter, reply.IsSuccess, items.Count())); } return(result); }
public async Task RefreshAddressMoves(List <ServiceReference.AddressMove> moves) { if (moves == null || !moves.Any()) { return; } foreach (var move in moves) { Helpers.DebugMessage(string.Format("Processing address move: {0}/{1}", moves.IndexOf(move), moves.Count)); if (move.FromSurveyorId == StateService.CurrentUserId && move.ToSurveyorId == StateService.CurrentUserId) { var foundCustomers = await new DataLoaderService().GetCustomers(); var addresses = await new DataLoaderService().GetAddresses(foundCustomers, move.AddressId); var newAddress = addresses.FirstOrDefault(); if (newAddress != null) { await new DbService().Save(newAddress); } await new DataLoaderService().ProcessAddressMove(move.Id, true, true); } else if (move.FromSurveyorId == StateService.CurrentUserId && move.ToSurveyorId != StateService.CurrentUserId) { var address = await new DbService().FindAddress(move.AddressId); if (address != null) { address.RemoveDataAfterSync = true; await new DbService().Save(address); var survelems = await new DbService().GetSurvelemsByAddressUPRN(address.UPRN); foreach (var s in survelems) { s.RemoveDataAfterSync = true; await new DbService().Save(s); } } await new DataLoaderService().ProcessAddressMove(move.Id, true, false); } else if (move.ToSurveyorId == StateService.CurrentUserId && move.FromSurveyorId != StateService.CurrentUserId) { var foundCustomers = await new DataLoaderService().GetCustomers(); var addresses = await new DataLoaderService().GetAddresses(foundCustomers, move.AddressId); var newAddress = addresses.FirstOrDefault(); if (newAddress != null) { await new DbService().Save(newAddress); var media = await new DataLoaderService().GetRichMedias(foundCustomers, newAddress.Id); foreach (var m in media) { await new DbService().Save(m); } await DownloadSurvelems(foundCustomers, new List <Address> { newAddress }, false); } await new DataLoaderService().ProcessAddressMove(move.Id, false, true); } } }
public static async Task Execute(bool isQA) { var notSuccessAddresses = new List <Address>(); if (!isQA) { var addresses = await new DbService().GetNotSyncedEntities <Address>(); foreach (var ad in addresses) { var addressesResult = await new DataLoaderService().SaveAddresses(new List <Address>() { ad }); if (!addressesResult[0].IsSuccess) { notSuccessAddresses.Add(ad); var pars = new Dictionary <string, string>(); pars.Add("UPRN", ad.UPRN); pars.Add("Id", ad.Id.ToString()); Helpers.LogEvent("AddressSaveFail", pars); } } // var addressStatuses = await new DbService().GetNotSyncedEntities <AddressStatus>(); await new DataLoaderService().SaveAddressStatuses(addressStatuses.Where(x => notSuccessAddresses.All(y => y.Id != x.AddressId)).ToList()); // var addressQuestionGroupStatuses = await new DbService().GetNotSyncedEntities <AddressQuestionGroupStatus>(); await new DataLoaderService().SaveAddressQuestionGroupStatus(addressQuestionGroupStatuses.Where(x => notSuccessAddresses.All(y => y.Id != x.AddressId)).ToList()); // var notSyncedSurvelems = await new DbService().GetNotSyncedEntities <Survelem>(); var syncResult = await new DataLoaderService().SaveSurvelems(notSyncedSurvelems.Where(x => notSuccessAddresses.All(y => string.Compare(y.UPRN, x.UPRN, StringComparison.InvariantCultureIgnoreCase) != 0)).ToList()); var syncFailedSurvelemCount = syncResult.Count(x => !x.IsSuccess); Helpers.DebugMessage(string.Format("Total survelems failed: {0}-{1}", syncFailedSurvelemCount, notSyncedSurvelems.Count)); var addressesToRemove = await new DbService().GetAddressesToRemove(); foreach (var ad in addressesToRemove) { await new DbService().Delete(ad); } var survelemsToRemove = await new DbService().GetSurvelemsToRemove(); foreach (var s in survelemsToRemove) { await new DbService().Delete(s); } } var medias = await new DbService().GetNotSyncedEntities <RichMedia>(); if (!isQA) { medias = medias.Where(x => notSuccessAddresses.All(y => string.Compare(y.UPRN, x.UPRN, StringComparison.InvariantCultureIgnoreCase) != 0)).ToList(); } foreach (var media in medias) { Helpers.DebugMessage(string.Format("Media: {0}-{1}", medias.IndexOf(media), medias.Count)); await UploadMediaFile(media); } var qaaddresses = await new DbService().GetNotSyncedEntities <QAAddress>(); await new DataLoaderService().SaveQAAddresses(qaaddresses); var qaaddressesComments = await new DbService().GetNotSyncedEntities <QAAddressComment>(); await new DataLoaderService().SaveQAAddressComments(qaaddressesComments); ///Remove isLoadToPhoneAddresses = false var addressesToRemoveIsNotLoadToPhone = await new DbService().GetAddressesToRemoveIsNotLoadToPhone(); foreach (var ad in addressesToRemoveIsNotLoadToPhone) { var survelems = await new DbService().GetSyncedSurvelemsByAddressUPRN(ad.UPRN); foreach (var s in survelems) { await new DbService().Delete(s); } await new DbService().Delete(ad); } /// }