Пример #1
0
 /// <summary>
 /// 清理缓存
 /// </summary>
 public virtual void ClearCache()
 {
     ProductApiInfoCache.Clear();
     ProductSearchResultCache.Clear();
 }
Пример #2
0
 /// <summary>
 /// 获取商品信息
 /// 商品不存在时返回null,但商品已下架或等待销售时仍然返回信息
 /// 结果会按商品Id和当前登录用户缓存一定时间
 /// </summary>
 /// <param name="productId">商品Id</param>
 /// <returns></returns>
 public virtual object GetProductApiInfo(long productId)
 {
     return(ProductApiInfoCache.GetOrCreate(productId, () =>
                                            UnitOfWork.ReadData <Database.Product, object>(r => {
         var product = r.GetByIdWhereNotDeleted(productId);
         if (product == null)
         {
             return null;                             // 商品不存在
         }
         // 卖家信息
         var seller = product.Seller;
         // 类目信息
         var productCategoryManager = Application.Ioc.Resolve <ProductCategoryManager>();
         var category = product.Category;
         // 相册信息
         var productAlbumManager = Application.Ioc.Resolve <ProductAlbumManager>();
         var mainImageWebPath = productAlbumManager.GetAlbumImageWebPath(
             product.Id, null, ProductAlbumImageType.Normal);
         var imageWebPaths = productAlbumManager.GetExistAlbumImageWebPaths(product.Id)
                             .Select(d => d.ToDictionary(p => p.Key.ToString(), p => p.Value)).ToList();
         // 销售信息
         var salesInfoDisplayFields = Application.Ioc.ResolveMany <IProductSalesInfoDisplayField>()
                                      .Select(d => new { name = new T(d.Name), html = d.GetDisplayHtml(r.Context, product) })
                                      .Where(d => !string.IsNullOrEmpty(d.html)).ToList();
         // 分类和标签
         var classes = product.Classes.Select(c => new { id = c.Id, name = c.Name }).ToList();
         var tags = product.Tags.Select(t => new { id = t.Id, name = t.Name }).ToList();
         var keywords = classes.Select(c => c.name).Concat(tags.Select(t => t.name)).ToList();
         // 匹配数据
         var matchedDataJson = JsonConvert.SerializeObject(
             product.MatchedDatas.Select(d => new {
             Conditions = d.Conditions,
             Affects = d.Affects,
             Price = d.Price,
             PriceCurrency = d.PriceCurrency,
             PriceCurrencyInfo = d.GetCurrency(),
             Weight = d.Weight,
             Stock = d.Stock,
             MatchOrder = d.MatchOrder
         }).OrderBy(d => d.MatchOrder));
         // 销售和非销售属性
         // 添加时遵守原有的显示顺序
         var saleProperties = new List <object>();
         var nonSaleProperties = new List <object>();
         var groups = product.PropertyValues.GroupBy(p => p.Property.Id).Select(g => new {
             property = g.First().Property,
             propertyValues = g,
         }).OrderBy(g => g.property.DisplayOrder);
         foreach (var group in groups)
         {
             var obj = new {
                 property = new {
                     id = group.property.Id,
                     name = new T(group.property.Name)
                 },
                 values = group.propertyValues
                          .OrderBy(value =>
                                   value.PropertyValue == null ? 0 : value.PropertyValue.DisplayOrder)
                          .Select(value => new {
                     id = value.PropertyValue == null ? null : (long?)value.PropertyValue.Id,
                     name = new T(value.PropertyValueName)
                 }).ToList()
             };
             (group.property.IsSalesProperty ? saleProperties : nonSaleProperties).Add(obj);
         }
         return new {
             id = product.Id,
             categoryId = category == null ? null : (long?)category.Id,
             categoryName = category == null ? null : category.Name,
             name = new T(product.Name),
             introduction = product.Introduction,
             type = product.Type,
             typeName = new T(product.Type),
             state = product.State,
             stateName = new T(product.State),
             stateText = new T(string.Format("Product is {0}", product.State)),
             stateTrait = product.GetStateTrait(),
             sellerId = seller == null ? null : (long?)seller.Id,
             sellerName = seller == null ? null : seller.Username,
             classes,
             tags,
             keywords,
             mainImageWebPath,
             imageWebPaths,
             salesInfoDisplayFields,
             matchedDataJson,
             saleProperties,
             nonSaleProperties
         };
     }), ProductApiInfoCacheTime));
 }