private bool CheckBuyProducts(List<CartProduct> buyProducts,out string errorMessage) { var products = new List<CartProduct>(); var messageSB = new StringBuilder(); var whereStr = new StringBuilder(); whereStr.Append("ID in ("); for (int i = 0; i < buyProducts.Count; i++) { if (i < buyProducts.Count - 1) { whereStr.Append(buyProducts[i].ProductID).Append(","); } else { whereStr.Append(buyProducts[i].ProductID); } } whereStr.Append(")"); var productList = new ProductService().Query(ProductType.Rand, 100, whereStr.ToString()); if (productList == null || productList.Count < 1) { errorMessage = "商品不存在或已下架。"; LogUtils.Log("根据商品ID从数据批量获取商品信息为NULL;商品查询条件:" + whereStr, "BuildCartProducts", Category.Warn); return false; } foreach (var cartProduct in buyProducts) { var quantity = 0; var response = this.CheckSpecialPromote(cartProduct.ProductID, ref quantity); if (response != null) //特殊全场活动赠品 { if (response.State == 0) //不允许领取赠品 { messageSB.Append("商品:").Append(cartProduct.ProductName).Append(" ").Append(response.Message).Append("\r\n"); continue; } else { cartProduct.GoujiuPrice = 0; if (cartProduct.Quantity > quantity) //若数量大于可赠送数量 { messageSB.Append("商品:") .Append(cartProduct.ProductName) .Append(" ") .Append("为活动赠品,最多可领取 " + quantity + "件。") .Append("\r\n"); continue; } } } var product = productList.FirstOrDefault(p => p.ID == cartProduct.ProductID); if (product.Status != 2) { messageSB.Append("商品:" + product.Name).Append(" 已下架;").Append("\r\n"); } else if (product.InventoryNumber < 1) { messageSB.Append("商品:" + product.Name).Append(" 库存不足;").Append("\r\n"); } else if (product.InventoryNumber < cartProduct.Quantity) { messageSB.Append("商品:" + product.Name).Append(" 库存剩余").Append(product.InventoryNumber).Append("件;\r\n"); products.Add(this.ConvertToCartProduct(product, product.InventoryNumber)); } else { products.Add(this.ConvertToCartProduct(product, cartProduct.Quantity)); } } errorMessage = messageSB.ToString(); if (!string.IsNullOrWhiteSpace(messageSB.ToString())) //检查不通过 { return false; } //检查通过 return true; }
private List<CartProduct> BuildCartProducts(int[] proIds, int[] quantity) { var products = new List<CartProduct>(); var whereStr = new StringBuilder(); var messageSB = new StringBuilder(); whereStr.Append("ID in ("); for (int i = 0; i < proIds.Length; i++) { if (i < proIds.Length - 1) { whereStr.Append(proIds[i]).Append(","); } else { whereStr.Append(proIds[i]); } } whereStr.Append(")"); var list = new ProductService().Query(ProductType.Rand, 100, whereStr.ToString()); if (list == null) { LogUtils.Log("根据商品ID从数据批量获取商品信息为NULL;商品列表:" + proIds, "BuildCartProducts", Category.Warn); return new List<CartProduct>(); } for (int i = 0; i < proIds.Length; i++) { var product = list.FirstOrDefault(p => p.ID == proIds[i]); if (product != null) { if (quantity[i] <= 0) { messageSB.Append("商品:" + product.Name).Append(" 购买数量不能为0").Append("\r\n"); } else if (product.Status!=2) { messageSB.Append("商品:" + product.Name).Append(" 已下架;").Append("\r\n"); } else if (product.InventoryNumber < 1) { messageSB.Append("商品:" + product.Name).Append(" 库存不足;").Append("\r\n"); } else if (product.InventoryNumber < quantity[i]) { messageSB.Append("商品:" + product.Name).Append(" 库存剩余").Append(product.InventoryNumber).Append("件;\r\n"); products.Add(this.ConvertToCartProduct(product, product.InventoryNumber)); } else { products.Add(this.ConvertToCartProduct(product, quantity[i])); } } } ViewBag.ExtraMessage = messageSB.ToString(); return products; }