/// <summary> /// 获取文章列表 /// </summary> /// <param name="KeyWords"></param> /// <param name="page"></param> /// <param name="TagName"></param> /// <returns></returns> public static SharedPageVM UserWritingQuery(string KeyWords, int page, string TagName = "") { var vm = new SharedPageVM(); KeyWords ??= ""; var pag = new SharedPaginationVM { PageNumber = Math.Max(page, 1), PageSize = 12 }; var dicQs = new Dictionary <string, string> { { "k", KeyWords } }; IQueryable <UserWriting> query; using var db = ContextBaseFactory.CreateDbContext(); if (!string.IsNullOrWhiteSpace(TagName)) { query = from a in db.UserWritingTags.Where(x => x.TagName == TagName) join b in db.UserWriting on a.UwId equals b.UwId select b; query = query.Distinct(); } else { query = from a in db.UserWriting select a; } query = query.Where(x => x.UwOpen == 1 && x.UwStatus == 1); if (!string.IsNullOrWhiteSpace(KeyWords)) { var kws = new JiebaSegmenter().Cut(KeyWords).ToList(); kws.Add(KeyWords); kws = kws.Distinct().ToList(); var inner = PredicateBuilder.New <UserWriting>(); switch (GlobalTo.TDB) { case SharedEnum.TypeDB.SQLite: kws.ForEach(k => inner.Or(x => EF.Functions.Like(x.UwTitle, $"%{k}%"))); break; case SharedEnum.TypeDB.PostgreSQL: kws.ForEach(k => inner.Or(x => EF.Functions.ILike(x.UwTitle, $"%{k}%"))); break; default: kws.ForEach(k => inner.Or(x => x.UwTitle.Contains(k))); break; } query = query.Where(inner); } pag.Total = query.Count(); query = query.OrderByDescending(x => x.UwId).Skip((pag.PageNumber - 1) * pag.PageSize).Take(pag.PageSize); var list = query.ToList(); //文章ID var listUwId = list.Select(x => x.UwId).ToList(); //文章的所有的标签 var queryTags = from a in db.UserWritingTags join b in db.Tags on a.TagName equals b.TagName where listUwId.Contains(a.UwId) || b.TagName == TagName orderby a.UwtId ascending select new { a.UwId, a.TagName, b.TagIcon }; var listUwTags = queryTags.ToList(); //文章人员ID var listUwUid = list.Select(x => x.UwLastUid).Concat(list.Select(x => x.Uid)).Distinct(); //文章人员ID对应的信息 var listUwUserInfo = db.UserInfo.Where(x => listUwUid.Contains(x.UserId)).Select(x => new { x.UserId, x.Nickname }).ToList(); //把信息赋值到文章表的备用字段上 foreach (var item in list) { //标签 item.Spare1 = listUwTags.Where(x => x.UwId == item.UwId).Select(x => new { x.TagName, x.TagIcon }).ToJson(); //写主昵称 item.Spare3 = listUwUserInfo.FirstOrDefault(x => x.UserId == item.Uid)?.Nickname; //有回复 if (item.UwLastUid > 0) { //回复用户昵称 item.Spare2 = listUwUserInfo.FirstOrDefault(x => x.UserId == item.UwLastUid)?.Nickname; } } vm.Rows = list; vm.Pag = pag; vm.QueryString = dicQs; if (!string.IsNullOrWhiteSpace(TagName)) { try { var jt = KeyValuesQuery(new List <string> { TagName }).FirstOrDefault()?.KeyValue.ToJObject(); if (jt != null) { var tags = new List <object> { new { TagName, listUwTags.FirstOrDefault(x => x.TagName == TagName)?.TagIcon } }; vm.Temp = new { abs = new List <string> { jt["abstract"].ToString(), jt["url"].ToString() }, tags }.ToJson(); } } catch (Exception ex) { Console.WriteLine(ex); } } return(vm); }