Esempio n. 1
0
        /// <summary>
        /// 返回匹配的商品匹配数据
        /// </summary>
        /// <param name="list">商品匹配数据的列表</param>
        /// <param name="matchParameters">商品匹配参数</param>
        /// <returns></returns>
        public static IEnumerable <ProductMatchedData> WhereMatched(
            this IEnumerable <ProductMatchedData> list, ProductMatchParameters matchParameters)
        {
            var matchers = Application.Ioc.ResolveMany <IProductMatchedDataMatcher>();

            return(list.Where(data => matchers.All(m => m.IsMatched(matchParameters, data))));
        }
        /// <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. 3
0
        /// <summary>
        /// 判断是否匹配
        /// </summary>
        public bool IsMatched(ProductMatchParameters parameters, ProductMatchedData data)
        {
            // 获取规格的条件
            // 格式 [ { PropertyId: ..., PropertyValueId: ... }, ... ]
            var exceptedProperties = data.Conditions.GetProperties();

            if (exceptedProperties == null || !exceptedProperties.Any())
            {
                return(true);                // 没有指定条件
            }
            // 判断参数中的规格值列表是否包含条件中的所有规格值
            // 例如 参数 { 颜色: 黑色, 尺码: XXS, 款式: 2013 }, 条件 { 颜色: 黑色, 尺码: XXS }时匹配成功
            // 参数的格式同上
            var incomeProperties = parameters.GetProperties();

            if (incomeProperties == null || !incomeProperties.Any())
            {
                return(false);                // 有指定条件,但参数中没有包含任何规格值
            }
            var incomePropertiesMapping = new Dictionary <long, long?>();

            foreach (var obj in incomeProperties)
            {
                incomePropertiesMapping[obj.PropertyId] = obj.PropertyValueId;
            }
            return(exceptedProperties.All(obj => {
                return incomePropertiesMapping.GetOrDefault(obj.PropertyId) == obj.PropertyValueId;
            }));
        }
Esempio n. 4
0
        /// <summary>
        /// 获取商品匹配参数的描述
        /// </summary>
        /// <param name="product">商品</param>
        /// <param name="parameters">商品匹配参数</param>
        /// <returns></returns>
        public static string GetMatchParametersDescription(
            this Domain.Entities.Product product,
            ProductMatchParameters parameters)
        {
            var descriptionProviders = Application.Ioc
                                       .ResolveMany <IProductMatchParametersDescriptionProvider>();

            return(string.Join(" ", descriptionProviders
                               .OrderBy(p => p.DisplayOrder)
                               .Select(p => p.GetDescription(product, parameters))
                               .Where(d => !string.IsNullOrEmpty(d))));
        }
        /// <summary>
        /// 获取描述,没有时返回null
        /// </summary>
        public string GetDescription(Product product, ProductMatchParameters parameters)
        {
            var data = product.MatchedDatas
                       .Where(d => !string.IsNullOrEmpty(d.ItemNo))
                       .WhereMatched(parameters).FirstOrDefault();

            if (data != null)
            {
                return(new T("ItemNo: {0}", data.ItemNo));
            }
            return(null);
        }
Esempio n. 6
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);
        }
        /// <summary>
        /// 获取描述,没有时返回null
        /// </summary>
        public string GetDescription(Database.Product product, ProductMatchParameters parameters)
        {
            var properties = parameters.GetProperties();

            if (properties == null || properties.Count <= 0)
            {
                return(null);
            }
            var parts = new List <string>();

            foreach (var productToPropertyValue in product.FindPropertyValuesFromPropertyParameters(properties))
            {
                parts.Add(string.Format("{0}: {1}",
                                        new T(productToPropertyValue.Property.Name),
                                        new T(productToPropertyValue.PropertyValueName)));
            }
            return(string.Join(" ", parts));
        }
        /// <summary>
        /// 判断商品匹配参数是否部分相等
        /// </summary>
        public bool IsPartialEqual(
            ProductMatchParameters lhs, ProductMatchParameters rhs)
        {
            var propertiesLhs = lhs.GetProperties();
            var propertiesRhs = rhs.GetProperties();

            if (propertiesLhs == null && propertiesRhs == null)
            {
                return(true);
            }
            else if (propertiesLhs == null || propertiesRhs == null)
            {
                return(false);
            }
            var disinctCount = propertiesLhs.Concat(propertiesRhs)             // 计算并集去除重复后的大小
                               .Select(p => new Pair <long, long>(p.PropertyId, p.PropertyValueId)).Distinct().Count();

            return(propertiesLhs.Count == disinctCount && propertiesLhs.Count == propertiesRhs.Count);
        }
Esempio n. 9
0
		/// <summary>
		/// 初始化
		/// </summary>
		public CartProduct() {
			MatchParameters = new ProductMatchParameters();
		}
 /// <summary>
 /// 设置订购数量
 /// </summary>
 /// <param name="parameters">商品匹配参数</param>
 /// <param name="orderCount">订购数量</param>
 public static void SetOrderCount(this ProductMatchParameters parameters, long orderCount)
 {
     parameters["OrderCount"] = orderCount;
 }
 /// <summary>
 /// 获取订购数量
 /// </summary>
 /// <param name="parameters">商品匹配参数</param>
 /// <returns></returns>
 public static long GetOrderCount(this ProductMatchParameters parameters)
 {
     return(parameters.GetOrDefault <long?>("OrderCount") ?? 0);
 }
 /// <summary>
 /// 获取商品属性参数
 /// </summary>
 /// <param name="parameters">商品匹配参数</param>
 /// <returns></returns>
 public static IList <PropertyParameter> GetProperties(this ProductMatchParameters parameters)
 {
     return(parameters.GetOrDefault <IList <PropertyParameter> >("Properties"));
 }
Esempio n. 13
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);
            }
        }
Esempio n. 14
0
 /// <summary>
 /// 初始化
 /// </summary>
 public OrderProduct()
 {
     MatchParameters = new ProductMatchParameters();
     PropertyValues  = new HashSet <OrderProductToPropertyValue>();
     Deliveries      = new HashSet <OrderDeliveryToOrderProduct>();
 }
 /// <summary>
 /// 初始化
 /// </summary>
 public CreateOrderProductParameters()
 {
     MatchParameters = new ProductMatchParameters();
     Extra           = new Dictionary <string, object>();
 }