Example #1
0
        public async Task <APIResult> GetOrderInfo([FromBody] GetOrderInfoArgsModel args)
        {
            if (!args.shopOrderId.HasValue)
            {
                throw new Exception("shopOrderId不能为空");
            }
            ShopOrder shopOrder = db.Query <ShopOrder>()
                                  .Where(m => m.Id == args.shopOrderId.Value)
                                  .Include(m => m.ShopOrderOtherFee)
                                  .Include(m => m.ShopOrderSelfHelp)
                                  .Include(m => m.ShopOrderMoneyOffRule)
                                  .Include(m => m.ShopPart)
                                  .FirstOrDefault();

            if (shopOrder == null)
            {
                throw new Exception("订单记录不存在");
            }

            ShopOrderServer shopOrderServer = new ShopOrderServer(shopOrder);



            GetOrderInfoResultModel rtn = new GetOrderInfoResultModel()
            {
                Code        = shopOrder.OrderNumber,
                CreateTime  = shopOrder.AddTime.ToString("yyyy-MM-dd HH:mm:ss"),
                PayTime     = shopOrder.PayTime?.ToString("yyyy-MM-dd HH:mm:ss"),
                PayMent     = shopOrder.Payment / 100m,
                PayStatus   = shopOrderServer.GetPayStatus(),
                OrderStatus = shopOrder.Status.ToString(),
                Remark      = shopOrder.Remark ?? "",
                IsTakeOut   = shopOrder.IsTakeOut,
                PayWay      = shopOrder.PayWay
            };

            if (shopOrder.ShopOrderMoneyOffRule != null)
            {
                rtn.MoneyOffRule = new MoneyOffRule()
                {
                    Discount   = shopOrder.ShopOrderMoneyOffRule.Discount / 100m,
                    FullAmount = shopOrder.ShopOrderMoneyOffRule.FullAmount / 100m
                };
            }
            if (shopOrder.ShopOrderOtherFee != null)
            {
                rtn.OtherFee = new OtherFee()
                {
                    BoxFee      = shopOrder.ShopOrderOtherFee.BoxFee / 100m,
                    DeliveryFee = shopOrder.ShopOrderOtherFee.DeliveryFee / 100m
                };
            }
            if (shopOrder.ShopOrderSelfHelp != null)
            {
                rtn.SelfHelp = new SelfHelpInfo()
                {
                    SelfHelpCode = shopOrder.ShopOrderSelfHelp.Number,
                    TakeWay      = shopOrder.ShopOrderSelfHelp.IsTakeOut ? "外带" : "堂食"
                };
            }
            if (shopOrder.ShopPart != null)
            {
                rtn.ShopPartName = shopOrder.ShopPart.Title;
            }
            if (shopOrder.IsTakeOut)
            {
                var takeOut = db.Query <ShopOrderTakeout>()
                              .Where(m => m.ShopOrderId == shopOrder.Id)
                              .FirstOrDefault();
                if (takeOut != null)
                {
                    rtn.TakeOutInfo = new TakeOutInfo()
                    {
                        DiningWay  = takeOut.TakeWay.ToString(),
                        PickTile   = takeOut.TakeWay == TakeWay.自提 ? "预计自提时间" : "期望配送时间",
                        PickUpTime = takeOut.PickupTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        Address    = takeOut.Address,
                        Person     = takeOut.Name,
                        Phone      = takeOut.Phone
                    };
                }
            }

            var orderItems = db.Query <ShopOrderItem>()
                             .Where(m => !m.IsDel && m.ShopOrderId == args.shopOrderId.Value)
                             .Select(m => new CommodityInfo()
            {
                Cover      = m.CommodityStock.Sku.Commodity.Cover,
                Count      = m.Count,
                VipPrice   = (m.CommodityStock.Sku.Commodity.CategoryId != 0 && m.CommodityStock.Sku.Commodity.UseMemberPrice),
                PrimePrice = m.PrimePrice * m.Count / 100m,
                Name       = $"{m.CommodityName}({m.SkuSummary})",
                SalePrice  = m.SalePrice * m.Count / 100m
            }).ToList();

            rtn.Items = orderItems;
            if (shopOrder.takeDistributionType == TakeDistributionType.达达配送)
            {
                ThirdServer thirdServer      = new ThirdServer(db, thirdConfig);
                var         thirdqueryresult = await thirdServer.ThirdOrderQuery(new BLL.Third.COrderInfoQueryModel()
                {
                    ShopId = shopOrder.ShopId, order_id = shopOrder.OrderNumber
                });

                rtn.cThirdInfoQuery = thirdqueryresult;
            }
            return(Success(rtn));
        }
