diff --git a/Yi.Framework/Yi.Framework.Interface/IGoodsService.cs b/Yi.Framework/Yi.Framework.Interface/IGoodsService.cs new file mode 100644 index 00000000..b3bcbe76 --- /dev/null +++ b/Yi.Framework/Yi.Framework.Interface/IGoodsService.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Model.Models; +using Yi.Framework.Model.Search; + +namespace Yi.Framework.Interface +{ + public interface IGoodsService + { + PageResult QuerySpuByPage(int page, int rows, string key, bool? saleable); + } +} diff --git a/Yi.Framework/Yi.Framework.Interface/ISearchService.cs b/Yi.Framework/Yi.Framework.Interface/ISearchService.cs new file mode 100644 index 00000000..4d503167 --- /dev/null +++ b/Yi.Framework/Yi.Framework.Interface/ISearchService.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Interface +{ + public interface ISearchService + { + void ImpDataBySpu(); + } +} diff --git a/Yi.Framework/Yi.Framework.Model/Search/Goods.cs b/Yi.Framework/Yi.Framework.Model/Search/Goods.cs new file mode 100644 index 00000000..35a905c7 --- /dev/null +++ b/Yi.Framework/Yi.Framework.Model/Search/Goods.cs @@ -0,0 +1,31 @@ +using Nest; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Model.Models; + +namespace Yi.Framework.Model.Search +{ + [ElasticsearchType(IdProperty = "id")]//主键声明,且主键必须是属性 + public class Goods + { + public long id { get; set; } + + + public spu spu { get; set; } + public string all; //所有需要被搜索的信息,包括品牌,分类,标题 + public string subtitle; //子标题 + public brand brand; + public category cid1; + public category cid2; + public category cid3; + public DateTime? createTime; + + + public HashSet price = new HashSet(); //是所有sku的价格集合。方便根据价格进行筛选过滤 + public List skus; //sku信息的json结构数据 + public Dictionary specs = new Dictionary(); //可搜索的规格参数,key是参数名,值是参数值 + } +} diff --git a/Yi.Framework/Yi.Framework.Model/Search/PageResult.cs b/Yi.Framework/Yi.Framework.Model/Search/PageResult.cs new file mode 100644 index 00000000..52fde5cb --- /dev/null +++ b/Yi.Framework/Yi.Framework.Model/Search/PageResult.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Yi.Framework.Model.Search +{ + public class PageResult + { + public static readonly long serialVersionUID = 4612105649493688532L; + public long total; // 总记录数 + public int totalPages; //总页数 + public List rows; // 每页显示的数据集合 + + public PageResult(long total, List rows) + { + this.total = total; + this.rows = rows; + } + } +} diff --git a/Yi.Framework/Yi.Framework.Model/Search/SearchRequest.cs b/Yi.Framework/Yi.Framework.Model/Search/SearchRequest.cs new file mode 100644 index 00000000..a3853d37 --- /dev/null +++ b/Yi.Framework/Yi.Framework.Model/Search/SearchRequest.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Model.Search +{ + public class SearchRequest + { + public static readonly int DEFAULT_PAGE = 1; + public static readonly int DEFAULT_SIZE = 20; + public string key { get; set; } + public int page { get; set; } + //排序字段 + public string sortBy { get; set; } + //是否降序 + public bool descending { get; set; } + //过滤字段 + public Dictionary filter = new Dictionary(); + + public int getPage() + { + if (page == 0) + { + return DEFAULT_PAGE; + } + // 获取页码时做一些校验,不能小于1 + return Math.Max(DEFAULT_PAGE, page); + } + + public int getSize() + { + return DEFAULT_SIZE; + } + + + } +} diff --git a/Yi.Framework/Yi.Framework.Model/Search/SearchResult.cs b/Yi.Framework/Yi.Framework.Model/Search/SearchResult.cs new file mode 100644 index 00000000..18043165 --- /dev/null +++ b/Yi.Framework/Yi.Framework.Model/Search/SearchResult.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Model.Models; + +namespace Yi.Framework.Model.Search +{ + public class SearchResult : PageResult + { + public List brands = new List(); + public List categories = new List(); + //规格参数过滤条件 + public List> specs = new List>(); + public SearchResult(long total, + int totalPage, + List items, + List categories, + List brands, + List> specs) : base + (total, items) + { + + this.categories = categories; + this.brands = brands; + this.specs = specs; + } + + } +} diff --git a/Yi.Framework/Yi.Framework.Service/GoodsService.cs b/Yi.Framework/Yi.Framework.Service/GoodsService.cs new file mode 100644 index 00000000..381972d6 --- /dev/null +++ b/Yi.Framework/Yi.Framework.Service/GoodsService.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Interface; +using Yi.Framework.Model.Models; +using Yi.Framework.Model.Search; + +namespace Yi.Framework.Service +{ + public class GoodsService : IGoodsService + { + public PageResult QuerySpuByPage(int page, int rows, string key, bool? saleable) + { + throw new NotImplementedException(); + } + } +} diff --git a/Yi.Framework/Yi.Framework.Service/SearchService.cs b/Yi.Framework/Yi.Framework.Service/SearchService.cs new file mode 100644 index 00000000..d1a3a389 --- /dev/null +++ b/Yi.Framework/Yi.Framework.Service/SearchService.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Interface; +using Yi.Framework.Model.Models; +using Yi.Framework.Model.Search; + +namespace Yi.Framework.Service +{ + public class SearchService : ISearchService + { + private IGoodsService _goodsService; + public SearchService(IGoodsService goodsService) + { + _goodsService = goodsService; + } + public void ImpDataBySpu() + { + ImportToEs(); + } + private void ImportToEs() + { + int page = 1; + int size; + int rows = 100; + do + { + List goodsList = new List(); + // 上架商品 + PageResult result = _goodsService.QuerySpuByPage(page, rows, null, true); + List spus = result.rows; + size = spus.Count; + foreach (var spu in spus) + { + try + { + Goods g = BuildGoods(spu); + // 处理好的数据添加到集合中 + goodsList.Add(g); + + } + catch (Exception e) + { + Console.WriteLine(e.Message); + continue;//部分数据不严格 + } + } + // 存入es,先留着,不写 + //_elasticSearchService.Send(goodsList); + page++; + } while (size == 100); + } + + private Goods BuildGoods(spu spu) + { + Goods goods = new Goods(); + return goods; + } + } +}