示例#1
0
        public static List <ProductBrowsedHistoryModel> GetBrowsingProducts(int num, long userId = 0)
        {
            List <ProductBrowsedHistoryModel> productIdList = new List <ProductBrowsedHistoryModel>();
            string productIds = Core.Helper.WebHelper.GetCookie(CookieKeysCollection.Mall_PRODUCT_BROWSING_HISTORY);

            if (!string.IsNullOrEmpty(productIds))
            {
                var arr = productIds.Split(',');
                foreach (var a in arr)
                {
                    var item = a.Split('#');
                    if (item.Length > 1)
                    {
                        productIdList.Add(new ProductBrowsedHistoryModel()
                        {
                            ProductId = long.Parse(item[0]), BrowseTime = DateTime.Parse(item[1])
                        });
                    }
                    else
                    {
                        productIdList.Add(new ProductBrowsedHistoryModel()
                        {
                            ProductId = long.Parse(item[0]), BrowseTime = DateTime.Now
                        });
                    }
                }
            }

            var ids = productIdList.Select(p => p.ProductId).ToList();
            List <FlashSalePrice> flashSaleList = LimitTimeApplication.GetPriceByProducrIds(ids);

            List <ProductBrowsedHistoryModel> model = new List <ProductBrowsedHistoryModel>();

            if (userId == 0)
            {
                var products = ProductManagerApplication.GetProductByIds(productIdList.Select(a => a.ProductId))
                               .Where(d => d.SaleStatus == Entities.ProductInfo.ProductSaleStatus.OnSale &&
                                      d.AuditStatus == Entities.ProductInfo.ProductAuditStatus.Audited).ToArray()
                               .Select(a => new ProductBrowsedHistoryModel()
                {
                    ImagePath    = Core.MallIO.GetProductSizeImage(a.RelativePath, 1, (int)ImageSize.Size_100),
                    ProductId    = a.Id,
                    ProductName  = a.ProductName,
                    ProductPrice = GetRealPrice(flashSaleList, a.Id, a.MinSalePrice),
                    ShopId       = a.ShopId
                }).ToList();


                return(products.OrderByDescending(a => a.BrowseTime).ToList());
            }
            else
            {
                foreach (var m in productIdList)
                {
                    AddBrowsingProduct(m.ProductId, userId);
                }
                var browsing = ProductManagerApplication.GetBrowsingProducts(userId);
                var products = ProductManagerApplication.GetOnSaleProducts(browsing.Select(p => p.ProductId).ToList());
                browsing = browsing.Where(p => products.Select(o => o.Id).Contains(p.ProductId)).ToList();
                model    =
                    browsing.OrderByDescending(a => a.BrowseTime).Take(num)
                    .Select(a => {
                    var product = products.FirstOrDefault(p => p.Id == a.ProductId);
                    return(new ProductBrowsedHistoryModel()
                    {
                        ImagePath = Core.MallIO.GetProductSizeImage(product.RelativePath, 1, (int)ImageSize.Size_100),
                        ProductId = a.ProductId,
                        ProductName = product.ProductName,
                        ProductPrice = GetRealPrice(flashSaleList, a.ProductId, product.MinSalePrice),
                        BrowseTime = a.BrowseTime,
                        ShopId = product.ShopId
                    });
                }).ToList();
            }
            return(model);
        }