Example #2
0
        public APIResult Add([FromBody] AddArgsModel args)
        {
            //TODO:这里还需要进行锁操作,保证每个店铺的库存不是负数
            if (args.Items == null || args.Items.Count <= 0)
            {
                throw new Exception("商品内容不能为空");
            }
            bool isTakeOut = args.IsTakeOut == null ? false : args.IsTakeOut.Value;
            bool isThird   = false;
            var  memberId  = GetMemberId();
            var  shop      = db.Query <Shop>()
                             .Where(m => !m.IsDel)
                             .Where(m => m.Id == args.ShopId)
                             .FirstOrDefault();

            if (shop == null)
            {
                throw new Exception("商铺纪录不存在");
            }

            ShopMember shopMember      = ShopMemberServer.GetShopMember(db, shop.Id, memberId);
            var        shopMemberSet   = ShopMemberServer.GetShopMemberSet(db, shop.Id);
            bool       IsTopUpDiscount = shopMemberSet == null ? false : shopMemberSet.IsTopUpDiscount;
            double     memberDiscount  = 1;

            if (shopMember != null)
            {
                var shopMemberLevel = db.GetSingle <ShopMemberLevel>(shopMember.ShopMemberLevelId);
                if (shopMemberLevel != null)
                {
                    memberDiscount = shopMemberLevel.Discount;
                }
            }

            var shoptakeoutinfo = db.ShopTakeOutInfo.FirstOrDefault(r => r.ShopId == args.ShopId && !r.IsDel);

            if (shoptakeoutinfo != null && shoptakeoutinfo.TakeDistributionType == TakeDistributionType.达达配送 && isTakeOut && args.TakeWay == TakeWay.送货上门)
            {
                isThird = true;
            }

            var model = new ShopOrder()
            {
                AddTime              = DateTime.Now,
                MemberId             = memberId,
                ShopId               = args.ShopId,
                Remark               = args.Remark,
                AddUser              = $"member{memberId}",
                AddIp                = GetIp(),
                Flag                 = Guid.NewGuid().ToString(),
                IsTakeOut            = isTakeOut,
                Status               = ShopOrderStatus.待支付,
                takeDistributionType = isThird == true ? TakeDistributionType.达达配送 : TakeDistributionType.商家配送
            };
            ShopOrderServer shopOrderServer = new ShopOrderServer(model);

            db.AddTo(model);
            if (isTakeOut)
            {
                shopOrderServer.RecordTakeout(db, shop, memberId, args.TakeWay.Value, args.PickupTime);
            }
            else if (args.IsSelfHelp.HasValue && args.IsSelfHelp.Value)
            {
                shopOrderServer.RecordShopOrderSelfHelp(db, shop.Id, args.IsSelfHelpTakeOut ?? false);
            }
            else if (!string.IsNullOrEmpty(args.ShopPartFlag))
            {
                shopOrderServer.RecordScancode(db, args.ShopPartFlag);
            }
            else
            {
                throw new Exception("请先扫描桌上二唯码");
            }
            int cartCount = 0;

            args.Items.ForEach(m => cartCount += m.Count);
            shopOrderServer.RecordOtherFee(db, args.TakeWay, cartCount);
            var commodityIdAndCounts = new Dictionary <int, int>();

            //商铺库存判断
            foreach (var item in args.Items)
            {
                var stock = db.Query <ShopCommodityStock>()
                            .Where(m => !m.IsDel)
                            .Where(m => m.ShopId == shop.Id)
                            .Where(m => m.Sku.Flag == item.SkuFlag)
                            .Select(m => new
                {
                    m.Id,
                    m.Stock,
                    IsCombo       = m.Sku.Commodity.CategoryId == 0, //是否套餐
                    CommodityName = m.Sku.Commodity.Name,
                    m.Sku.CommodityId,
                    m.CostPrice,
                    m.SalePrice,
                    m.MarketPrice,
                    HasVipPrice = m.Sku.Commodity.UseMemberPrice,
                    SkuSummary  = m.Sku.Summary
                })
                            .FirstOrDefault();
                if (stock == null)
                {
                    throw new Exception("商品不存在或者商铺商品未上架");
                }
                if (stock.Stock <= 0 || stock.Stock < item.Count)
                {
                    throw new Exception("库存不足");
                }

                db.AddStock(stock.Id, -item.Count);

                //纪录下commodity的count
                if (commodityIdAndCounts.ContainsKey(stock.CommodityId))
                {
                    commodityIdAndCounts[stock.CommodityId] += item.Count;
                }
                else
                {
                    commodityIdAndCounts.Add(stock.CommodityId, item.Count);
                }


                var orderItem = new ShopOrderItem()
                {
                    CommodityName    = stock.CommodityName,
                    CommodityStockId = stock.Id,
                    AddIp            = model.AddIp,
                    AddTime          = model.AddTime,
                    AddUser          = model.AddUser,
                    CostPrice        = stock.CostPrice,
                    SalePrice        = (stock.HasVipPrice && !stock.IsCombo && (!IsTopUpDiscount || args.IsBalance)) ? ShopMemberServer.GetMemberPrice(stock.SalePrice, memberDiscount) : stock.SalePrice,
                    PrimePrice       = stock.SalePrice,
                    ShopOrder        = model,
                    Count            = item.Count,
                    SkuSummary       = stock.SkuSummary
                };
                db.AddTo(orderItem);

                model.Amount += orderItem.SalePrice * orderItem.Count;
            }

            if (args.ComboPrice.HasValue)
            {
                model.Amount += args.ComboPrice.Value;
            }

            //这里尝试扣钱
            //var memberAmount = db.GetMemberAmountList(memberId);
            //var availAmount = memberAmount.GetAvailAmount();
            //if (availAmount >= model.Amount)
            //{
            //    memberAmount.DecreaseAvailAmount(model.Amount, 0, $"{shop.Name}消费", Newtonsoft.Json.JsonConvert.SerializeObject(new { shopId = shop.Id, orderFlag = model.Flag }), FinaceType.商品购买支出);
            //    memberAmount.UpdateMemberAmountCache();
            //    model.PayTime = DateTime.Now;

            //    //更新商品的销售量
            //    //注意,这里如果有一个品牌,多个店铺的情况,会出现销售额共享的情况
            //    var commodityIds = commodityIdAndCounts.Select(m => m.Key).ToList();
            //    var commoditys = db.Query<ShopBrandCommodity>()
            //            .Where(m => commodityIds.Contains(m.Id))
            //            .ToList();
            //    foreach (var item in commoditys)
            //    {
            //        item.SalesForMonth += commodityIdAndCounts[item.Id];
            //    }
            //    db.SaveChanges();
            //    AfterOrderPlacing(model, shop.Name);
            //}
            //else
            //{
            //    db.SaveChanges();
            //}
            model.OrderNumber = shopOrderServer.GenerateOrderNumber();

            shopOrderServer.ComputePayment(db);
            db.SaveChanges();
            model.Shop = null;  //不返回商店信息
            return(Success(model));
        }