Esempio n. 1
0
        public ShoppingCartItemViewModel Update(ShoppingCartItemViewModel shoppingCartItemViewModel)
        {
            var shoppingCartItem = Mapper.Map <ShoppingCartItemViewModel, ShoppingCartItem>(shoppingCartItemViewModel);

            BeginTransaction();

            if (!shoppingCartItem.ValidationResult.IsValid)
            {
                return(shoppingCartItemViewModel);
            }

            _shoppingCartItemService.Update(shoppingCartItem);

            Commit();

            return(shoppingCartItemViewModel);
        }
        public ShoppingCartItemViewModel Update(ShoppingCartItemViewModel shoppingCartItemViewModel)
        {
            var shoppingCartItem = new ShoppingCartItem
            {
                Id             = shoppingCartItemViewModel.Id,
                Active         = shoppingCartItemViewModel.Active,
                ProductId      = shoppingCartItemViewModel.ProductId,
                ShoppingCartId = shoppingCartItemViewModel.ShoppingCartId
            };

            BeginTransaction();

            _shoppingCartItemService.Update(shoppingCartItem);

            Commit();

            return(shoppingCartItemViewModel);
        }
Esempio n. 3
0
        public async Task <ShoppingCartItems> AddItemToCart(ShoppingCartAddItemDto dto, int userId)
        {
            try
            {
                var cartList = await _shoppingCartService.GetListWithShoppingCartItems_ShoppingCarts(x => x.User_Id == userId && x.Status && !x.Is_Deleted);

                var cart    = cartList.FirstOrDefault();
                var product = await _productService.Get(x => x.Id == dto.productId);

                if (product.InStock < dto.quantity)
                {
                    throw new Exception("no stock");
                }
                else
                {
                    if (cart != null)
                    {
                        var cartItems = cart.ShoppingCartItems;
                        if (cartItems != null && cartItems.Any(x => x.Product_Id == dto.productId))
                        {
                            var cartProduct = await _shoppingCartItemService.Get(x => x.Product_Id == dto.productId && x.ShoppingCart_Id == cart.Id);

                            cartProduct.Quantity += dto.quantity;
                            if (product.InStock < cartProduct.Quantity)
                            {
                                throw new Exception("no stock");
                            }
                            return(await _shoppingCartItemService.Update(cartProduct));
                        }
                        else
                        {
                            var itemCartItem = new ShoppingCartItems()
                            {
                                Product_Id      = dto.productId,
                                Quantity        = dto.quantity,
                                ShoppingCart_Id = cart.Id,
                            };
                            return(await _shoppingCartItemService.Add(itemCartItem));
                        }
                    }
                    else
                    {
                        var itemCart = new ShoppingCarts()
                        {
                            User_Id    = userId,
                            Status     = true,
                            Is_Deleted = false,
                        };
                        var newCart = await _shoppingCartService.Add(itemCart);

                        var itemCartItem = new ShoppingCartItems()
                        {
                            Product_Id      = dto.productId,
                            Quantity        = dto.quantity,
                            ShoppingCart_Id = newCart.Id,
                        };
                        return(await _shoppingCartItemService.Add(itemCartItem));
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 4
0
        public IHttpActionResult Create([FromBody] OrderDTO OrderDto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(Ok(new ApiResult <System.Web.Http.ModelBinding.ModelStateDictionary>()
                    {
                        Code = 3,
                        Data = ModelState,
                        Info = "请仔细填写表单!"
                    }));
                }

                if (!OrderDto.ShopCartIds.Any())
                {
                    return(Ok(new ApiResult <string>()
                    {
                        Code = 4,
                        Info = "请选择商品然后支付!"
                    }));
                }

                using (TransactionScope scope = new TransactionScope())
                {
                    var     shopCartItems = ShoppingCartItemService.GetAll().Where(x => OrderDto.ShopCartIds.Contains(x.Id)).ToList();
                    decimal orderTotal    = 0;
                    foreach (var item in shopCartItems)
                    {
                        orderTotal += item.UnitPrice * item.Quantity;
                    }

                    WxPayData data    = new WxPayData();
                    var       orderNo = WxPayApi.GenerateOutTradeNo();
                    data.SetValue("body", "麦呗商城-微信收款");
                    data.SetValue("attach", "麦呗商城");
                    data.SetValue("out_trade_no", orderNo);
                    data.SetValue("total_fee", 1);
                    // data.SetValue("total_fee", Convert.ToInt32(entity.OrderTotal*100));
                    data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
                    data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));
                    data.SetValue("trade_type", "APP");

                    WxPayData result = WxPayApi.UnifiedOrder(data);

                    WxPayData appData = new WxPayData();
                    appData.SetValue("prepayid", result.GetValue("prepay_id").ToString());
                    WxPayData appResult = WxPayApi.AppOrder(appData);

                    var entity = OrderDto.ToEntity();
                    entity.PrePayId    = result.GetValue("prepay_id").ToString();
                    entity.WeChatSign  = appResult.GetValue("sign").ToString();
                    entity.NonceStr    = appResult.GetValue("noncestr").ToString();
                    entity.TimeSpan    = appResult.GetValue("timestamp").ToString();
                    entity.OrderGuid   = Guid.NewGuid();
                    entity.OrderStatus = OrderStatus.NotPay;
                    entity.OrderTotal  = orderTotal;
                    entity.OutTradeNo  = orderNo;
                    entity.CustomerId  = User.Identity.GetUserId();
                    entity.CustomerIp  = Request.GetOwinContext().Request.RemoteIpAddress;
                    entity.CreateTime  = DateTime.Now;
                    OrderService.Insert(entity);

                    //更新购物车ItemStatus
                    foreach (var shopCart in shopCartItems)
                    {
                        shopCart.OrderId            = entity.Id;
                        shopCart.ShoppingCartStatus = ShoppingCartStatus.Order;
                    }

                    ShoppingCartItemService.Update(shopCartItems);

                    scope.Complete();
                    return(Ok(new ApiResult <OrderDTO>()
                    {
                        Data = entity.ToModel(),
                        Info = "添加订单成功"
                    }));
                }
            }
            catch (Exception ex)
            {
                return(Ok(new ApiResult <string>()
                {
                    Code = 1,
                    Info = ex.Message
                }));
            }
        }