Exemplo n.º 1
0
        private void ProcessAddToCartBySkus(System.Web.HttpContext context)
        {
            context.Response.ContentType = "application/json";
            int    quantity = int.Parse(context.Request["quantity"], System.Globalization.NumberStyles.None);
            string skuId    = context.Request["productSkuId"];



            //判断库存
            int oldQuan = ShoppingCartProcessor.GetSkuStock(skuId);

            if (quantity > oldQuan)
            {
                context.Response.Write("{\"Status\":\"1\",\"oldQuan\":\"" + oldQuan + "\"}");
                return;
            }
            //检查商品是否超过限购数量
            Member member = HiContext.Current.User as Member;

            if (member != null)
            {
                int MaxCount    = 0;
                int Payquantity = ProductHelper.CheckPurchaseCount(skuId, member.UserId, out MaxCount);
                if ((Payquantity + quantity) > MaxCount && MaxCount != 0) //当前购买数量大于限购剩余购买数量
                {
                    context.Response.Write("{\"Status\":\"4\"}");
                    return;
                }
            }
            if (ShoppingCartProcessor.AddLineItem(skuId, quantity, 0) != AddCartItemStatus.Successed)
            {
                context.Response.Write("{\"Status\":\"2\"}");
                return;
            }

            DataTable        dt           = ProductHelper.GetAdOrderInfo(skuId);
            ShoppingCartInfo shoppingCart = ShoppingCartProcessor.GetShoppingCart();

            if (shoppingCart != null)
            {
                context.Response.Write(string.Concat(new object[]
                {
                    "{\"Status\":\"OK\",\"TotalMoney\":\"", shoppingCart.GetTotal().ToString(".00"),
                    "\",\"Quantity\":\"", shoppingCart.GetQuantity().ToString(),
                    "\",\"SkuQuantity\":\"", shoppingCart.GetQuantity_Sku(skuId),
                    "\",\"data\":", Newtonsoft.Json.JsonConvert.SerializeObject(dt), "}"
                }));

                return;
            }
            context.Response.Write("{\"Status\":\"3\"}");
        }
Exemplo n.º 2
0
        private void CheckBuyCardinality(System.Web.HttpContext context)
        {
            var unselectedProductId       = context.Request["productIds"];
            ShoppingCartInfo shoppingCart = ShoppingCartProcessor.GetShoppingCart();

            if (shoppingCart.LineItems.Count == 0)
            {
                return;
            }
            List <object> result = new List <object>();

            if (!string.IsNullOrEmpty(unselectedProductId))
            {
                string[] pid_skuids = unselectedProductId.Split(',');
                foreach (var s in pid_skuids)
                {
                    int    productId = int.Parse(s.Split('|')[0]);
                    string skuId     = s.Split('|')[1];
                    var    item      = shoppingCart.LineItems.FirstOrDefault(p => p.ProductId == productId && p.SkuId == skuId);
                    if (item != null)
                    {
                        shoppingCart.LineItems.Remove(item);
                    }
                }
            }
            var checkProduct = context.Request["checkProducts"];

            if (!string.IsNullOrEmpty(checkProduct))
            {
                //检查商品是否超过限购数量
                Member   member     = HiContext.Current.User as Member;
                string[] pid_skuids = checkProduct.Split(',');
                foreach (var s in pid_skuids)
                {
                    int    productId = int.Parse(s.Split('|')[0]);
                    string skuId     = s.Split('|')[1];
                    var    item      = shoppingCart.LineItems.FirstOrDefault(p => p.ProductId == productId && p.SkuId == skuId);
                    if (member != null)
                    {
                        int MaxCount = 0;
                        int count    = ProductHelper.CheckPurchaseCount(skuId, member.UserId, out MaxCount);
                        if ((count + item.Quantity) > MaxCount && MaxCount != 0) //当前购买数量大于限购剩余购买数量
                        {
                            result.Add(new { ProductId = item.ProductId, BuyCardinality = ((MaxCount - count) < 0 ? 0 : MaxCount - count), ProductName = item.Name, Quantity = item.Quantity, Purchase = "1" });
                        }
                    }
                }
            }
            if (result.Count > 0)
            {
                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(result));
                return;
            }
            var productIds = shoppingCart.LineItems.Select(p => p.ProductId).Distinct();

            if (productIds.Count() <= 0)
            {
                return;
            }

            Dictionary <int, int> dict = new EcShop.SqlDal.Commodities.ProductDao().GetBuyCardinality(productIds.ToArray());

            if (dict == null || dict.Count == 0)
            {
                return;
            }


            foreach (KeyValuePair <int, int> item in dict)
            {
                string productName = shoppingCart.LineItems.First(p => p.ProductId == item.Key).Name;
                int    quantity    = shoppingCart.LineItems.Where(p => p.ProductId == item.Key).Sum(p => p.Quantity);
                if (quantity >= item.Value)
                {
                    continue;
                }

                result.Add(new { ProductId = item.Key, BuyCardinality = item.Value, ProductName = productName, Quantity = quantity, Purchase = "0" });
            }

            context.Response.ContentType = "application/json";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }