public ApiResult <ProductEditOuput> GetProductView([FromQuery] ProductEditInput parameter)
        {
            if (!this.IsFormValid())
            {
                return(ApiResult.Failure <ProductEditOuput>(this.FormInvalidReason()));
            }
            var result = Resolve <IStoreProductService>().GetProductView(parameter);

            return(ApiResult.Success(result));
        }
 public ApiResult <ProductEditOuput> GetProductView([FromQuery] ProductEditInput parameter)
 {
     try
     {
         var result = Resolve <IStoreProductService>().GetProductView(parameter);
         return(ApiResult.Success(result));
     }
     catch (Exception ex)
     {
         return(ApiResult.Failure <ProductEditOuput>(ex.Message));
     }
 }
Exemple #3
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);
        }
        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);
        }