ArticleLogic.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 
  2. using System.Collections.Generic;
  3. using CZFW.Framework.Logic;
  4. using System.Linq;
  5. using CZFW.Framework.Model.Entity;
  6. using CZKJ.GBRS2.Entity;
  7. using CZKJ.GBRS2.Interface;
  8. namespace CZKJ.GBRS2.Logic
  9. {
  10. public class ArticleLogic : LogicBase<ArticleEntity>, IArticle
  11. {
  12. public IList<ArticleEntity> GetList(int pageIndex, int pageSize, out int rowsCount, int? parentId, int? typeId, string keywords)
  13. {
  14. var queryable = Queryable;
  15. if (parentId.HasValue)
  16. {
  17. var idList = DbContext.Set<TypeEntity>().Where(x => x.ParentId == parentId).Select(x => x.Id).ToList();
  18. queryable = queryable.Where(x => idList.Contains(x.TypeId));
  19. }
  20. if (typeId.HasValue)
  21. {
  22. queryable = queryable.Where(x => x.TypeId == typeId);
  23. }
  24. if (!string.IsNullOrWhiteSpace(keywords))
  25. {
  26. queryable = queryable.Where(x => x.Title.Contains(keywords) || x.Summary.Contains(keywords) || x.Detail.Contains(keywords));
  27. }
  28. rowsCount = queryable.Count();
  29. var list = queryable.Join(DbContext.Set<TypeEntity>(), x => x.TypeId, y => y.Id, (x, y) => new { x, y.Name }).OrderByDescending(x => x.x.SortOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  30. return list.Select(x => new ArticleEntity
  31. {
  32. Id = x.x.Id,
  33. Image = x.x.Image,
  34. Title = x.x.Title,
  35. TypeName = x.Name,
  36. Summary = x.x.Summary,
  37. SortOrder = x.x.SortOrder,
  38. Source = x.x.Source,
  39. PublishTime = x.x.PublishTime
  40. }).ToList();
  41. }
  42. public IList<ArticleEntity> GetSelect(IList<int> idList, int? typeId, string keywords, int pageIndex, int pageSize, out int rowsCount)
  43. {
  44. var queryable = Queryable;
  45. if (idList != null && idList.Count > 0)
  46. {
  47. queryable = queryable.Where(x => !idList.Contains(x.Id));
  48. }
  49. if (typeId.HasValue)
  50. {
  51. var typeIdList = DbContext.Set<TypeEntity>().Where(x => x.ParentId == typeId).Select(x => x.Id).ToList();
  52. queryable = queryable.Where(x => typeIdList.Contains(x.TypeId));
  53. }
  54. if (!string.IsNullOrWhiteSpace(keywords))
  55. {
  56. queryable = queryable.Where(x => x.Title.Contains(keywords) || x.Summary.Contains(keywords) || x.Detail.Contains(keywords));
  57. }
  58. rowsCount = queryable.Count();
  59. var list = queryable.Join(DbContext.Set<TypeEntity>(), x => x.TypeId, y => y.Id, (x, y) => new { x, y.Name }).OrderByDescending(x => x.x.SortOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  60. return list.Select(x => new ArticleEntity
  61. {
  62. Id = x.x.Id,
  63. Image = x.x.Image,
  64. Title = x.x.Title,
  65. TypeName = x.Name,
  66. Summary = x.x.Summary,
  67. PublishTime = x.x.PublishTime
  68. }).ToList();
  69. }
  70. /// <summary>
  71. /// 根据类型集合获取文章列表(可选取几条)
  72. /// </summary>
  73. /// <param name="count"></param>
  74. /// <param name="typeIdList"></param>
  75. /// <returns></returns>
  76. public IList<ArticleEntity> GetList(int? count, params int[] typeIdList)
  77. {
  78. var articleList = new List<ArticleEntity>();
  79. if (count.HasValue)
  80. {
  81. foreach (var list in typeIdList.Select(item => DbContext.Set<ArticleEntity>().Where(x => x.TypeId == item).OrderByDescending(x => x.SortOrder).Take(count.Value).ToList()))
  82. {
  83. articleList.AddRange(list);
  84. }
  85. }
  86. else
  87. {
  88. articleList = DbContext.Set<ArticleEntity>().Where(x => typeIdList.Contains(x.TypeId)).ToList();
  89. }
  90. return articleList;
  91. }
  92. public IList<ArticleEntity> GetListCount(int? parentId, int? typeId, int count)
  93. {
  94. var queryable = Queryable;
  95. if (parentId.HasValue)
  96. {
  97. var idList = DbContext.Set<TypeEntity>().Where(x => x.ParentId == parentId).Select(x => x.Id).ToList();
  98. queryable = queryable.Where(x => idList.Contains(x.TypeId));
  99. }
  100. if (typeId.HasValue)
  101. {
  102. queryable = queryable.Where(x => x.TypeId == typeId);
  103. }
  104. var result = queryable.Join(DbContext.Set<TypeEntity>(), x => x.TypeId, y => y.Id, (x, y) => new { x, y.Name }).OrderByDescending(x => x.x.SortOrder).Take(count).ToList();
  105. return result.Select(x => new ArticleEntity
  106. {
  107. Id = x.x.Id,
  108. Image = x.x.Image,
  109. Title = x.x.Title,
  110. TypeName = x.Name,
  111. Summary = x.x.Summary,
  112. PublishTime = x.x.PublishTime
  113. }).ToList();
  114. }
  115. public IList<ArticleEntity> GetArticleList(int typeId, int pageIndex, int pageSize, out int rowsCount)
  116. {
  117. var queryable = Queryable.Where(x => x.TypeId == typeId);
  118. rowsCount = queryable.Count();
  119. return queryable.OrderByDescending(x => x.SortOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  120. }
  121. public IList<ArticleEntity> GetSearchResult(string keywords, int pageIndex, int pageSize, out int rowsCount)
  122. {
  123. var queryable = Queryable.Where(x => x.Title.Contains(keywords) || x.Summary.Contains(keywords) || x.Detail.Contains(keywords));
  124. rowsCount = queryable.Count();
  125. var result = queryable.Join(DbContext.Set<TypeEntity>(), x => x.TypeId, y => y.Id, (x, y) => new { x, y.Name }).Select(x => new { x.x.Title, x.x.PublishTime,x.x.SortOrder, TypeName = x.Name, x.x.Id }).OrderByDescending(x => x.SortOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize);
  126. return result.ToList().Select(x => new ArticleEntity
  127. {
  128. Id = x.Id,
  129. Title = x.Title,
  130. PublishTime = x.PublishTime,
  131. TypeName = x.TypeName
  132. }).ToList();
  133. }
  134. }
  135. }