Beispiel #1
0
        private void InsertChildCategories(ExportCategory category, List <ExportCategory> categoryList, List <ExportProductAttributeValueList> productAttributeValueLists)
        {
            InsertCategory(category, categoryList, productAttributeValueLists);
            var childCategories = categoryList.Where(x => x.ParentCategoryId == category.CategoryId).ToList();

            foreach (var childCategory in childCategories)
            {
                InsertChildCategories(childCategory, categoryList, productAttributeValueLists);
            }
        }
Beispiel #2
0
        private void InsertCategory(ExportCategory category, List <ExportCategory> categoryList, List <ExportProductAttributeValueList> productAttributeValueLists)
        {
            int parentCategoryId = 0;

            if (category.ParentCategoryId != 0)
            {
                parentCategoryId = categoryList.Where(x => x.CategoryId == category.ParentCategoryId).FirstOrDefault().RecievedId;
            }
            int groupId = 0;

            if (category.AttributeValueListId != 0 && category.AttributeValueListId != int.MaxValue)
            {
                groupId = productAttributeValueLists.Where(x => x.Id == category.AttributeValueListId).First().RecievedId;
            }
            var newCategory = new Category()
            {
                CreatedOnUtc     = DateTime.UtcNow,
                UpdatedOnUtc     = DateTime.UtcNow,
                Published        = true,
                DisplayOrder     = 0,
                Name             = category.Title,
                ParentCategoryId = parentCategoryId,
            };

            _categoryService.InsertCategory(newCategory);
            _localizedEntityService.SaveLocalizedValue(newCategory, x => x.Name, category.Title, 1);
            _localizedEntityService.SaveLocalizedValue(newCategory, x => x.Name, category.TitleRu, 2);
            _localizedEntityService.SaveLocalizedValue(newCategory, x => x.Name, category.TitleDe, 3);
            _localizedEntityService.SaveLocalizedValue(newCategory, x => x.Name, category.TitleEs, 4);

            category.RecievedId = newCategory.Id;

            if (groupId != 0)
            {
                var categoryToCategoryAttributeGroup = new CategoryToCategoryProductAttributeGroup()
                {
                    CategoryId = category.RecievedId,
                    CategoryProductAttributeGroupId = groupId,
                    DisplayOrder = 0
                };
                _categoryProductAttributeService.InsertCategoryToCategoryProductAttributeGroup(categoryToCategoryAttributeGroup);
            }
        }
