public ApiResult EditProduct([FromBody] ProductEditOuput parameter)
        {
            if (!this.IsFormValid())
            {
                return(ApiResult.Failure(this.FormInvalidReason()));
            }

            var result = Resolve <IStoreProductService>().EditProduct(parameter);

            return(ToResult(result));
        }
Beispiel #2
0
        /// <summary>
        /// 获取商品编辑和添加视图
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public ProductEditOuput GetProductView(ProductEditInput parameter)
        {
            #region 安全验证

            var viewProduct = new ProductEditOuput();
            if (parameter == null)
            {
                throw new ValidException("参数不能为空");
            }
            var user = Resolve <IUserService>().GetNomarlUser(parameter.UserId);
            if (user == null)
            {
                throw new ValidException("用户不存在");
            }
            var priceStyleConfigs = Resolve <IAutoConfigService>().GetList <PriceStyleConfig>();

            var store = Resolve <IStoreService>().GetUserStore(user.Id);
            if (store == null)
            {
                throw new ValidException("当前用户不存在店铺");
            }

            #endregion 安全验证

            if (parameter.Id > 0)
            {
                #region 商品编辑视图

                viewProduct.Product = Resolve <IProductService>().GetSingle(parameter.Id);
                if (viewProduct.Product == null)
                {
                    throw new ValidException("商品不存在");
                }
                viewProduct.ProductDetail = Resolve <IProductDetailService>().GetSingle(e => e.ProductId == viewProduct.Product.Id);
                if (viewProduct.ProductDetail == null)
                {
                    throw new ValidException("商品详细不存在");
                }

                // 商品扩展属性

                viewProduct.Product.Detail            = viewProduct.ProductDetail;
                viewProduct.Product.ProductExtensions = new ProductExtensions {
                    ProductCategory = viewProduct.Product.Detail.PropertyJson.DeserializeJson <Category>(),
                    ProductThums    = viewProduct.Product.Detail.ImageJson.DeserializeJson <List <ProductThum> >()
                };

                // 商品详情拓展属性
                viewProduct.ProductDetail.ProductDetailExtension = viewProduct.ProductDetail.Extension.DeserializeJson <ProductDetailExtension>();

                var productThums = viewProduct.ProductDetail.ImageJson.DeserializeJson <List <ProductThum> >();
                viewProduct.Images = productThums.Select(o => o.OriginalUrl).ToList();

                //  商品SKU
                viewProduct.ProductSkus =
                    Resolve <IProductSkuService>().GetList(e => e.ProductId == parameter.Id).ToList();

                viewProduct.Store = Resolve <IStoreService>().GetSingle(e => e.Id == viewProduct.Product.StoreId.ToObjectId());
                if (viewProduct.Store == null)
                {
                    throw new ValidException("商品店铺不存在");
                }

                // 商城模式
                viewProduct.PriceStyleConfig = priceStyleConfigs.FirstOrDefault(r => r.Id == viewProduct.Product.PriceStyleId);
                if (viewProduct.PriceStyleConfig == null)
                {
                    throw new ValidException("商城模式不存在");
                }

                //类目处理
                var outCategory = viewProduct.ProductDetail.PropertyJson.ToObject <Category>();
                if (outCategory != null && outCategory.SalePropertys != null && outCategory.SalePropertys.Count > 0 &&
                    outCategory.SalePropertys[0].PropertyValues != null)
                {
                    outCategory.SalePropertys[0].PropertyValues =
                        outCategory.SalePropertys[0].PropertyValues.Where(s => s != null).ToList();
                }
                viewProduct.Category = outCategory;

                viewProduct.Relation = GetRelationView(viewProduct.Store, parameter.UserId);

                viewProduct.Setting = new ProductViewEditSetting {
                    Title = $"{viewProduct.PriceStyleConfig.Name} 商品编辑",
                    IsAdd = false
                };

                #endregion 商品编辑视图
            }
            else
            {
                #region 添加时的安全验证

                // 类目验证
                if (parameter.CategoryId.IsGuidNullOrEmpty())
                {
                    throw new ValidException("商品添加时请传入类目ID");
                }
                viewProduct.Category = Resolve <ICategoryService>().GetSingle(parameter.CategoryId);
                if (viewProduct.Category == null)
                {
                    throw new ValidException("商城类目不存在");
                }
                viewProduct.Product.CategoryId = viewProduct.Category.Id;

                // 商城模式验证
                if (parameter.PriceStyleId.IsGuidNullOrEmpty())
                {
                    throw new ValidException("商品添加时请传入商品模式Id");
                }
                viewProduct.PriceStyleConfig = priceStyleConfigs.FirstOrDefault(r => r.Id == parameter.PriceStyleId);
                if (viewProduct.PriceStyleConfig == null)
                {
                    throw new ValidException("商城模式不存在,请在DIY中重新配置");
                }

                viewProduct.Product.PriceStyleId = viewProduct.PriceStyleConfig.Id;

                #endregion 添加时的安全验证

                #region 商品添加视图

                var config = Resolve <IAutoConfigService>().GetValue <ProductConfig>();
                var maxId  = "" + (Resolve <IProductService>().MaxId() + 1);
                maxId = maxId.PadLeft(3, '0');
                viewProduct.Product = new Product {
                    Bn            = config.Bn + maxId,
                    PurchasePrice = config.PurchasePrice,
                    MarketPrice   = config.MarketPrice,
                    ProductStatus = GoodsStatus.Online,
                    CostPrice     = config.CostPrice,
                    Price         = config.Price,
                    Weight        = config.Weight,
                    Stock         = config.Stock
                };
                viewProduct.ProductDetail = new ProductDetail();

                viewProduct.Product.ProductExtensions = new ProductExtensions {
                    ProductSkus     = new List <ProductSku>(),
                    ProductCategory = viewProduct.Category,
                    ProductThums    = new List <ProductThum>()
                };

                // 店铺
                viewProduct.Store           = store;
                viewProduct.Product.StoreId = store.Id.ToString();

                // 标签、分类、级联
                viewProduct.Relation = GetRelationView(viewProduct.Store, parameter.UserId);
                viewProduct.Product.DeliveryTemplateId = viewProduct.Relation.DeliveryTemplates.FirstOrDefault()?.Key.ToStr();

                viewProduct.Setting = new ProductViewEditSetting {
                    Title = $"{viewProduct.PriceStyleConfig.Name} 商品添加",
                    IsAdd = false
                };

                #endregion 商品添加视图
            }

            if (viewProduct.Product.ProductStatus != GoodsStatus.Online)
            {
                viewProduct.OnSale = false;
            }
            else
            {
                viewProduct.OnSale = true;
            }

            viewProduct.IsAdmin = Resolve <IUserService>().IsAdmin(parameter.UserId);
            return(viewProduct);
        }
