/// <summary>
        /// 添加购物车商品到当前会话
        /// 如果商品已在购物车则增加里面的数量
        /// </summary>
        /// <param name="productId">商品Id</param>
        /// <param name="type">购物车商品类型</param>
        /// <param name="parameters">匹配参数</param>
        public virtual void AddCartProduct(
            Guid productId, CartProductType type, ProductMatchParameters parameters)
        {
            // 检查是否允许非会员下单
            var configManager  = Application.Ioc.Resolve <GenericConfigManager>();
            var settings       = configManager.GetData <OrderSettings>();
            var sessionManager = Application.Ioc.Resolve <SessionManager>();
            var session        = sessionManager.GetSession();
            var user           = session.GetUser();

            if (user == null && !settings.AllowAnonymousVisitorCreateOrder)
            {
                throw new ForbiddenException(new T("Create order require user logged in"));
            }
            // 调用仓储添加购物车商品
            using (UnitOfWork.Scope()) {
                var repository = Application.Ioc.Resolve <CartProductRepository>();
                repository.AddOrIncrease(session, productId, type, parameters);
            }
            // 非会员登录时,在购物车商品添加成功后需要延长会话时间
            if (user == null)
            {
                session.SetExpiresAtLeast(SessionExpireDaysForNonUserPurcharse);
                sessionManager.SaveSession();
            }
            // 删除相关的缓存
            CartProductTotalCountCache.Remove(type);
        }
Esempio n. 2
0
        /// <summary>
        /// 获取当前会话下的购物车商品列表
        /// 为了保证数据的实时性,这个函数不使用缓存
        /// </summary>
        /// <param name="type">购物车商品类型</param>
        /// <returns></returns>
        public virtual IList <CartProduct> GetCartProducts(CartProductType type)
        {
            var sessionManager = Application.Ioc.Resolve <SessionManager>();

            return(UnitOfWork.ReadRepository <CartProductRepository, IList <CartProduct> >(r =>
                                                                                           r.GetManyBySession(sessionManager.GetSession(), type).OrderBy(c => c.Id).ToList()));
        }
        /// <summary>
        /// 获取当前会话下的购物车商品列表
        /// 为了保证数据的实时性,这个函数不使用缓存
        /// </summary>
        /// <param name="type">购物车商品类型</param>
        /// <returns></returns>
        public virtual IList <CartProduct> GetCartProducts(CartProductType type)
        {
            var sessionManager = Application.Ioc.Resolve <SessionManager>();
            var session        = sessionManager.GetSession();
            var repository     = Application.Ioc.Resolve <CartProductRepository>();

            using (UnitOfWork.Scope()) {
                return(repository
                       .GetManyBySession(sessionManager.GetSession(), type)
                       .OrderBy(c => c.Id).ToList());
            }
        }
 /// <summary>
 /// 获取购物车商品的总数量(商品 * 订购数量)
 /// 结果会按当前用户Id和购物车商品类型缓存一定时间
 /// </summary>
 /// <param name="type">购物车商品类型</param>
 /// <returns></returns>
 public virtual long GetCartProductTotalCount(CartProductType type)
 {
     return(CartProductTotalCountCache.GetOrCreate(type, () => {
         var sessionManager = Application.Ioc.Resolve <SessionManager>();
         var session = sessionManager.GetSession();
         var repository = Application.Ioc.Resolve <CartProductRepository>();
         using (UnitOfWork.Scope()) {
             return repository
             .GetManyBySession(sessionManager.GetSession(), type)
             .Sum(p => (long?)p.Count);
         }
     }, CartProductTotalCountCacheTime) ?? 0);
 }
Esempio n. 5
0
        /// <summary>
        /// 获取购物车商品的总数量(商品 * 订购数量)
        /// 结果会按当前用户Id和购物车商品类型缓存一定时间
        /// </summary>
        /// <param name="type">购物车商品类型</param>
        /// <returns></returns>
        public virtual long GetCartProductTotalCount(CartProductType type)
        {
            // 从缓存获取
            var count = CartProductTotalCountCache.GetOrDefault(type);

            if (count != null)
            {
                return(count.Value);
            }
            // 从数据库获取
            var sessionManager = Application.Ioc.Resolve <SessionManager>();

            count = UnitOfWork.ReadRepository <CartProductRepository, long>(r =>
                                                                            r.GetManyBySession(sessionManager.GetSession(), type).Sum(p => (long?)p.Count) ?? 0);
            // 保存到缓存并返回
            CartProductTotalCountCache.Put(type, count, CartProductTotalCountCacheTime);
            return(count.Value);
        }
        /// <summary>
        /// 获取当前会话中的购物车的信息
        /// 为了保证数据的实时性,这个函数不使用缓存
        /// </summary>
        /// <param name="type">购物车类型</param>
        /// <returns></returns>
        public virtual object GetCartApiInfo(CartProductType type)
        {
            // 购物车商品显示信息
            var cartProducts    = GetCartProducts(type);
            var displayInfos    = cartProducts.Select(c => c.ToOrderProductDisplayInfo()).ToList();
            var anyRealProducts = displayInfos.Any(d => d.IsRealProduct);
            // 收货地址表单
            var sessionManager      = Application.Ioc.Resolve <SessionManager>();
            var orderManager        = Application.Ioc.Resolve <SellerOrderManager>();
            var user                = sessionManager.GetSession().GetUser();
            var userId              = user?.Id;
            var shippingAddressForm = new ShippingAddressForm();

            shippingAddressForm.Bind();
            // 订单留言表单
            var commentForm = new CreateOrderCommenForm();

            commentForm.Bind();
            // 物流列表,各个卖家都有单独的列表
            // 没有实体商品的卖家不包含在列表中
            var sellers = displayInfos
                          .Where(d => d.IsRealProduct)
                          .Select(d => new { d.SellerId, d.Seller }).GroupBy(d => d.SellerId);
            var logisticsWithSellers = sellers.Select(s => new {
                sellerId      = s.Key,
                seller        = s.First().Seller,
                logisticsList = orderManager.GetAvailableLogistics(userId, s.Key)
                                .Select(l => l.ToLiquid()).ToList()
            }).ToList();
            // 支付接口列表,各个卖家使用同一个列表
            // 卖家不应该提供单独的支付接口,应该使用结算处理
            var paymentApis = orderManager.GetAvailablePaymentApis(userId)
                              .Select(a => a.ToLiquid()).ToList();
            // 订单创建表单
            var createOrderForm = new CreateOrderForm();

            createOrderForm.Bind();
            return(new {
                displayInfos, anyRealProducts,
                shippingAddressForm, commentForm,
                logisticsWithSellers, paymentApis, createOrderForm,
            });
        }
Esempio n. 7
0
        /// <summary>
        /// 添加购物车商品
        /// 如果商品已在购物车则增加里面的数量
        /// </summary>
        /// <param name="session">会话</param>
        /// <param name="productId">商品Id</param>
        /// <param name="type">购物车商品类型</param>
        /// <param name="parameters">商品匹配参数</param>
        public virtual void AddOrIncrease(Session session,
                                          Guid productId, CartProductType type, ProductMatchParameters parameters)
        {
            // 判断商品是否可以购买(只判断商品本身,不判断规格等匹配参数)
            var productRepository = Application.Ioc.Resolve <ProductRepository>();
            var product           = productRepository.Get(p => p.Id == productId);

            if (product == null)
            {
                throw new BadRequestException(new T("The product you are try to purchase does not exist."));
            }
            else if (!(product.GetProductState() is IAmPurchasable))
            {
                throw new BadRequestException(new T("The product you are try to purchase does not purchasable."));
            }
            // 获取订购数量
            var orderCount = parameters.GetOrderCount();

            if (orderCount <= 0)
            {
                throw new BadRequestException(new T("Order count must larger than 0"));
            }
            // 立刻购买时删除原有的购物车商品列表
            // 加入购物车时获取现有的购物车商品列表,判断是否可以增加已有的数量
            CartProduct increaseTo        = null;
            var         existCartProducts = GetManyBySession(session, type).ToList();

            if (type == CartProductType.Buynow)
            {
                existCartProducts.ForEach(p => Delete(p));
            }
            else
            {
                var comparers = Application.Ioc.ResolveMany <IProductMatchParametersComparer>();
                increaseTo = existCartProducts.FirstOrDefault(cartProduct => {
                    // 比较商品是否相同
                    if (cartProduct.Product.Id != product.Id)
                    {
                        return(false);
                    }
                    // 比较匹配参数是否相同
                    var existParameters = cartProduct.MatchParameters;
                    return(comparers.All(c => c.IsPartialEqual(parameters, existParameters)));
                });
            }
            // 修改到数据库
            if (increaseTo != null)
            {
                // 修改已有的数量
                Save(ref increaseTo, p => p.UpdateOrderCount(checked (p.Count + orderCount)));
            }
            else
            {
                // 添加新的购物车商品
                var configManager  = Application.Ioc.Resolve <GenericConfigManager>();
                var orderSettings  = configManager.GetData <OrderSettings>();
                var userRepository = Application.Ioc.Resolve <UserRepository>();
                var now            = DateTime.UtcNow;
                var cartProduct    = new CartProduct()
                {
                    Type            = type,
                    Owner           = userRepository.Get(u => u.Id == session.ReleatedId),
                    OwnerSessionId  = (session.ReleatedId != null) ? null : (Guid?)session.Id,
                    Product         = product,
                    Count           = orderCount,
                    MatchParameters = parameters,
                    CreateTime      = now,
                    ExpireTime      = now.AddDays(
                        (type == CartProductType.Buynow) ?
                        orderSettings.BuynowCartProductExpiresDays :
                        orderSettings.NormalCartProductExpiresDays)
                };
                Save(ref cartProduct);
            }
        }