public TbSpu QuerySpuBySpuId(long spuId) { //根据spuId查询spu TbSpu spu = _db.TbSpu.Where(m => m.Id == spuId).FirstOrDefault(); //查询spuDetail TbSpuDetail detail = QuerySpuDetailBySpuId(spuId); //查询skus List <TbSku> skus = QuerySkuBySpuId(spuId); spu.SpuDetail = detail; spu.Skus = skus; return(spu); }
public List <long> InsertSpu(int index) { List <long> spuIdList = new List <long>(); for (int i = 0; i < index; i++) { var tbSpu = new TbSpu(); #region SPU tbSpu.Title = "华为 G9 青春版 " + i.ToString("00") + "_" + DateTime.Now.ToString("yyyyMMdd HHmmss fff"); tbSpu.SubTitle = "骁龙芯片!3GB运行内存!索尼1300万摄像头!" + i.ToString("00") + "_" + DateTime.Now.ToString("yyyyMMdd HHmmss fff") + "<a href='https://sale.jd.com/act/DhKrOjXnFcGL.html' target='_blank'>华为新品全面上线,更多优惠猛戳》》</a>"; tbSpu.Cid1 = 74; tbSpu.Cid2 = 75; tbSpu.Cid3 = 76; tbSpu.BrandId = 8557; tbSpu.Saleable = true; tbSpu.Valid = true; tbSpu.CreateTime = DateTime.Now; tbSpu.LastUpdateTime = DateTime.Now; _orangeContext.TbSpu.Add(tbSpu); _orangeContext.SaveChanges(); #endregion #region SpuDetail var tbSpuDetail = new TbSpuDetail(); tbSpuDetail.SpuId = tbSpu.Id; //todo tbSpuDetail.Description = "<divclass='content_tpl'><divclass='formwork'><divclass='formwork_img'><imgsrc='//img20.360buyimg.com/vc/jfs/t5893/141/6838703316/1369626/15c9d88f/596c753aN075ee827.jpg'/></div></div></div><br/>"; tbSpuDetail.GenericSpec = "{'1':'其它','2':'G9青春版(全网通版)','3':2016.0,'5':143,'6':'其它','7':'Android','8':'骁龙(Snapdragon)','9':'骁龙617(msm8952)','10':'八核','11':1.5,'14':5.2,'15':'1920*1080(FHD)','16':800.0,'17':1300.0,'18':3000.0}"; tbSpuDetail.SpecialSpec = "{'4':['白色','金色','玫瑰金'],'12':['3GB'],'13':['16GB']}"; tbSpuDetail.PackingList = "手机(电池内置)*1,中式充电器*1,数据线*1,半入耳式线控耳机*1,华为手机凭证*1,快速指南*1,取卡针*1,屏幕保护膜(出厂已贴)*1" + i.ToString("00") + "_" + DateTime.Now.ToString("yyyyMMdd HHmmss fff"); tbSpuDetail.AfterService = "本产品全国联保,享受三包服务,质保期为:一年质保" + i.ToString("00") + "_" + DateTime.Now.ToString("yyyyMMdd HHmmss fff"); _orangeContext.TbSpuDetail.Add(tbSpuDetail); _orangeContext.SaveChanges(); #endregion { #region Sku var tbSku = new TbSku(); tbSku.SpuId = tbSpu.Id;// todo tbSku.Title = "华为 G9 青春版 白色 移动联通电信4G手机 双卡双待" + i.ToString("00") + "_" + DateTime.Now.ToString("yyyyMMdd HHmmss fff"); tbSku.Images = "http://yt.image.com/images/9/15/1524297313793.jpg"; tbSku.Price = 8400; tbSku.Indexes = "0_0_0"; tbSku.OwnSpec = "{'4':'白色','12':'3GB','13':'16GB'}"; tbSku.Enable = true; tbSku.CreateTime = DateTime.Now; tbSku.LastUpdateTime = DateTime.Now; _orangeContext.TbSku.Add(tbSku); _orangeContext.SaveChanges(); #endregion #region Stock var tbStock = new TbStock(); tbStock.SkuId = tbSku.Id; tbStock.Stock = 10000 + i + DateTime.Now.Millisecond; _orangeContext.TbStock.Add(tbStock); _orangeContext.SaveChanges(); #endregion } { #region Sku var tbSku = new TbSku(); tbSku.SpuId = tbSpu.Id;// todo tbSku.Title = "华为 G9 青春版 白色 移动联通电信4G手机 双卡双待" + i.ToString("00") + "_____" + DateTime.Now.ToString("yyyyMMdd HHmmss fff"); tbSku.Images = "http://yt.image.com/images/9/15/1524297313793.jpg"; tbSku.Price = 8400; tbSku.Indexes = "0_0_0"; tbSku.OwnSpec = "{'4':'白色','12':'3GB','13':'16GB'}"; tbSku.Enable = true; tbSku.CreateTime = DateTime.Now; tbSku.LastUpdateTime = DateTime.Now; _orangeContext.TbSku.Add(tbSku); _orangeContext.SaveChanges(); #endregion #region Stock var tbStock = new TbStock(); tbStock.SkuId = tbSku.Id; tbStock.Stock = 10000 + i + DateTime.Now.Millisecond; _orangeContext.TbStock.Add(tbStock); _orangeContext.SaveChanges(); #endregion } spuIdList.Add(tbSpu.Id); } return(spuIdList); }
/// <summary> /// 根据Spu构建Goods对象 /// </summary> /// <param name="spu"></param> private Goods BuildGoods(TbSpu spu) { Goods goods = new Goods(); // 1、查询商品分类名称组成的集合 //List<TbCategory> lists = _categoryService.QueryCategoryByIds(new List<long>() { spu.Cid1, spu.Cid2, spu.Cid3 }); var ids = new List <long>() { spu.Cid1, spu.Cid2, spu.Cid3 }; var lists = _orangeContext.TbCategory.Where(m => ids.Contains(m.Id)).ToList(); var cnames = lists.Select(c => c.Name).ToList(); // 2、根据品牌ID查询品牌信息 //var brand = _brandService.QueryBrandByBid(spu.BrandId); var brand = _orangeContext.TbBrand.Where(m => m.Id == spu.BrandId).FirstOrDefault(); // 3、所有的搜索字段拼接到all中,all存入索引库,并进行分词处理,搜索时与all中的字段进行匹配查询 Regex regex = new Regex(@"<[^>]+>|</[^>]+>"); string subTitle = regex.Replace(spu.SubTitle, ""); string all = subTitle + " " + string.Join(" ", cnames) + " " + brand.Name; // 4、根据spu查询所有的sku集合 List <TbSku> skuList = _goodsService.QuerySkuBySpuId(spu.Id); if (skuList == null && skuList.Count > 0) { throw new Exception("查询商品对应sku不存在"); } //4.1 存储price的集合 HashSet <double> priceSet = new HashSet <double>(); //4.2 设置存储skus的json结构的集合,用map结果转化sku对象,转化为json之后与对象结构相似(或者重新定义一个对象,存储前台要展示的数据,并把sku对象转化成自己定义的对象) List <Dictionary <string, object> > skus = new List <Dictionary <string, object> >(); foreach (TbSku sku in skuList) { priceSet.Add(sku.Price); Dictionary <string, object> dic = new Dictionary <string, object>(); dic.Add("id", sku.Id); dic.Add("title", sku.Title); //sku中有多个图片,只展示第一张 dic.Add("image", sku.Images.Split(",")[0]); dic.Add("price", sku.Price); // 添加到字典中 skus.Add(dic); } //查询规格参数,规格参数中分为通用规格参数和特有规格参数 List <TbSpecParam> specParams = _specService.QuerySpecParams(null, spu.Cid3, true, null); if (specParams == null && specParams.Count > 0) { throw new Exception("规格参数不存在"); } //查询商品详情 TbSpuDetail spuDetail = _goodsService.QuerySpuDetailBySpuId(spu.Id); //获取通用规格参数 Dictionary <long, string> genericSpec = JsonHelper.JsonToT <Dictionary <long, string> >(spuDetail.GenericSpec); //获取特有规格参数 Dictionary <long, List <string> > specialSpec = JsonHelper.JsonToT <Dictionary <long, List <string> > >(spuDetail.SpecialSpec); //定义spec对应的map Dictionary <string, object> specDic = new Dictionary <string, object>(); //对规格进行遍历,并封装spec,其中spec的key是规格参数的名称,值是商品详情中的值 foreach (TbSpecParam param in specParams) { //key是规格参数的名称 string key = param.Name; object value = ""; if (param.Generic == true) { //参数是通用属性,通过规格参数的ID从商品详情存储的规格参数中查出值 value = genericSpec[param.Id]; if (param.Numeric == true) { //参数是数值类型,处理成段,方便后期对数值类型进行范围过滤 value = ChooseSegment(value.ToString(), param); } } else { //参数不是通用类型 value = specialSpec[param.Id]; } value ??= "其他"; //存入map specDic.Add(key, value); } // 封装商品对象 goods.id = spu.Id; goods.brandId = spu.BrandId; goods.cid1 = spu.Cid1; goods.cid2 = spu.Cid2; goods.cid3 = spu.Cid3; goods.createTime = spu.CreateTime; goods.all = all; goods.price = priceSet; goods.subtitle = spu.SubTitle; goods.specs = specDic; goods.skus = skus.ToJson(); return(goods); }