public async Task <IActionResult> AddToCart(int?flowerId)
        {
            HouseUser currnetUser = await GetCurrentUser();

            if (!flowerId.HasValue)
            {
                return(Json(new { status = 0, msg = "添加失败" }));
            }

            if (currnetUser == null)
            {
                return(Json(new { status = 0, msg = "请先登录" }));
            }

            var flowerAddToCart = await _flowerHouseContext.Flowers.Include(f => f.Stock).FirstOrDefaultAsync(f => f.FlowerId == flowerId.Value);

            var shoppingCart = await GetShoppingCartAsync(currnetUser);

            if (shoppingCart == null)
            {
                shoppingCart = new ShoppingCart
                {
                    HouseUserId = currnetUser.Id,
                    TotalPrice  = 0,
                    CartItems   = new List <CartItem>()
                };
                await _flowerHouseContext.AddAsync(shoppingCart);
            }
            var exitedCartItem = shoppingCart.CartItems.FirstOrDefault(ci => ci.FlowerId == flowerId.Value);

            if (exitedCartItem == null)
            {
                exitedCartItem = new CartItem
                {
                    Count     = 1,
                    IsChecked = true,
                    Flower    = flowerAddToCart
                };
                shoppingCart.CartItems.Add(exitedCartItem);
            }
            else
            {
                exitedCartItem.Count    += 1;
                exitedCartItem.IsChecked = true;
            }
            ComputeTotalPrice(shoppingCart);
            await _flowerHouseContext.SaveChangesAsync();

            return(Json(new { status = 1, msg = "添加成功" }));
        }
        public async Task <IActionResult> ShipmentFlower(int?flowerId, int?orderItemId, int?shipmentCount)
        {
            if (flowerId.HasValue && orderItemId.HasValue && shipmentCount.HasValue)
            {
                var flower = await flowerHouseContext.Flowers.Include(f => f.Stock).FirstOrDefaultAsync(f => f.FlowerId == flowerId.Value);

                var orderItem = await flowerHouseContext.OrderItems.Include(oi => oi.Order).ThenInclude(o => o.OrderItems).FirstOrDefaultAsync(oi => oi.OrderItemId == orderItemId.Value);

                if (flower != null && orderItem != null && orderItem.FlowerId == flowerId.Value)
                {
                    orderItem.OrderItemStatus = OrderItemStatus.已发货;
                    if (orderItem.Order.OrderItems.All(oi => oi.OrderItemStatus == OrderItemStatus.已发货))
                    {
                        orderItem.Order.OrderStatus = OrderStatus.已发货;
                    }
                    Shipment shipment = new Shipment
                    {
                        FlowerId      = flowerId.Value,
                        OrderItemId   = orderItemId.Value,
                        ShipmentCount = shipmentCount.Value,
                        ShipmentPrice = shipmentCount.Value * flower.Stock.SellPrice,
                        ShipmentTime  = DateTime.Now
                    };
                    await flowerHouseContext.Shipments.AddAsync(shipment);

                    await flowerHouseContext.SaveChangesAsync();

                    return(Json(new { status = 1, msg = "出货成功", returnUrl = "/Admin/Shipment/List" }));
                }
            }
            return(Json(new { status = 0, msg = "出货失败" }));
        }
Beispiel #3
0
        public async Task <IActionResult> CreateOrUpdate(Tag tag)
        {
            var updateTag = await flowerHouseContext.Tags.FirstOrDefaultAsync(t => t.TagId == tag.TagId);

            if (updateTag == null)
            {
                await flowerHouseContext.AddAsync(tag);
            }
            else
            {
                updateTag.TagName      = tag.TagName;
                updateTag.TagImg       = tag.TagImg;
                updateTag.AssortmentId = tag.AssortmentId;
            }
            await flowerHouseContext.SaveChangesAsync();

            return(RedirectToAction("List"));
        }
        public async Task <IActionResult> BuyConfirmed(string address)
        {
            var currentUser = await GetCurrentUser();

            var shoppingCart = await GetShoppingCartAsync(currentUser);

            if (shoppingCart == null)
            {
                return(Json(new { status = 0, msg = "购物车为空" }));
            }

            Order order = new Order
            {
                Address     = address,
                CreateTime  = DateTime.Now,
                TimeOutTime = DateTime.Now.AddMinutes(30),
                HouseUser   = currentUser,
                OrderStatus = OrderStatus.待支付,
                OrderItems  = new List <OrderItem>()
            };

            foreach (var checkedCartItem in shoppingCart.CartItems.Where(ci => ci.IsChecked))
            {
                order.OrderItems.Add(new OrderItem
                {
                    Count    = checkedCartItem.Count,
                    FlowerId = checkedCartItem.FlowerId
                });
                checkedCartItem.Flower.Stock.StockCount -= checkedCartItem.Count;
                order.TotalPrice        += checkedCartItem.Flower.Stock.SellPrice * checkedCartItem.Count;
                shoppingCart.TotalPrice -= checkedCartItem.Flower.Stock.SellPrice * checkedCartItem.Count;
            }

            shoppingCart.CartItems.RemoveAll(ci => ci.IsChecked);

            await _flowerHouseContext.Orders.AddAsync(order);

            await _flowerHouseContext.SaveChangesAsync();

            string returnUrl = "/Order/Pay?orderid=" + order.OrderId;

            return(Json(new { status = 1, returnUrl }));
        }
 public async Task<IActionResult> CreateOrUpdate(OrderItem orderItem)
 {
     //flowerHouseContext.Entry(flower).State = (flowerHouseContext.Flowers.Any(f => f.FlowerId == flower.FlowerId) ? EntityState.Modified : EntityState.Added);
     var updateEntity = await flowerHouseContext.OrderItems.FirstOrDefaultAsync(o => o.OrderItemId == orderItem.OrderItemId);
     if (updateEntity == null)
     {
         updateEntity = orderItem;
         await flowerHouseContext.AddAsync(updateEntity);
     }
     else
     {
         updateEntity.Count = orderItem.Count;
         updateEntity.FlowerId = orderItem.FlowerId;
         updateEntity.OrderId = orderItem.OrderId;
         flowerHouseContext.Update(updateEntity);
     }
     await flowerHouseContext.SaveChangesAsync();
     return RedirectToAction("List");
 }
