示例#1
0
 private Boolean IsProductValid(ProductsImportModel product)
 {
     if (!Regex.IsMatch(product.Title, @"^.{1,45}$"))
     {
         return(false);
     }
     if (product.ShortDescription != "" && !Regex.IsMatch(product.ShortDescription, @"^.{1,400}$"))
     {
         return(false);
     }
     if (product.Price <= 0)
     {
         return(false);
     }
     for (int i = 0; i < product.Images.Count; i++)
     {
         if (!Regex.IsMatch(product.Images[i], @"(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\.(?:jpg|gif|png|jpe|jpeg|pjpeg|x-png))(?:\?([^#]*))?(?:#(.*))?"))
         {
             return(false);
         }
     }
     if (!Regex.IsMatch(product.SkuCode, @"^.{1,20}$"))
     {
         return(false);
     }
     if (!Regex.IsMatch(product.Description, @"^.{1,16383}$"))
     {
         return(false);
     }
     for (int i = 0; i < product.Categories.Count; i++)
     {
         if (!Regex.IsMatch(product.Categories[i], @"^.{1,45}$"))
         {
             return(false);
         }
     }
     for (int i = 0; i < product.PropertiesKey.Count; i++)
     {
         if (!Regex.IsMatch(product.PropertiesKey[i], @"^.{1,40}$"))
         {
             return(false);
         }
         if (!Regex.IsMatch(product.PropertiesValue[i], @"^.{1,40}$"))
         {
             return(false);
         }
         if (product.PropertiesKey.Count(a => a == product.PropertiesKey[i]) > 1)
         {
             return(false);
         }
     }
     return(true);
 }
示例#2
0
        public IActionResult UploadProductByExcel(IFormFile file)
        {
            if (file == null)
            {
                return(RedirectToAction("Product", "Admin"));
            }
            ;
            if (!IsExcelFile(file))
            {
                return(RedirectToAction("Product", "Admin"));
            }
            ;

            var filePath = Path.GetTempFileName();

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                file.CopyTo(stream);
            }

            FileInfo fileInfo = new FileInfo(filePath);
            List <ProductsImportModel> importProducts = new List <ProductsImportModel>();

            using (ExcelPackage package = new ExcelPackage(fileInfo))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                int            rowCount  = worksheet.Dimension.Rows;
                int            ColCount  = worksheet.Dimension.Columns;


                for (int row = 2; row <= rowCount; row++)
                {
                    if (IsPropertyLine(worksheet, row) == 1)
                    {
                        importProducts[importProducts.Count - 1].PropertiesKey.Add(worksheet.Cells[row, 8].Text.Trim());
                        importProducts[importProducts.Count - 1].PropertiesValue.Add(worksheet.Cells[row, 9].Text.Trim());
                    }
                    else if (IsCorrectLine(worksheet, row))
                    {
                        ProductsImportModel product = new ProductsImportModel();

                        product.Title            = worksheet.Cells[row, 1].Text.Trim();
                        product.ShortDescription = worksheet.Cells[row, 2].Text.Trim();
                        Decimal price;
                        if (!Regex.IsMatch(worksheet.Cells[row, 3].Text.Trim().Replace('.', ','), @"^\d{0,8}(,\d{1,2})?$"))
                        {
                            return(RedirectToAction("Product", "Admin"));                                                                                               //@"^[0-9]{1,8}[,.][0-9]{2}$"
                        }
                        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

                        if (!Decimal.TryParse(worksheet.Cells[row, 3].Text.Trim().Replace(',', '.'), NumberStyles.Number, CultureInfo.InvariantCulture, out price))
                        {
                            return(RedirectToAction("Product", "Admin"));
                        }
                        product.Price  = price;
                        product.Images = worksheet.Cells[row, 4].Text.Trim().Split(' ').ToList();

                        product.Images      = DeleteEmpty(product.Images);
                        product.SkuCode     = worksheet.Cells[row, 5].Text.Trim();
                        product.Description = worksheet.Cells[row, 6].Text.Trim();

                        product.Categories = worksheet.Cells[row, 7].Text.Trim().Split('/').ToList();
                        if (product.Categories.Count > 3)
                        {
                            return(null);
                        }

                        if (worksheet.Cells[row, 8].Text.Trim() != "" && worksheet.Cells[row, 9].Text.Trim() != "")
                        {
                            product.PropertiesKey.Add(worksheet.Cells[row, 8].Text.Trim());
                            product.PropertiesValue.Add(worksheet.Cells[row, 9].Text.Trim());
                        }

                        if (!IsProductValid(product))
                        {
                            return(RedirectToAction("Product", "Admin"));
                        }
                        if (db.Product.FirstOrDefault(p => p.SkuCode == product.SkuCode) != null)
                        {
                            return(RedirectToAction("Product", "Admin"));
                        }
                        importProducts.Add(product);
                    }
                    else
                    {
                        return(RedirectToAction("Product", "Admin"));
                    }
                }
            }
            AddProducts(importProducts);

            return(RedirectToAction("Product", "Admin"));
        }
