Exemple #1
0
        /// <summary>
        /// 判断是否匹配
        /// </summary>
        public bool IsMatched(ProductMatchParameters parameters, ProductMatchedData data)
        {
            // 获取订购数量的条件
            var orderCountGE = data.Conditions.GetOrderCountGE();

            if (orderCountGE == null || orderCountGE <= 1)
            {
                return(true);                // 没有指定条件
            }
            // 判断订购数量是否大于条件中指定的数量
            var orderCount = parameters.GetOrderCount();

            return(orderCount >= orderCountGE.Value);
        }
Exemple #2
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);
            }
        }