private CartProduct ConvertToCartProduct(ProductSearchResult product, int quantity)
 {
     if (product != null && product.InventoryNumber >0) //若库存不足,则不允许下单
     {
         var cartProduct = new CartProduct
                               {
                                   ProductID = product.ID,
                                   ProductName = product.Name,
                                   GoujiuPrice = product.GoujiuPrice,
                                   Discount = 0,
                                   ProductPic = product.ThumbnailPath,
                                   Quantity =
                                       quantity > product.InventoryNumber
                                           ? product.InventoryNumber
                                           : quantity,
                                   UpdateTime = DateTime.Now
                               };
         return cartProduct;
     }
     return null;
 }
        public string GetHtmlByTemplete(string htmlTemplete, ProductSearchResult product)
        {
            var properties = TypeDescriptor.GetProperties(product);

            if (htmlTemplete.Contains("$Path$"))
            {
                htmlTemplete = htmlTemplete.Replace("$Path$", Utils.GetProductImage(product.Path));
            }

            if (htmlTemplete.Contains("$ThumbnailPath$"))
            {
                htmlTemplete = htmlTemplete.Replace("$ThumbnailPath$", Utils.GetProductImage(product.Path, "2"));
            }

            foreach (PropertyDescriptor propertyDescriptror in properties)
            {
                htmlTemplete = htmlTemplete.Replace("$" + propertyDescriptror.Name + "$", Utils.ToString(propertyDescriptror.GetValue(product)));
            }
            return htmlTemplete;
        }
 private CartProduct ConvertToCartProduct(ProductSearchResult product,int quantity)
 {
     var cartProduct = new CartProduct();
     cartProduct.GoujiuPrice = product.GoujiuPrice;
     cartProduct.ProductName = product.Name;
     cartProduct.Quantity = quantity;
     cartProduct.ProductID = product.ID;
     cartProduct.Discount = 0;
     cartProduct.UpdateTime = DateTime.Now;
     cartProduct.ProductPic = product.ThumbnailPath;
     return cartProduct;
 }
        /// <summary>
        /// 检查库存、限购信息并设置购买数量
        /// </summary>
        /// <param name="product"></param>
        /// <param name="updateQuantity"></param>
        /// <returns></returns>
        private AjaxResponse SetUpdateQuantity(ProductSearchResult product, ref int updateQuantity)
        {
            AjaxResponse ajaxResponse = null;

            ajaxResponse = CheckSpecialPromote(product.ID, ref updateQuantity);

            //此商品参加了特殊全局促销活动
            if (ajaxResponse != null)
            {
                return ajaxResponse;
            }

            var billProduct = new OrderBillServices().QueryCartProduct(product.ID, updateQuantity, this.UserSession.UserID);

            //不允许购买情况
            if (billProduct.DenyFlag > 0)
            {
                switch (billProduct.DenyFlag)
                {
                    case 1:
                        ajaxResponse = new AjaxResponse(
                            11,
                            "对不起,此商品只允许新会员购买!");
                        updateQuantity = 0;
                        return ajaxResponse;
                    case 2:
                        ajaxResponse = new AjaxResponse(
                            12,
                            "对不起,此商品只允许老会员购买!");
                        updateQuantity = 0;
                        return ajaxResponse;
                    case 3:
                        ajaxResponse = new AjaxResponse(
                            13,
                            "对不起,此商品只允许通过手机验证会员购买。");
                        updateQuantity = 0;
                        return ajaxResponse;
                    case 4:
                        ajaxResponse = new AjaxResponse(
                            14,
                            "对不起,此商品只允许通过邮箱验证会员购买。");
                        updateQuantity = 0;
                        return ajaxResponse;
                    case 5:
                        ajaxResponse = new AjaxResponse(
                            15,
                            "对不起,此商品仅限新会员购买,请先注册或登录。");
                        updateQuantity = 0;
                        return ajaxResponse;
                    case 6:
                        ajaxResponse = new AjaxResponse(
                            16,
                            "对不起,您已参加过此活动。");
                        updateQuantity = 0;
                        return ajaxResponse;
                    default:
                        ajaxResponse = new AjaxResponse(
                            0,
                            "对不起,您不满足此商品的购买条件。");
                        updateQuantity = 0;
                        return ajaxResponse;
                }
            }

            var promnoteItems = billProduct.PromoteTypes.Split(',');

            //验证多选一互斥促销
            if (promnoteItems.Contains("4")&&!string.IsNullOrWhiteSpace(billProduct.Exclude))
            {
                var userCart = MongoDBHelper.GetModel<UserCartModel>(u => u.VisitorKey == this.UserSession.VisitorKey);
                if (userCart != null && userCart.ProductItems.Count > 0)
                {
                    var excludes = billProduct.Exclude.Split(',');
                    if (excludes.Length > 0)
                    {
                        foreach (var item in userCart.ProductItems)
                        {
                            if (item.ProductID != billProduct.ProductID && excludes.Contains(item.ProductID.ToString()))
                            {
                                ajaxResponse = new AjaxResponse(
                                    0,
                                    string.Format("对不起,【{0}】与【{1}】不能同时购买。", item.ProductName, billProduct.ProductName));
                                updateQuantity = 0;
                                return ajaxResponse;
                            }
                        }
                    }
                }
            }

            var islimited = promnoteItems.Contains("1"); // 是否参加限时抢购
            if (islimited)
            {
                if (product.InventoryNumber <= 0)
                {
                    ajaxResponse = new AjaxResponse(
                        0,
                        "对不起,商品已售完!",
                        new { quantity = updateQuantity, totalDiscount = billProduct.FavorablePrice });
                    updateQuantity = 0;
                }
                else if (billProduct.PromoteResidueQuantity <= 0)
                {
                    ajaxResponse = new AjaxResponse(
                        0,
                        "对不起,活动商品库存不足!",
                        new { quantity = updateQuantity, totalDiscount = billProduct.FavorablePrice });
                    updateQuantity = 0;
                }
                else
                {
                    // 检查此商品是否限购件数(不限购)
                    if (billProduct.LimitedBuyQuantity <= 0)
                    {
                        if (billProduct.PromoteResidueQuantity < updateQuantity)
                        {
                            // 购物车数量必须小于活动库存
                            ajaxResponse = new AjaxResponse(
                                0,
                                "对不起,此商品库存不足!你最多可购买" + billProduct.PromoteResidueQuantity + "件",
                                new { quantity = updateQuantity, totalDiscount = billProduct.FavorablePrice });
                            updateQuantity = billProduct.PromoteResidueQuantity;
                        }
                    }
                    else
                    {
                        // 每人限购
                        if (billProduct.MaxBuyQuantity < updateQuantity)
                        {

                            ajaxResponse = new AjaxResponse(
                                0,
                                string.Format("对不起,此商品每人限购{0}件", billProduct.LimitedBuyQuantity),
                                new { quantity = updateQuantity, totalDiscount = billProduct.FavorablePrice });
                            updateQuantity = billProduct.MaxBuyQuantity;
                        }
                    }
                }
            }
            else if (product.InventoryNumber < updateQuantity) // 检查库存
            {
                if (product.InventoryNumber > 0)
                {
                    ajaxResponse = new AjaxResponse(
                        0,
                        "对不起,此商品的库存不足,您最多可购买" + product.InventoryNumber + "件",
                        new { quantity = product.InventoryNumber, totalDiscount = billProduct.FavorablePrice });
                    //cart.ProductItems.Add(this.ConvertToCartProduct(product, product.InventoryNumber)); //库存不够,则设置库存值
                    updateQuantity = product.InventoryNumber;
                }
                else
                {
                    ajaxResponse = new AjaxResponse(
                        0,
                        "对不起,此商品已售完",
                        new { quantity = product.InventoryNumber, totalDiscount = billProduct.FavorablePrice });
                    //cart.ProductItems.Add(this.ConvertToCartProduct(product, product.InventoryNumber)); //库存不够,则设置库存值
                    updateQuantity = 0;
                }
            }

            return ajaxResponse;
        }
        /// <summary>
        /// 添加商品到购物车
        /// </summary>
        /// <param name="updateQuantity"></param>
        /// <param name="product"></param>
        /// <param name="cart"></param>
        /// <returns></returns>
        private AjaxResponse AddCartProducts(int updateQuantity, ProductSearchResult product, ref UserCartModel cart)
        {
            if (cart.ProductItems == null)
            {
                cart.ProductItems = new List<CartProduct>();
            }

            AjaxResponse ajaxResponse = SetUpdateQuantity(product, ref updateQuantity);

            if (updateQuantity > 0)
            {
                cart.ProductItems.Add(this.ConvertToCartProduct(product, updateQuantity)); //设置购买数量
            }

            return ajaxResponse;
        }
        public string GetHtmlByTemplete(string htmlTemplete, ProductSearchResult product)
        {
            var properties = TypeDescriptor.GetProperties(product);

            if (htmlTemplete.Contains("$Path$"))
            {
                htmlTemplete = htmlTemplete.Replace("$Path$", Utils.GetProductImage(product.Path));
            }

            if (htmlTemplete.Contains("$ThumbnailPath$"))
            {
                htmlTemplete = htmlTemplete.Replace("$ThumbnailPath$", Utils.GetProductImage(product.Path, "1"));
            }

            if (htmlTemplete.Contains("$ThumbnailPath2$"))
            {
                htmlTemplete = htmlTemplete.Replace("$ThumbnailPath2$", Utils.GetProductImage(product.Path, "2"));
            }

            if (htmlTemplete.Contains("$Sold$"))
            {
                htmlTemplete = htmlTemplete.Replace("$Sold$", Utils.ToString(product.SoldOfReality + product.SoldOfVirtual));
            }

            var cart = new OrderBillServices().QueryCartProduct(product.ID);
            if (cart != null)
            {
                htmlTemplete = htmlTemplete.Replace("$GoujiuPrice$", Utils.ToString(cart.PromotePrice));
            }

            foreach (PropertyDescriptor propertyDescriptror in properties)
            {
                htmlTemplete = htmlTemplete.Replace("$" + propertyDescriptror.Name + "$", Utils.ToString(propertyDescriptror.GetValue(product)));
            }
            return htmlTemplete;
        }
        public string GetGroupItemHtml(ProductSearchResult product, bool end)
        {
            double marketprice = product.MarketPrice == null ? 0 : product.MarketPrice;
            double goujiuprice = product.GoujiuPrice == null ? 0 : product.GoujiuPrice;
            string discount = marketprice == 0 ? "" : (Math.Round(goujiuprice / marketprice, 2) * 10).ToString();

            return "<dl class=\"group_item " + (end ? "group_item_end" : "") + "\">" +
              "	<dt class=\"group_item_top image_group\"><em class=\"tp\">直降</em><em class=\"btm\">" + Utils.ToString(marketprice - goujiuprice) + "</em></dt>" +
              "	<dd class=\"group_item_list shadow\">" +
              "		<a href=\"/Home/TuanItem/" + product.ID.ToString() + ".html\" title=\"" + Utils.ToString(product.Name) + "\" target=\"_blank\">" +
              "		<dl>" +
              "			<div class=\"group_item_image\"><img class=\"lazy\" data-original=" + Utils.ToString(product.ThumbnailPath) + " alt=\"\" /></div>" +
              "			<div class=\"group_item_name\">" + Utils.ToString(product.Name) + "</div>" +
              "		</dl>" +
              "		<div class=\"group_item_price\">" +
              "			<div class=\"item_price\">&#65509;<em class=\"goujiu_price\">" + Utils.ToString(product.GoujiuPrice) + "</em></div>" +
              "			<div class=\"item_discount\">" +
              "				<em class=\"discount\">" + discount + "折</em>" +
              "				<em class=\"market_price\">&#65509;" + Utils.ToString(product.MarketPrice) + "</em>" +
              "			</div>" +
              "			<div class=\"item_buyers\">" +
              "				<i class=\"image_group\"></i><em class=\"buyers\"><em>" + Utils.ToString(product.SoldOfReality + product.SoldOfVirtual) + "</em>人已购买</em>" +
              "			</div>" +
              "			<div class=\"clear\"></div>                 " +
              "		</div></a>" +
              "	</dd>" +
              "</dl>";
        }
 public string GetDiscount(ProductSearchResult product)
 {
     if (product.MarketPrice == 0) return "0";
     return Math.Floor(product.GoujiuPrice * 10.00 / product.MarketPrice).ToString();
 }
        public string GetTuanSilderColumnHtml(ProductSearchResult product)
        {
            var cart = new OrderBillServices().QueryCartProduct(product.ID);
            if (cart != null)
            {
                product.GoujiuPrice= cart.PromotePrice;
            }
            double marketprice = product.MarketPrice == null ? 0 : product.MarketPrice;
            double goujiuprice = product.GoujiuPrice == null ? 0 : product.GoujiuPrice;
            string discount = marketprice == 0 ? "" : (Math.Round(goujiuprice / marketprice, 2) * 10).ToString();
            long time = GetTime();

            System.Diagnostics.Trace.WriteLine(time.ToString());

            return "<div class=\"silder_column_image\" style=\"background-image:url(" + Utils.ToString(product.ThumbnailPath) + ");\" ></div>" +
            "<ul class=\"silder_column_intro\">" +
            "    <li class=\"silder_column_name\"><i class=\"image_group\"></i><span>" + Utils.ToString(product.Name) + "</span></li>" +
            "    <li class=\"silder_column_desc\">" + Utils.ToString(product.Advertisement, "没有任何描述信息,请添加描述信息") + "</li>" +
            "    <li class=\"silder_column_price\">" +
            "        <div class=\"goujiu_price\">&#65509;<span>" + Utils.ToString(product.GoujiuPrice) + "</span></div>" +
            "        <div class=\"goujiu_discount\">" +
            "            <div class=\"discount\"><span>" + discount + "</span>折</div>" +
            "            <div class=\"market_price\">&#65509;<span>" + Utils.ToString(product.MarketPrice) + "</span></div>" +
            "        </div>" +
            "        <div class=\"split\"></div>" +
            "        <a href=\"###\" class=\"image_group buy\"></a>" +
            "        <div class=\"clear\"></div>" +
            "    </li>" +
            "    <li class=\"silder_column_time\"><i class=\"image_group\"></i>" +
            "        <div class=\"time\" time=\"" + time.ToString() + "\"></div>" +
            "        <div class=\"clear\"></div>" +
            "    </li>" +
            "    <li class=\"silder_column_other\"><i class=\"image_group\"></i>" +
            "        <div class=\"attention\">共有<span class=\"attention_count\">" + Utils.ToString(product.PageView) + "</span>人关注</div>" +
            "        <div class=\"buyer\"><span class=\"buyer_count\">" + Utils.ToString(product.SoldOfVirtual) + "</span>人购买</div>" +
            "        <div class=\"clear\"></div>" +
            "    </li>" +
            "</ul>" +
            "<div class=\"clear\"></div>";
        }