Esempio n. 1
0
        /// <summary>
        /// 搜索
        /// </summary>
        public ActionResult Search()
        {
            //搜索词
            string keyword = WebHelper.GetQueryString("keyword");

            WorkContext.SearchWord = WebHelper.HtmlEncode(keyword);
            if (keyword.Length == 0)
            {
                return(PromptView(WorkContext.UrlReferrer, "请输入搜索词"));
            }
            if (!SecureHelper.IsSafeSqlString(keyword))
            {
                return(PromptView(WorkContext.UrlReferrer, "您搜索的商品不存在"));
            }

            //异步保存搜索历史
            Asyn.UpdateSearchHistory(WorkContext.Uid, keyword);

            //判断搜索词是否为分类名称,如果是则重定向到分类页面
            int cateId = Categories.GetCateIdByName(keyword);

            if (cateId > 0)
            {
                return(Redirect(Url.Action("category", new RouteValueDictionary {
                    { "cateId", cateId }
                })));
            }
            else
            {
                cateId = WebHelper.GetQueryInt("cateId");
            }

            //分类列表
            List <CategoryInfo> categoryList = null;
            //分类信息
            CategoryInfo categoryInfo = null;
            //品牌列表
            List <BrandInfo> brandList = null;

            //品牌id
            int brandId = Brands.GetBrandIdByName(keyword);

            if (brandId > 0)//当搜索词为品牌名称时
            {
                //获取品牌相关的分类
                categoryList = Brands.GetBrandCategoryList(brandId);

                //由于搜索结果的展示是以分类为基础的,所以当分类不存在时直接将搜索结果设为“搜索的商品不存在”
                if (categoryList.Count == 0)
                {
                    return(PromptView(WorkContext.UrlReferrer, "您搜索的商品不存在"));
                }

                if (cateId > 0)
                {
                    categoryInfo = Categories.GetCategoryById(cateId);
                }
                else
                {
                    //当没有进行分类的筛选时,将分类列表中的首项设为当前选中的分类
                    categoryInfo = categoryList[0];
                    cateId       = categoryInfo.CateId;
                }
                brandList = new List <BrandInfo>();
                brandList.Add(Brands.GetBrandById(brandId));
            }
            else//当搜索词为商品关键词时
            {
                //获取商品关键词相关的分类
                categoryList = Searches.GetCategoryListByKeyword(keyword);

                //由于搜索结果的展示是以分类为基础的,所以当分类不存在时直接将搜索结果设为“搜索的商品不存在”
                if (categoryList.Count == 0)
                {
                    return(PromptView(WorkContext.UrlReferrer, "您搜索的商品不存在"));
                }

                if (cateId > 0)
                {
                    categoryInfo = Categories.GetCategoryById(cateId);
                }
                else
                {
                    categoryInfo = categoryList[0];
                    cateId       = categoryInfo.CateId;
                }
                //根据商品关键词获取分类相关的品牌
                brandList = Searches.GetCategoryBrandListByKeyword(cateId, keyword);
                if (brandList.Count == 0)
                {
                    return(PromptView(WorkContext.UrlReferrer, "您搜索的商品不存在"));
                }
                brandId = WebHelper.GetQueryInt("brandId");
            }
            //最后再检查一遍分类是否存在
            if (categoryInfo == null)
            {
                return(PromptView(WorkContext.UrlReferrer, "您搜索的商品不存在"));
            }

            //筛选价格
            int filterPrice = WebHelper.GetQueryInt("filterPrice");
            //筛选属性
            string filterAttr = WebHelper.GetQueryString("filterAttr");
            //是否只显示有货
            int onlyStock = WebHelper.GetQueryInt("onlyStock");
            //排序列
            int sortColumn = WebHelper.GetQueryInt("sortColumn");
            //排序方向
            int sortDirection = WebHelper.GetQueryInt("sortDirection");
            //当前页数
            int page = WebHelper.GetQueryInt("page");

            //分类筛选属性及其值列表
            List <KeyValuePair <AttributeInfo, List <AttributeValueInfo> > > cateAAndVList = Categories.GetCategoryFilterAAndVList(cateId);

            //分类价格范围列表
            string[] catePriceRangeList = StringHelper.SplitString(categoryInfo.PriceRange, "\r\n");

            //筛选属性处理
            List <int> attrValueIdList = new List <int>();

            string[] filterAttrValueIdList = StringHelper.SplitString(filterAttr, "-");
            if (filterAttrValueIdList.Length != cateAAndVList.Count)//当筛选属性和分类的筛选属性数目不对应时,重置筛选属性
            {
                if (cateAAndVList.Count == 0)
                {
                    filterAttr = "0";
                }
                else
                {
                    int           count = cateAAndVList.Count;
                    StringBuilder sb    = new StringBuilder();
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append("0-");
                    }
                    filterAttr = sb.Remove(sb.Length - 1, 1).ToString();
                }
            }
            else
            {
                foreach (string attrValueId in filterAttrValueIdList)
                {
                    int temp = TypeHelper.StringToInt(attrValueId);
                    if (temp > 0)
                    {
                        attrValueIdList.Add(temp);
                    }
                }
            }


            //分页对象
            PageModel pageModel = new PageModel(20, page, Searches.GetSearchProductCount(keyword, cateId, brandId, filterPrice, catePriceRangeList, attrValueIdList, onlyStock));
            //视图对象
            SearchModel model = new SearchModel()
            {
                Word               = keyword,
                CateId             = cateId,
                BrandId            = brandId,
                FilterPrice        = filterPrice,
                FilterAttr         = filterAttr,
                OnlyStock          = onlyStock,
                SortColumn         = sortColumn,
                SortDirection      = sortDirection,
                CategoryList       = categoryList,
                CategoryInfo       = categoryInfo,
                BrandList          = brandList,
                CatePriceRangeList = catePriceRangeList,
                AAndVList          = cateAAndVList,
                PageModel          = pageModel,
                ProductList        = Searches.SearchProducts(pageModel.PageSize, pageModel.PageNumber, keyword, cateId, brandId, filterPrice, catePriceRangeList, attrValueIdList, onlyStock, sortColumn, sortDirection)
            };

            return(View(model));
        }