Exemplo n.º 1
0
    public void SaveEbayUserItems(int UserCode)
    {
        EbayServiceBL service = new EbayServiceBL(UserCode);
        DataModelEntities context = new DataModelEntities();
        DateTime today = DateTime.Today;
        if (service.UserTokens != null)
        {
            foreach (KeyValuePair<int, string> account in service.UserTokens)
            {
                try
                {
                   // List<Seller_Item> items = service.GetUserItems(account.Value);
                    List<Seller_Item> items = service.GetUserItemsList(account.Value);
                    foreach (Seller_Item item in items)
                    {
                        SellerItem sellerItem = context.SellerItems.FirstOrDefault(a => a.Is_Active == true && a.User_Account_Code == account.Key && a.Item_ID == item.ItemID && a.Country_Code == service.countryID);
                        if (sellerItem == null)
                        {
                            if (item.EndDate.Date > today.Date)
                            {
                                /*Save to seller item*/
                                SellerItem dbItem = new SellerItem();
                                dbItem.BIN_Price = (decimal)item.BinPrice;
                                dbItem.Created_Date = DateTime.Now;
                                dbItem.Current_Price = (decimal)item.CurrentPrice;
                                dbItem.End_Date = item.EndDate;
                                dbItem.Is_Active = true;
                                dbItem.Item_ID = item.ItemID;
                                dbItem.Item_Name = item.ItemName;
                                dbItem.Start_Date = item.StartDate;
                                dbItem.Is_Automated = false;
                                dbItem.User_Code = UserCode;
                                dbItem.User_Account_Code = account.Key;
                                dbItem.Picture_URL = item.PictureURL;
                                dbItem.Item_View_URL = item.ItemViewURL;
                                dbItem.Is_Promo_Item = item.IsPromoItem;
                                dbItem.Item_Category_ID = item.CategoryID;
                                dbItem.Item_Category_Name = item.CategoryName;
                                dbItem.Currency = item.Currency;
                                dbItem.Country_Code = item.CountryCode;
                                dbItem.LocatedIn = item.CountryShortCode;
                                dbItem.Current_Sales = item.CurrentSales;
                                dbItem.QuantityAvailable = item.QuantityAvailable;
                                context.SellerItems.AddObject(dbItem);
                            }
                        }
                        else
                        {

                            if (item.EndDate.Date > today.Date)
                            {
                                sellerItem.User_Account_Code = account.Key;
                                sellerItem.BIN_Price = (decimal)item.BinPrice;
                                sellerItem.Current_Price = (decimal)item.CurrentPrice;
                                sellerItem.End_Date = item.EndDate;
                                sellerItem.Item_Name = item.ItemName;
                                sellerItem.Start_Date = item.StartDate;
                                sellerItem.Is_Promo_Item = item.IsPromoItem;
                                sellerItem.Picture_URL = item.PictureURL;
                                sellerItem.Item_View_URL = item.ItemViewURL;
                                sellerItem.Item_Category_ID = item.CategoryID;
                                sellerItem.Item_Category_Name = item.CategoryName;
                                if (item.IsPromoItem == true && sellerItem.Is_Automated == true)
                                    sellerItem.Is_Automated = false;
                                sellerItem.Currency = item.Currency;
                                sellerItem.Country_Code = item.CountryCode;
                                sellerItem.Current_Sales = item.CurrentSales;
                                sellerItem.QuantityAvailable = item.QuantityAvailable;

                                ItemTitle currentTitle = context.ItemTitles.FirstOrDefault(f => f.ItemId == sellerItem.Item_Code && (f.Is_Current == true || f.Title.ToLower() == sellerItem.Item_Name.ToLower()));

                                if (currentTitle != null)
                                {
                                    if(context.TitleHistories.Any(a => a.Item_Code == sellerItem.Item_Code))
                                    {
                                       var totalSales = context.GetSalesForOldTitles(sellerItem.Item_Code, sellerItem.Item_Name).FirstOrDefault();
                                       currentTitle.TotalSales = totalSales.TotalSales;

                                    }
                                }

                            }
                            else
                            {
                                sellerItem.Is_Automated = false;
                                sellerItem.Is_Active = false;
                            }

                        }

                        if (1 == 1)
                        {
                            Decimal weightMajor = item.Weight != null ? Convert.ToDecimal(item.Weight) * 1000 : 0; //Weight Major comes in Kgs so converted in to gms
                            Decimal weightMinor = item.Weight != null ? Convert.ToDecimal(item.WeightMinor) : 0; //Weight Minor comes in gms
                            Decimal weight = (weightMajor + weightMinor) / 1000; // Added both major and minor and converted into kgs

                            Item partmaster = context.Items.FirstOrDefault(a => a.Item_ID == item.ItemID && a.UserCode == UserCode && a.Country_Code == service.countryID);
                            if (partmaster == null)
                            {
                                partmaster = new Item();
                                partmaster.Item_ID = item.ItemID;
                                partmaster.CustomLabel = item.CustomLabel;
                                partmaster.Description = item.ItemName;
                                partmaster.UserCode = UserCode;
                                partmaster.User_Account_Code = account.Key;
                                partmaster.Height = item.Height != null ? item.Height.ToString() : string.Empty;
                                partmaster.Length = item.Length != null ? item.Length.ToString() : string.Empty;
                                partmaster.Width = item.Width != null ? item.Width.ToString() : string.Empty;
                                partmaster.Weight = item.Weight != null || item.WeightMinor != null ? weight.ToString("0.00") : string.Empty;
                                partmaster.Balance_Quantity = item.Quantity;
                                partmaster.Current_Price = Convert.ToDecimal(item.CurrentPrice);
                                partmaster.BIN_Price = Convert.ToDecimal(item.BinPrice);
                                partmaster.Picture_URL = item.PictureURL;
                                partmaster.Item_View_URL = item.ItemViewURL;
                                partmaster.Item_Category_ID = item.CategoryID;
                                partmaster.Item_Category_Name = item.CategoryName;
                                partmaster.Start_Date = item.StartDate;
                                partmaster.End_Date = item.EndDate;
                                partmaster.User_Account_Code = account.Key;
                                partmaster.Detail_Description = item.Discription;
                                partmaster.Country_Code = item.CountryCode;
                                context.AddToItems(partmaster);
                            }
                            else
                            {
                                partmaster.CustomLabel = item.CustomLabel;
                                partmaster.Description = item.ItemName;
                                partmaster.UserCode = UserCode;
                                partmaster.User_Account_Code = account.Key;

                                if (string.IsNullOrEmpty(partmaster.Height))
                                    partmaster.Height = item.Height != null ? item.Height.ToString() : string.Empty;

                                if (string.IsNullOrEmpty(partmaster.Length))
                                    partmaster.Length = item.Length != null ? item.Length.ToString() : string.Empty;

                                if (string.IsNullOrEmpty(partmaster.Width))
                                    partmaster.Width = item.Width != null ? item.Width.ToString() : string.Empty;

                                if (string.IsNullOrEmpty(partmaster.Weight))
                                    partmaster.Weight = item.Weight != null || item.WeightMinor != null ? weight.ToString("0.00") : string.Empty;

                                partmaster.Height = item.Height != null ? item.Height.ToString() : string.Empty;
                                partmaster.Length = item.Length != null ? item.Length.ToString() : string.Empty;
                                partmaster.Width = item.Width != null ? item.Width.ToString() : string.Empty;
                                partmaster.Weight = item.Weight != null || item.WeightMinor != null ? weight.ToString("0.00") : string.Empty;
                                partmaster.Current_Price = Convert.ToDecimal(item.CurrentPrice);
                                partmaster.BIN_Price = Convert.ToDecimal(item.BinPrice);
                                partmaster.Picture_URL = item.PictureURL;
                                partmaster.Item_View_URL = item.ItemViewURL;
                                partmaster.Item_Category_ID = item.CategoryID;
                                partmaster.Item_Category_Name = item.CategoryName;
                                partmaster.Start_Date = item.StartDate;
                                partmaster.User_Account_Code = account.Key;
                                partmaster.End_Date = item.EndDate;
                                partmaster.Balance_Quantity = item.Quantity;
                                partmaster.Country_Code = item.CountryCode;

                            }
                        }
                        context.SaveChanges();
                    }
                    //context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logging.WriteLog(LogType.Critical, ex.ToString());
                }

            }

        }
    }