Ejemplo n.º 1
0
        public async Task <IActionResult> Payment(PaymentViewModel paymentViewModel)
        {
            if (ModelState.IsValid)
            {
                Dictionary <int, string> result = new Dictionary <int, string>();
                int                  isReturn   = 0;                                      // Success or fail
                string               message    = string.Empty;                           // Message
                ProductDetail        productDetailUpdate;                                 // Update product detail
                List <ProductDetail> lstProductDetailUpdate = new List <ProductDetail>(); // Update product detail list
                CartDetail           cartDetailUpdate;                                    // Update cart detail
                List <CartDetail>    lstCartDetailUpdate = new List <CartDetail>();       // Update cart detail list

                foreach (var item in paymentViewModel.Products.Cart)
                {
                    productDetailUpdate = productDetailService.GetOne(item.ProductDetail.ID);
                    if (productDetailUpdate != null)
                    {
                        if (productDetailUpdate.Quantity <= 0)
                        {
                            isReturn = 0;
                            message  = "outofstock";
                            goto Finish;
                        }
                        lstProductDetailUpdate.Add(productDetailUpdate);
                    }
                }

                Order order = null; // Order Entity
                try
                {
                    DataTranfer(out order, paymentViewModel);
                }
                catch (Exception e)
                {
                    message = e.Message;
                    goto Finish;
                }

                // Call Service => Execute
                #region --- Main Execute ---

                // Create new order
                result = orderService.Create(order);

                // Check create new order success or fail
                paymentViewModel.OrderId = result.Keys.FirstOrDefault();
                if (paymentViewModel.OrderId <= 0)             // Create fail
                {
                    result.TryGetValue(isReturn, out message); // Get error message
                    goto Finish;
                }
                else // Add new order successful
                {
                    try
                    {
                        DataTranferOrderDetail(ref order, paymentViewModel);
                    }
                    catch (Exception e)
                    {
                        message = e.Message;
                        goto Finish;
                    }
                }

                result.Clear();
                // Create new order detail
                result = orderDetailService.AddOrderDetail(order);

                // Check create new order detail success or fail
                isReturn = result.Keys.FirstOrDefault();
                if (isReturn <= 0)                             // Create fail
                {
                    result.TryGetValue(isReturn, out message); // Get error message
                    goto Finish;
                }
                else // Create order detail successful, then update quantity of product
                {
                    // Update quantity of product
                    for (int i = 0; i < lstProductDetailUpdate.Count; i++)
                    {
                        lstProductDetailUpdate[i].Quantity -= paymentViewModel.Products.Cart[i].BuyedQuantity;
                    }
                    isReturn = productDetailService.Update(lstProductDetailUpdate); // Update

                    if (isReturn <= 0)
                    {
                        message = "Cập nhật số lượng sản phẩm không thành công";
                        goto Finish;
                    }

                    // Update cart detail
                    foreach (var item in paymentViewModel.Products.Cart)
                    {
                        cartDetailUpdate = cartDetailService.Get(item.CartDetailId);
                        if (cartDetailUpdate != null)
                        {
                            cartDetailUpdate.State = 2;
                            lstCartDetailUpdate.Add(cartDetailUpdate);
                        }
                    }
                    isReturn = cartDetailService.Updates(lstCartDetailUpdate); // Update

                    if (isReturn <= 0)
                    {
                        message = "Cập nhật trạng thái của sản phẩm trong giỏ hàng không thành công";
                        goto Finish;
                    }
                }

Finish:
                // isReturn = 0: Error || 1: Success
                // Giải thích: isReturn = dbContext.Commit = số dòng được thay đổi trong sql
                paymentViewModel.IsError = (isReturn == 0);
                paymentViewModel.Message = message;

                #endregion --- Main Execute ---

                if (paymentViewModel.IsError) // Error
                {
                    return(View(paymentViewModel));
                }

                List <ProductDetail> temp = new List <ProductDetail>();
                foreach (var item in lstProductDetailUpdate)
                {
                    productDetailUpdate = new ProductDetail()
                    {
                        Id       = item.Id,
                        Quantity = item.Quantity
                    };
                    temp.Add(productDetailUpdate);
                }

                // Call SignalR update quantity
                await _hubContext.Clients.All.SendAsync("ReloadQuantity", temp);

                // Call SignalR update statistical
                await _hubContext.Clients.All.SendAsync("ReloadStatistical");

                // Send email
                User user = await userManager.GetUserAsync(User);

                SendEmail(paymentViewModel, user.Email);

                // Call SignalR notification
                await _hubContext.Clients.All.SendAsync("OrderSuccess");

                paymentViewModel.Email            = user.Email;
                paymentViewModel.IsPaymentSuccess = true;
                return(View(paymentViewModel));
            }

            //    //string userIDProc = order.UserId;
            //    //DataTable dataTable = new DataTable();
            //    //dataTable.Columns.Add(new DataColumn("ID", typeof(int)));
            //    //dataTable.Columns.Add(new DataColumn("Quantity", typeof(int)));
            //    //dataTable.Columns.Add(new DataColumn("Processed", typeof(int)));
            //    //dataTable.Columns.Add(new DataColumn("UserIDProc", typeof(string)));
            //    //foreach(var item in order.OrderDetail)
            //    //{
            //    //    dataTable.Rows.Add(item.ProductDetailId, item.Quantity, 0, userIDProc);
            //    //}
            //    //string query = "UpdateProductDetail @ListID";
            //    //orderDetailService.ExecStoreUpdate(query, new SqlParameter("@ListID", dataTable));

            return(View(paymentViewModel));
        }