protected SyncResult UpdateExistingUnshippedOrders(CancellationToken?token, ILogService log, IMarketApi api, Func <ILogService, IMarketApi, ISyncInformer, string, IList <ListingOrderDTO> > getOrderItemsFromMarketFunc, ISyncInformer syncInfo, IList <IShipmentApi> rateProviders, IQuantityManager quantityManager, IEmailService emailService, IOrderValidatorService validatorService, IList <string> excludeOrderIdList, ICompanyAddressService companyAddress, ITime time) { IList <Order> ordersToUpdate = new List <Order>(); try { using (var db = new UnitOfWork(log)) { var existOrders = db.Orders.GetOrdersByStatus(api.Market, api.MarketplaceId, new[] { OrderStatusEnumEx.Pending, OrderStatusEnumEx.Unshipped, OrderStatusEnumEx.PartiallyShipped } ).ToList(); if (excludeOrderIdList != null) { ordersToUpdate = existOrders.Where(o => !excludeOrderIdList.Contains(o.AmazonIdentifier)).ToList(); } else { ordersToUpdate = existOrders; } log.Info("Orders to update count:" + ordersToUpdate.Count + " (existing in DB=" + existOrders.Count + ", exclude list=" + (excludeOrderIdList != null ? excludeOrderIdList.Count.ToString() : "[null]") + ")"); var index = 0; var step = 50; while (index < ordersToUpdate.Count) { var ordersToProcess = ordersToUpdate.Skip(index).Take(step).ToList(); log.Info("Start process orders from api, from=" + index + ", count=" + ordersToProcess.Count); if (!ProcessExistOrdersPack(token, log, api, getOrderItemsFromMarketFunc, syncInfo, db, rateProviders, quantityManager, emailService, validatorService, companyAddress, ordersToProcess, time)) { syncInfo.AddWarning("", "The pending orders pack processing has failed"); //Break cycle if pack processing is fail, something wrong (m.b. unavailable Db, m.b. parallel sync) return(new SyncResult() { IsSuccess = false }); } index += step; syncInfo.PingSync(); } } return(new SyncResult() { IsSuccess = true, ProcessedOrders = ordersToUpdate.Select(o => new SyncResultOrderInfo() { OrderId = o.AmazonIdentifier, OrderStatus = o.OrderStatus }).ToList() }); } catch (Exception ex) { LogError(log, syncInfo, ex, "", "Error when updating existing orders"); return(new SyncResult() { IsSuccess = false }); } }
/// <summary> /// Requested new pending orders (from last exist pending order date) /// </summary> /// <returns></returns> protected SyncResult StoreNewPendingOrders(CancellationToken?token, ILogService log, IMarketApi api, Func <ILogService, IMarketApi, ISyncInformer, string, IList <ListingOrderDTO> > getOrderItemsFromMarketFunc, IList <IShipmentApi> rateProviders, IOrderValidatorService validatorService, IEmailService emailService, ISyncInformer syncInfo, ISettingsService settings, IQuantityManager quantityManager, ICacheService cacheService, ICompanyAddressService companyAddress, ITime time) { try { using (var db = new UnitOfWork(_log)) { var lastPending = db.Orders .GetFiltered(o => o.OrderStatus == OrderStatusEnumEx.Pending && o.Market == (int)api.Market && o.MarketplaceId == api.MarketplaceId) .OrderBy(o => o.Id).FirstOrDefault(); var date = lastPending != null && lastPending.OrderDate.HasValue ? lastPending.OrderDate : null; log.Info(date != null ? "Last pending order date: " + date.Value : "No pending orders!"); var updatedAfter = date ?? DateHelper.GetAmazonNowTime().AddDays(-1); log.Info(string.Format("Process Orders created since {0}", updatedAfter)); var statusList = new List <string> { OrderStatusEnum.Pending.Str() }; var pendingOrders = api.GetOrders(log, updatedAfter, statusList).ToList(); log.Info("Total pending orders:" + pendingOrders.Count()); syncInfo.PingSync(); var result = ProcessNewOrdersPack(token, log, api, getOrderItemsFromMarketFunc, rateProviders, validatorService, syncInfo, settings, quantityManager, emailService, cacheService, companyAddress, pendingOrders, time); if (!result.IsSuccess) { syncInfo.AddWarning("", "The orders pack processing has failed"); return(new SyncResult() { IsSuccess = false }); } return(result); } } catch (Exception ex) { syncInfo.AddError(null, "Error when storing new orders", ex); log.Error("Error when storing new orders", ex); return(new SyncResult() { IsSuccess = false }); } }