예제 #1
0
        public override void Seed(UmbracoDbContext context)
        {
            var languageCodes           = _cmsContent.GetLanguageIsoCodes(context);
            var productDefinitionFields = LookupProductDefinitionFields(context, true);
            var priceGroupIds           = context.UCommercePriceGroup.Select(pg => pg.PriceGroupId).ToArray();

            var productFamilyIds = context.UCommerceProduct
                                   .Where(p => p.ProductDefinition.UCommerceProductDefinitionField.Any(f =>
                                                                                                       f.IsVariantProperty)) // pick families only
                                   .Where(p => p.ParentProductId == null)                                                    // don't pick variants
                                   .Select(product => new ProductWithDefinition
            {
                ProductId = product.ProductId, ProductDefinitionId = product.ProductDefinitionId, Sku = product.Sku
            })
                                   .ToArray();

            var mediaIds   = _cmsContent.GetAllMediaIds(context);
            var contentIds = _cmsContent.GetAllMediaIds(context);

            var products = GenerateVariants(context, productFamilyIds, mediaIds);

            GenerateDescriptions(context, languageCodes, products);

            GenerateProperties(context, products, productDefinitionFields, mediaIds, contentIds);

            GeneratePrices(context, priceGroupIds, products);
        }
        private void GenerateProperties(UmbracoDbContext context, int[] definitionIds,
                                        IEnumerable <UCommerceProductCatalog> catalogs,
                                        string[] languageCodes)
        {
            Console.Write($"Generating properties for {Count:N0} catalogs...");
            using (var p = new ProgressBar())
            {
                var  mediaIds         = _cmsContent.GetAllMediaIds(context);
                var  contentIds       = _cmsContent.GetAllContentIds(context);
                var  definitionFields = LookupDefinitionFields(context, definitionIds);
                uint batchSize        = 100_000;
                uint numberOfBatches  = definitionFields.Any()
                    ? (uint)Math.Ceiling(
                    1.0 * batchSize / definitionFields.Average(x => x.Count()) / catalogs.Count())
                    : 1;

                var propertiyBatches = catalogs
                                       .Where(catalog => catalog.DefinitionId.HasValue)
                                       // ReSharper disable once PossibleInvalidOperationException
                                       .SelectMany(category => definitionFields[category.DefinitionId.Value].SelectMany(field =>
                                                                                                                        AddEntityProperty(category.Guid, field.Field, languageCodes, mediaIds,
                                                                                                                                          contentIds, field.Editor, field.Enums)))
                                       .Batch(batchSize);

                propertiyBatches.EachWithIndex((properties, index) =>
                {
                    context.BulkInsert(properties.ToList(), options => options.SetOutputIdentity = false);
                    p.Report(1.0 * index / numberOfBatches);
                });
            }
        }
        private void GenerateProperties(DataContext context, int[] definitionIds,
                                        IEnumerable <UCommerceProductCatalogGroup> stores)
        {
            Console.Write($"Generating properties for {Count:N0} stores. ");
            using (var p = new ProgressBar())
            {
                var mediaIds         = _cmsContent.GetAllMediaIds(context);
                var contentIds       = _cmsContent.GetAllContentIds(context);
                var languageCodes    = _cmsContent.GetLanguageIsoCodes(context);
                var definitionFields = LookupDefinitionFields(context, definitionIds);

                uint batchSize       = 100_000;
                uint numberOfBatches = definitionFields.Any()
                    ?  (uint)Math.Ceiling(1.0 * stores.Count() * (uint)definitionFields.Average(x => x.Count()) /
                                          batchSize)
                    : 1;

                var propertyBatches = stores
                                      .Where(store => store.DefinitionId.HasValue)
                                      .SelectMany(store => definitionFields[store.DefinitionId.Value].SelectMany(field =>
                                                                                                                 AddEntityProperty(store.Guid, field.Field, languageCodes, mediaIds,
                                                                                                                                   contentIds, field.Editor, field.Enums)))
                                      .Batch(batchSize);

                propertyBatches.EachWithIndex((properties, index) =>
                {
                    context.Ucommerce.BulkInsert(properties.ToList(), options => options.SetOutputIdentity = false);
                    p.Report(1.0 * index / numberOfBatches);
                });
            }
        }
예제 #4
0
        public override void Seed(DataContext context)
        {
            var productDefinitionIds    = context.Ucommerce.UCommerceProductDefinition.Select(x => x.ProductDefinitionId).ToArray();
            var languageCodes           = _cmsContent.GetLanguageIsoCodes(context);
            var productDefinitionFields = LookupProductDefinitionFields(context, false);
            var priceGroupIds           = context.Ucommerce.UCommercePriceGroup.Select(pg => pg.PriceGroupId).ToArray();
            var productRelationTypeIds  =
                context.Ucommerce.UCommerceProductRelationType.Select(prt => prt.ProductRelationTypeId).ToArray();
            var mediaIds   = _cmsContent.GetAllMediaIds(context);
            var contentIds = _cmsContent.GetAllMediaIds(context);

            var products = GenerateProducts(context, productDefinitionIds, languageCodes, mediaIds);

            GenerateDescriptions(context, languageCodes, products);

            GenerateProperties(context, products, productDefinitionFields, mediaIds,
                               contentIds);

            GeneratePrices(context, priceGroupIds, products);

            GenerateRelations(context, products, productRelationTypeIds);
        }
        public override void Seed(DataContext context)
        {
            var catalogIds    = context.Ucommerce.UCommerceProductCatalog.Select(c => c.ProductCatalogId).ToArray();
            var definitionIds = context.Ucommerce.UCommerceDefinition
                                .Where(d => d.DefinitionTypeId == (int)DefinitionType.Category).Select(c => c.DefinitionId)
                                .ToArray();
            var languageCodes = _cmsContent.GetLanguageIsoCodes(context);
            var mediaIds      = _cmsContent.GetAllMediaIds(context);

            var topLevelCategories = GenerateCategories(context, definitionIds, catalogIds, mediaIds);

            var secondLevelCategories =
                GenerateSubCategories(context, definitionIds, mediaIds, topLevelCategories);

            var categories = topLevelCategories.Concat(secondLevelCategories).ToList();

            GenerateDescriptions(context, categories, languageCodes);

            GenerateProperties(context, definitionIds, categories, languageCodes, mediaIds);
        }
        private void GenerateProperties(DataContext context, IEnumerable <UCommerceDataType> dataTypes,
                                        ILookup <int, DefinitionFieldEditorAndEnum> definitionFields)
        {
            Console.Write($"Generating properties for {Count:N0} data types.");
            using (var p = new ProgressBar())
            {
                var languageCodes = _cmsContent.GetLanguageIsoCodes(context);
                var mediaIds      = _cmsContent.GetAllMediaIds(context);
                var contentIds    = _cmsContent.GetAllContentIds(context);

                UCommerceEntityProperty[] properties = dataTypes
                                                       .Where(dataType => dataType.DefinitionId.HasValue)
                                                       .SelectMany(dataType => definitionFields[dataType.DefinitionId.Value].SelectMany(field =>
                                                                                                                                        AddEntityProperty(dataType.Guid, field.Field, languageCodes, mediaIds,
                                                                                                                                                          contentIds, field.Editor, field.Enums)))
                                                       .ToArray();

                p.Report(0.5);
                context.Ucommerce.BulkInsert(properties, options => options.SetOutputIdentity = false);
            }
        }