public ActionResult Add(int addressID, int payMethod, string productIds, string intro, int isRequireInvoice, string invoiceTitle, int invoiceContent, int ctype, int cId, double account) { /*************** * 前台添加订单流程: * 1.检查是否用券,若用,则检查券是否存在,是否符合使用条件。 * 2.检查是否用余额抵扣,若用,则检查用户是否有足够余额。 * 3.检查用户是否开发票,若是,检查发票内容是否填写。 * 4.检查用户收货地址ID是否填写正确 * 5.检查商品ID列表是否正确 * todo:下单时检查商品库存 * ********************/ #region 检查是否用券 //todo:券检查 #endregion #region 余额抵用检查 if (account > 0) { if (!CheckAccountBalance(account)) { return this.Json(new AjaxResponse(-1, "账户余额不足")); } } #endregion #region 检查发票信息 if (isRequireInvoice > 0) { if (invoiceContent < 0) { return this.Json(new AjaxResponse(-1, "发票内容错误")); } } #endregion #region 检查收货地址ID是否填写正确 if (addressID <= 0) { return this.Json(new AjaxResponse(-1, "收货地址错误")); } var address = new UserReceiveAddressService().QueryByID(addressID); //收货地址必须与用户对应 if (address == null || address.UserID != this.UserSession.UserID) { return this.Json(new AjaxResponse(-1, "收货地址错误")); } var addressModel = DataTransfer.Transfer<UserReceiveAddressModel>(address, typeof(User_RecieveAddress)); if (!new UtilityController().ValidSupportRegion(addressModel.ProvinceID)) { return this.Json(new AjaxResponse(-1, "对不起," + addressModel.CountyName("-") + "暂不支持。")); } #endregion var orderService = new OrderService(this.UserSession.UserID, false); try { var cart = MongoDBHelper.GetModel<UserCartModel>(u => u.VisitorKey == this.UserSession.VisitorKey); if (cart == null || cart.BuyList == null || cart.BuyList.Count < 1) { return this.Json(new AjaxResponse(-1, "商品不存在或已下架。")); } string message; #region 检查商品列表信息 if (!CheckBuyProducts(cart.BuyList, out message)) { LogUtils.Log( message, "前台提交订单:Add", Category.Info, this.UserSession.SessionId, this.UserSession.UserID, "Order/Add"); return this.Json(new AjaxResponse(0, message)); } #endregion //获取促销相关信息 var productDictionary = new Dictionary<int, int>(); foreach (var orderProduct in cart.BuyList) { productDictionary.Add(orderProduct.ProductID, orderProduct.Quantity); } var orderBill = this.GetOrderBill(productDictionary); //new OrderBillServices().QueryOrderBill(productDictionary, this.UserSession.UserID); if (orderBill == null || ((orderBill.Products == null || orderBill.Products.Count < 1) && (orderBill.SuitPromoteInfos == null || orderBill.SuitPromoteInfos.Count < 1))) { LogUtils.Log("没有获取订单促销信息", "Add", Category.Warn); return this.Json(new AjaxResponse(-1, "商品不存在或者已下架。")); } #region 检查促销活动 //限购验证 foreach (var cartProduct in orderBill.Products) { //验证是否有多选一情况 if (cartProduct.DenyFlag > 0) { switch (cartProduct.DenyFlag) { case 1: return this.Json(new AjaxResponse(0, string.Format("对不起,{0} 只允许新会员购买!", cartProduct.ProductName))); case 2: return this.Json(new AjaxResponse(0, string.Format("对不起,{0} 只允许老会员购买!", cartProduct.ProductName))); case 3: return this.Json(new AjaxResponse(0, string.Format("对不起,{0} 只允许通过手机验证会员购买!", cartProduct.ProductName))); case 4: return this.Json(new AjaxResponse(0, string.Format("对不起,{0} 只允许通过邮箱验证会员购买。", cartProduct.ProductName))); case 5: return this.Json(new AjaxResponse(0, string.Format("对不起,{0} 请先登录。", cartProduct.ProductName))); default: return this.Json(new AjaxResponse(0, string.Format("对不起,{0} 您不满足此商品的购买条件。", cartProduct.ProductName))); } } //验证多选一互斥促销 if (cartProduct.Promotes!=null&&cartProduct.Promotes.FirstOrDefault(p => p.PromoteType == 4) != null && !string.IsNullOrWhiteSpace(cartProduct.Exclude)) { var userCart = MongoDBHelper.GetModel<UserCartModel>(u => u.VisitorKey == this.UserSession.VisitorKey); if (userCart != null && userCart.ProductItems.Count > 0) { var excludes = cartProduct.Exclude.Split(','); if (excludes.Length > 0) { foreach (var item in userCart.ProductItems) { if (item.ProductID != cartProduct.ProductID && excludes.Contains(item.ProductID.ToString())) { return this.Json( new AjaxResponse( 0, string.Format( "对不起,【{0}】与【{1}】不能同时购买。", item.ProductName, cartProduct.ProductName))); } } } } } //判断是否是限时抢购,且是否超出了限制量。 if (cartProduct.LimitedBuyQuantity > 0 && cartProduct.MaxBuyQuantity < cartProduct.Quantity) { LogUtils.Log("用户订单商品数量超出了限时抢购数量", "Add", Category.Info); return this.Json( new AjaxResponse( 0, string.Format( "对不起,{0} 每人限购{1}件,你还可以购买{2}件。", cartProduct.ProductName, cartProduct.LimitedBuyQuantity, cartProduct.MaxBuyQuantity))); } //若是限时抢购,判断是否还有活动库存 if (cartProduct.LimitedBuyQuantity > 0 && cartProduct.PromoteResidueQuantity < cartProduct.Quantity) { LogUtils.Log( string.Format( "{0} 活动库存不满足,库存仅剩 {1},用户下单{2}", cartProduct.ProductName, cartProduct.PromoteResidueQuantity, cartProduct.Quantity), "Add", Category.Info); return this.Json( new AjaxResponse( 0, string.Format( "对不起,{0} 活动库存不满足,库存仅剩 {1}件。", cartProduct.ProductName, cartProduct.PromoteResidueQuantity))); } } //检查组合促销商品限购情况 if (orderBill.SuitPromoteInfos != null) { foreach (var suitPromoteInfo in orderBill.SuitPromoteInfos) { if (suitPromoteInfo.Products != null) { foreach (var cartProduct in suitPromoteInfo.Products) { //判断是否是限时抢购,且是否超出了限制量。 if (cartProduct.LimitedBuyQuantity > 0 && cartProduct.MaxBuyQuantity < cartProduct.Quantity) { LogUtils.Log("用户订单商品数量超出了限时抢购数量", "Add", Category.Info); return this.Json( new AjaxResponse( 0, string.Format( "对不起,{0} 每人限购{1}件,你还可以购买{2}件。", cartProduct.ProductName, cartProduct.LimitedBuyQuantity, cartProduct.MaxBuyQuantity))); } //若是限时抢购,判断是否还有活动库存 if (cartProduct.LimitedBuyQuantity > 0 && cartProduct.PromoteResidueQuantity < cartProduct.Quantity) { LogUtils.Log( string.Format( "{0} 活动库存不满足,库存仅剩 {1},用户下单{2}", cartProduct.ProductName, cartProduct.PromoteResidueQuantity, cartProduct.Quantity), "Add", Category.Info); return this.Json( new AjaxResponse( 0, string.Format( "对不起,{0} 活动库存不满足,库存仅剩 {1}件。", cartProduct.ProductName, cartProduct.PromoteResidueQuantity))); } } } } } #endregion DateTime createTime; var orderCode = MadeCodeService.GetOrderCode(out createTime); var order = new Order { UserID = this.UserSession.UserID, RecieveAddressID = addressID, CpsID = 0, PaymentMethodID = payMethod, OrderCode = orderCode, OrderNumber = MadeCodeService.ReverseOrderCode(orderCode, createTime), TotalMoney = 0, TotalIntegral = 0, PaymentStatus = 0, IsRequireInvoice = isRequireInvoice != 0, Status = payMethod == 0 ? 100 : 0, // 若是在线支付,则设置为等待付款,否则设置为等待确认 Remark = intro, CreateTime = createTime }; //获取CPS信息 order.CpsID = this.GetCpsID(); Order_Invoice invoice = null; if (isRequireInvoice!=0) //需要开发票 { invoice = new Order_Invoice { InvoiceContentID = invoiceContent, InvoiceTitle = string.IsNullOrWhiteSpace(invoiceTitle) ? "个人" : invoiceTitle, InvoiceTypeID = 0 }; } var promoteList = new List<Order_Product_Promote>(); var buyProducts=new List<Order_Product>(); var giftCoupons = new List<Gift_Coupon>(); double totalAmount = 0, totalDiscount = 0, tempDiscount = 0; totalAmount += SetBuyList(order.ID, orderBill.Products, ref buyProducts, ref promoteList, out tempDiscount); totalDiscount += tempDiscount; //组合促销商品 if (orderBill.SuitPromoteInfos != null) { foreach (var suitPromote in orderBill.SuitPromoteInfos) { totalDiscount += suitPromote.PromoteDiscount; totalAmount += SetBuyList( order.ID, suitPromote.Products, ref buyProducts, ref promoteList, out tempDiscount); totalDiscount += tempDiscount; //成交价 = 单品成交价 - 组合优惠摊牌金额 if (suitPromote.PromoteDiscount > 0) { for (var i = 0; i < buyProducts.Count; i++) { if (suitPromote.Products.Exists(p => p.ProductID == buyProducts[i].ProductID)) { buyProducts[i].TransactPrice -= buyProducts[i].TransactPrice / suitPromote.TotalPrice * suitPromote.PromoteDiscount; //组合促销信息 promoteList.Add( new Order_Product_Promote { ProductID = buyProducts[i].ProductID, PromoteID = suitPromote.PromoteID, PromoteDiscount = Math.Round( buyProducts[i].TransactPrice / suitPromote.TotalPrice * suitPromote.PromoteDiscount, 2), PromoteType = suitPromote.PromoteType }); } } } //赠品 if (suitPromote.GiftProducts != null) { foreach (var giftProduct in suitPromote.GiftProducts) { var product = new Order_Product { ProductID = giftProduct.ProductID, ProductName = "【赠品】" + giftProduct.ProductName, Quantity = giftProduct.Quantity, PromotionID = giftProduct.PromotID, PromotionType = giftProduct.PromotType, TransactPrice = 0, PromotionResult = 1, Integral = 0, RebateRate = 0, Commission = 0, CreateTime = DateTime.Now }; buyProducts.Add(product); promoteList.Add( new Order_Product_Promote { OrderID = order.ID, ProductID = giftProduct.ProductID, PromoteID = giftProduct.PromotID, PromoteType = giftProduct.PromotType, ExtField = "赠品" }); } } if (suitPromote.GiftCoupons != null) { giftCoupons.AddRange(suitPromote.GiftCoupons); } } } order.TotalMoney = orderBill.TotalPrice - orderBill.TotalDiscount; //todo:特殊促销处理,此处为了快速开发,硬编码 foreach (var cartProduct in buyProducts) { if (cartProduct.ProductID == 4153) { order.TotalMoney = order.TotalMoney - cartProduct.TransactPrice; cartProduct.TransactPrice = 0; cartProduct.Quantity = 1; } } order.DeliveryCost = order.TotalMoney >= 100 ? 0 : 10; //todo:需要改掉 order.Discount = totalDiscount; var orderId = orderService.Add(order, buyProducts, invoice, giftCoupons, promoteList); this.ResetUserCart(cart, buyProducts); //更新特殊活动 foreach (var orderProduct in buyProducts) { var specialPromotes = MongoDBHelper.GetModels<SpecialPromote>(p => p.GiftProductID == orderProduct.ProductID && p.ID > 0); if (specialPromotes != null && specialPromotes.Count > 0) //若不是,则直接跳过 { var specialPromote = specialPromotes[0]; //一个赠品只能参加一个活动 var userPromote = specialPromote.UserPromotes.FirstOrDefault(u => u.UserID == this.UserSession.UserID); //即使找不到参加活动的记录,但领取了商品,我们依然认为此用户已经参加了活动 if (userPromote == null) { userPromote = new UserPromote { UserID = this.UserSession.UserID }; } userPromote.OrderID = order.ID; userPromote.HasGetGift=true; userPromote.GetGiftTime = DateTime.Now; userPromote.GiftID = orderProduct.ProductID; specialPromote.UserPromotes.Remove(u => u.UserID == userPromote.UserID); specialPromote.UserPromotes.Add(userPromote); MongoDBHelper.UpdateModel<SpecialPromote>(specialPromote, sp => sp.ID == specialPromote.ID); } } LogUtils.Log( "前台添加订单成功,订单编码:" + orderId + "\n\r", "Order.Add", Category.Info, this.UserSession.SessionId, this.UserSession.UserID); return this.Json(new AjaxResponse(1, "订单提交成功", order.OrderCode)); } catch (Exception ex) { LogUtils.Log( "前台添加订单出错了,错误信息:"+ex.Message+"\n\r"+ex.StackTrace, "Order.Add", Category.Error, this.UserSession.SessionId, this.UserSession.UserID); return this.Json(new AjaxResponse(-2, "对不起,提交订单出错了。")); } }
public ActionResult AddOrder( List<ProductModel> products, int userID, int receiveAddressID, int paymentMethodID, bool isRequireInvoice, OrderInvoiceModel invoiceInfo, string description = null) { try { if (products == null || products.Count < 1) { var data = new AjaxResponse(-1, "订单商品为空!"); return this.Json(data); } if (userID < 1) { var data = new AjaxResponse(-1, "用户编码错误!"); return this.Json(data); } if (receiveAddressID < 1) { var data = new AjaxResponse(-1, "收货信息地址编码错误!"); return this.Json(data); } if (paymentMethodID < 0) { var data = new AjaxResponse(-1, "支付地址错误!"); return this.Json(data); } Order_Invoice invoice = null; if (isRequireInvoice) { if (invoiceInfo.InvoiceContentID < 0) { var data = new AjaxResponse(-1, "发票消费类别错误!"); return this.Json(data); } else if (string.IsNullOrWhiteSpace(invoiceInfo.InvoiceTitle)) { var data = new AjaxResponse(-1, "发票抬头为空!"); return this.Json(data); } else if (invoiceInfo.InvoiceCost < 0) { var data = new AjaxResponse(-1, "开票金额不能小于或等于0!"); return this.Json(data); } invoice = DataTransfer.Transfer<Order_Invoice>(invoiceInfo, typeof(OrderInvoiceModel)); } DateTime createTime; var orderCode = MadeCodeService.GetOrderCode(out createTime); var orderService = new OrderService(this.SystemUserSession.EmployeeID); var order = new Order { UserID = userID, RecieveAddressID = receiveAddressID, CpsID = 0, PaymentMethodID = paymentMethodID, OrderCode = orderCode, OrderNumber = MadeCodeService.ReverseOrderCode(orderCode, createTime), TotalMoney = 0, TotalIntegral = 0, PaymentStatus = 0, IsRequireInvoice = isRequireInvoice, Status = paymentMethodID == 0 ? 100 : 0, Description = string.IsNullOrWhiteSpace(description) ? "后台添加订单" : description, CreateTime = createTime }; var orderProducts = new List<Order_Product>(); foreach (var productModel in products) { if (productModel.Quantity > 0) { orderProducts.Add( new Order_Product { ProductID = productModel.ID, Quantity = productModel.Quantity, ProductName = productModel.Name, TransactPrice = productModel.GoujiuPrice, CreateTime = DateTime.Now }); } } // 判断是否能够由系统自动确认订单,若能,则自动确认。 orderService.Add(order, orderProducts, invoice); if (orderService.ValidateConfirmOrderBySystem(order)) { orderService.ConfirmOrderBySystem(order); } return this.Json(new AjaxResponse(1, "执行成功")); } catch (Exception exception) { var data = new AjaxResponse(-1, exception.Message); TextLogger.Instance.Log("后台添加订单发生错误", Category.Error, exception); return this.Json(data); } }