/// <summary> /// 下载选择的商品 /// </summary> /// <param name="fts">关键词</param> /// <param name="catId">分类</param> /// <param name="productSetName">商品组名称</param> /// <param name="selectList"></param> internal void DownloadProductsByProducts(string fts, string catId, string productSetName, List<Product> selectList) { var productSetID = new Guid(); foreach (var product in selectList) { using (var dc = new CatalogDataContext()) { if (dc.SS_Products.FirstOrDefault(x => x.product_id == product.getId()) != null) { continue; } } using (var context = new CatalogDataContext()) { var ps = (from set in context.SS_Product_Sets where set.product_set_name == productSetName select set).SingleOrDefault(); if (ps == null) { var productSet = new SS_Product_Set() { id = System.Guid.NewGuid(), product_set_name = productSetName, tb_seller_cid = ProductService.AddSellerCat(productSetName).Cid, datetimecreated = (DateTime?)DateTime.Now }; context.SS_Product_Sets.InsertOnSubmit(productSet); context.SubmitChanges(); productSetID = productSet.id; } else productSetID = ps.id; } #region SS_Products using (var dc = new CatalogDataContext()) { var ssProduct = new SS_Product() { id = System.Guid.NewGuid(), brand_id = (int?)product.getBrand().getId(), click_url = product.getClickUrl(), currency = product.getCurrency().getCurrencyCode(), description = product.getDescription(), chinese_description = getTranslateResult(stripOffHTMLTags(product.getDescription()), true) ?? string.Empty, image_id = product.getImage().getId(), in_stock = product.isInStock(), locale = product.getLocale().getDisplayCountry(), name = product.getName(), chinese_name = product.getBrand().getName() + getTranslateResult(product.getName(), false) ?? string.Empty, price = (decimal?)product.getPrice(), price_label = product.getPriceLabel(), retailer_id = (int?)product.getRetailer().getId(), product_id = (int)product.getId(), sale_price = (decimal?)product.getSalePrice() == 0 ? null : (decimal?)product.getSalePrice(), sale_price_label = product.getSalePriceLabel(), istranslated = false, keyword = fts, datetimecreated = DateTime.Now, product_set_name_id = productSetID }; dc.SS_Products.InsertOnSubmit(ssProduct); dc.SubmitChanges(); } #endregion #region SS_Product_Category_Mapping using (CatalogDataContext dc = new CatalogDataContext()) { SS_Product ssProduct = dc.SS_Products.FirstOrDefault(x => x.product_id == product.getId()); //Insert categories and product-category mapping records if (!string.IsNullOrEmpty(catId)) { SS_Product_Category_Mapping pcMapping = new SS_Product_Category_Mapping() { id = System.Guid.NewGuid(), product_id = ssProduct.product_id, cat_id = catId }; dc.SS_Product_Category_Mappings.InsertOnSubmit(pcMapping); dc.SubmitChanges(); } else { foreach (var category in product.getCategories()) { if (ssProduct != null) { SS_Product_Category_Mapping pcMapping = new SS_Product_Category_Mapping() { id = System.Guid.NewGuid(), product_id = ssProduct.product_id, cat_id = category.getId() }; dc.SS_Product_Category_Mappings.InsertOnSubmit(pcMapping); dc.SubmitChanges(); } else { throw new ArgumentNullException("Could not find the newly inserted product"); } } } } #endregion //insert main image #region insert main image foreach (ImageSize imageSize in product.getImage().getSizes().values().toArray()) { using (CatalogDataContext dc1 = new CatalogDataContext()) { SS_Product ssProduct = dc1.SS_Products.FirstOrDefault(x => x.product_id == product.getId()); SS_Image image = new SS_Image() { id = System.Guid.NewGuid(), size_name = imageSize.getSizeName().toString(), image_id = ssProduct.image_id, url = imageSize.getUrl(), height = imageSize.getHeight(), width = imageSize.getWidth() }; dc1.SS_Images.InsertOnSubmit(image); dc1.SubmitChanges(); } } #endregion //Isert product_color_image_mapping records #region Colors foreach (var color in product.getColors()) { using (CatalogDataContext dc = new CatalogDataContext()) { SS_Product ssProduct = dc.SS_Products.FirstOrDefault(x => x.product_id == product.getId()); SS_Product_Color_Image_Mapping pciMapping = new SS_Product_Color_Image_Mapping(); pciMapping.id = System.Guid.NewGuid(); pciMapping.color_name = color.getName(); pciMapping.product_id = (int)product.getId(); if (color.getImage() == null) { pciMapping.image_id = ssProduct.image_id;//if there's no image element inside color element, that means only one color available. } else { pciMapping.image_id = color.getImage().getId(); } if (color.getImage() != null) { var ssImage = (from image in dc.SS_Images where image.image_id == color.getImage().getId() select image).FirstOrDefault(); if (ssImage == null) { foreach (ImageSize imageSize in color.getImage().getSizes().values().toArray()) { using (CatalogDataContext dc1 = new CatalogDataContext()) { SS_Image image = new SS_Image() { id = System.Guid.NewGuid(), size_name = imageSize.getSizeName().toString(), image_id = pciMapping.image_id, url = imageSize.getUrl(), height = imageSize.getHeight(), width = imageSize.getWidth() }; dc1.SS_Images.InsertOnSubmit(image); dc1.SubmitChanges(); } } } } dc.SS_Product_Color_Image_Mappings.InsertOnSubmit(pciMapping); dc.SubmitChanges(); } } #endregion //Insert size and product_size_mapping #region Size foreach (var size in product.getSizes()) { using (CatalogDataContext dc = new CatalogDataContext()) { SS_Size result = dc.SS_Sizes.FirstOrDefault(x => x.name == size.getName()); if (result == null) { System.Guid sizeId; using (CatalogDataContext dc1 = new CatalogDataContext()) { SS_Size s = new SS_Size() { id = System.Guid.NewGuid(), name = size.getName() }; dc1.SS_Sizes.InsertOnSubmit(s); dc1.SubmitChanges(); sizeId = s.id; } using (CatalogDataContext dc2 = new CatalogDataContext()) { SS_Product_Size_Mapping psMapping = new SS_Product_Size_Mapping() { id = System.Guid.NewGuid(), product_id = (int)product.getId(), size_id = sizeId }; dc2.SS_Product_Size_Mappings.InsertOnSubmit(psMapping); dc2.SubmitChanges(); } } else { SS_Product_Size_Mapping psMapping = new SS_Product_Size_Mapping() { id = System.Guid.NewGuid(), product_id = (int)product.getId(), size_id = result.id }; dc.SS_Product_Size_Mappings.InsertOnSubmit(psMapping); dc.SubmitChanges(); } } } #endregion } }
partial void DeleteSS_Product(SS_Product instance);
public void DownloadProducts(string fts, string catID, string brandFilterID, string retailerFilterID, string priceFilterID, string discountFilterID, string productSetName) { ProductQuery query = new ProductQuery(); if (!string.IsNullOrEmpty(fts)) query.withFreeText(fts); if (!string.IsNullOrEmpty(catID)) query.withCategory(catID); // Category jackets if (!string.IsNullOrEmpty(brandFilterID)) query.withFilter(brandFilterID); // Brand filter id if (!string.IsNullOrEmpty(retailerFilterID)) query.withFilter(retailerFilterID);// Retailer filter id if (!string.IsNullOrEmpty(priceFilterID)) query.withFilter(priceFilterID); // Price filter id if (!string.IsNullOrEmpty(discountFilterID)) query.withFilter(discountFilterID); // Discount filter id var response = api.getProducts(query); ProductSearchMetadata metadata = response.getMetadata(); int total = metadata.getTotal(); // pageCounter = total/limit, these three values are being used to calculate paging. int limit = metadata.getLimit(); int offset = metadata.getOffset(); int pageCount = (int)Math.Ceiling((double)total / 100); System.Guid productSetID; using (CatalogDataContext context = new CatalogDataContext()) { var ps = (from set in context.SS_Product_Sets where set.product_set_name == productSetName select set).SingleOrDefault(); if (ps == null) { SS_Product_Set productSet = new SS_Product_Set() { id = System.Guid.NewGuid(), product_set_name = productSetName, tb_seller_cid = ProductService.AddSellerCat(productSetName).Cid, datetimecreated = (DateTime?)DateTime.Now }; context.SS_Product_Sets.InsertOnSubmit(productSet); context.SubmitChanges(); productSetID = productSet.id; } else productSetID = ps.id; } for (int i = 0; i < pageCount; i++) { PageRequest page = new PageRequest().withLimit(100).withOffset(offset); var productResponse = api.getProducts(query, page, ProductSort.PriceLoHi); foreach (var product in productResponse.getProducts()) { try { using (CatalogDataContext dc = new CatalogDataContext()) { if (dc.SS_Products.FirstOrDefault(x => x.product_id == product.getId()) != null) { continue; } } using (CatalogDataContext dc = new CatalogDataContext()) { SS_Product ssProduct = new SS_Product() { id = System.Guid.NewGuid(), brand_id = (int?)product.getBrand().getId(), click_url = product.getClickUrl(), currency = product.getCurrency().getCurrencyCode(), description = product.getDescription(), chinese_description = getTranslateResult(stripOffHTMLTags(product.getDescription()), true) ?? string.Empty, image_id = product.getImage().getId(), in_stock = product.isInStock(), locale = product.getLocale().getDisplayCountry(), name = product.getName(), chinese_name = product.getBrand().getName() + getTranslateResult(product.getName(), false) ?? string.Empty, price = (decimal?)product.getPrice(), price_label = product.getPriceLabel(), retailer_id = (int?)product.getRetailer().getId(), product_id = (int)product.getId(), sale_price = (decimal?)product.getSalePrice() == 0 ? null : (decimal?)product.getSalePrice(), sale_price_label = product.getSalePriceLabel(), istranslated = false, keyword = fts, datetimecreated = DateTime.Now, product_set_name_id = productSetID }; dc.SS_Products.InsertOnSubmit(ssProduct); dc.SubmitChanges(); } using (CatalogDataContext dc = new CatalogDataContext()) { SS_Product ssProduct = dc.SS_Products.FirstOrDefault(x => x.product_id == product.getId()); //Insert categories and product-category mapping records if (!string.IsNullOrEmpty(catID)) { SS_Product_Category_Mapping pcMapping = new SS_Product_Category_Mapping() { id = System.Guid.NewGuid(), product_id = ssProduct.product_id, cat_id = catID }; dc.SS_Product_Category_Mappings.InsertOnSubmit(pcMapping); dc.SubmitChanges(); } else { foreach (var category in product.getCategories()) { if (ssProduct != null) { SS_Product_Category_Mapping pcMapping = new SS_Product_Category_Mapping() { id = System.Guid.NewGuid(), product_id = ssProduct.product_id, cat_id = category.getId() }; dc.SS_Product_Category_Mappings.InsertOnSubmit(pcMapping); dc.SubmitChanges(); } else { throw new ArgumentNullException("Could not find the newly inserted product"); } } } } //insert main image foreach (ImageSize imageSize in product.getImage().getSizes().values().toArray()) { using (CatalogDataContext dc1 = new CatalogDataContext()) { SS_Product ssProduct = dc1.SS_Products.FirstOrDefault(x => x.product_id == product.getId()); SS_Image image = new SS_Image() { id = System.Guid.NewGuid(), size_name = imageSize.getSizeName().toString(), image_id = ssProduct.image_id, url = imageSize.getUrl(), height = imageSize.getHeight(), width = imageSize.getWidth() }; dc1.SS_Images.InsertOnSubmit(image); dc1.SubmitChanges(); } } //Isert product_color_image_mapping records foreach (var color in product.getColors()) { using (CatalogDataContext dc = new CatalogDataContext()) { SS_Product ssProduct = dc.SS_Products.FirstOrDefault(x => x.product_id == product.getId()); SS_Product_Color_Image_Mapping pciMapping = new SS_Product_Color_Image_Mapping(); pciMapping.id = System.Guid.NewGuid(); pciMapping.color_name = color.getName(); pciMapping.product_id = (int)product.getId(); if (color.getImage() == null) { pciMapping.image_id = ssProduct.image_id;//if there's no image element inside color element, that means only one color available. } else { pciMapping.image_id = color.getImage().getId(); } if (color.getImage() != null) { var ssImage = (from image in dc.SS_Images where image.image_id == color.getImage().getId() select image).FirstOrDefault(); if (ssImage == null) { foreach (ImageSize imageSize in color.getImage().getSizes().values().toArray()) { using (CatalogDataContext dc1 = new CatalogDataContext()) { SS_Image image = new SS_Image() { id = System.Guid.NewGuid(), size_name = imageSize.getSizeName().toString(), image_id = pciMapping.image_id, url = imageSize.getUrl(), height = imageSize.getHeight(), width = imageSize.getWidth() }; dc1.SS_Images.InsertOnSubmit(image); dc1.SubmitChanges(); } } } } dc.SS_Product_Color_Image_Mappings.InsertOnSubmit(pciMapping); dc.SubmitChanges(); } } //Insert size and product_size_mapping foreach (var size in product.getSizes()) { using (CatalogDataContext dc = new CatalogDataContext()) { SS_Size result = dc.SS_Sizes.FirstOrDefault(x => x.name == size.getName()); if (result == null) { System.Guid sizeId; using (CatalogDataContext dc1 = new CatalogDataContext()) { SS_Size s = new SS_Size() { id = System.Guid.NewGuid(), name = size.getName() }; dc1.SS_Sizes.InsertOnSubmit(s); dc1.SubmitChanges(); sizeId = s.id; } using (CatalogDataContext dc2 = new CatalogDataContext()) { SS_Product_Size_Mapping psMapping = new SS_Product_Size_Mapping() { id = System.Guid.NewGuid(), product_id = (int)product.getId(), size_id = sizeId }; dc2.SS_Product_Size_Mappings.InsertOnSubmit(psMapping); dc2.SubmitChanges(); } } else { SS_Product_Size_Mapping psMapping = new SS_Product_Size_Mapping() { id = System.Guid.NewGuid(), product_id = (int)product.getId(), size_id = result.id }; dc.SS_Product_Size_Mappings.InsertOnSubmit(psMapping); dc.SubmitChanges(); } } } } catch (Exception) { continue; } } offset += 100; } }
partial void UpdateSS_Product(SS_Product instance);
partial void InsertSS_Product(SS_Product instance);
private void detach_SS_Products(SS_Product entity) { this.SendPropertyChanging(); entity.SS_Product_Set = null; }
private void attach_SS_Products(SS_Product entity) { this.SendPropertyChanging(); entity.SS_Product_Set = this; }
//先传puffer-coats分类下的 public TB_Product ProductConvert(SS_Product ssproduct) { using (CatalogDataContext dct = new CatalogDataContext()) { TB_Product tbproduct = new TB_Product(); #region 需要计算的字段 long? cid = null; #region cid var allitemcatemapping = dct.Mapping_Categories; var listcategory = from mapping in ssproduct.SS_Product_Category_Mappings select mapping.SS_Category; if (listcategory != null && listcategory.ToList<SS_Category>().Count > 0) { foreach (var item in listcategory) { //判断Mapping_Categories是否有对应关系,如果有则可以找到对应的cid var catemappingentity = allitemcatemapping.Where(i => i.ss_cat_id == item.cat_id).FirstOrDefault(); if (catemappingentity != null) { cid = catemappingentity.tb_cid; break; } //如果没有找到分类的对应关系,查看关键字和分类的组合 var keywordCatEntity = from mapping in allitemcatemapping where mapping.keyword == ssproduct.keyword && mapping.ss_cat_id == item.cat_id select mapping.tb_cid; if (keywordCatEntity != null && keywordCatEntity.Count() > 0) { cid = keywordCatEntity.First(); break; } } } #endregion if (cid == null) { return null; } tbproduct.Cid = cid; List<KeyValuePair<string, string>> listProp = new List<KeyValuePair<string, string>>(); List<KeyValuePair<string, string>> listInputProp = new List<KeyValuePair<string, string>>(); List<KeyValuePair<string, string>> listColors = new List<KeyValuePair<string, string>>(); List<KeyValuePair<string, string>> listSizes = new List<KeyValuePair<string, string>>(); #region props //获取分类下的必填属性 var allItemProp = dct.TB_ItemProps; var keyItemProp = allItemProp.Where(i => i.Cid == cid && i.IsKeyProp == true).ToList(); var allsstbBrandMapping = dct.SS_TB_Brand_Mappings; foreach (var item in keyItemProp) { if (item.Name.Contains("货号")) { listInputProp.Add(new KeyValuePair<string, string>(item.Pid.ToString(), "ss_" + ssproduct.product_id.ToString()));//用SS商品ID作为货号,可以用来搜索 } else if (item.Name.Contains("品牌")) { //品牌 SS_Brand ssBrand = ssproduct.SS_Brand; if (ssBrand != null) { //var DD = allsstbBrandMapping.Where(i => i.brand_name.Equals(ssBrand.brand_name, StringComparison.CurrentCultureIgnoreCase)).ToList(); //var ee = allsstbBrandMapping.Where(i => i.Cid == cid).ToList(); SS_TB_Brand_Mapping sstbBrandMappingEntity = allsstbBrandMapping.Where(i => i.brand_name == ssBrand.brand_name && i.Cid == cid).ToList().FirstOrDefault(); if (sstbBrandMappingEntity != null) { listProp.Add(new KeyValuePair<string, string>(sstbBrandMappingEntity.Pid.ToString(), sstbBrandMappingEntity.Vid.ToString())); } else { listInputProp.Add(new KeyValuePair<string, string>(item.Pid.ToString(), ssproduct.SS_Brand.brand_name)); } } } else { //其它重要属性,面料主成分含量,质地 } } //获得颜色分类属性, 颜色分类属性Pid 1627207,同时获得颜色别名 StringBuilder sbAlias = new StringBuilder(); foreach (var ssColor in ssproduct.SS_Product_Color_Image_Mappings) { var colorProp = (from prop in dct.TB_ItemProps where prop.Cid == cid && prop.IsSaleProp == true && prop.Name.Contains("颜色") select prop).Single(); var color = new KeyValuePair<string, string>(colorProp.Pid.ToString(), dct.Mapping_Colors.Where(m => m.ss_color == ssColor.color_name && m.tb_cid == cid).Single().tb_vid.ToString()); listProp.Add(color); listColors.Add(color); sbAlias.Append(color.Key + ":" + color.Value + ":" + ssColor.color_name + ";"); } //获得尺寸分类属性 foreach (var m in ssproduct.SS_Product_Size_Mappings) { var sizeProp = dct.TB_PropValues.Where(pv => pv.Cid == cid && (pv.Name.Contains("尺码") || pv.Name.Contains("尺寸") || pv.Name.Contains("包袋大小") || pv.Name.Contains("长度") || pv.Name.Contains("周长")) && pv.Name == m.SS_Size.name).SingleOrDefault(); if ( sizeProp != null) { var s = new KeyValuePair<string, string>(sizeProp.Pid.ToString(), sizeProp.Vid.ToString()); listProp.Add(s); listSizes.Add(s); } else { //如果在淘宝属性值里找不到对应的尺寸,那么到尺寸的对应表里去找值。 var sProp = (from prop in dct.TB_ItemProps where prop.Cid == cid && prop.IsSaleProp == true && (prop.Name.Contains("尺码") || prop.Name.Contains("尺寸") || prop.Name.Contains("包袋大小") || prop.Name.Contains("长度") || prop.Name.Contains("周长")) select prop).SingleOrDefault(); var size = dct.Mapping_Sizes.Where(tm => tm.tb_cid == cid && tm.ss_size_name == m.SS_Size.name).FirstOrDefault(); if (size != null && sProp != null) { var s = new KeyValuePair<string, string>(sProp.Pid.ToString(), size.tb_vid.ToString()); listProp.Add(s); listSizes.Add(s); sbAlias.Append(s.Key + ":" + s.Value + ":" + m.SS_Size.name + ";"); } } } //获得必填属性 var mustProps = from props in dct.TB_ItemProps where props.Must == true && props.Cid == cid select props; foreach (var prop in mustProps) { var mustPropMapping = from mcp in dct.Mapping_Categories_Props where mcp.Mapping_Category.tb_cid == prop.Cid && mcp.pid == prop.Pid select mcp; if (mustPropMapping.SingleOrDefault() != null) { var m = new KeyValuePair<string, string>(prop.Pid.ToString(), mustPropMapping.Single().vid.ToString()); listProp.Add(m); } else throw new ApplicationException("没有找到匹配的淘宝必填属性"); } //计算sku属性 string strSkuProps = GetSkuPidVid(listColors, listSizes); int countSku = string.IsNullOrEmpty(strSkuProps) == true ? 0 : strSkuProps.Split(new char[] { ',' }).Count(); #endregion //if (listProp == null || listProp.Count != mustItemProp.Count - 1) //{ // return null; //} tbproduct.Props = GetStrPidVid(listProp); #region ImgFilePath string ImgFilePath = ""; string imgurl = dct.SS_Images.Where(i => i.image_id == ssproduct.image_id && i.size_name == "Original").FirstOrDefault().url; Utils.SaveImage(imgurl, out ImgFilePath); tbproduct.ImgFilePath = ImgFilePath; #endregion tbproduct.PostageId = 804240630;//180-实际80元运费 tbproduct.ChangeProp = null; #endregion decimal exchangeRate = decimal.Parse(ConfigurationManager.AppSettings["ExchangeRate"]); double normalRate = double.Parse(ConfigurationManager.AppSettings["NormalMarginRate"]); double saleRate = double.Parse(ConfigurationManager.AppSettings["SaleMarginRate"]); tbproduct.Title = ssproduct.chinese_name; //Todo: 不能超过30字符. tbproduct.Desc = ssproduct.chinese_description;//字数要大于5个字符,小于25000个字符 if (ssproduct.sale_price == null) { tbproduct.Price = Math.Round(Utils.CalculateSellPrice(ssproduct.price, normalRate, exchangeRate)); } else { tbproduct.Price = Math.Round( Utils.CalculateSellPrice(ssproduct.sale_price, saleRate, exchangeRate)); } tbproduct.StuffStatus = "new"; tbproduct.PropertyAlias = string.IsNullOrEmpty(sbAlias.ToString()) == false ? sbAlias.ToString().Substring(0, sbAlias.Length - 1):string.Empty; tbproduct.Num = countSku == 0 ? 5 : 5 * countSku; tbproduct.Type = "fixed"; tbproduct.LocationState = "海外"; tbproduct.LocationCity = "海外"; tbproduct.ApproveStatus = "instock";//可选值:onsale(出售中),instock(仓库中) tbproduct.FreightPayer = "buyer"; tbproduct.InputPids = GetInputKeys(listInputProp); tbproduct.InputStr = GetInputValues(listInputProp); tbproduct.ValidThru = 7; tbproduct.HasInvoice = false; tbproduct.HasWarranty = false; tbproduct.HasShowcase = false; tbproduct.HasDiscount = false; tbproduct.PostFee = 80; tbproduct.ExpressFee = 600; tbproduct.EmsFee = 600; tbproduct.ListTime = null; tbproduct.Increment = null; tbproduct.AuctionPoint = null; tbproduct.SkuProperties = strSkuProps; tbproduct.SkuQuantities = GetSkuProps("5", countSku); tbproduct.SkuPrices = GetSkuProps(tbproduct.Price.ToString(), countSku); tbproduct.SkuOuterIds = GetSkuProps("", countSku); tbproduct.Lang = "zh_CN"; tbproduct.OuterId = null; tbproduct.IsTaobao = true; tbproduct.IsEx = false; tbproduct.Is3D = false; tbproduct.SellPromise = false; tbproduct.AfterSaleId = null; tbproduct.CodPostageId = null; tbproduct.IsLightningConsignment = false; tbproduct.Weight = null; tbproduct.IsXinpin = null; tbproduct.SubStock = 2L; tbproduct.ItemSize = null; tbproduct.ItemWeight = null; tbproduct.DescModules = null; tbproduct.GlobalStockType = "2";//值为2时代表代购 tbproduct.GlobalStockCountry = "英国"; tbproduct.NumIid = null; tbproduct.IsReplaceSku = null; tbproduct.EmptyFields = null; tbproduct.Status = 1; tbproduct.SellerCids = ssproduct.SS_Product_Set.tb_seller_cid.ToString(); //"841851052"; //TODO: 建立新的分类,名称是ProductSetName tbproduct.PicPath = null; tbproduct.SSProductID = ssproduct.product_id; tbproduct.IsUploaded = false; tbproduct.ID = System.Guid.NewGuid(); return tbproduct; } }