Beispiel #3
0
        /// <summary>
        ///     商品保存
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public ServiceResult EditProduct(ProductEditOuput parameter)
        {
            #region 基础数据赋值

            var isAdmin = Resolve <IUserService>().IsAdmin(parameter.UserId);
            parameter.Product.CategoryId = parameter.Category.Id;
            //if (parameter.Product.Id == 0) parameter.Product.StoreId = parameter.Store.Id;

            parameter.ProductDetail.ProductId = parameter.Product.Id;

            #endregion 基础数据赋值

            #region 基础安全验证

            if (parameter.PriceStyleConfig == null)
            {
                return(ServiceResult.Failure("商城模式不能为空"));
            }
            if (parameter.ProductSkus == null || parameter.ProductSkus.Count == 0)
            {
                return(ServiceResult.Failure("商品Sku不能为空"));
            }
            if (parameter.Store == null)
            {
                return(ServiceResult.Failure("店铺不能为空"));
            }
            if (!isAdmin)
            {
                if (parameter.Store.UserId != parameter.UserId)
                {
                    return(ServiceResult.Failure("您无权编辑其他店铺商品"));
                }
            }

            //if (parameter.Product.PurchasePrice > parameter.Product.CostPrice) {
            //    return ServiceResult.Failure("进货价不能大于成本价");
            //}
            //尝试过特性,属性set函数 均无效
            if (parameter.Product.CostPrice > parameter.Product.Price)
            {
                return(ServiceResult.Failure("商品销售价必须为大于等于成本价"));
            }

            if (parameter.Product.CostPrice > parameter.Product.MarketPrice)
            {
                return(ServiceResult.Failure("商品市场价必须为大于等于成本价"));
            }

            //判断sku的价格是否低于成本
            foreach (var item in parameter.ProductSkus)
            {
                if (item.CostPrice > item.Price)
                {
                    return(ServiceResult.Failure("商品SKU销售价必须为大于等于成本价"));
                }

                if (item.CostPrice > item.MarketPrice)
                {
                    return(ServiceResult.Failure("商品SKU市场价必须为大于等于成本价"));
                }

                if (item.FenRunPrice > item.CostPrice || item.FenRunPrice > item.MarketPrice ||
                    item.FenRunPrice > item.MarketPrice)
                {
                    return(ServiceResult.Failure("分润价格不能大于其他价格"));
                }
            }

            if (parameter.Product.StoreId.IsObjectIdNullOrEmpty())
            {
                return(ServiceResult.Failure("请为商品选择店铺"));
            }

            var category = Resolve <ICategoryService>().GetSingle(parameter.Product.CategoryId);
            if (category == null)
            {
                return(ServiceResult.Failure("未选择商品类目,或者商品类目已不存在"));
            }

            if (Repository <IProductRepository>().Count(r => r.Bn == parameter.Product.Bn && r.Id != parameter.Product.Id) >
                0)
            {
                return(ServiceResult.Failure("该货号已存在,请使用其他货号"));
            }

            #endregion 基础安全验证

            #region 商品图片处理

            parameter.ProductDetail.ImageJson =
                Resolve <IProductApiService>().CreateImage(parameter.Product, parameter.Images);

            #endregion 商品图片处理

            #region 核心数据赋值

            if (parameter.Product.PriceStyleId.IsGuidNullOrEmpty())
            {
                // 商城模式
                parameter.Product.PriceStyleId = parameter.PriceStyleConfig.Id;
            }
            parameter.Product.DisplayPrice = Resolve <IProductService>().GetDisplayPrice(parameter.Product.Price,
                                                                                         parameter.Product.PriceStyleId, parameter.Product.MinCashRate);

            parameter.ProductDetail.PropertyJson = ObjectExtension.ToJson(parameter.Category);
            //如果是管理员就直接上架或者下架
            if (isAdmin)
            {
                if (parameter.OnSale)
                {
                    parameter.Product.ProductStatus = GoodsStatus.Online;
                }
                else
                {
                    parameter.Product.ProductStatus = GoodsStatus.SoldOut;
                }
            }
            else
            {
                //如果是供应商直接状态为审核状态
                parameter.Product.ProductStatus = GoodsStatus.Auditing;
            }
            parameter.Product.ModifiedTime = DateTime.Now;

            #endregion 核心数据赋值

            #region 数据保存

            var context = Repository <IProductRepository>().RepositoryContext;
            context.BeginTransaction();
            try {
                parameter.ProductDetail.Extension = ObjectExtension.ToJson(parameter.ProductDetail.ProductDetailExtension);

                if (parameter.Product.Id == 0)
                {
                    Resolve <IProductService>().Add(parameter.Product);             // 添加Shop_product 表
                    parameter.ProductDetail.ProductId = parameter.Product.Id;
                    Resolve <IProductDetailService>().Add(parameter.ProductDetail); // 添加Shop_productdetai表
                }
                else
                {
                    Resolve <IProductService>().Update(parameter.Product);                       // 更新Shop_product 表
                    Resolve <IProductDetailService>().UpdateNoTracking(parameter.ProductDetail); // 更新Shop_productdetai表
                }

                // 更新商品Sku
                var skuResult = Resolve <IProductSkuService>()
                                .AddUpdateOrDelete(parameter.Product, parameter.ProductSkus); // 更新Shop_productsku表
                if (!skuResult.Succeeded)
                {
                    throw new ValidException(skuResult.ToString());
                }

                //添加商品分类和标签
                Resolve <IRelationIndexService>().AddUpdateOrDelete <ProductClassRelation>(parameter.Product.Id,
                                                                                           string.Join(',', parameter.ProductDetail.ProductDetailExtension.StoreClass));
                Resolve <IRelationIndexService>().AddUpdateOrDelete <ProductTagRelation>(parameter.Product.Id,
                                                                                         string.Join(',', parameter.ProductDetail.ProductDetailExtension.Tags));

                context.SaveChanges();
                context.CommitTransaction();
            } catch (Exception ex) {
                context.RollbackTransaction();
                return(ServiceResult.Failure("更新失败:" + ex.Message));
            } finally {
                context.DisposeTransaction();
            }

            #endregion 数据保存

            #region 保存后操作

            //删除缓存
            var cacheKey = "ApiProduct_" + parameter.Product.Id;
            ObjectCache.Remove(cacheKey);

            // 更新商品Sku价格
            //Resolve<IProductSkuService>().AutoUpdateSkuPrice(parameter.Product.Id);
            Resolve <IProductService>().Log($"成功添加一件商品,商品货号:{parameter.Product.Bn}");

            // 更新商品数据到数据中台
            var result = Resolve <IProductGoodsService>().AddOrUpdateGoods(parameter.Product.Id);
            return(result);

            #endregion 保存后操作
        }
        public ProductEditOuput GetProductView(ProductEditInput parameter)
        {
            #region 安全验证

            var productEditOuput = new ProductEditOuput();
            if (parameter == null)
            {
                throw new Exception("参数不能为空");
            }

            var user = Resolve <IUserService>().GetNomarlUser(parameter.UserId);
            if (user == null)
            {
                throw new Exception("用户不存在");
            }

            var store = Resolve <IStoreService>().GetUserStore(user.Id);
            if (store == null)
            {
                throw new Exception("当前用户不存在店铺");
            }

            productEditOuput.Store = store;

            if (parameter.ProductId != 0)
            {
                productEditOuput.Setting = new ProductViewEditSetting {
                    Title = "商品编辑",
                    IsAdd = false
                };
            }

            productEditOuput.Relation = GetRelationView(store, parameter.UserId);

            #endregion 安全验证

            var productView = Resolve <IProductAdminService>().GetViewProductEdit(parameter.ProductId, ObjectId.Empty);
            if (productView == null)
            {
                throw new Exception("商品不存在");
            }

            productEditOuput.Product       = productView.Product;
            productEditOuput.ProductDetail = productView.ProductDetail;

            // 新加商品, 返回默认配置值.
            if (parameter.ProductId == 0)
            {
                if (parameter.CategoryId.IsGuidNullOrEmpty())
                {
                    throw new Exception("商品添加是请传入类目ID");
                }

                var category = Resolve <ICategoryService>().GetSingle(parameter.CategoryId);
                productEditOuput.Category = category;
                if (productEditOuput.Category == null)
                {
                    throw new Exception("类目不存在");
                }

                productEditOuput.Product.DeliveryTemplateId =
                    productEditOuput.Relation.DeliveryTemplates.FirstOrDefault()?.Key.ToStr();
            }
            else
            {
                if (productEditOuput.Product.Id <= 0)
                {
                    throw new Exception("商品不存在");
                }

                productEditOuput.ProductSkus = Resolve <IProductSkuService>()
                                               .GetList(o => o.ProductId == parameter.ProductId)?.ToList();
                if (productEditOuput.ProductSkus == null)
                {
                    throw new Exception("商品SKU不存在");
                }

                var outCategory = productView.ProductDetail.PropertyJson.ToObject <Category>();
                //var categoryEntity = productView.Category;

                //var checkCategory = outCategory.SalePropertys?[0]?.PropertyValues;
                if (outCategory != null && outCategory.SalePropertys != null && outCategory.SalePropertys.Count > 0 &&
                    outCategory.SalePropertys[0].PropertyValues != null)
                {
                    outCategory.SalePropertys[0].PropertyValues =
                        outCategory.SalePropertys[0].PropertyValues.Where(s => s != null).ToList();
                }
                //    productView?.Category?.SalePropertys?[0]?.PropertyValues.Select(s => new CategoryPropertyValue()
                //{
                //    Id = s.Id,
                //    CreateTime = s.CreateTime,
                //    Image = s.Image,
                //    IsCheck = (checkCategory.FirstOrDefault(c => c.Id == s.Id)?.IsCheck).Value,
                //    //ObjectCache = s.ObjectCache,
                //    PropertyId = s.PropertyId,
                //    SortOrder = s.SortOrder,
                //    ValueAlias = checkCategory.FirstOrDefault(c => c.Id == s.Id)?.ValueAlias,
                //    ValueName = s.ValueName,
                //    Version = s.Version
                //}).ToList();

                //productView?.Category?.SalePropertys?.Where(s => s != null)?.Select(s=>return new );
                productEditOuput.Category = outCategory;
                if (productEditOuput.Category == null)
                {
                    throw new Exception("该商品类目不存在");
                }

                // 商品分类,和商品标签,商品副标题会通过此处来回绑定
                if (productView.ProductDetail != null)
                {
                    productView.ProductDetail.ProductDetailExtension =
                        productView.ProductDetail.Extension.ToObject <ProductDetailExtension>();
                    //回绑图片
                    productEditOuput.Images = productView.ProductDetail.ImageJson.DeserializeJson <List <ProductThum> >()
                                              .Select(o => o.OriginalUrl).ToList();
                }

                if (productEditOuput.Product.ProductStatus != ProductStatus.Online)
                {
                    productEditOuput.OnSale = false;
                }
                else
                {
                    productEditOuput.OnSale = true;
                }
            }

            return(productEditOuput);
        }