示例#1
0
        private ProductOptionCollection GetProductOptionCollection(Guid productKey)
        {
            var sql = new Sql();

            sql.Select("*")
            .From <ProductOptionDto>()
            .InnerJoin <Product2ProductOptionDto>()
            .On <ProductOptionDto, Product2ProductOptionDto>(left => left.Key, right => right.OptionKey)
            .Where <Product2ProductOptionDto>(x => x.ProductKey == productKey)
            .OrderBy <Product2ProductOptionDto>(x => x.SortOrder);

            var dtos = Database.Fetch <ProductOptionDto, Product2ProductOptionDto>(sql);

            var productOptions = new ProductOptionCollection();
            var factory        = new ProductOptionFactory();

            foreach (var option in dtos.Select(factory.BuildEntity))
            {
                var attributes = GetProductAttributeCollection(option.Key);
                option.Choices = attributes;
                productOptions.Insert(0, option);
            }

            return(productOptions);
        }
示例#2
0
        private void SaveProductOption(IProduct product, IProductOption productOption)
        {
            var factory = new ProductOptionFactory();

            if (!productOption.HasIdentity)
            {
                ((Entity)productOption).AddingEntity();
                var dto = factory.BuildDto(productOption);

                Database.Insert(dto);
                productOption.Key = dto.Key;

                // associate the product with the product option
                var association = new Product2ProductOptionDto()
                {
                    ProductKey = product.Key,
                    OptionKey  = productOption.Key,
                    SortOrder  = productOption.SortOrder,
                    CreateDate = DateTime.Now,
                    UpdateDate = DateTime.Now
                };

                Database.Insert(association);
            }
            else
            {
                ((Entity)productOption).UpdatingEntity();
                var dto = factory.BuildDto(productOption);
                Database.Update(dto);

                // TODO : this should be refactored
                const string update = "UPDATE merchProduct2ProductOption SET SortOrder = @So, updateDate = @Ud WHERE productKey = @pk AND optionKey = @OKey";

                Database.Execute(update,
                                 new
                {
                    So   = productOption.SortOrder,
                    Ud   = productOption.UpdateDate,
                    pk   = product.Key,
                    OKey = productOption.Key
                });
            }

            // now save the product attributes
            SaveProductAttributes(product, productOption);
        }
        public async Task InitFactories(string url, string account)
        {
            string       baseUrl  = url;
            const string password = "";

            ProductFactory         = new ProductFactory(baseUrl, account, password);
            CategoryFactory        = new CategoryFactory(baseUrl, account, password);
            StockFactory           = new StockAvailableFactory(baseUrl, account, password);
            FeatureValuesFactory   = new ProductFeatureValueFactory(baseUrl, account, password);
            ImageFactory           = new ImageFactory(baseUrl, account, password);
            ProductSupplierFactory = new ProductSupplierFactory(baseUrl, account, password);

            ManufacturerFactory = new ManufacturerFactory(baseUrl, account, password);

            SpecialPriceFactory = new SpecificPriceFactory(baseUrl, account, password);

            var featuresFactory = new ProductFeatureFactory(baseUrl, account, password);
            var features        = await featuresFactory.GetAll();

            //SizeFeature = features.FirstOrDefault(f => f.name.First().Value.Equals("Размер", StringComparison.OrdinalIgnoreCase));
            //ColorFeature = features.FirstOrDefault(f => f.name.First().Value.Equals("Цвет", StringComparison.OrdinalIgnoreCase));

            BatteryFeature = features.FirstOrDefault(f => f.name.First().Value.Equals("Батарейки", StringComparison.OrdinalIgnoreCase));
            if (BatteryFeature == null)
            {
                BatteryFeature = new product_feature()
                {
                    name = new List <language> {
                        new language(1, "Батарейки")
                    }
                };
                BatteryFeature = await featuresFactory.Add(BatteryFeature);
            }

            MaterialFeature = features.FirstOrDefault(f => f.name.First().Value.Equals("Материал", StringComparison.OrdinalIgnoreCase));
            if (MaterialFeature == null)
            {
                MaterialFeature = new product_feature()
                {
                    name = new List <language> {
                        new language(1, "Материал")
                    }
                };
                MaterialFeature = await featuresFactory.Add(MaterialFeature);
            }

            CountryFeature = features.FirstOrDefault(f => f.name.First().Value.Equals("Страна", StringComparison.OrdinalIgnoreCase));
            if (CountryFeature == null)
            {
                CountryFeature = new product_feature()
                {
                    name = new List <language> {
                        new language(1, "Страна")
                    }
                };
                CountryFeature = await featuresFactory.Add(CountryFeature);
            }

            PackingFeature = features.FirstOrDefault(f => f.name.First().Value.Equals("Упаковка", StringComparison.OrdinalIgnoreCase));
            if (PackingFeature == null)
            {
                PackingFeature = new product_feature()
                {
                    name = new List <language> {
                        new language(1, "Упаковка")
                    }
                };
                PackingFeature = await featuresFactory.Add(PackingFeature);
            }

            LengthFeature = features.FirstOrDefault(f => f.name.First().Value.Equals("Длина", StringComparison.OrdinalIgnoreCase));
            if (LengthFeature == null)
            {
                LengthFeature = new product_feature()
                {
                    name = new List <language> {
                        new language(1, "Длина")
                    }
                };
                LengthFeature = await featuresFactory.Add(LengthFeature);
            }

            DiameterFeature = features.FirstOrDefault(f => f.name.First().Value.Equals("Диаметр", StringComparison.OrdinalIgnoreCase));
            if (DiameterFeature == null)
            {
                DiameterFeature = new product_feature()
                {
                    name = new List <language> {
                        new language(1, "Диаметр")
                    }
                };
                DiameterFeature = await featuresFactory.Add(DiameterFeature);
            }

            var optionsFactory = new ProductOptionFactory(baseUrl, account, password);
            var options        = await optionsFactory.GetAll();

            SizeOption = options.FirstOrDefault(f => f.name.First().Value.Equals("size", StringComparison.OrdinalIgnoreCase));
            if (SizeOption == null)
            {
                Log.Error("Size option not found, add size option!");
                throw new Exception("Size option not found");
            }

            ColorOption = options.FirstOrDefault(f => f.name.First().Value.Equals("color", StringComparison.OrdinalIgnoreCase));
            if (ColorOption == null)
            {
                Log.Error("Color option not found, add size option!");
                throw new Exception("Color option not found");
            }

            OptionsValueFactory = new ProductOptionValueFactory(baseUrl, account, password);

            CombinationFactory = new CombinationFactory(baseUrl, account, password);

            SupplierFactory = new SupplierFactory(baseUrl, account, password);
            Suppliers       = await SupplierFactory.GetAll();

            if (!Suppliers.Any())
            {
                throw new Exception("Suppliers not found, add them manualy");
            }
        }