private async Task UpdateProductAttributeValuesAsync(ProductAttributeMappingDto productAttributeMappingDto, Delta <ProductDto> productDtoDelta)
        {
            // If no product attribute values are specified means we don't have to update anything
            if (productAttributeMappingDto.ProductAttributeValues == null)
            {
                return;
            }

            // delete unused product attribute values
            var toBeUpdatedIds = productAttributeMappingDto.ProductAttributeValues.Where(y => y.Id != 0).Select(x => x.Id);

            var unusedProductAttributeValues = (await _productAttributeService.GetProductAttributeValuesAsync(productAttributeMappingDto.Id)).Where(x => !toBeUpdatedIds.Contains(x.Id)).ToList();

            foreach (var unusedProductAttributeValue in unusedProductAttributeValues)
            {
                await _productAttributeService.DeleteProductAttributeValueAsync(unusedProductAttributeValue);
            }

            foreach (var productAttributeValueDto in productAttributeMappingDto.ProductAttributeValues)
            {
                if (productAttributeValueDto.Id > 0)
                {
                    // update existing product attribute mapping
                    var productAttributeValueToUpdate = await _productAttributeService.GetProductAttributeValueByIdAsync(productAttributeValueDto.Id);

                    if (productAttributeValueToUpdate != null)
                    {
                        productDtoDelta.Merge(productAttributeValueDto, productAttributeValueToUpdate, false);

                        await _productAttributeService.UpdateProductAttributeValueAsync(productAttributeValueToUpdate);
                    }
                }
                else
                {
                    var newProductAttributeValue = new ProductAttributeValue();
                    productDtoDelta.Merge(productAttributeValueDto, newProductAttributeValue);

                    newProductAttributeValue.ProductAttributeMappingId = productAttributeMappingDto.Id;
                    // add new product attribute value
                    await _productAttributeService.InsertProductAttributeValueAsync(newProductAttributeValue);
                }
            }
        }
Esempio n. 2
0
        private async Task MergeFramesAsync(AbcMattressModel model, ProductAttribute pa, Product product)
        {
            var pam = (await _productAttributeService.GetProductAttributeMappingsByProductIdAsync(product.Id))
                      .Where(pam => pam.ProductAttributeId == pa.Id)
                      .FirstOrDefault();
            var abcMattressEntry = _abcMattressEntryService.GetAbcMattressEntriesByModelId(model.Id)
                                   .Where(ame => pa.Name == $"Frame ({ame.Size})")
                                   .FirstOrDefault();

            if (abcMattressEntry == null)
            {
                return;
            }

            var frames = _abcMattressFrameService.GetAbcMattressFramesBySize(abcMattressEntry.Size);

            if (pam == null && frames.Any())
            {
                var sizeAttrs = await GetSizeAttributesAsync(product, abcMattressEntry);

                pam = new ProductAttributeMapping()
                {
                    ProductId             = product.Id,
                    ProductAttributeId    = pa.Id,
                    IsRequired            = false,
                    AttributeControlType  = AttributeControlType.DropdownList,
                    DisplayOrder          = 30,
                    TextPrompt            = "Frame",
                    ConditionAttributeXml = $"<Attributes><ProductAttribute ID=\"{sizeAttrs.pam.Id}\"><ProductAttributeValue><Value>{sizeAttrs.pav.Id}</Value></ProductAttributeValue></ProductAttribute></Attributes>"
                };
                await _productAttributeService.InsertProductAttributeMappingAsync(pam);
            }
            else if (pam != null && !frames.Any())
            {
                await _productAttributeService.DeleteProductAttributeMappingAsync(pam);
            }
            else if (pam != null)
            {
                await UpdatePamAsync(product, pam, abcMattressEntry);
            }

            if (!frames.Any())
            {
                return;
            }

            var existingFrames = (await _productAttributeService.GetProductAttributeValuesAsync(pam.Id))
                                 .Where(pav =>
                                        pav.ProductAttributeMappingId == pam.Id
                                        );
            var newFrames = frames.Select(np => np.ToProductAttributeValue(
                                              pam.Id
                                              )).OrderBy(f => f.PriceAdjustment).ToList();

            ApplyDisplayOrder(newFrames);

            var toBeDeleted = existingFrames
                              .Where(e => !newFrames.Any(n => n.Name == e.Name && n.DisplayOrder == e.DisplayOrder &&
                                                         n.PriceAdjustment == e.PriceAdjustment));
            var toBeInserted = newFrames
                               .Where(n => !existingFrames.Any(e => n.Name == e.Name && n.DisplayOrder == e.DisplayOrder &&
                                                               n.PriceAdjustment == e.PriceAdjustment));

            toBeInserted.ToList().ForEach(async n => await _productAttributeService.InsertProductAttributeValueAsync(n));
            toBeDeleted.ToList().ForEach(async e => await _productAttributeService.DeleteProductAttributeValueAsync(e));
        }