/// <summary>
        /// 把ProcedToCheckout()傳回來的CreateOrderViewModel轉成對應的Order
        /// 因為Order可能是「送餐到府」也有可能是「來店取餐」,所以需要做到判斷
        /// </summary>
        /// <param name="viewModel"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        private Order ConvertViewModelToOrder(CreateOrderViewModel viewModel, string userId)
        {
            Order order = new Order()
            {
                UserId = userId,
                OrderDateTime = DateTime.Now,
                RequireDateTime = viewModel.RequireDateTime,
                TotalPrice = 0,
                PaymentMethodId = viewModel.PaymentMethodId,
                CollectionMethodId = viewModel.CollectionMethodId,
                Comment = viewModel.Comment

            };
            if ((viewModel.PaymentMethodId == 1) && (viewModel.IsUserRegAddress == false))
            {
                order.Address_AddCity = viewModel.AddCity;
                order.Address_AddDistrict = viewModel.AddDistrict;
                order.Address_AddFull = viewModel.AddFull;
                order.Address_PostCode = viewModel.PostCode;

            }
            else
            {
                Customer customer = customerRepo.GetSingleEntity(x => x.UserId == User.Identity.Name);
                order.Address_AddCity = customer.Address.AddCity;
                order.Address_AddDistrict = customer.Address.AddDistrict;
                order.Address_AddFull = customer.Address.AddFull;
                order.Address_PostCode = customer.Address.PostCode;
            }
            return order;
        }
        /// <summary>
        /// 把購物車裏面每一筆資料轉換成對應的OrderDetail,用來做購買完成下定單的動作。
        /// </summary>
        /// <param name="order">主要爲了Order的Id,這樣OrderDetail才知道對應那個</param>
        /// <returns>傳回這一筆Order的總金額</returns>
        public decimal ShoppingCartToOrderDetails(Order order)
        {
            var cartItems = GetShoppingCartItems();

            decimal totalPrice = 0;

            foreach(var item in cartItems)
            {
                var orderDetail = new OrderDetial()
                {
                    MealId = item.MealId,
                    OrderId = order.OrderId,
                    Quantity = item.Quantity,
                    UnitPrice = item.UnitPrice
                };

                totalPrice += (orderDetail.UnitPrice * orderDetail.Quantity);
                orderDetailRepo.Insert(orderDetail);
            }

            orderDetailRepo.SaveChanges();
            EmptyCart();

            return totalPrice;
        }
        /// <summary>
        /// 把Order對應的OrderDetail轉為ShoppingCartViewModel。
        /// 主要是當對一個訂單點選「詳細」,裏面OrderDetail其實是使用購物車出現的那種模型
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public ShoppingCartViewModel PopulateOrderDetails(Order order)
        {
            var orderDetials = order.OrderDetial;

            ShoppingCartViewModel viewModel = new ShoppingCartViewModel()
                {
                     CartItems = new List<ShoppingCart>()
                };

            decimal totalPrice = 0;

            foreach (var item in orderDetials)
            {
                var orderDetail = new ShoppingCart()
                {

                    MealId = item.MealId,
                    MealName = item.Meal.MealName,
                    Quantity = item.Quantity,
                    UnitPrice = item.UnitPrice,
                };

                totalPrice += (orderDetail.UnitPrice * orderDetail.Quantity);
                viewModel.CartItems.Add(orderDetail);
            }

            viewModel.TotalPrice = totalPrice;

            return viewModel;
        }