Mvc里查询商品页面

        /// <summary>
        /// 商品小类筛选页面 GoodsTypeName ----------------SelectGoods--商品筛选 --图文
        /// Home/SelectGoods
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult selectgoods(int id = 0, string pvlueIDs = null)
        {
            ViewBag.ReturnUrl = Request.Url;
            #region -------------SelectGoods 商品筛选 --图文
            if (id == 0)//商品小类ID为空
            {
                return HttpNotFound();
            }
            else //商品小类ID不为空
            {
                if (id != 0 && pvlueIDs == null)//商品小类ID不等于0, 商品属性不为空
                {
                    #region -------------首次请求
                    var vbv = from f in db.GoodsPropertys
                              where f.GoodsType.GoodsTypeID == id
                              orderby f.GoodsPropertyID
                              select f;
                    int sproCout = vbv.ToList().Count;
                    GoodsProperty[] sfArray = new GoodsProperty[sproCout];
                    for (int i = 0; i < sproCout; i++)
                    {
                        sfArray[i] = new GoodsProperty();
                        sfArray[i].GoodsPropertyID = 0;//盒子
                    }
                    GoodsType slg = db.GoodsTypes.Find(id);//转换
                    SKUtypebrandgoods sbg = new SKUtypebrandgoods();
                    sbg.SmallType = slg;//商品小类
 
                    #region -------------Linq
                    GoodsType middleType = db.GoodsTypes//商品中类
                                        .Where(f => f.GoodsTypeID == id)
                                        .Select(f => f.GoodsTypes)
                                        .ToArray()[0];
                    sbg.BigType = db.GoodsTypes//商品大类
                                    .Where(f => f.GoodsTypeID == middleType.GoodsTypeID)
                                    .Select(f => f.GoodsTypes)
                                    .ToArray()[0];
                    sbg.GoodsBrandLst = db.Goodss//商品品牌
                                        .Where(f => f.GoodsTypeID == slg.GoodsTypeID)
                                        .Select(f => f.GoodsBrand)
                                        .Distinct()
                                        .ToList();
                    sbg.GoodsProertyLst = db.GoodsPropertys//商品属性
                                        .Where(f => f.GoodsType.GoodsTypeID == id)
                                        .Select(f => f)
                                        .ToList();
                    sbg.GoodsPropertyValueLst = db.GoodsPropertyValues.ToList();//所有商品属性值
                    sbg.Goodses = db.Goodss//商品
                                .Where(f => f.GoodsTypeID == id)
                                .Select(f => f)
                                .ToList();
                    #endregion
                    sbg.GproArray = sfArray;//商品属性数组
 
                    #region  -------------取模分页
                    sbg.BrandID = 0;
                    sbg.shelvesTimeArray = new string[3];
                    sbg.shelvesTimeArray[0] = "0";//Url倒数第三位(所有商品排序)0:按上架时间降序,1:按上架时间升序,2:按价格升序,3:按价格降序,4:按好评,5:按销量;
                    sbg.shelvesTimeArray[1] = "0";//Url倒数第二位(商品仅显示有货)0:显示全部,1:仅显示有货;
                    sbg.shelvesTimeArray[2] = "1";//Url倒数第一位(商品分页)【所传过来的分页】
                    sbg.Goodses = sbg.Goodses
                        .Where(s => s.GoodsSKUs.Count > 0)
                        .OrderByDescending(f => f.GoodsAddTime)
                        .Select(f => f)
                        .ToList();//排序(只显示有goodsSku的商品)
                    int page = sbg.Goodses.Count;
                    if (page % 24 == 0)//取模分页
                    {
                        page = page / 24;
                    }
                    else
                    {
                        page = (page / 24) + 1;
                    }
                    sbg.page = page;//商品有多少页
                    sbg.Goodses = sbg.Goodses.Skip(0).Take(24).ToList();
                    #endregion
 
                    return View(sbg);
 
                    #endregion
                }
                else//都不为空
                {
                    #region -------------二次请求---商品
 
                    GoodsType gds = db.GoodsTypes.Find(id);//转换
 
                    string[] strs = pvlueIDs.Split('-');//截断字符串
 
                    string[] str = strs.Take(1).ToArray();//品牌ID
 
                    string[] strPro = strs.Skip(1).Take(strs.Length - 4).ToArray();//从第二个到倒数第四个截断的字符串数组(包括倒数第四个)
 
                    SKUtypebrandgoods sku = new SKUtypebrandgoods();//中间表
 
                    sku.shelvesTimeArray = strs.Skip(strs.Length - 3).Take(3).ToArray();//Url后三位截断字符串赋值
                    sku.BrandID = Int32.Parse(str[0]);//品牌ID
                    sku.GoodsBrandLst = db.Goodss//商品品牌
                                        .Where(f => f.GoodsTypeID == gds.GoodsTypeID)
                                        .Select(f => f.GoodsBrand)
                                        .Distinct()
                                        .ToList();
                    var pro = from f in db.GoodsPropertys
                              where f.GoodsType.GoodsTypeID == id
                              select f;
                    int ig = pro.ToList().Count;
                    GoodsProperty[] pros = new GoodsProperty[ig];
                    for (int i = 0; i < ig; i++)
                    {
                        pros[i] = new GoodsProperty();
                        pros[i].GoodsPropertyID = int.Parse(strPro[i]);
                    }
                    sku.GproArray = pros;
 
                    #region -------------商品Linq语句
                    sku.SmallType = gds;
                    GoodsType middleType = db.GoodsTypes//商品中类
                                        .Where(f => f.GoodsTypeID == id)
                                        .Select(f => f.GoodsTypes)
                                        .ToArray()[0];
                    sku.BigType = db.GoodsTypes//商品大类
                                    .Where(f => f.GoodsTypeID == middleType.GoodsTypeID)
                                    .Select(f => f.GoodsTypes)
                                    .ToArray()[0];
                    sku.GoodsProertyLst = db.GoodsPropertys//商品属性
                                        .Where(f => f.GoodsType.GoodsTypeID == id)
                                        .Select(f => f)
                                        .ToList();
                    sku.GoodsPropertyValueLst = db.GoodsPropertyValues.ToList();//所有商品属性值
                    #endregion
                    var goodslst = db.Goodss//商品类别全部商品
                                .Where(f => f.GoodsTypeID == id)
                                .Select(f => f);
                    #region -------------商品属性值筛选
                    if (sku.BrandID > 0)
                    {
                        goodslst = goodslst
                                .Where(f => f.GoodsBrandID == sku.BrandID)
                                .Select(f => f);
                    }
                    foreach(var v in strPro)
                    {
                        if (v == "0")
                        {
                            continue;//继续
                        }
                        else
                        {
                            int propertyValueID = int.Parse(v);
                            var pvgs = db.PropertyValueGoods
                                        .Where(f => f.GoodsPropertyValueID == propertyValueID)
                                        .Select(f => f);
                            goodslst = from f in goodslst
                                        join s in pvgs
                                        on f.GoodsID equals s.GoodsID
                                        select f;
                        }
                    }
                    sku.Goodses = goodslst.Where(s => s.GoodsSKUs.Count > 0).Distinct().ToList();
                    #endregion
 
                    #region -------------商品排序筛选
 
                    if (sku.shelvesTimeArray[0] == "0")//Url倒数第三位(所有商品排序)0:按上架时间降序,1:按上架时间升序,2:按价格升序,3:按价格降序,4:按好评,5:按销量;
                    {
                        sku.Goodses = goodslst.OrderByDescending(f => f.GoodsAddTime).ToList();
                    }
                    else if(sku.shelvesTimeArray[0] == "1")
                    {
                        sku.Goodses = goodslst.OrderBy(f => f.GoodsAddTime).ToList();
                    }
                    else if (sku.shelvesTimeArray[0] == "2")
                    {
                        sku.Goodses = sku.Goodses
                            .OrderBy(f => f.GoodsSKUs.OrderBy
                                (fs => fs.GoodsNormalPrice)
                                .Select(fs => fs).ToArray()[0]
                                .GoodsNormalPrice)
                            .Select(f => f)
                            .ToList();
                    }
                    else if (sku.shelvesTimeArray[0] == "3")
                    {
                        sku.Goodses = sku.Goodses
                            .OrderByDescending(f => f.GoodsSKUs.OrderBy
                                (fs => fs.GoodsNormalPrice)
                                .Select(fs => fs).ToArray()[0]
                                .GoodsNormalPrice)
                            .Select(f => f)
                            .ToList();
                    }
                    else if (sku.shelvesTimeArray[0] == "4")//好评度
                    {
 
                    }
                    else if (sku.shelvesTimeArray[0] == "5")
                    {
 
                    }
                    else
                    {
                        return HttpNotFound();
                    }
                    #endregion
 
                    #region -------------是否仅显示有货的商品
                    if (sku.shelvesTimeArray[1] == "0")//Url倒数第二位(商品仅显示有货)0:显示全部,1:仅显示有货;
                    {
                    }
                    else if (sku.shelvesTimeArray[1] == "1")
                    {
                        //商品库存数量 != Null
                        sku.Goodses = sku.Goodses
                            .Where(f => f.GoodsSKUs.Where(fs => fs.GoodsQty > 0).Select(fs => fs).ToList().Count > 0)
                            .Select(f => f)
                            .ToList();
                    }
                    #endregion
 
                    #region -------------商品分页
                    int page;
                    if (sku.Goodses.Count % 24 == 0)
                    {
                        page = sku.Goodses.Count / 24;
                    }
                    else
                    {
                        page = (sku.Goodses.Count / 24) + 1;
                    }
                    sku.page = page;
                    int currentPage = 1;
                    string pageStr = sku.shelvesTimeArray[2];
                    bool parseOk = int.TryParse(pageStr,out currentPage);
                    if(currentPage - 1 <= 0)
                    {
                        sku.Goodses = sku.Goodses.Take(24).ToList();//表示第一页
                    }
                    else
                    {
                        sku.Goodses = sku.Goodses.Skip((currentPage - 1)*24).Take(24).ToList();//商品分页当前页
                    }
 
                    #endregion
 
                    return View(sku);
 
                    #endregion
                }
            }
            #endregion
        }

 

posted @ 2015-06-25 17:29  Linda_Je  Views(369)  Comments(0Edit  收藏  举报