/// <summary> /// 创建订单 /// </summary> /// <param name="guid"></param> /// <param name="channelId"></param> /// <param name="uid"></param> /// <param name="userId"></param> /// <param name="orderEntity"></param> /// <returns></returns> public static MResult <ItemOrder> CreateOrder(string guid, int channelId, string uid, int userId, OrderEntity orderEntity) { var result = new MResult <ItemOrder>(true); try { var memberDal = DALFactory.Member(); var baseDataDal = DALFactory.BaseData(); var shoppingCartDal = DALFactory.ShoppingCartDal(); var orderDal = DALFactory.Order(); var payId = MCvHelper.To <int>(orderEntity.payid, -1); #region 验证数据 #region 用户是否登录 if (userId <= 0) { result.msg = "请登录后再操作!"; result.status = MResultStatus.ParamsError; return(result); } #endregion #region 收货地址 if (orderEntity.addressid <= 0) { result.msg = "请选择收货地址!"; result.status = MResultStatus.ParamsError; return(result); } #endregion #region 支付方式 if (payId < 0) { result.msg = "请选择支付方式!"; result.status = MResultStatus.ParamsError; return(result); } #endregion #region 配送方式 if (orderEntity.logisticsid <= 0) { result.msg = "请选择配送方式!"; result.status = MResultStatus.ParamsError; return(result); } #endregion #region 发票信息是否完整 if (orderEntity.titletype == null) { result.msg = "请选择发票类型!"; result.status = MResultStatus.ParamsError; return(result); } if (orderEntity.titletype != Entity.Enum.Invoice.TitleType.NoNeed) { if (orderEntity.invoicecategory == null) { result.msg = "请选择发票分类!"; result.status = MResultStatus.ParamsError; return(result); } if (orderEntity.titletype == Entity.Enum.Invoice.TitleType.Company && string.IsNullOrEmpty(orderEntity.invoicetitle)) { result.msg = "请填写发票抬头!"; result.status = MResultStatus.ParamsError; return(result); } } #endregion #endregion var memberInfo = memberDal.GetMemberInfo(uid); #region 验证用户是否存在 if (memberInfo == null || memberInfo.membNo <= 0) { result.msg = "该用户不存在!"; result.status = MResultStatus.ParamsError; return(result); } #endregion //购物车商品数据 List <ShoppingCartEntity> norMalShoppingCartList = null; #region 判断购物车是否有商品 var shoppingCartList = shoppingCartDal.GetShoppingCartProductInfosByUserIDGuidChannelID(userId, guid, channelId); if (shoppingCartList == null || !shoppingCartList.Any()) { result.msg = "购物车没有商品!"; result.status = MResultStatus.LogicError; return(result); } norMalShoppingCartList = (from a in shoppingCartList where a.intIsDelete == 0 select a).ToList(); if (!norMalShoppingCartList.Any()) { result.msg = "购物车没有商品!"; result.status = MResultStatus.LogicError; return(result); } #endregion #region 该用户是否是黑名单 var isExistBacklist = memberDal.CheckUserIdInBackList(userId); if (isExistBacklist) { result.msg = "您的用户出现异常,请联系我们的客服人员进行解决!"; result.status = MResultStatus.LogicError; return(result); } #endregion //收货地址信息 var addressInfo = memberDal.GetMemberAddressInfo(orderEntity.addressid); #region 验证收货地址 #region 是否存在 if (addressInfo == null || addressInfo.intAddressID <= 0 || addressInfo.intCityID <= 0 && payId <= 0 && orderEntity.logisticsid <= 0) { result.msg = "收货地址信息不正确!"; result.status = MResultStatus.ParamsError; return(result); } #endregion #endregion //配送方式 var deliverInfo = baseDataDal.GetDeliverInfo(orderEntity.logisticsid); #region 验证配送方式 if (deliverInfo == null || deliverInfo.intDeliverID == 0) { result.msg = "验证配送方式信息不正确!"; result.status = MResultStatus.ParamsError; return(result); } #endregion #region 检查商品销售区域 var checkGoodsSaleAreaState = CheckGoodsSaleArea(norMalShoppingCartList, userId, MCvHelper.To <int>(addressInfo.intCityID), channelId); if (checkGoodsSaleAreaState.Any()) { result.msg = "有部分商品不在您选择的区域内销售!"; result.status = MResultStatus.LogicError; result.data = String.Join(",", checkGoodsSaleAreaState.ToArray()); return(result); } #endregion var summaryOrderInfo = SummaryOrderInfo(norMalShoppingCartList, channelId, userId, payId, orderEntity.logisticsid, MCvHelper.To <int>(addressInfo.intCityID)); #region 开始创建订单 if (summaryOrderInfo != null && summaryOrderInfo.TotalGoodsFee > 0) { var order = new Sale_Order(); #region 订单主表信息 order.dtCreateDate = DateTime.Now; order.dtSendDate = CheckDateTime(orderEntity.posttimetype, orderEntity.logisticsid); //处理送货日期 order.intChannel = channelId; order.intCreaterID = 555; order.intDeliverID = orderEntity.logisticsid; order.intLogisticsID = 21; order.intOrderState = 1; order.intOrderType = 1; order.intPayID = payId; order.intPayState = 0; order.intStockID = 100; order.intTotalStars = summaryOrderInfo.TotalScore; order.intUserID = userId; order.numAddAmount = 0; order.numCarriage = summaryOrderInfo.TotalFreight; order.numChange = 0; order.numGoodsAmount = summaryOrderInfo.TotalGoodsFee; order.numCouponAmount = summaryOrderInfo.TotalDiscountFee; order.numReceAmount = summaryOrderInfo.TotalOrderFee; order.numWeight = summaryOrderInfo.TotalWeight; order.vchSendTime = order.dtSendDate.ToShortTimeString(); order.vchUserCode = memberInfo.userCode; order.vchOrderCode = GetOrderCode(); #endregion #region 配送信息 var deliver = new Sale_Order_Deliver(); deliver.intAddressID = addressInfo.intAddressID; deliver.intCityID = MCvHelper.To <int>(addressInfo.intCityID, 0); deliver.vchCityName = addressInfo.vchCityName; deliver.vchConsignee = addressInfo.vchConsignee; deliver.intCountyID = MCvHelper.To <int>(addressInfo.intCountyID, 0); deliver.vchCountyName = addressInfo.vchCountyName; deliver.vchDetailAddr = addressInfo.vchStateName + "," + addressInfo.vchCityName + "," + addressInfo.vchCountyName + "," + addressInfo.vchDetailAddr; deliver.vchHausnummer = addressInfo.vchHausnummer; deliver.vchMobile = addressInfo.vchMobile; deliver.vchPhone = addressInfo.vchPhone; deliver.vchPostCode = addressInfo.vchPostCode; deliver.vchRoadName = addressInfo.vchRoadName; deliver.intStateID = MCvHelper.To <int>(addressInfo.intStateID, 0); deliver.vchStateName = addressInfo.vchStateName; deliver.vchUserMemo = orderEntity.remark; deliver.vchOrderCode = order.vchOrderCode; #endregion #region 发票信息 var invoice = new Sale_Order_Invoice(); if (orderEntity.titletype != null && orderEntity.titletype != Invoice.TitleType.NoNeed) { if (orderEntity.titletype == Invoice.TitleType.Personal) { invoice.vchInvoicTitile = "个人"; } else if (orderEntity.titletype == Invoice.TitleType.Company) { invoice.vchInvoicTitile = orderEntity.invoicetitle; } invoice.intInvoiceType = (int)orderEntity.invoicecategory; invoice.intInvoiceKind = 1; invoice.numAmount = order.numReceAmount; invoice.dtBillingTime = DateTime.Now; invoice.dtCreateDate = DateTime.Now; invoice.intIsBilling = 1; invoice.intIsDetail = 0; invoice.vchOrderCode = order.vchOrderCode; invoice.vchPhone = addressInfo.vchPhone; invoice.intUserID = userId; } #endregion #region 保存订单 string message; result.info.oid = orderDal.SaveWebOrder(order, invoice, deliver, null, userId, guid, channelId, MCvHelper.To <int>(memberInfo.clusterId, 0), -1, out message); if (result.info.oid > 0) { #region 清空购物车 shoppingCartDal.ClearShoppingCart(userId); #endregion #region 步订单信息到 BBHome orderDal.SyncOrderInfoToBBHome(order.vchOrderCode); #endregion result.status = MResultStatus.Success; var payType = string.Empty; if (payId == 0) { payType = "货到付款"; } else if (payId == 1) { payType = "在线支付"; } var postTimetype = string.Empty; switch (orderEntity.posttimetype) { case 1: postTimetype = "工作日送货"; break; case 2: postTimetype = "工作日、双休日均可送货"; break; case 3: postTimetype = "只双休日送货"; break; } result.info.ocode = order.vchOrderCode; result.info.paytype = payType; result.info.logisticstype = deliverInfo.vchDeliverName; result.info.total_fee = order.numGoodsAmount; result.info.total_freight = order.numCarriage; result.info.total_order = order.numReceAmount; result.info.posttimetype = postTimetype; } #endregion } #endregion } catch (Exception ex) { result.status = MResultStatus.ExecutionError; MLogManager.Error(MLogGroup.Order.创建订单, null, "", ex); } return(result); }
/// <summary> /// 创建订单 /// </summary> /// <param name="order"></param> /// <param name="invoice"></param> /// <param name="deliver"></param> /// <param name="orderProm"></param> /// <param name="userId"></param> /// <param name="guid"></param> /// <param name="channelId"></param> /// <param name="clusterId"></param> /// <param name="promID"></param> /// <param name="message"></param> /// <returns></returns> public int SaveWebOrder(Sale_Order order, Sale_Order_Invoice invoice, Sale_Order_Deliver deliver, Sale_Order_Prom_Rela orderProm, int userId, string guid, int channelId, int clusterId, int promID, out string message) { var orderNo = -1; message = ""; var order_rela = new List <Sale_Order_Prom_Rela>(); using (var ctx = new HolycaEntities()) { using (var cart = new HolycaEntities()) { ctx.Connection.Open(); var tran = ctx.Connection.BeginTransaction(); try { //保存订单主表 ctx.Sale_Order.AddObject(order); //先提交得到订单编号 ctx.SaveChanges(); //返回订单编号 orderNo = order.intOrderNO; #region 保存发票表 if (invoice != null) { invoice.vchOrderCode = order.vchOrderCode; invoice.dtCreateDate = DateTime.Now; invoice.dtBillingTime = DateTime.Now; ctx.Sale_Order_Invoice.AddObject(invoice); } #endregion Sale_Order_Detail item = null; //获取购物车信息 var all = from cartItem in cart.Sale_ShoppingCart where cartItem.intUserID == userId && cartItem.intChannelID == channelId && cartItem.intIsDelete == 0 select cartItem; var v = from allitem in all where (allitem.intProductType == null || allitem.intProductType == 0) select allitem; if (v.Any()) { decimal totalMoney = 0; foreach (var cartItem in v) { #region 购物车转订单明细 item = new Sale_Order_Detail(); item.numTotalAmount = ((decimal?)cartItem.numSalePrice ?? 0) * cartItem.intBuyCount; if (invoice != null) { item.intIsInvoice = 1; } else { item.intIsInvoice = 0; } item.numCleanCost = 0; item.numCost = 0; item.intBaseStar = (int?)cartItem.intOrgScore ?? 0; item.intUserID = userId; item.dtOpDate = DateTime.Now; item.vchOrderCode = order.vchOrderCode; item.numStandarPrice = cartItem.numOrgPrice; item.numSalePrice = cartItem.numSalePrice; item.intHerdPriceID = clusterId; item.intProductID = cartItem.intProductID; item.vchProductName = cartItem.nchProductName; item.vchProductPrinted = cartItem.vchProductPrinted; if (promID > 0) { if (cartItem.intPromID > 0) { item.intPromID = cartItem.intPromID ?? 0; } else { item.intPromID = promID; } } else { item.intPromID = cartItem.intPromID ?? 0; } item.intQty = cartItem.intBuyCount; item.intRtnQty = 0; item.intScores = (cartItem.intScore == 0 ? 0 : cartItem.intOrgScore) * cartItem.intBuyCount; item.intChannel = channelId; item.intStockID = order.intStockID; //修复ckid为空的情况 item.intLogisticsID = 21; ctx.Sale_Order_Detail.AddObject(item); totalMoney += item.numTotalAmount; #endregion } #region 促销处理 /* * PromOrderRelation orderrelation = null; * //保存满减促销的关系 * foreach (Sale_ShoppingCart cartItem in p) * { * orderrelation = new PromOrderRelation(); * orderrelation.vchOrderCode = order.vchOrderCode; * orderrelation.intPromID = (int)cartItem.intPromID; * orderrelation.numCouponAmount = cartItem.numSalePrice; * ctx.PromOrderRelation.InsertOnSubmit(orderrelation); * } * * //修改已经保存的满减和购物车关系中的订单号 * //var productrelation= from r in ctx.PromOrderProductRelation * // from q in v * // where r.intShopCartID == q.intShopCartID * // select r; * string intshopcartids = ","; * foreach (Sale_ShoppingCart productid in v) * { * intshopcartids = intshopcartids + productid.intShopCartID.ToString() + ","; * } * * var productrelation = ctx.PromOrderProductRelation.Where(c => intshopcartids.IndexOf("," + c.intShopCartID.ToString() + ",") >= 0).ToList(); * //保存满减促销的关系 * foreach (PromOrderProductRelation pre in productrelation) * { * pre.vchOrderCode = order.vchOrderCode; * ctx.SubmitChanges(); * } */ #endregion #region 货款+运费 if (order.numGoodsAmount != totalMoney - order.numCouponAmount || order.numReceAmount != totalMoney + order.numCarriage - order.numCouponAmount) { order.numGoodsAmount = totalMoney; order.numReceAmount = totalMoney + order.numCarriage; } #endregion #region 保存支付状态 var orderPayStatus = new Sale_Order_PayState(); orderPayStatus.intUserID = 555; //顾客 orderPayStatus.dtOpdate = DateTime.Now; orderPayStatus.vchOrderCode = order.vchOrderCode; orderPayStatus.intPayState = 0; //未付款 ctx.AddToSale_Order_PayState(orderPayStatus); #endregion #region 保存订单状态 var orderStatus = new Sale_Order_State(); orderStatus.intUserID = 555; //顾客 orderStatus.intOrderState = 1; //新建 orderStatus.dtOpdate = DateTime.Now; orderStatus.vchOrderCode = order.vchOrderCode; ctx.Sale_Order_State.AddObject(orderStatus); #endregion #region 保存销售订单配送信息 ctx.Sale_Order_Deliver.AddObject(deliver); ctx.SaveChanges(); #endregion #region 保存订单和促销关系 foreach (var rela_item in v) { var shopcart_rela = cart.Sale_ShopCart_Prom_Rela.FirstOrDefault(c => c.intShopCartID == rela_item.intShopCartID); if (shopcart_rela != null) { orderProm = new Sale_Order_Prom_Rela(); orderProm.vchOrderCode = order.vchOrderCode; orderProm.intChannelID = channelId; orderProm.intUserID = userId; orderProm.intProductID = rela_item.intProductID; orderProm.intPromID = shopcart_rela.intPromID ?? 0; orderProm.intDetailID = item.intDetailID; orderProm.dtCreateDate = DateTime.Now; ctx.Sale_Order_Prom_Rela.AddObject(orderProm); } } #endregion //提交所有更改 tran.Commit(); } } catch (Exception e) { orderNo = -1; message = e.Message; tran.Rollback(); } } } return(orderNo); }