private XmlDocument ComposeDocument(IUnitOfWork db,
                                            MarketType market,
                                            string marketplaceId,
                                            IList <string> skuList,
                                            string merchantId,
                                            string type,
                                            out int nodesCount,
                                            out IList <FeedItemDTO> feedItems)
        {
            var             toDate = Time.GetAppNowTime().AddHours(-30);
            IList <ItemDTO> dtoItems;

            if (skuList == null || !skuList.Any())
            {
                var requestInfoes = db.SystemActions.GetAllAsDto()
                                    .Where(a => a.Type == (int)SystemActionType.DeleteOnMarketProduct &&
                                           a.Status != (int)SystemActionStatus.Done &&
                                           a.InputData.Contains("\"MarketplaceId\": \"" + marketplaceId))
                                    .ToList();

                var requestedSKUs = requestInfoes.Select(i => i.Tag).ToList();


                dtoItems = (from i in db.Items.GetAll()
                            join l in db.Listings.GetAll() on i.Id equals l.ItemId
                            where requestedSKUs.Contains(l.SKU) &&
                            !l.IsRemoved &&
                            i.IsExistOnAmazon == true &&
                            i.Market == (int)market &&
                            (String.IsNullOrEmpty(marketplaceId) || i.MarketplaceId == marketplaceId)
                            select new ItemDTO
                {
                    Id = i.Id,
                    SKU = l.SKU,
                }).ToList();

                foreach (var dtoItem in dtoItems)
                {
                    var requestInfo = requestInfoes.FirstOrDefault(i => i.Tag == dtoItem.SKU);
                    dtoItem.Id = (int)(requestInfo?.Id ?? 0);
                }
            }
            else
            {
                dtoItems = skuList.Select(i => new ItemDTO()
                {
                    SKU = i
                }).ToList();
                //dtoItems = db.Items.GetAllViewAsDto()
                //    .Where(i => i.Market == (int) market
                //                && (String.IsNullOrEmpty(marketplaceId) || i.MarketplaceId == marketplaceId)
                //                && skuList.Contains(i.SKU))
                //    .ToList();
                //dtoItems.ForEach(i => i.Id = 0);
            }

            nodesCount = dtoItems.Count;

            if (dtoItems.Any())
            {
                Log.Info("Items to submit=" + String.Join(", ", dtoItems.Select(i => i.SKU).ToList()));

                var newFeed = new Feed()
                {
                    Market        = (int)market,
                    MarketplaceId = marketplaceId,
                    Type          = (int)Type,
                    Status        = (int)FeedStatus.Submitted,
                    SubmitDate    = Time.GetAppNowTime()
                };
                db.Feeds.Add(newFeed);
                db.Commit();

                Log.Info("Feed id=" + newFeed.Id);

                var builder = new ProductDeleteFeedBuilder();
                var items   = builder.Build(
                    dtoItems);

                feedItems = items
                            .Select(i => new FeedItemDTO()
                {
                    FeedId    = newFeed.Id,
                    MessageId = i.MessageID,
                    ItemId    = i.Id ?? 0
                }).ToList();

                var xml = builder.ToXmlFeed(items,
                                            merchantId,
                                            Type.ToString());

                var doc = new XmlDocument();
                doc.LoadXml(xml);
                return(doc);
            }

            feedItems = new List <FeedItemDTO>();
            return(null);
        }
        private XmlDocument ComposeDocument(IUnitOfWork db,
                                            MarketType market,
                                            string marketplaceId,
                                            IList <string> skuList,
                                            string merchantId,
                                            string type,
                                            out int nodesCount,
                                            out IList <FeedItemDTO> feedItems)
        {
            var             toDate = Time.GetAppNowTime().AddHours(-30);
            IList <ItemDTO> dtoItems;

            if (skuList == null || !skuList.Any())
            {
                dtoItems = (from i in db.Items.GetAll()
                            join l in db.Listings.GetAll() on i.Id equals l.ItemId
                            where i.ItemPublishedStatus == (int)PublishedStatuses.HasUnpublishRequest &&
                            i.Market == (int)market &&
                            (i.MarketplaceId == marketplaceId || String.IsNullOrEmpty(marketplaceId)) &&
                            i.IsExistOnAmazon == true &&
                            !l.IsRemoved
                            select new ItemDTO()
                {
                    Id = i.Id,
                    SKU = l.SKU
                }).ToList();
            }
            else
            {
                dtoItems = skuList.Select(i => new ItemDTO()
                {
                    SKU = i
                }).ToList();
                //dtoItems = db.Items.GetAllViewAsDto()
                //    .Where(i => i.Market == (int) market
                //                && (String.IsNullOrEmpty(marketplaceId) || i.MarketplaceId == marketplaceId)
                //                && skuList.Contains(i.SKU))
                //    .ToList();
                //dtoItems.ForEach(i => i.Id = 0);
            }

#if DEBUG
            dtoItems = dtoItems.Where(i => i.SKU.Contains("-FBP")).ToList();
#endif

            nodesCount = dtoItems.Count;

            if (dtoItems.Any())
            {
                Log.Info("Items to submit=" + String.Join(", ", dtoItems.Select(i => i.SKU).ToList()));

                var newFeed = new Feed()
                {
                    Market        = (int)market,
                    MarketplaceId = marketplaceId,
                    Type          = (int)Type,
                    Status        = (int)FeedStatus.Submitted,
                    SubmitDate    = Time.GetAppNowTime()
                };
                db.Feeds.Add(newFeed);
                db.Commit();

                Log.Info("Feed id=" + newFeed.Id);

                var builder = new ProductDeleteFeedBuilder();
                var items   = builder.Build(
                    dtoItems);

                feedItems = items
                            .Select(i => new FeedItemDTO()
                {
                    FeedId    = newFeed.Id,
                    MessageId = i.MessageID,
                    ItemId    = i.Id ?? 0
                }).ToList();

                var xml = builder.ToXmlFeed(items,
                                            merchantId,
                                            Type.ToString());

                var doc = new XmlDocument();
                doc.LoadXml(xml);
                return(doc);
            }

            feedItems = new List <FeedItemDTO>();
            return(null);
        }