示例#1
0
        public static ProductActionStatus AddProduct(ProductInfo product, Dictionary <string, SKUItem> skus, Dictionary <int, IList <int> > attrs)
        {
            if (null == product)
            {
                return(ProductActionStatus.UnknowError);
            }
            Globals.EntityCoding(product, true);
            int decimalLength = HiContext.Current.SiteSettings.DecimalLength;

            if (product.MarketPrice.HasValue)
            {
                product.MarketPrice = new decimal?(Math.Round(product.MarketPrice.Value, decimalLength));
            }
            product.LowestSalePrice = Math.Round(product.LowestSalePrice, decimalLength);
            ProductActionStatus unknowError = ProductActionStatus.UnknowError;

            using (DbConnection connection = DatabaseFactory.CreateDatabase().CreateConnection())
            {
                connection.Open();
                DbTransaction dbTran = connection.BeginTransaction();
                try
                {
                    ProductProvider provider  = ProductProvider.Instance();
                    int             productId = provider.AddProduct(product, dbTran);
                    if (productId == 0)
                    {
                        dbTran.Rollback();
                        return(ProductActionStatus.DuplicateSKU);
                    }
                    product.ProductId = productId;
                    if (((skus != null) && (skus.Count > 0)) && !provider.AddProductSKUs(productId, skus, dbTran))
                    {
                        dbTran.Rollback();
                        return(ProductActionStatus.SKUError);
                    }
                    if (((attrs != null) && (attrs.Count > 0)) && !provider.AddProductAttributes(productId, attrs, dbTran))
                    {
                        dbTran.Rollback();
                        return(ProductActionStatus.AttributeError);
                    }
                    dbTran.Commit();
                    unknowError = ProductActionStatus.Success;
                }
                catch (Exception)
                {
                    dbTran.Rollback();
                }
                finally
                {
                    connection.Close();
                }
            }
            if (unknowError == ProductActionStatus.Success)
            {
                EventLogs.WriteOperationLog(Privilege.AddProducts, string.Format(CultureInfo.InvariantCulture, "上架了一个新商品:”{0}”", new object[] { product.ProductName }));
            }
            return(unknowError);
        }