Beispiel #6
0
        public async Task <IActionResult> payment(int num)
        {
            HouseUser currentUser = await _userManager.GetUserAsync(User);

            currentUser.Balance = currentUser.Balance + num;
            await flowerHouseContext.SaveChangesAsync();

            return(Redirect("/Home/Index"));
            //return Json(new { msg = "充值成功" });
        }
        public async Task <IActionResult> CreateOrUpdate(Flower flower, int[] tagid)
        {
            var updateFlower = await flowerHouseContext.Flowers.Include(f => f.Stock).FirstOrDefaultAsync(f => f.FlowerId == flower.FlowerId);

            if (updateFlower == null)
            {
                updateFlower           = flower;
                updateFlower.FlowerBuy = -1;
                //Purchase purchase = new Purchase
                //{
                //    Flower = updateFlower,
                //    PurchaseCount = updateFlower.FlowerCount,
                //    PurchasePrice = updateFlower.FlowerCount * updateFlower.FlowerPrice,
                //    PurchaseTime = DateTime.Now
                //};
                //await flowerHouseContext.AddAsync(purchase);
                await flowerHouseContext.AddAsync(updateFlower);
            }
            else
            {
                updateFlower.FlowerName        = flower.FlowerName;
                updateFlower.FlowerMaterial    = flower.FlowerMaterial;
                updateFlower.FlowerEmblem      = flower.FlowerEmblem;
                updateFlower.DistributionRange = flower.DistributionRange;
                updateFlower.FlowerBuy         = flower.FlowerBuy;
                //updateFlower.FlowerPrice = flower.FlowerPrice;
                updateFlower.FlowerImg = flower.FlowerImg;

                flowerHouseContext.Update(updateFlower);
            }
            updateFlower.FlowerTags = new List <FlowerTag>();
            foreach (int tid in tagid)
            {
                updateFlower.FlowerTags.Add(new FlowerTag {
                    FlowerId = updateFlower.FlowerId, TagId = tid
                });
            }
            await flowerHouseContext.SaveChangesAsync();

            return(RedirectToAction("List"));
        }
Beispiel #8
0
        public async Task <IActionResult> AcceptOrder(string orderId)
        {
            var order = await flowerHouseContext.Orders.Include(o => o.OrderItems).FirstOrDefaultAsync(o => o.OrderId == Convert.ToInt32(orderId));

            if (order.OrderStatus == OrderStatus.已支付)
            {
                order.OrderStatus = OrderStatus.待发货;
                order.OrderItems.ForEach(oi =>
                {
                    oi.OrderItemStatus = OrderItemStatus.待发货;
                });

                order.FinishTime = DateTime.Now;
                await flowerHouseContext.SaveChangesAsync();

                return(Json(new { status = 1, msg = "接单成功" }));
            }
            else
            {
                return(Json(new { status = 0, msg = "接单失败" }));
            }
        }
Beispiel #9
0
        public async Task <IActionResult> PurchaseFlower(int?flowerId, int?purchaseCount, int?sellPrice, int?purchasePrice)
        {
            if (flowerId.HasValue && purchaseCount.Value >= 0)
            {
                var flower = await flowerHouseContext.Flowers.Include(f => f.Stock).FirstOrDefaultAsync(f => f.FlowerId == flowerId.Value);

                if (sellPrice.HasValue && flower.Stock == null && purchaseCount.HasValue)
                {
                    Stock stock = new Stock
                    {
                        FlowerId      = flowerId.Value,
                        PurchasePrice = purchasePrice.Value,
                        SellPrice     = sellPrice.Value,
                        StockCount    = purchaseCount.Value
                    };
                    flower.Stock     = stock;
                    flower.FlowerBuy = 0;
                    await flowerHouseContext.Stocks.AddAsync(stock);
                }
                else
                {
                    flower.Stock.StockCount += purchaseCount.Value;
                }
                Purchase purchaseRecord = new Purchase
                {
                    FlowerId      = flowerId.Value,
                    PurchaseCount = purchaseCount.Value,
                    PurchasePrice = purchaseCount.Value * flower.Stock.PurchasePrice,
                    PurchaseTime  = DateTime.Now
                };
                await flowerHouseContext.Purchases.AddAsync(purchaseRecord);

                await flowerHouseContext.SaveChangesAsync();

                return(Json(new { status = 1, msg = "进货成功", returnUrl = "/Admin/Purchase/List" }));
            }
            else
            {
                return(Json(new { status = 0, msg = "进货失败" }));
            }
        }
        public async Task <IActionResult> CreateOrUpdate(HouseUser houseUser)
        {
            var user = await userManager.FindByIdAsync(houseUser.Id);

            if (user == null)
            {
                await flowerHouseContext.Users.AddAsync(houseUser);
            }

            else
            {
                user.Balance        = houseUser.Balance;
                user.Email          = houseUser.Email;
                user.EmailConfirmed = houseUser.EmailConfirmed;
                user.UserName       = houseUser.UserName;
            }

            await flowerHouseContext.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }