Exemplo n.º 1
0
        public bool SyncLocalListingByID(int id)
        {
            try
            {
                var listing = GetListingByID(id);

                //get ebay active listing
                var request = new GeteBaySellerListBySKUsRequest()
                {
                    SellerID = GetListingChannelByID(listing.ListingChannelID).Ref1, SKUs = listing.ListingSKU.ToEnumerable().ToList()
                };
                var activeListing      = _eBayAPICallManager.GeteBaySellerListBySKUs(request);
                var matchActiveListing = activeListing.FirstOrDefault(al => al.ItemID.Equals(listing.ListingID) && al.SKU.ToUpper().Equals(listing.ListingSKU.ToUpper()));
                var lastUpdateTime     = DateTime.Now;
                if (matchActiveListing != null)
                {
                    listing.ListingTitle       = matchActiveListing.Title;
                    listing.ListingDescription = matchActiveListing.Description;
                    if (matchActiveListing.SellingStatus != null)
                    {
                        listing.ListingInventoryQty = matchActiveListing.Quantity - matchActiveListing.SellingStatus.QuantitySold;
                        listing.ListingPrice        = Convert.ToDecimal(matchActiveListing.SellingStatus.CurrentPrice.Value);
                    }
                    UpdateListingPostageRule(matchActiveListing.ShippingDetails, listing);

                    listing.LastUpdateTime = lastUpdateTime;
                    _listingRepository.Update(listing);
                }
                else
                {
                    listing.ListingStatusID = 4;
                    listing.LastUpdateTime  = lastUpdateTime;
                    _listingRepository.Update(listing, l => l.ListingStatusID, l => l.LastUpdateTime);
                }


                return(true);
            }
            catch (Exception ex)
            {
                LogManager.Instance.Error(ex.Message);
                return(false);
            }
        }
Exemplo n.º 2
0
        public bool SyncLocalListingByChannel(int listingChannelID)
        {
            try
            {
                //get local listing
                var localListing = GetListingByChannelID(listingChannelID, false);

                //get local item
                var localItem = _itemService.GetAllItems();

                //get ebay active listing
                var request = new GeteBaySellerListBySKUsRequest()
                {
                    SellerID = GetListingChannelByID(listingChannelID).Ref1, SKUs = localItem.Select(s => s.SKU).ToList()
                };
                var activeListing = _eBayAPICallManager.GeteBaySellerListBySKUs(request);

                //check if there is same sku in active online listing
                //var countSKU=activeListing.GroupBy(i=>i.SKU).Where(g=>g.Count()>1).Count();
                //if(countSKU>1)
                //    throw new Exception("Online Listing SKU Duplicated");
                //var countSKU = activeListing.GroupBy(i => i.SKU).Where(g => g.Count() > 1);

                //whether ebay listing match local listing(sku, itemID), update local listing with ebay item, if local item is disable, set online listing qty=0
                //whether ebay listing match local listing(sku, itemID), if sku is equal but itemID not, then update item id and local listing;
                //if cannot find local listing by sku but can find in item, then add local listing and link with item;
                //if local listing not in active listing list, then disable the local listing
                //if ebay listing sku not in local listing or item, then ignore


                var createTime = DateTime.Now;
                var createBy   = Constants.SystemUser;
                #region UPDATE:whether ebay listing match local listing(sku, itemID), update local listing with ebay item, if local item is disable, set online listing qty=0
                var updateInfoList = from al in activeListing
                                     join ll in localListing on new { sku = al.SKU.ToUpper(), itemid = al.ItemID } equals new { sku = ll.ListingSKU.ToUpper(), itemid = ll.ListingID }
                select new
                {
                    ll.ID,
                    al.ItemID,
                    al.SKU,
                    Price           = Convert.ToDecimal(al.SellingStatus.CurrentPrice.Value),
                    Qty             = al.Quantity - al.SellingStatus.QuantitySold,
                    ShippingDetails = al.ShippingDetails
                };

                foreach (var updateMatch in updateInfoList)
                {
                    var localListingItem = localListing.FirstOrDefault(ll => ll.ID == updateMatch.ID);
                    if (localListingItem != null)
                    {
                        localListingItem.ListingInventoryQty = updateMatch.Qty;
                        localListingItem.ListingPrice        = updateMatch.Price;

                        UpdateListingPostageRule(updateMatch.ShippingDetails, localListingItem);

                        localListingItem.LastUpdateTime = createTime;
                        _listingRepository.Update(localListingItem, l => l.ListingInventoryQty, l => l.ListingPrice, l => l.ListingPostageRuleID, l => l.LastUpdateTime);
                    }
                }

                //var matchList = from al in activeListing
                //                from ll in localListing
                //                where al.SKU.Equals(ll.ListingSKU) && al.ItemID.Equals(ll.ListingID)
                //                select new {
                //                    ll.ID,
                //                    al.ItemID,
                //                    al.SKU,
                //                    al.QuantityAvailable
                //                };

                #endregion

                #region DELETE: if local listing not in active listing list, then disable the local listing
                //var disableLocalListing = from ll in localListing
                //                          where ll.ListingStatusID == 3
                //                          && !activeListing.Select(l => l.SKU.ToUpper()).Contains(ll.ListingSKU.ToUpper())
                //                          select ll;

                var disableLocalListing = from ll in localListing
                                          where ll.ListingStatusID == 3 &&
                                          !activeListing.Any(al => al.SKU == ll.ListingSKU && al.ItemID == ll.ListingID)
                                          select ll;

                foreach (var dl in disableLocalListing)
                {
                    dl.ListingStatusID = 4;
                    dl.LastUpdateTime  = createTime;
                    _listingRepository.Update(dl, l => l.ListingStatusID, l => l.LastUpdateTime);
                }
                #endregion

                #region ADD: if cannot find local listing by sku but can find in item, then add local listing and link with item;
                var addListingList = from al in activeListing
                                     join li in localItem on al.SKU.ToUpper() equals li.SKU.ToUpper() into g
                                     from alli in g.DefaultIfEmpty()
                                     //where !localListing.Select(ll => ll.ListingSKU.ToUpper()).Contains(al.SKU.ToUpper())
                                         where !localListing.Any(ll => ll.ListingSKU == al.SKU && ll.ListingID == al.ItemID)
                                     select new {
                    ItemID             = (alli.ID == null?0:alli.ID),
                    ListingChannelID   = listingChannelID,
                    ListingID          = al.ItemID,
                    ListingSKU         = al.SKU.ToUpper(),
                    ListingTitle       = al.Title,
                    ListingDescription = al.Description,
                    ListingPrice       = Convert.ToDecimal(al.SellingStatus.CurrentPrice.Value),
                    ListingInventory   = al.Quantity - al.SellingStatus.QuantitySold,
                    ShippingDetails    = al.ShippingDetails,
                    LastUpdateTime     = DateTime.Now
                };

                foreach (var addListing in addListingList)
                {
                    var newLocalListing = new D_Listing();

                    newLocalListing.ItemID                    = addListing.ItemID;
                    newLocalListing.ListingChannelID          = addListing.ListingChannelID;
                    newLocalListing.ListingID                 = addListing.ListingID;
                    newLocalListing.ListingSKU                = addListing.ListingSKU;
                    newLocalListing.ListingTitle              = addListing.ListingTitle;
                    newLocalListing.ListingDescription        = addListing.ListingDescription;
                    newLocalListing.ListingPrice              = addListing.ListingPrice;
                    newLocalListing.ListingInventoryQty       = addListing.ListingInventory;
                    newLocalListing.ListingStatusID           = 3; //TODO: get active lising status ID in status list
                    newLocalListing.ListingPriceRuleID        = 1; //TODO: Default price rule ID
                    newLocalListing.ListingInventoryQtyRuleID = 2; //TODO: Default inventory qty rule ID

                    UpdateListingPostageRule(addListing.ShippingDetails, newLocalListing);

                    newLocalListing.LastUpdateTime = createTime;
                    newLocalListing.CreateTime     = createTime;
                    newLocalListing.CreateBy       = createBy;
                    newLocalListing.EditTime       = createTime;
                    newLocalListing.EditBy         = createBy;
                    newLocalListing.FillOutNull();

                    _listingRepository.Insert(newLocalListing);
                }

                #endregion

                //#region whether ebay listing match local listing(sku, itemID), if sku is equal but itemID not, then update item id and local listing;
                //var updateInfoItemIDList = from al in activeListing
                //                           join ll in localListing on al.SKU.ToUpper() equals ll.ListingSKU.ToUpper()
                //                           where al.ItemID != ll.ListingID
                //                           select new {
                //                               ll.ID,
                //                               al.ItemID,
                //                               al.SKU,
                //                               Qty = al.Quantity - al.SellingStatus.QuantitySold
                //                           };

                //foreach (var updateMatch in updateInfoItemIDList)
                //{
                //    var localListingItem = localListing.FirstOrDefault(ll => ll.ID == updateMatch.ID);
                //    if (localListingItem != null)
                //    {
                //        localListingItem.ListingID = updateMatch.ItemID;
                //        localListingItem.ListingInventoryQty = updateMatch.Qty;
                //        _listingRepository.Update(localListingItem,l=>l.ListingID, l => l.ListingInventoryQty);
                //    }
                //}
                //#endregion



                return(true);
            }
            catch (Exception ex)
            {
                LogManager.Instance.Error(ex.Message);
                return(false);
            }
        }