public static List <QT.Entities.Product> GetProductFromBizweb(string shopname, string accesstoken, QT.Entities.Company company) { List <QT.Entities.Product> ListProducts = new List <QT.Entities.Product>(); var authorizeState = new BizwebAuthorizationState() { ShopName = shopname, AccessToken = accesstoken }; var client = new BizwebAPIClient(authorizeState); ProductBizwebInfo listproductBizweb = new ProductBizwebInfo() { products = new List <Product>() }; bool check = true; int page = 1; while (check) { Object obj = null; try { //Mỗi lần lấy về 50 product của bizweb string querry = "admin/products.json?page=" + page.ToString() + "&limit=250"; //string querry = "/admin/products.json"; obj = client.Get(querry); //Thread.Sleep(1000); } catch (Exception ex) { check = false; Log.ErrorFormat("BIZWEB : Get/admin/products.json FAILS. ERROR : {0}", ex); } if (obj != null) { ProductBizwebInfo listproduct = JsonConvert.DeserializeObject <ProductBizwebInfo>(obj.ToString()); if (listproduct.products.Count > 0) { listproductBizweb.products.AddRange(listproduct.products); page++; } else { check = false; } } else { check = false; } } if (listproductBizweb.products.Count > 0) { #region Chuyển từ ListProducts of Bizweb sang ListProductWebsosanh for (int i = 0; i < listproductBizweb.products.Count; i++) { //Check số lượng phiên bản variant int variant = listproductBizweb.products[i].variants.Count; //TH variant = 1 (1 phiên bản) if (variant == 1) { #region Product var tmpProduct = new QT.Entities.Product(); tmpProduct.Domain = company.Domain; string parameter = "?"; if (!string.IsNullOrEmpty(listproductBizweb.products[i].alias) && listproductBizweb.products[i].alias.Contains("?")) { parameter = "&"; } tmpProduct.DetailUrl = company.Website + "products/" + listproductBizweb.products[i].alias + parameter + "utm=websosanh"; tmpProduct.ID = Common.GetIDProduct(tmpProduct.DetailUrl); if (listproductBizweb.products[i].variants[0].sku != null) { tmpProduct.MerchantSku = listproductBizweb.products[i].variants[0].sku.ToString(); } if (listproductBizweb.products[i].variants[0].inventory_policy == "continue" || listproductBizweb.products[i].variants[0].inventory_management == "") { tmpProduct.Status = Common.ProductStatus.Available; } else if (listproductBizweb.products[i].variants[0].inventory_policy == "deny") { var inventory_quantity = Common.Obj2Int(listproductBizweb.products[i].variants[0].inventory_quantity); if (inventory_quantity > 0) { tmpProduct.Status = Common.ProductStatus.Available; } else { tmpProduct.Status = Common.ProductStatus.Clear; } } else { tmpProduct.Status = Common.ProductStatus.LienHe; } tmpProduct.Instock = QT.Entities.Common.GetProductInstockFormStatus(tmpProduct.Status); tmpProduct.Manufacture = listproductBizweb.products[i].vendor; tmpProduct.Name = listproductBizweb.products[i].name; tmpProduct.HashName = Common.GetHashNameProduct(company.Domain, tmpProduct.Name.Trim()); tmpProduct.ProductContent = listproductBizweb.products[i].meta_description; CultureInfo cultureInfo = CultureInfo.GetCultureInfo("vi-VN"); int originPrice = 0; int price = 0; string compare_at_price; string price_temp; if ((listproductBizweb.products[i].variants[0].compare_at_price == null || listproductBizweb.products[i].variants[0].compare_at_price == 0) && listproductBizweb.products[i].variants[0].price == 0) { Log.Error("BIZWEB : Product price equal = 0" + "Product: ID " + tmpProduct.ID + " - " + tmpProduct.Name); continue; } else { compare_at_price = listproductBizweb.products[i].variants[0].compare_at_price.ToString(); compare_at_price = (compare_at_price.Contains('.')) ? compare_at_price.Remove(compare_at_price.IndexOf('.')) : compare_at_price; price_temp = listproductBizweb.products[i].variants[0].price.ToString(); price_temp = (price_temp.Contains('.')) ? price_temp.Remove(price_temp.IndexOf('.')) : price_temp; } try { originPrice = (int)Decimal.Parse(compare_at_price, cultureInfo); } catch (FormatException) { } catch (Exception exception) { Log.Error(string.Format("BIZWEB : Parse discounted_price Error. {0}", tmpProduct.DetailUrl), exception); } try { price = (int)Decimal.Parse(price_temp, cultureInfo); } catch (FormatException ex) { Log.Error("BIZWEB : Parse discounted_price Error. " + listproductBizweb.products[i].variants[0].price + "\r\n" + ex); } catch (Exception exception) { Log.Error(string.Format("BIZWEB : Parse discounted_price Error. {0}", tmpProduct.DetailUrl), exception); } if (price <= 0) { Log.Error("BIZWEB : Product price equal = 0" + "Product: ID " + tmpProduct.ID + " - " + tmpProduct.Name); continue; } if (originPrice > price) { tmpProduct.OriginPrice = originPrice; tmpProduct.Price = price; } else { tmpProduct.OriginPrice = tmpProduct.Price = price; } //Categories tmpProduct.Categories = new List <string>(); tmpProduct.Categories.Add(company.Domain); tmpProduct.Categories.Add(listproductBizweb.products[i].product_type); tmpProduct.IDCategories = Common.GetIDClassification(Common.ConvertToString(tmpProduct.Categories, " -> ")); // list Url Images if (listproductBizweb.products[i].images != null && listproductBizweb.products[i].images.Count != 0) { tmpProduct.ImageUrls = new List <string>() { listproductBizweb.products[i].images[0].src }; } else { tmpProduct.ImageUrls = new List <string>(); } if (listproductBizweb.products[i].variants[0] != null) { tmpProduct.VATStatus = listproductBizweb.products[i].variants[0].taxable == false ? Common.VATStatus.NotVAT : Common.VATStatus.HaveVAT; } else { tmpProduct.VATStatus = Common.VATStatus.UndefinedVAT; } ListProducts.Add(tmpProduct); #endregion } // TH có nhiều phiên bản else if (variant > 1) { //check image nếu có ảnh thì = true, ko có = false bool checkImage = false; Dictionary <int, string> dicImage = new Dictionary <int, string>(); if (listproductBizweb.products[i].images != null && listproductBizweb.products[i].images.Count != 0) { checkImage = true; for (int k = 0; k < listproductBizweb.products[i].images.Count; k++) { dicImage.Add(listproductBizweb.products[i].images[k].id, listproductBizweb.products[i].images[k].src); } } else { checkImage = false; } for (int j = 0; j < variant; j++) { #region Product var tmpProduct = new QT.Entities.Product(); tmpProduct.Domain = company.Domain; string parameter = "?"; if (!string.IsNullOrEmpty(listproductBizweb.products[i].alias) && listproductBizweb.products[i].alias.Contains("?")) { parameter = "&"; } tmpProduct.DetailUrl = company.Website + "products/" + listproductBizweb.products[i].alias + "#" + (j + 1).ToString() + parameter + "utm=websosanh"; tmpProduct.ID = Common.GetIDProduct(tmpProduct.DetailUrl); if (listproductBizweb.products[i].variants[j].sku != null) { tmpProduct.MerchantSku = listproductBizweb.products[i].variants[j].sku.ToString(); } if (listproductBizweb.products[i].variants[j].inventory_policy == "continue" || listproductBizweb.products[i].variants[j].inventory_management == "") { tmpProduct.Status = Common.ProductStatus.Available; } else if (listproductBizweb.products[i].variants[j].inventory_policy == "deny") { int inventory_quantity = Common.Obj2Int(listproductBizweb.products[i].variants[j].inventory_quantity); if (inventory_quantity > 0) { tmpProduct.Status = Common.ProductStatus.Available; } else { tmpProduct.Status = Common.ProductStatus.Clear; } } else { tmpProduct.Status = Common.ProductStatus.LienHe; } tmpProduct.Instock = QT.Entities.Common.GetProductInstockFormStatus(tmpProduct.Status); tmpProduct.Manufacture = listproductBizweb.products[i].vendor; string nameproduct = listproductBizweb.products[i].name; if (listproductBizweb.products[i].variants[j].option1 != null) { nameproduct = nameproduct + " " + listproductBizweb.products[i].options[0].name + " " + listproductBizweb.products[i].variants[j].option1; } if (listproductBizweb.products[i].variants[j].option2 != null) { nameproduct = nameproduct + " " + listproductBizweb.products[i].variants[j].option2; } if (listproductBizweb.products[i].variants[j].option3 != null) { nameproduct = nameproduct + " " + listproductBizweb.products[i].variants[j].option3; } tmpProduct.Name = nameproduct; tmpProduct.HashName = Common.GetHashNameProduct(company.Domain, tmpProduct.Name.Trim()); tmpProduct.ProductContent = listproductBizweb.products[i].meta_description; CultureInfo cultureInfo = CultureInfo.GetCultureInfo("vi-VN"); int originPrice = 0; int price = 0; try { originPrice = (int)Decimal.Parse(listproductBizweb.products[i].variants[0].compare_at_price.ToString(), cultureInfo); } catch (FormatException) { } try { price = (int)Decimal.Parse(listproductBizweb.products[i].variants[0].price.ToString(), cultureInfo); } catch (FormatException ex) { Log.Error("BIZWEB : Parse discounted_price Error. " + listproductBizweb.products[i].variants[0].price + "\r\n" + ex); continue; } if (price <= 0) { Log.Error("BIZWEB : Product price equal = 0" + "Product: ID " + tmpProduct.ID + " - " + tmpProduct.Name); continue; } if (originPrice > price) { tmpProduct.OriginPrice = originPrice; tmpProduct.Price = price; } else { tmpProduct.OriginPrice = tmpProduct.Price = price; } //Categories tmpProduct.Categories = new List <string>(); tmpProduct.Categories.Add(company.Domain); tmpProduct.Categories.Add(listproductBizweb.products[i].product_type); tmpProduct.IDCategories = Common.GetIDClassification(Common.ConvertToString(tmpProduct.Categories, " -> ")); // list Url Images if (checkImage) { int image_id = Common.Obj2Int(listproductBizweb.products[i].variants[j].image_id); //nếu != 0 thì tìm values trong dicImage tương ứng if (image_id != 0) { string imageurl = dicImage.Where(kvp => kvp.Key == image_id).Select(kvp => kvp.Value).FirstOrDefault(); if (!string.IsNullOrEmpty(imageurl)) { tmpProduct.ImageUrls = new List <string>() { imageurl } } ; else { tmpProduct.ImageUrls = new List <string>() { listproductBizweb.products[i].images[0].src } }; } // nếu = 0 (null) thì lấy ảnh mặc định images[0].src else { tmpProduct.ImageUrls = new List <string>() { listproductBizweb.products[i].images[0].src } }; } else { tmpProduct.ImageUrls = new List <string>(); } if (listproductBizweb.products[i].variants[j] != null) { tmpProduct.VATStatus = listproductBizweb.products[i].variants[j].taxable == false ? Common.VATStatus.NotVAT : Common.VATStatus.HaveVAT; } else { tmpProduct.VATStatus = Common.VATStatus.UndefinedVAT; } ListProducts.Add(tmpProduct); #endregion } } else if (variant < 1) { Log.ErrorFormat("Error : Variant of {0} = 0", listproductBizweb.products[i].name); } #endregion } } return(ListProducts); }
public static List <QT.Entities.Product> GetProductFromHaravan(string shopname, string accesstoken, QT.Entities.Company company) { List <QT.Entities.Product> ListProducts = new List <QT.Entities.Product>(); var authorizeState = new HaravanAuthorizationState() { ShopName = shopname, AccessToken = accesstoken }; var client = new HaravanAPIClient(authorizeState); ProductHaravanInfo listproductHaravan = new ProductHaravanInfo() { products = new List <Product>() }; bool check = true; int page = 1; while (check) { Object obj = null; try { //Mỗi lần lấy về 50 product của haravan string querry = "/admin/products.json?page=" + page.ToString(); obj = client.Get(querry); } catch (Exception ex) { check = false; Log.ErrorFormat("HARAVAN : Get/admin/products.json FAILS. ERROR : {0}", ex); } if (obj != null) { ProductHaravanInfo listproduct = JsonConvert.DeserializeObject <ProductHaravanInfo>(obj.ToString()); if (listproduct.products.Count > 0) { listproductHaravan.products.AddRange(listproduct.products); page++; } else { check = false; } } else { check = false; } } if (listproductHaravan.products.Count > 0) { #region Chuyển từ ListProducts of Haravan sang ListProductWebsosanh for (int i = 0; i < listproductHaravan.products.Count; i++) { if (string.IsNullOrEmpty(listproductHaravan.products[i].published_at) || Common.ObjectToDataTime(listproductHaravan.products[i].published_at) > DateTime.Now) { continue; } //Check số lượng phiên bản variant int variant = listproductHaravan.products[i].variants.Count; //TH variant = 1 (1 phiên bản) if (variant == 1) { #region Product var tmpProduct = new QT.Entities.Product(); tmpProduct.Domain = company.Domain; var originalurl = string.Empty; originalurl = company.Website + "products/" + listproductHaravan.products[i].handle; if (company.ID == 7605281528637856297) { tmpProduct.DetailUrl = originalurl + "?ref=10066"; } else { tmpProduct.DetailUrl = originalurl + "?utm=websosanh"; } tmpProduct.ID = Common.GetIDProduct(originalurl); if (listproductHaravan.products[i].variants[0].sku != null) { tmpProduct.MerchantSku = listproductHaravan.products[i].variants[0].sku; } if (listproductHaravan.products[i].variants[0].inventory_policy == "deny") { //check số lượng hàng còn trong kho if (listproductHaravan.products[i].variants[0].inventory_quantity <= 0) { //17.09.2015 trạng thái là deny thì để là liên hệ tmpProduct.Status = Common.ProductStatus.LienHe; //tmpProduct.Status = Common.ProductStatus.Clear; } else { tmpProduct.Status = Common.ProductStatus.Available; } } else if (listproductHaravan.products[i].variants[0].inventory_policy == "continue") { int inventory_quantity = Common.Obj2Int(listproductHaravan.products[i].variants[0].inventory_quantity); if (inventory_quantity > 0) { tmpProduct.Status = Common.ProductStatus.Available; } else { tmpProduct.Status = Common.ProductStatus.Clear; } } else { tmpProduct.Status = Common.ProductStatus.LienHe; } tmpProduct.Instock = QT.Entities.Common.GetProductInstockFormStatus(tmpProduct.Status); tmpProduct.Manufacture = listproductHaravan.products[i].vendor; tmpProduct.Name = listproductHaravan.products[i].title; tmpProduct.HashName = Common.GetHashNameProduct(company.Domain, tmpProduct.Name.Trim()); tmpProduct.ProductContent = listproductHaravan.products[i].body_html; CultureInfo cultureInfo = CultureInfo.GetCultureInfo("vi-VN"); int originPrice = 0; int price = 0; try { originPrice = (int)Decimal.Parse(listproductHaravan.products[i].variants[0].compare_at_price.ToString(), cultureInfo); } catch (FormatException) { } try { price = (int)Decimal.Parse(listproductHaravan.products[i].variants[0].price.ToString(), cultureInfo); } catch (FormatException ex) { Log.Error("HARAVAN : Parse discounted_price Error. " + listproductHaravan.products[i].variants[0].price + "/r/n" + ex); continue; } if (price <= 0) { Log.Error("HARAVAN : Product price equal = 0" + "Product: ID" + tmpProduct.ID + " - " + tmpProduct.Name); continue; } if (originPrice > price) { tmpProduct.OriginPrice = originPrice; tmpProduct.Price = price; } else { tmpProduct.OriginPrice = tmpProduct.Price = price; } //Categories tmpProduct.Categories = new List <string>(); tmpProduct.Categories.Add(company.Domain); tmpProduct.Categories.Add(listproductHaravan.products[i].product_type); tmpProduct.IDCategories = Common.GetIDClassification(Common.ConvertToString(tmpProduct.Categories, " -> ")); // list Url Images if (listproductHaravan.products[i].images != null && listproductHaravan.products[i].images.Count != 0) { tmpProduct.ImageUrls = new List <string>() { listproductHaravan.products[i].images[0].src }; } else { tmpProduct.ImageUrls = new List <string>(); } if (listproductHaravan.products[i].variants[0] != null) { tmpProduct.VATStatus = listproductHaravan.products[i].variants[0].taxable == false ? Common.VATStatus.NotVAT : Common.VATStatus.HaveVAT; } else { tmpProduct.VATStatus = Common.VATStatus.UndefinedVAT; } ListProducts.Add(tmpProduct); #endregion } // TH có nhiều phiên bản else if (variant > 1) { //check image nếu có ảnh thì = true, ko có = false bool checkImage = false; Dictionary <int, string> dicImage = new Dictionary <int, string>(); if (listproductHaravan.products[i].images != null && listproductHaravan.products[i].images.Count != 0) { checkImage = true; for (int k = 0; k < listproductHaravan.products[i].images.Count; k++) { dicImage.Add(listproductHaravan.products[i].images[k].id, listproductHaravan.products[i].images[k].src); } } else { checkImage = false; } for (int j = 0; j < variant; j++) { #region Product var tmpProduct = new QT.Entities.Product(); tmpProduct.Domain = company.Domain; string originalurl = string.Empty; originalurl = company.Website + "products/" + listproductHaravan.products[i].handle + "?index=" + (j + 1).ToString(); //guphukien.com if (company.ID == 7605281528637856297) { tmpProduct.DetailUrl = originalurl + "&ref=10066"; } else { tmpProduct.DetailUrl = originalurl + "&utm=websosanh"; } tmpProduct.ID = Common.GetIDProduct(originalurl); if (listproductHaravan.products[i].variants[j].sku != null) { tmpProduct.MerchantSku = listproductHaravan.products[i].variants[0].sku; } //check status inventory_policy if (listproductHaravan.products[i].variants[j].inventory_policy == "deny") { //check số lượng hàng còn trong kho if (listproductHaravan.products[i].variants[j].inventory_quantity <= 0) { //17.09.2015 trạng thái là deny thì để là liên hệ tmpProduct.Status = Common.ProductStatus.LienHe; //tmpProduct.Status = Common.ProductStatus.Clear; } else { tmpProduct.Status = Common.ProductStatus.Available; } } else if (listproductHaravan.products[i].variants[j].inventory_policy == "continue") { if (listproductHaravan.products[i].variants[j].inventory_quantity <= 0) { tmpProduct.Status = Common.ProductStatus.Clear; } else { tmpProduct.Status = Common.ProductStatus.Available; } } else { tmpProduct.Status = Common.ProductStatus.LienHe; } tmpProduct.Instock = QT.Entities.Common.GetProductInstockFormStatus(tmpProduct.Status); tmpProduct.Manufacture = listproductHaravan.products[i].vendor; string nameproduct = listproductHaravan.products[i].title; if (listproductHaravan.products[i].variants[j].option1 != null) { nameproduct += " " + listproductHaravan.products[i].options[0].name + " " + listproductHaravan.products[i].variants[j].option1; } if (listproductHaravan.products[i].variants[j].option2 != null) { nameproduct += " " + listproductHaravan.products[i].options[1].name + " " + listproductHaravan.products[i].variants[j].option2; } if (listproductHaravan.products[i].variants[j].option3 != null) { nameproduct += " " + listproductHaravan.products[i].options[2].name + " " + listproductHaravan.products[i].variants[j].option3; } tmpProduct.Name = nameproduct; tmpProduct.HashName = Common.GetHashNameProduct(company.Domain, tmpProduct.Name.Trim()); tmpProduct.ProductContent = listproductHaravan.products[i].body_html; CultureInfo cultureInfo = CultureInfo.GetCultureInfo("vi-VN"); int originPrice = 0; int price = 0; try { originPrice = (int)Decimal.Parse(listproductHaravan.products[i].variants[j].compare_at_price.ToString(), cultureInfo); } catch (FormatException) { } try { price = (int)Decimal.Parse(listproductHaravan.products[i].variants[j].price.ToString(), cultureInfo); } catch (FormatException ex) { Log.Error("HARAVAN : Parse discounted_price Error. " + listproductHaravan.products[i].variants[0].price + "/r/n" + ex); continue; } if (price <= 0) { Log.Error("HARAVAN : Product price equal = 0" + "Product: ID " + tmpProduct.ID + " - " + tmpProduct.Name); continue; } if (originPrice > price) { tmpProduct.OriginPrice = originPrice; tmpProduct.Price = price; } else { tmpProduct.OriginPrice = tmpProduct.Price = price; } //Categories tmpProduct.Categories = new List <string>(); tmpProduct.Categories.Add(company.Domain); tmpProduct.Categories.Add(listproductHaravan.products[i].product_type); tmpProduct.IDCategories = Common.GetIDClassification(Common.ConvertToString(tmpProduct.Categories, " -> ")); // list Url Images if (checkImage) { //check image_id của variant int image_id = Common.Obj2Int(listproductHaravan.products[i].variants[j].image_id); //nếu != 0 thì tìm values trong dicImage tương ứng if (image_id != 0) { string imageurl = dicImage.Where(kvp => kvp.Key == image_id).Select(kvp => kvp.Value).FirstOrDefault(); if (!string.IsNullOrEmpty(imageurl)) { tmpProduct.ImageUrls = new List <string>() { imageurl } } ; else { tmpProduct.ImageUrls = new List <string>() { listproductHaravan.products[i].images[0].src } }; } // nếu = 0 (null) thì lấy ảnh mặc định images[0].src else { tmpProduct.ImageUrls = new List <string>() { listproductHaravan.products[i].images[0].src } }; } else { tmpProduct.ImageUrls = new List <string>(); } if (listproductHaravan.products[i].variants[0] != null) { tmpProduct.VATStatus = listproductHaravan.products[i].variants[0].taxable == false ? Common.VATStatus.NotVAT : Common.VATStatus.HaveVAT; } else { tmpProduct.VATStatus = Common.VATStatus.UndefinedVAT; } ListProducts.Add(tmpProduct); #endregion } } else if (variant < 1) { Log.ErrorFormat("HARAVAN : Error : Variant of {0} = 0", listproductHaravan.products[i].title); } } #endregion } return(ListProducts); }