Пример #1
0
        public void UpdateSubmittedFeed(AmazonApi api,
                                        string feedId,
                                        string forResponseDirectory)
        {
            var    marketplaceId = api.MarketplaceId;
            Stream result        = null;

            try
            {
                result = api.GetFeedSubmissionResult(feedId);
            }
            catch (MarketplaceWebServiceReportsException ex)
            {
                //Feed Submission Result not available. Feed Submission has been canceled for Feed: 67659017019
                if (ex.Message.Contains("Feed Submission has been canceled"))
                {
                    Log.Error("GetFeedSubmissionResult", ex);
                    using (var db = new UnitOfWork(Log))
                    {
                        var feed = db.Feeds.GetFiltered(f => f.AmazonIdentifier == feedId && f.MarketplaceId == marketplaceId)
                                   .First();
                        feed.Status = (int)FeedStatus.Cancelled;
                        db.Commit();
                        Log.Info("Feed market as deleted, feedId=" + feedId);
                    }
                }
                else
                {
                    throw ex;
                }
            }
            if (result != null)
            {
                //var now = DateHelper.GetAppNowTime();
                Log.Info("Stream to document");
                XmlDocument document = null;
                try
                {
                    document = StreamHelper.GetXmlFromStream(result);
                }
                catch (Exception ex)
                {
                    Log.Error("GetDocumentXml", ex);
                }

                IList <FeedResultMessage> errorList = new List <FeedResultMessage>();
                long dbFeedId = 0;
                using (var db = DbFactory.GetRWDb())
                {
                    Log.Info("Get feed");
                    var feed = db.Feeds.GetFiltered(f => f.AmazonIdentifier == feedId && f.MarketplaceId == marketplaceId).First();
                    errorList = FeedHelper.GetErrorMessageList(document);
                    dbFeedId  = feed.Id;

                    //Save file
                    var toDirectory = FileHelper.ToDirectoryNameWithBackslash(forResponseDirectory) + Time.GetAppNowTime().ToString("yyyy_MM_dd");
                    if (!Directory.Exists(toDirectory))
                    {
                        Directory.CreateDirectory(toDirectory);
                    }

                    using (var errorfileStream = File.Open(toDirectory + "/" + string.Format("{0}_{1}_{2}.xml", marketplaceId ?? "", Type, feedId),
                                                           FileMode.OpenOrCreate,
                                                           FileAccess.ReadWrite))
                    {
                        result.Position = 0;
                        result.CopyTo(errorfileStream);
                    }

                    if (errorList.Any())
                    {
                        Log.Info("Errors found");
                    }
                    else
                    {
                        Log.Info("No errors");
                    }

                    if (errorList.Any())
                    {
                        feed.Status = (int)FeedStatus.ProcessedWithErrors;
                    }
                    else
                    {
                        feed.Status = (int)FeedStatus.Processed;
                    }

                    db.FeedMessages.Insert(errorList.Select(e => new FeedMessageDTO()
                    {
                        FeedId      = feed.Id,
                        Message     = e.Message,
                        MessageId   = e.MessageId,
                        MessageCode = e.MessageCode,
                        ResultCode  = e.ResultCode,
                        CreateDate  = Time.GetAppNowTime(),
                    }).ToList());

                    db.Commit();
                }

                try
                {
                    UpdateEntitiesAfterResponse(dbFeedId, errorList);
                }
                catch (Exception ex)
                {
                    Log.Fatal("Amazon: error when processing feed submission result", ex);
                }
            }
            else
            {
                Log.Info(string.Format("No result by api for feed type: {0}; feedId {1}!", Type, feedId));
            }
        }