Ejemplo n.º 1
0
        public async Task <IActionResult> PostNewsDocAsync([FromHeader] String source,
                                                           [FromRoute] SiteRoute route,
                                                           string newsId = "10000000")
        {
            var response = new Response <Object>();

            try {
                var categories = await _IWebCategoryRepository.Query(a => a.SiteId == route.mark && a.IsEnable == 1,
                                                                     a => a.Sequence).ToListAsync();

                var iidn = await _IWebNewsElastic.AddIndexAsync(_IWebNewsElastic.IndexName);

                for (int pageIndex = 1; pageIndex <= 20; pageIndex++)
                {
                    var news = new List <WebNews>();
                    news = _IMySqlRepository.GetList(1, pageIndex, 1000, Convert.ToInt32(newsId));
                    var lastNews = news.LastOrDefault();
                    if (lastNews != null)
                    {
                        newsId = news.LastOrDefault().NewsId;
                        var docs      = new List <WebNewsDoc>();
                        var newsDatas = new List <WebNews>();
                        news.ForEach(x => {
                            var category = categories.SingleOrDefault(a => a.CategoryName == x.CategoryName);
                            if (category != null)
                            {
                                newsDatas.Add(SetWebNews(x, category));
                                docs.Add(GetWebNewsDoc(x));
                            }
                        });
                        if (newsDatas.Count > 0)
                        {
                            await _IWebNewsRepository.BatchAddAsync(newsDatas);//写入数据库
                        }
                        //if (docs.Count > 0)
                        //    response.Other += (await _IWebNewsElastic.BatchAddDocumentAsync(_IWebNewsElastic.IndexName, docs)).ToString() + "a|" + docs.Count;//写入es
                    }
                }
                response.Code  = true;
                response.Other = null;
            } catch (Exception ex) {
                LogHelper.LogError("错误信息:{0}", ex.Message + ex.StackTrace);
            }
            response.Data = newsId;
            return(response.ToHttpResponse());
        }