Beispiel #3
0
        public void UploadStructure(Download download)
        {
            var productAttributeList       = new List <ExportProductAttribute>();
            var productAttributeValueLists = new List <ExportProductAttributeValueList>();
            var categoryList = new List <ExportCategory>();
            var binary       = download.DownloadBinary;

            if (binary.Length == 0)
            {
                return;
            }
            using (MemoryStream ms = new MemoryStream(binary))
            {
                ExcelPackage pck = new ExcelPackage(ms);

                ExportProductAttributeValueList currentAttributesValueList = null;
                var currentProductAttribute = new ExportProductAttribute();
                var rootCategory            = new ExportCategory();
                var firstCategory           = new ExportCategory();
                var secondCategory          = new ExportCategory();
                var thirdCategory           = new ExportCategory();
                int categoryIdIncrement     = 1;
                int productAttributeValueListIdIncrement = 1;
                foreach (var workSheet in pck.Workbook.Worksheets)
                {
                    for (int i = 2; i <= workSheet.Dimension.End.Row - workSheet.Dimension.Start.Row + 1; i++)
                    {
                        if (workSheet.Cells[i, 1].Style.Font.Size == 20)
                        {
                            rootCategory = new ExportCategory()
                            {
                                CategoryId       = categoryIdIncrement,
                                ParentCategoryId = 0,
                                Title            = workSheet.GetValue <string>(i, 1).TrimEnd(),
                                TitleRu          = workSheet.GetValue <string>(i, 2).TrimEnd(),
                                TitleDe          = workSheet.GetValue <string>(i, 3).TrimEnd(),
                                TitleEs          = workSheet.GetValue <string>(i, 4).TrimEnd()
                            };
                            firstCategory  = null;
                            secondCategory = null;
                            thirdCategory  = null;
                            categoryList.Add(rootCategory);
                            categoryIdIncrement++;
                        }
                        if (workSheet.Cells[i, 1].Style.Font.Size == 18)
                        {
                            firstCategory = new ExportCategory()
                            {
                                CategoryId       = categoryIdIncrement,
                                ParentCategoryId = rootCategory.CategoryId,
                                Title            = workSheet.GetValue <string>(i, 1).TrimEnd(),
                                TitleRu          = workSheet.GetValue <string>(i, 2).TrimEnd(),
                                TitleDe          = workSheet.GetValue <string>(i, 3).TrimEnd(),
                                TitleEs          = workSheet.GetValue <string>(i, 4).TrimEnd()
                            };
                            if (secondCategory != null)
                            {
                                currentAttributesValueList = null;
                                secondCategory             = null;
                                thirdCategory = null;
                            }
                            else
                            {
                                if (currentAttributesValueList != null)
                                {
                                    if (firstCategory.Title != "Other")
                                    {
                                        firstCategory.AttributeValueListId = currentAttributesValueList.Id;
                                    }
                                    else
                                    {
                                        firstCategory.AttributeValueListId = int.MaxValue;
                                    }
                                }
                            }
                            categoryList.Add(firstCategory);
                            categoryIdIncrement++;
                        }

                        if (workSheet.Cells[i, 1].Style.Font.Size == 16)
                        {
                            secondCategory = new ExportCategory()
                            {
                                CategoryId       = categoryIdIncrement,
                                ParentCategoryId = firstCategory.CategoryId,
                                Title            = workSheet.GetValue <string>(i, 1).TrimEnd(),
                                TitleRu          = workSheet.GetValue <string>(i, 2).TrimEnd(),
                                TitleDe          = workSheet.GetValue <string>(i, 3).TrimEnd(),
                                TitleEs          = workSheet.GetValue <string>(i, 4).TrimEnd()
                            };
                            if (thirdCategory != null)
                            {
                                currentAttributesValueList = null;
                                thirdCategory = null;
                            }
                            else
                            {
                                if (currentAttributesValueList != null)
                                {
                                    if (secondCategory.Title != "Other")
                                    {
                                        secondCategory.AttributeValueListId = currentAttributesValueList.Id;
                                    }
                                    else
                                    {
                                        secondCategory.AttributeValueListId = int.MaxValue;
                                    }
                                }
                            }
                            categoryList.Add(secondCategory);
                            categoryIdIncrement++;
                        }

                        if (workSheet.Cells[i, 1].Style.Font.Size == 14)
                        {
                            thirdCategory = new ExportCategory()
                            {
                                CategoryId       = categoryIdIncrement,
                                ParentCategoryId = secondCategory.CategoryId,
                                Title            = workSheet.GetValue <string>(i, 1).TrimEnd(),
                                TitleRu          = workSheet.GetValue <string>(i, 2).TrimEnd(),
                                TitleDe          = workSheet.GetValue <string>(i, 3).TrimEnd(),
                                TitleEs          = workSheet.GetValue <string>(i, 4).TrimEnd()
                            };
                            if (currentAttributesValueList != null)
                            {
                                if (thirdCategory.Title != "Other")
                                {
                                    thirdCategory.AttributeValueListId = currentAttributesValueList.Id;
                                }
                                else
                                {
                                    thirdCategory.AttributeValueListId = int.MaxValue;
                                }
                            }
                            categoryList.Add(thirdCategory);
                            if (thirdCategory.Title == "Other")
                            {
                                thirdCategory = null;
                            }
                            categoryIdIncrement++;
                        }

                        if (workSheet.Cells[i, 1].Style.Font.Size == 10 && workSheet.Cells[i, 1].Style.Font.Bold)
                        {
                            var fontSize = workSheet.Cells[i - 1, 1].Style.Font.Size;
                            if (fontSize > 10)
                            {
                                var    cat    = categoryList.Last();
                                var    parent = categoryList.Where(x => x.CategoryId == cat.ParentCategoryId).FirstOrDefault();
                                string name   = parent.Title + "-" + cat.Title;
                                string nameRu = parent.TitleRu + "-" + cat.TitleRu;
                                string nameDe = parent.TitleDe + "-" + cat.TitleDe;
                                string nameEs = parent.TitleEs + "-" + cat.TitleEs;
                                currentAttributesValueList = new ExportProductAttributeValueList()
                                {
                                    List   = new List <ExportProductAttributeValue>(),
                                    Id     = productAttributeValueListIdIncrement,
                                    Name   = name,
                                    NameDe = nameDe,
                                    NameRu = nameRu,
                                    NameEs = nameEs
                                };
                                cat.AttributeValueListId = currentAttributesValueList.Id;
                                productAttributeValueListIdIncrement++;
                                productAttributeValueLists.Add(currentAttributesValueList);
                            }

                            var attrVal = workSheet.GetValue <string>(i, 1).TrimEnd();
                            var attr    = productAttributeList.Where(x => x.Name == attrVal).FirstOrDefault();
                            if (attr == null)
                            {
                                productAttributeList.Add(new ExportProductAttribute()
                                {
                                    Name   = attrVal,
                                    NameRu = workSheet.GetValue <string>(i, 2).TrimEnd(),
                                    NameDe = workSheet.GetValue <string>(i, 3).TrimEnd(),
                                    NameEs = workSheet.GetValue <string>(i, 4).TrimEnd()
                                });
                                currentProductAttribute = new ExportProductAttribute()
                                {
                                    Name   = attrVal,
                                    NameRu = workSheet.GetValue <string>(i, 2).TrimEnd(),
                                    NameDe = workSheet.GetValue <string>(i, 3).TrimEnd(),
                                    NameEs = workSheet.GetValue <string>(i, 4).TrimEnd()
                                };
                            }
                            else
                            {
                                currentProductAttribute = attr;
                            }
                        }

                        if (workSheet.Cells[i, 1].Style.Font.Size == 10 && !workSheet.Cells[i, 1].Style.Font.Bold)
                        {
                            currentAttributesValueList.List.Add(new ExportProductAttributeValue()
                            {
                                AttributeTitle = currentProductAttribute.Name,
                                Title          = workSheet.GetValue <string>(i, 1).TrimEnd(),
                                TitleRu        = workSheet.GetValue <string>(i, 2).TrimEnd(),
                                TitleDe        = workSheet.GetValue <string>(i, 3).TrimEnd(),
                                TitleEs        = workSheet.GetValue <string>(i, 4).TrimEnd(),
                            });
                        }
                    }
                }

                var resetCategoryList = categoryList.Where(x => categoryList.Where(c => x.CategoryId == c.ParentCategoryId).FirstOrDefault() != null);
                foreach (var cats in resetCategoryList)
                {
                    cats.AttributeValueListId = 0;
                }
            }

            UpdateDb(categoryList, productAttributeValueLists, productAttributeList);
            _downloadService.DeleteDownload(download);
        }