public CustomJsonResult Reserve(string operater, string clientUserId, RopOrderReserve rop) { CustomJsonResult result = new CustomJsonResult(); if (rop.ProductSkus == null || rop.ProductSkus.Count == 0) { return(new CustomJsonResult(ResultType.Failure, ResultCode.Failure, "选择商品为空")); } LocalS.BLL.Biz.RopOrderReserve bizRop = new LocalS.BLL.Biz.RopOrderReserve(); bizRop.Source = rop.Source; bizRop.StoreId = rop.StoreId; bizRop.ReserveMode = E_ReserveMode.Online; bizRop.ClientUserId = clientUserId; foreach (var item in rop.ProductSkus) { bizRop.ProductSkus.Add(new LocalS.BLL.Biz.RopOrderReserve.ProductSku() { CartId = item.CartId, Id = item.Id, Quantity = item.Quantity, ReceptionMode = item.ReceptionMode }); } var bizResult = BizFactory.Order.Reserve(operater, bizRop); if (bizResult.Result == ResultType.Success) { RetOrderReserve ret = new RetOrderReserve(); ret.OrderId = bizResult.Data.OrderId; ret.OrderSn = bizResult.Data.OrderSn; result = new CustomJsonResult(ResultType.Success, ResultCode.Success, "操作成功", ret); } else { result = new CustomJsonResult(ResultType.Failure, ResultCode.Failure, bizResult.Message); } return(result); }
public CustomJsonResult <RetOrderReserve> Reserve(string operater, RopOrderReserve rop) { CustomJsonResult <RetOrderReserve> result = new CustomJsonResult <RetOrderReserve>(); if (rop.ReserveMode == E_ReserveMode.Unknow) { return(new CustomJsonResult <RetOrderReserve>(ResultType.Failure, ResultCode.Failure, "未知预定方式", null)); } if (rop.ReserveMode == E_ReserveMode.OffLine) { if (string.IsNullOrEmpty(rop.SellChannelRefId)) { return(new CustomJsonResult <RetOrderReserve>(ResultType.Failure, ResultCode.Failure, "机器ID不能为空", null)); } } using (TransactionScope ts = new TransactionScope()) { RetOrderReserve ret = new RetOrderReserve(); var skuIds = rop.ProductSkus.Select(m => m.Id).ToArray(); //检查是否有可买的商品 List <string> warn_tips = new List <string>(); List <PrdProductSkuModel> productSkus = new List <BLL.PrdProductSkuModel>(); foreach (var sku in rop.ProductSkus) { var productSku = BizFactory.PrdProduct.GetProductSku(rop.StoreId, sku.Id); productSkus.Add(productSku); var sellQuantity = 0; if (rop.ReserveMode == E_ReserveMode.OffLine) { sellQuantity = productSku.Stocks.Where(m => m.RefType == E_StoreSellChannelRefType.Machine && m.RefId == rop.SellChannelRefId).Sum(m => m.SellQuantity); } else if (rop.ReserveMode == E_ReserveMode.Online) { if (sku.ReceptionMode == E_ReceptionMode.Machine) { sellQuantity = productSku.Stocks.Where(m => m.RefType == E_StoreSellChannelRefType.Machine).Sum(m => m.SellQuantity); } else if (sku.ReceptionMode == E_ReceptionMode.Express) { sellQuantity = productSku.Stocks.Where(m => m.RefType == E_StoreSellChannelRefType.Express).Sum(m => m.SellQuantity); } else if (sku.ReceptionMode == E_ReceptionMode.SelfTake) { sellQuantity = productSku.Stocks.Where(m => m.RefType == E_StoreSellChannelRefType.SelfTake).Sum(m => m.SellQuantity); } } if (productSku.IsOffSell) { warn_tips.Add(string.Format("{0}已经下架", productSku.Name)); } else { if (sellQuantity < sku.Quantity) { warn_tips.Add(string.Format("{0}的可销售数量为{1}个", productSku.Name, sellQuantity)); } } } if (warn_tips.Count > 0) { return(new CustomJsonResult <RetOrderReserve>(ResultType.Failure, ResultCode.Failure, string.Join(";", warn_tips.ToArray()), null)); } var store = CurrentDb.Store.Where(m => m.Id == rop.StoreId).FirstOrDefault(); if (store == null) { return(new CustomJsonResult <RetOrderReserve>(ResultType.Failure, ResultCode.Failure, "店铺无效", null)); } var order = new Order(); order.Id = GuidUtil.New(); order.Sn = RedisSnUtil.Build(RedisSnType.Order, store.MerchId); order.MerchId = store.MerchId; order.StoreId = rop.StoreId; order.StoreName = store.Name; order.ClientUserId = rop.ClientUserId; order.Quantity = rop.ProductSkus.Sum(m => m.Quantity); order.Status = E_OrderStatus.WaitPay; order.Source = rop.Source; order.SubmitTime = DateTime.Now; order.PayExpireTime = DateTime.Now.AddSeconds(300); order.Creator = operater; order.CreateTime = DateTime.Now; //todo Random rd = new Random(); int num = rd.Next(100000, 1000000); order.PickCode = num.ToString(); #region 更改购物车标识 if (!string.IsNullOrEmpty(rop.ClientUserId)) { var cartsIds = rop.ProductSkus.Select(m => m.CartId).Distinct().ToArray(); if (cartsIds != null) { var clientCarts = CurrentDb.ClientCart.Where(m => cartsIds.Contains(m.Id) && m.ClientUserId == rop.ClientUserId).ToList(); if (clientCarts != null) { foreach (var cart in clientCarts) { cart.Status = E_ClientCartStatus.Settling; cart.Mender = operater; cart.MendTime = DateTime.Now; CurrentDb.SaveChanges(); } } } } #endregion var reserveDetails = GetReserveDetail(rop.ProductSkus, productSkus); order.OriginalAmount = reserveDetails.Sum(m => m.OriginalAmount); order.DiscountAmount = reserveDetails.Sum(m => m.DiscountAmount); order.ChargeAmount = reserveDetails.Sum(m => m.ChargeAmount); foreach (var detail in reserveDetails) { var orderDetails = new OrderDetails(); orderDetails.Id = GuidUtil.New(); orderDetails.Sn = order.Sn + reserveDetails.IndexOf(detail).ToString(); orderDetails.ClientUserId = rop.ClientUserId; orderDetails.MerchId = store.MerchId; orderDetails.StoreId = rop.StoreId; orderDetails.SellChannelRefType = detail.SellChannelRefType; orderDetails.SellChannelRefId = detail.SellChannelRefId; switch (detail.SellChannelRefType) { case E_StoreSellChannelRefType.Express: orderDetails.SellChannelRefName = "【快递】"; orderDetails.Receiver = rop.Receiver; orderDetails.ReceiverPhone = rop.ReceiverPhone; orderDetails.ReceptionAddress = rop.ReceptionAddress; orderDetails.SellChannelRefType = E_StoreSellChannelRefType.Express; orderDetails.SellChannelRefId = GuidUtil.Empty(); break; case E_StoreSellChannelRefType.SelfTake: orderDetails.SellChannelRefName = "【店内自取】"; orderDetails.Receiver = rop.Receiver; orderDetails.ReceiverPhone = rop.ReceiverPhone; orderDetails.ReceptionAddress = rop.ReceptionAddress; orderDetails.SellChannelRefType = E_StoreSellChannelRefType.SelfTake; orderDetails.SellChannelRefId = GuidUtil.Empty(); break; case E_StoreSellChannelRefType.Machine: var machine = CurrentDb.Machine.Where(m => m.Id == detail.SellChannelRefId).FirstOrDefault(); if (machine == null) { LogUtil.Info("machine:为空"); } orderDetails.SellChannelRefName = "【机器自提】 " + machine.Name; //todo 若 ChannelType 为1 则机器昵称,2为自取 orderDetails.SellChannelRefType = E_StoreSellChannelRefType.Machine; orderDetails.SellChannelRefId = detail.SellChannelRefId; orderDetails.Receiver = null; orderDetails.ReceiverPhone = null; orderDetails.ReceptionAddress = store.Address; break; } orderDetails.OrderId = order.Id; orderDetails.OrderSn = order.Sn; orderDetails.OriginalAmount = detail.OriginalAmount; orderDetails.DiscountAmount = detail.DiscountAmount; orderDetails.ChargeAmount = detail.ChargeAmount; orderDetails.Quantity = detail.Quantity; orderDetails.SubmitTime = DateTime.Now; orderDetails.Creator = operater; orderDetails.CreateTime = DateTime.Now; CurrentDb.OrderDetails.Add(orderDetails); foreach (var detailsChild in detail.Details) { var orderDetailsChild = new OrderDetailsChild(); orderDetailsChild.Id = GuidUtil.New(); orderDetailsChild.Sn = orderDetails.Sn + detail.Details.IndexOf(detailsChild).ToString(); orderDetailsChild.ClientUserId = rop.ClientUserId; orderDetailsChild.MerchId = store.MerchId; orderDetailsChild.StoreId = rop.StoreId; orderDetailsChild.SellChannelRefType = detailsChild.SellChannelRefType; orderDetailsChild.SellChannelRefId = detailsChild.SellChannelRefId; orderDetailsChild.OrderId = order.Id; orderDetailsChild.OrderSn = order.Sn; orderDetailsChild.OrderDetailsId = orderDetails.Id; orderDetailsChild.OrderDetailsSn = orderDetails.Sn; orderDetailsChild.PrdProductSkuId = detailsChild.SkuId; orderDetailsChild.PrdProductSkuName = detailsChild.SkuName; orderDetailsChild.PrdProductSkuMainImgUrl = detailsChild.SkuImgUrl; orderDetailsChild.SalePrice = detailsChild.SalePrice; orderDetailsChild.SalePriceByVip = detailsChild.SalePriceByVip; orderDetailsChild.Quantity = detailsChild.Quantity; orderDetailsChild.OriginalAmount = detailsChild.OriginalAmount; orderDetailsChild.DiscountAmount = detailsChild.DiscountAmount; orderDetailsChild.ChargeAmount = detailsChild.ChargeAmount; orderDetailsChild.SubmitTime = DateTime.Now; orderDetailsChild.Status = E_OrderStatus.WaitPay; orderDetailsChild.Creator = operater; orderDetailsChild.CreateTime = DateTime.Now; CurrentDb.OrderDetailsChild.Add(orderDetailsChild); foreach (var detailsChildSon in detailsChild.Details) { var orderDetailsChildSon = new OrderDetailsChildSon(); orderDetailsChildSon.Id = GuidUtil.New(); orderDetailsChildSon.Sn = orderDetailsChild.Sn + detailsChild.Details.IndexOf(detailsChildSon); orderDetailsChildSon.ClientUserId = rop.ClientUserId; orderDetailsChildSon.MerchId = store.MerchId; orderDetailsChildSon.StoreId = rop.StoreId; orderDetailsChildSon.SellChannelRefType = detailsChildSon.SellChannelRefType; orderDetailsChildSon.SellChannelRefId = detailsChildSon.SellChannelRefId; orderDetailsChildSon.OrderId = order.Id; orderDetailsChildSon.OrderSn = order.Sn; orderDetailsChildSon.OrderDetailsId = orderDetails.Id; orderDetailsChildSon.OrderDetailsSn = orderDetails.Sn; orderDetailsChildSon.OrderDetailsChildId = orderDetailsChild.Id; orderDetailsChildSon.OrderDetailsChildSn = orderDetailsChild.Sn; orderDetailsChildSon.SlotId = detailsChildSon.SlotId; orderDetailsChildSon.PrdProductSkuId = detailsChildSon.SkuId; orderDetailsChildSon.PrdProductSkuName = detailsChildSon.SkuName; orderDetailsChildSon.PrdProductSkuMainImgUrl = detailsChildSon.SkuImgUrl; orderDetailsChildSon.SalePrice = detailsChildSon.SalePrice; orderDetailsChildSon.SalePriceByVip = detailsChildSon.SalePriceByVip; orderDetailsChildSon.Quantity = detailsChildSon.Quantity; orderDetailsChildSon.OriginalAmount = detailsChildSon.OriginalAmount; orderDetailsChildSon.DiscountAmount = detailsChildSon.DiscountAmount; orderDetailsChildSon.ChargeAmount = detailsChildSon.ChargeAmount; orderDetailsChildSon.SubmitTime = DateTime.Now; orderDetailsChildSon.Creator = operater; orderDetailsChildSon.CreateTime = DateTime.Now; orderDetailsChildSon.Status = E_OrderDetailsChildSonStatus.WaitPay; CurrentDb.OrderDetailsChildSon.Add(orderDetailsChildSon); } foreach (var slotStock in detailsChild.SlotStock) { var machineStock = CurrentDb.StoreSellChannelStock.Where(m => m.PrdProductSkuId == slotStock.SkuId && m.SlotId == slotStock.SlotId && m.RefId == slotStock.SellChannelRefId).FirstOrDefault(); machineStock.LockQuantity += slotStock.Quantity; machineStock.SellQuantity -= slotStock.Quantity; machineStock.Mender = operater; machineStock.MendTime = DateTime.Now; var storeSellStockLog = new StoreSellChannelStockLog(); storeSellStockLog.Id = GuidUtil.New(); storeSellStockLog.MerchId = store.MerchId; storeSellStockLog.StoreId = rop.StoreId; storeSellStockLog.RefType = slotStock.SellChannelRefType; storeSellStockLog.RefId = slotStock.SellChannelRefId; storeSellStockLog.SlotId = slotStock.SlotId; storeSellStockLog.PrdProductSkuId = slotStock.SkuId; storeSellStockLog.SumQuantity = machineStock.SumQuantity; storeSellStockLog.LockQuantity = machineStock.LockQuantity; storeSellStockLog.SellQuantity = machineStock.SellQuantity; storeSellStockLog.ChangeType = E_StoreSellChannelStockLogChangeTpye.Lock; storeSellStockLog.ChangeQuantity = slotStock.Quantity; storeSellStockLog.Creator = operater; storeSellStockLog.CreateTime = DateTime.Now; storeSellStockLog.RemarkByDev = string.Format("预定锁定库存:{0}", slotStock.Quantity); CurrentDb.StoreSellChannelStockLog.Add(storeSellStockLog); } } } CurrentDb.Order.Add(order); CurrentDb.SaveChanges(); ts.Complete(); //Task4Factory.Global.Enter(TimerTaskType.CheckOrderPay, order.PayExpireTime.Value, order); ret.OrderId = order.Id; ret.OrderSn = order.Sn; result = new CustomJsonResult <RetOrderReserve>(ResultType.Success, ResultCode.Success, "预定成功", ret); } return(result); }