private static ProductInfo.ProductEditStatus GetEditStatus(ProductInfo.ProductEditStatus status) { if (status > ProductInfo.ProductEditStatus.EditedAndPending) { return(ProductInfo.ProductEditStatus.CompelPendingHasEdited); } return(ProductInfo.ProductEditStatus.EditedAndPending); }
/// <summary> /// 更新诊疗项目 /// </summary> /// <param name="product">修改后的诊疗项目</param> /// <param name="pics">需要转移的诊疗项目图片地址</param> /// <param name="skus">skus,至少要有一项</param> /// <param name="description">描述</param> /// <param name="attributes">诊疗项目属性</param> /// <param name="goodsCategory">诊所分类</param> /// <param name="sellerSpecifications">诊所自定义规格</param> public static void UpdateProduct(Product product, string[] pics, SKU[] skus, ProductDescription description, ProductAttribute[] attributes, long[] goodsCategory, SellerSpecificationValue[] sellerSpecifications) { Func <string, bool> predicate = null; ProductInfo productInfo = _productService.GetProduct(product.Id); if (productInfo == null) { throw new HimallException("指定id对应的数据不存在"); } ProductInfo.ProductEditStatus editStatus = (ProductInfo.ProductEditStatus)productInfo.EditStatus; if (product.ProductName != productInfo.ProductName) { editStatus = GetEditStatus(editStatus); } if (product.ShortDescription != productInfo.ShortDescription) { editStatus = GetEditStatus(editStatus); } product.AddedDate = productInfo.AddedDate; if (productInfo.SaleStatus != ProductInfo.ProductSaleStatus.InDraft) { product.SaleStatus = productInfo.SaleStatus; } product.AuditStatus = productInfo.AuditStatus; product.DisplaySequence = productInfo.DisplaySequence; product.ShopId = productInfo.ShopId; product.HasSKU = productInfo.HasSKU; product.ImagePath = productInfo.ImagePath; product.SaleCounts = productInfo.SaleCounts; if (pics != null) { if (predicate == null) { predicate = path => string.IsNullOrWhiteSpace(path) || !path.StartsWith(productInfo.ImagePath); } if (pics.Any <string>(predicate)) { editStatus = GetEditStatus(editStatus); } } product.DynamicMap <Himall.DTO.Product.Product, ProductInfo>(productInfo); productInfo.EditStatus = (int)editStatus; SKUInfo[] infoArray = skus.Map <SKUInfo[]>(); ProductDescriptionInfo info = description.Map <ProductDescriptionInfo>(); ProductAttributeInfo[] infoArray2 = attributes.Map <ProductAttributeInfo[]>(); SellerSpecificationValueInfo[] infoArray3 = sellerSpecifications.Map <SellerSpecificationValueInfo[]>(); _productService.UpdateProduct(productInfo, pics, infoArray, info, infoArray2, goodsCategory, infoArray3); CreateHtml(product.Id); }
public JsonResult ImportProductsFromExcel(long shopCategoyId, string filename) { /*供应商产品批量导入*/ if (!string.IsNullOrWhiteSpace(filename)) { string serverFilepath = Server.MapPath("/temp/"); string fullFilePath = serverFilepath + filename; FileInfo file = new FileInfo(fullFilePath); /*计数统计*/ int ErrorCount = 0, SuccessCount = 0; if (file != null && file.Length > 0) { //_importManager.ImportProductsFromXlsx(file.InputStream); // ok, we can run the real code of the sample now using (var xlPackage = new ExcelPackage(file)) { // get the first worksheet in the workbook var worksheet = xlPackage.Workbook.Worksheets.FirstOrDefault(); if (worksheet == null) { return(Json(new { success = false, message = 6 }));//文件为空 } //the columns var properties = new[] { "CategoryId", "ProductName", "ProductCode", "ShortDescription", "Pictures", "MarketPrice", "MinSalePrice", "FreightTemplateId", "Weight", "Volume", "Quantity", "MeasureUnit", "Description", "Meta_Title", "Meta_Description", "Meta_Keywords" }; int iRow = 3;//从Excel第三行开始读取 while (true) { int errorTemp = 0; bool allColumnsAreEmpty = true; for (var i = 1; i <= properties.Length; i++) { if (worksheet.Cells[iRow, i].Value != null && !String.IsNullOrEmpty(worksheet.Cells[iRow, i].Value.ToString())) { allColumnsAreEmpty = false; break; } } if (allColumnsAreEmpty) { break; } long categoryId = Convert.ToInt64(worksheet.Cells[iRow, GetColumnIndex(properties, "CategoryId")].Value); string productName = ConvertColumnToString(worksheet.Cells[iRow, GetColumnIndex(properties, "ProductName")].Value); string productCode = ConvertColumnToString(worksheet.Cells[iRow, GetColumnIndex(properties, "ProductCode")].Value); string shortDescription = ConvertColumnToString(worksheet.Cells[iRow, GetColumnIndex(properties, "ShortDescription")].Value); string pictures = ConvertColumnToString(worksheet.Cells[iRow, GetColumnIndex(properties, "pictures")].Value); decimal marketPrice = Convert.ToDecimal(worksheet.Cells[iRow, GetColumnIndex(properties, "MarketPrice")].Value); decimal minSalePrice = Convert.ToDecimal(worksheet.Cells[iRow, GetColumnIndex(properties, "MinSalePrice")].Value); long freightTemplateId = Convert.ToInt64(worksheet.Cells[iRow, GetColumnIndex(properties, "FreightTemplateId")].Value); decimal weight = Convert.ToDecimal(worksheet.Cells[iRow, GetColumnIndex(properties, "Weight")].Value); decimal volume = Convert.ToDecimal(worksheet.Cells[iRow, GetColumnIndex(properties, "Volume")].Value); int quantity = Convert.ToInt32(worksheet.Cells[iRow, GetColumnIndex(properties, "Quantity")].Value); string measureUnit = ConvertColumnToString(worksheet.Cells[iRow, GetColumnIndex(properties, "MeasureUnit")].Value); string description = ConvertColumnToString(worksheet.Cells[iRow, GetColumnIndex(properties, "Description")].Value); string meta_Title = ConvertColumnToString(worksheet.Cells[iRow, GetColumnIndex(properties, "Meta_Title")].Value); string meta_Description = ConvertColumnToString(worksheet.Cells[iRow, GetColumnIndex(properties, "Meta_Description")].Value); string meta_Keywords = ConvertColumnToString(worksheet.Cells[iRow, GetColumnIndex(properties, "Meta_Keywords")].Value); ProductDetailModel productDetailModel = new ProductDetailModel(); productDetailModel.adWord = shortDescription; productDetailModel.saleStatus = 1; productDetailModel.brandId = 0; productDetailModel.productId = 0; productDetailModel.categoryId = categoryId; if (!string.IsNullOrWhiteSpace(description)) { productDetailModel.des = "<p>" + description + "</p>"; } productDetailModel.FreightTemplateId = freightTemplateId; productDetailModel.goodsName = productName; productDetailModel.mallPrce = minSalePrice.ToString(); productDetailModel.marketPrice = marketPrice.ToString(); productDetailModel.MeasureUnit = measureUnit; productDetailModel.productCode = productCode; productDetailModel.seoDes = meta_Description; productDetailModel.seoKey = meta_Keywords; productDetailModel.seoTitle = meta_Title; productDetailModel.stock = quantity; productDetailModel.Weight = weight; productDetailModel.Volume = volume; productDetailModel.styleTemplateId = new List <long> { 0, 0 }; productDetailModel.goodsCategory = new List <long> { shopCategoyId }; if (pictures != null && pictures.Length > 4) { string[] picArray = pictures.Split(new char[] { ',' }); productDetailModel.pic = new List <string>(picArray); } else { productDetailModel.pic = new List <string>(); } List <AttrSelectData> attrSelectDataList = new List <AttrSelectData> { new AttrSelectData { attrId = 10, valueId = "" }, new AttrSelectData { attrId = 11, valueId = "" } }; productDetailModel.attrSelectData = attrSelectDataList; productDetailModel.specifications = new List <Specifications>(); productDetailModel.specificationsValue = new List <SpecificationsValue>(); IProductService productService = ServiceHelper.Create <IProductService>(); ICategoryService categoryService = ServiceHelper.Create <ICategoryService>(); long num = productDetailModel.productId; ProductInfo productInfo = ProductDetailModel.GetProductInfo(productDetailModel, num); ShopInfo shop = ServiceHelper.Create <IShopService>().GetShop(base.CurrentSellerManager.ShopId, false); int shopAllProducts = productService.GetShopAllProducts(base.CurrentSellerManager.ShopId); ProductInfo.ProductEditStatus editStatus = productService.GetEditStatus(num, productInfo); foreach (string str in productDetailModel.pic) { if (str.IndexOf("Storage") >= 0) { continue; } editStatus = (editStatus <= ProductInfo.ProductEditStatus.EditedAndPending ? ProductInfo.ProductEditStatus.EditedAndPending : ProductInfo.ProductEditStatus.CompelPendingHasEdited); } productInfo.EditStatus = (short)editStatus; if (productDetailModel.productId != 0) { productInfo.ShopId = shopId; productInfo.ImagePath = string.Format("/Storage/Shop/{0}/Products/{1}", shopId, productInfo.Id); productInfo.CategoryPath = categoryService.GetCategory(productInfo.CategoryId).Path; productService.UpdateProduct(productInfo); } else { productInfo.ShopId = base.CurrentSellerManager.ShopId; if (productDetailModel.specificationsValue.Count > 0) { productInfo.MinSalePrice = decimal.Parse(productDetailModel.specificationsValue.Min <SpecificationsValue, string>((SpecificationsValue p) => p.mallPrice)); } productInfo.ImagePath = string.Format("/Storage/Shop/{0}/Products/{1}", shopId, productInfo.Id); productInfo.CategoryPath = categoryService.GetCategory(productInfo.CategoryId).Path; if (ServiceHelper.Create <IShopService>().GetShopSpaceUsage(base.CurrentSellerManager.ShopId) == -1) { return(Json(new { successful = false, msg = "数据提交失败。 原因:店铺存储图片空间不足,不能发布商品!" })); } ShopGradeInfo shopGrade = ServiceHelper.Create <IShopService>().GetShopGrade(shop.GradeId); if (shopGrade != null && shopAllProducts >= shopGrade.ProductLimit) { return(Json(new { successful = false, msg = string.Concat("数据提交失败。 原因:此店铺等级最多只能发布", shopGrade.ProductLimit, "件商品") })); } productService.AddProduct(productInfo); string str1 = string.Format("/Storage/Shop/{0}/Products/{1}", shopId, productInfo.Id); productService.UpdateProductImagePath(productInfo.Id, str1); } ProcessSKU(productDetailModel, productInfo); productService.AddSKU(productInfo); productService.GetSellerSpecifications(shopId, categoryService.GetCategory(productDetailModel.categoryId).TypeId); CategoryInfo category = categoryService.GetCategory(productDetailModel.categoryId); List <SellerSpecificationValueInfo> sellerSpecificationValueInfos = new List <SellerSpecificationValueInfo>(); foreach (Specifications specification in productDetailModel.specifications) { if (!specification.selected) { continue; } SellerSpecificationValueInfo sellerSpecificationValueInfo = new SellerSpecificationValueInfo() { ShopId = shopId, ValueId = specification.Id, Value = specification.newValue, TypeId = category.TypeId }; sellerSpecificationValueInfos.Add(sellerSpecificationValueInfo); } productService.SaveSellerSpecifications(sellerSpecificationValueInfos); int num1 = 1; #region 生成图片 foreach (string str2 in productDetailModel.pic) { //string str3 = Server.MapPath(string.Concat(@"/temp/", str2));//图片位置 string str3 = Server.MapPath(string.Format("/Storage/Original/Shop/{0}/{1}", shopId, str2)); string str4 = Server.MapPath(string.Format("/Storage/Shop/{0}/Products/{1}", shopId, productInfo.Id)); if (string.IsNullOrWhiteSpace(str2)) { string str5 = string.Format("{0}\\{1}.png", str4, num1); if (System.IO.File.Exists(str5)) { System.IO.File.Delete(str5); } IEnumerable <int> dictionary = from t in EnumHelper.ToDictionary <ProductInfo.ImageSize>() select t.Key; foreach (int num2 in dictionary) { string str6 = string.Format("{0}/{1}_{2}.png", str4, num1, num2); if (!System.IO.File.Exists(str6)) { continue; } System.IO.File.Delete(str6); } num1++; } else { try { if (!Directory.Exists(str4)) { Directory.CreateDirectory(str4); } string str7 = string.Format("{0}\\{1}.png", str4, num1);//生成图片的位置和名字 if (System.IO.File.Exists(str3)) { if (str3 != str7) { using (Image image = Image.FromFile(str3))//从temp位置把图片eg:/temp/201607201045475671970.jpg 生成到/Storage/Shop/shopid/Products/productId/ 文件夹下 { image.Save(str7, ImageFormat.Png); IEnumerable <int> nums = from t in EnumHelper.ToDictionary <ProductInfo.ImageSize>() select t.Key; foreach (int num3 in nums)//根据ProductInfo.ImageSize的枚举值生成五张小图 { string str8 = string.Format("{0}/{1}_{2}.png", str4, num1, num3); ImageHelper.CreateThumbnail(str7, str8, num3, num3); } } num1++; } else { num1++; } } } catch (FileNotFoundException fileNotFoundException1) { FileNotFoundException fileNotFoundException = fileNotFoundException1; num1++; ErrorCount++; errorTemp++; Log.Error("发布商品时候,没有找到文件", fileNotFoundException); } catch (ExternalException externalException1) { ExternalException externalException = externalException1; num1++; ErrorCount++; errorTemp++; Log.Error("发布商品时候,ExternalException异常", externalException); } catch (Exception exception1) { Exception exception = exception1; num1++; ErrorCount++; errorTemp++; Log.Error("发布商品时候,Exception异常", exception); } } } #endregion //next product iRow++; if (errorTemp == 0) { SuccessCount++; } } } return(Json(new { success = true, message = 1, ErrorCount = ErrorCount, SuccessCount = SuccessCount })); } else { return(Json(new { success = false, message = 6 }));//"文件为空" } } else { return(Json(new { success = false, message = 6 }));//"没有找到文件!" } }