/// <inheritdoc/> public async Task CreateProduct(OperableProductDto input) { input.CheckNotNull("input"); input.ClassifyId.CheckGreaterThan("input.ClassifyId", 0); if (!_classifyRepository.CheckExists(p => p.Id == input.ClassifyId)) { throw new UserFriendlyException("指定的分类不存在"); } var product = input.MapTo <Domain.Product>(); if (input.IsOnShelf) { product.OnShelfTime = DateTime.Now; } foreach (var group in input.GroupAttributes) { foreach (var item in group.Attributes) { product.Attributes.Add(new ProductAttributeMap { AttributeId = item.Id, Value = item.Value, AttributeOptionIds = item.AttributeType == ProductAttributeType.Switch ? FormatOptionIds(item.attributeOptionId) : item.AttributeType == ProductAttributeType.Multiple ? FormatOptionIds(item.attributeOptionIds.ExpandAndToString()) : "" }); } } product.Assets = input.Albums.Select(p => new ProductAsset { Path = p, AssetType = AssetType.Picture }).ToList(); await _productRepository.InsertAsync(product); }
/// <inheritdoc/> public async Task UpdateProduct(OperableProductDto input) { input.CheckNotNull("input"); input.Id.CheckGreaterThan("input.Id", 0); var product = await _productRepository.GetAsync(input.Id); int onShelfState = 0;//0:未更改;1:上架;2:下架; if (product.IsOnShelf != input.IsOnShelf) { onShelfState = input.IsOnShelf ? 1 : 2; } input.MapTo(product); if (onShelfState == 1) { product.OnShelfTime = DateTime.Now; } if (onShelfState == 2) { product.OffShelfTime = DateTime.Now; } //更新属性 var exsitAttributes = await _productAttributeRepository.GetAll().Where(p => p.ProductId == input.Id).ToListAsync(); foreach (var group in input.GroupAttributes) { foreach (var item in group.Attributes) { var attribute = new ProductAttributeMap { ProductId = input.Id, AttributeId = item.Id, Value = item.Value, AttributeOptionIds = item.AttributeType == ProductAttributeType.Switch ? FormatOptionIds(item.attributeOptionId) : item.AttributeType == ProductAttributeType.Multiple ? FormatOptionIds(item.attributeOptionIds.ExpandAndToString()) : "" }; var exsitAttribute = exsitAttributes.SingleOrDefault(p => p.AttributeId == attribute.AttributeId); if (exsitAttribute == null) { await _productAttributeRepository.InsertAsync(attribute); } else { if (attribute.Value != exsitAttribute.Value || attribute.AttributeOptionIds != exsitAttribute.AttributeOptionIds) { exsitAttribute.Value = attribute.Value; exsitAttribute.AttributeOptionIds = attribute.AttributeOptionIds; await _productAttributeRepository.UpdateAsync(exsitAttribute); } } } } //更新相册 string[] existPaths = product.Assets.Select(m => m.Path).ToArray(); string[] addPaths = input.Albums.Except(existPaths).ToArray(); string[] removePaths = existPaths.Except(input.Albums).ToArray(); foreach (var path in addPaths) { await _assetRepository.InsertAsync(new ProductAsset { Path = path, ProductId = input.Id, AssetType = AssetType.Picture }); } await _assetRepository.DeleteAsync(p => removePaths.Contains(p.Path)); //更新商品 await _productRepository.UpdateAsync(product); }