Beispiel #1
0
        public static void AddBrowsingProduct(long productId, 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
                        });
                    }
                }
            }
            if (productIdList.Count < 20 && !productIdList.Any(a => a.ProductId == productId))
            {
                productIdList.Add(new ProductBrowsedHistoryModel()
                {
                    ProductId = productId, BrowseTime = DateTime.Now
                });
            }
            else if (productIdList.Count >= 20 && !productIdList.Any(a => a.ProductId == productId))
            {
                productIdList.RemoveAt(productIdList.Count - 1);
                productIdList.Add(new ProductBrowsedHistoryModel()
                {
                    ProductId = productId, BrowseTime = DateTime.Now
                });
            }
            else
            {
                var model = productIdList.Where(a => a.ProductId == productId).FirstOrDefault();
                productIdList.Remove(model);
                productIdList.Add(new ProductBrowsedHistoryModel()
                {
                    ProductId = productId, BrowseTime = DateTime.Now
                });
            }
            if (userId == 0)
            {
                var productsStr = "";
                foreach (var item in productIdList)
                {
                    productsStr += item.ProductId + "#" + item.BrowseTime.ToString() + ",";
                }
                Core.Helper.WebHelper.SetCookie(CookieKeysCollection.Mall_PRODUCT_BROWSING_HISTORY, productsStr.TrimEnd(','), DateTime.Now.AddDays(7));
            }
            else
            {
                foreach (var item in productIdList)
                {
                    try
                    {
                        ProductManagerApplication.AddBrowsingProduct(new Entities.BrowsingHistoryInfo
                        {
                            MemberId   = userId,
                            BrowseTime = item.BrowseTime,
                            ProductId  = item.ProductId
                        });
                    }
                    catch
                    {
                        continue;
                    }
                }
                Core.Helper.WebHelper.DeleteCookie(CookieKeysCollection.Mall_PRODUCT_BROWSING_HISTORY);
            }
        }