示例#3
0
        private Boolean AddProducts(List <ProductsImportModel> excelProducts)
        {
            using (var transaction = db.Database.BeginTransaction())
            {
                Category parentCategory = new Category();
                for (int i = 0; i < excelProducts.Count; i++)
                {
                    ProductsImportModel excelProduct = excelProducts[i];
                    Category            category     = new Category();
                    try
                    {
                        for (int j = 0; j < excelProduct.Categories.Count; j++)
                        {
                            if (j > 0)
                            {
                                category = db.Category.FirstOrDefault(c => c.Title == excelProduct.Categories[j] && c.Parent_id == parentCategory.Id);
                            }
                            else
                            {
                                category = db.Category.FirstOrDefault(c => c.Title == excelProduct.Categories[j]);
                            }
                            if (category == null && j == 0) /*Root category*/
                            {
                                category       = new Category();
                                category.Title = excelProduct.Categories[j];
                                db.Category.Add(category);
                                db.SaveChanges();
                            }
                            else if (category == null)
                            {
                                category           = new Category();
                                category.Title     = excelProduct.Categories[j];
                                category.Parent_id = parentCategory.Id;
                                db.Category.Add(category);
                                db.SaveChanges();
                            }
                            parentCategory = category;
                        }
                    }
                    catch (Exception) { transaction.Rollback(); }
                    Product product = new Product();
                    product.SkuCode     = excelProduct.SkuCode;
                    product.Price       = excelProduct.Price;
                    product.Title       = excelProduct.Title;
                    product.Discount    = excelProduct.Discount;
                    product.Description = excelProduct.Description;
                    //product.ShortDescription = excelProduct.ShortDescription;

                    product.CategoryId = category.Id;//db.Category.FirstOrDefault(c=>c.Title == excelProduct.Categories[excelProduct.Categories.Count-1]).Id;

                    try
                    {
                        db.Product.Add(product);
                        db.SaveChanges();
                    }
                    catch (Exception) { transaction.Rollback(); }

                    try
                    {
                        for (int j = 0; j < excelProduct.Images.Count; j++)
                        {
                            Image image = new Image();
                            image.Id        = Guid.NewGuid().ToString();
                            image.Url       = excelProduct.Images[j];
                            image.ProductId = product.Id;
                            db.Image.Add(image);
                            if (j == 0)
                            {
                                product.MainImageId = image.Id;
                            }
                        }
                        db.SaveChanges();
                    }
                    catch (Exception) { transaction.Rollback(); }

                    try
                    {
                        for (int j = 0; j < excelProduct.PropertiesKey.Count; j++)
                        {
                            ProductProperty property = new ProductProperty();
                            property.Name      = excelProduct.PropertiesKey[j];
                            property.Value     = excelProduct.PropertiesValue[j];
                            property.ProductId = product.Id;
                            db.ProductProperty.Add(property);
                        }
                        db.SaveChanges();
                    }
                    catch (Exception) { transaction.Rollback(); }
                }
                transaction.Commit();
            }
            return(true);
        }