AppHomePageService.Search.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Dapper;
  5. using Datory;
  6. using GxPress.Request.Navigation;
  7. using GxPress.Result.Navigation;
  8. namespace GxPress.Service.Implement.AppHomePage
  9. {
  10. public partial class AppHomePageService
  11. {
  12. /// <summary>
  13. /// 获取搜索内容
  14. /// </summary>
  15. /// <param name="request"></param>
  16. /// <returns></returns>
  17. public async Task<NavigationSearchResult> GetNavigationSearchResultAsync(NavigationSearchRequest request)
  18. {
  19. var result = new NavigationSearchResult()
  20. {
  21. PressItems = await GetNavigationPressAsync(request),
  22. TeacherItems = await GetNavigationSearchResultAysnc(request),
  23. YearItems = await GetNavigationYearResult(request)
  24. };
  25. return result;
  26. }
  27. /// <summary>
  28. /// 获取搜索作者
  29. /// </summary>
  30. /// <returns></returns>
  31. public async Task<IEnumerable<NavigationSearchTeacherResult>> GetNavigationSearchResultAysnc(NavigationSearchRequest request)
  32. {
  33. var database = new Database(DatabaseType.MySql, Common.Tools.ConfigHelper.GetValue("Database:ConnectionString"));
  34. var connection = database.GetConnection();
  35. var categorySql = "";
  36. if (request.CategoryId > 0)
  37. categorySql += $" and lableId={request.CategoryId}";
  38. var teacherSql = "";
  39. if (request.TeacherId > 0)
  40. teacherSql += $" and TeacherId={request.TeacherId}";
  41. if (!string.IsNullOrEmpty(request.Press))
  42. teacherSql += $" and Press='{request.Press}'";
  43. if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
  44. {
  45. if (DateTime.TryParse(request.BeginTime, out var beginTime) && DateTime.TryParse(request.EndTime, out var endTime))
  46. teacherSql += $" and AddDate>='{request.BeginTime}' and AddDate<='{request.EndTime}'";
  47. }
  48. var sql = $"SELECT TeacherId,Author,count(1) as Count FROM tede_media where id in(select MediaId from tede_system_lable_media where TypeValue=1 {categorySql}) {teacherSql} and Author is not null group by Author,TeacherId";
  49. var reuslt = await connection.QueryAsync<NavigationSearchTeacherResult>(sql);
  50. if (request.TeacherId > 0)
  51. foreach (var item in reuslt)
  52. {
  53. item.IsChecked = item.TeacherId.Equals(request.TeacherId);
  54. }
  55. return reuslt;
  56. }
  57. /// <summary>
  58. /// 获取出版社
  59. /// </summary>
  60. /// <param name="categoryId"></param>
  61. /// <returns></returns>
  62. public async Task<IEnumerable<PressResult>> GetNavigationPressAsync(NavigationSearchRequest request)
  63. {
  64. var result = new List<PressResult>();
  65. var categorySql = "";
  66. if (request.CategoryId > 0)
  67. categorySql += $" and lableId={request.CategoryId}";
  68. var teacherSql = "";
  69. if (request.TeacherId > 0)
  70. teacherSql += $" and TeacherId={request.TeacherId}";
  71. if (!string.IsNullOrEmpty(request.Press))
  72. teacherSql += $" and Press='{request.Press}'";
  73. if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
  74. {
  75. if (DateTime.TryParse(request.BeginTime, out var beginTime) && DateTime.TryParse(request.EndTime, out var endTime))
  76. teacherSql += $" and AddDate>='{request.BeginTime}' and AddDate<='{request.EndTime}'";
  77. }
  78. var sql = $"SELECT Press FROM tede_media where id in(select MediaId from tede_system_lable_media where TypeValue=1 {categorySql}) {teacherSql} and Press is not null group by Press";
  79. // if (request.CategoryId > 0)
  80. // sql = "SELECT Press FROM tede_media where id in(select MediaId from tede_system_lable_media where TypeValue=1 and lableId={categoryId}) and Press is not null group by Press";
  81. var database = new Database(DatabaseType.MySql, Common.Tools.ConfigHelper.GetValue("Database:ConnectionString"));
  82. var connection = database.GetConnection();
  83. var pressList = await connection.QueryAsync<string>(sql);
  84. if (request.Press != null)
  85. foreach (var item in pressList)
  86. {
  87. result.Add(new PressResult { PressName = item, IsChecked = request.Press.Equals(item) });
  88. }
  89. return result;
  90. }
  91. /// <summary>
  92. /// 获取年份数据
  93. /// </summary>
  94. /// <param name="request"></param>
  95. /// <returns></returns>
  96. public async Task<IEnumerable<NavigationYearResult>> GetNavigationYearResult(NavigationSearchRequest request)
  97. {
  98. var result = new List<NavigationYearResult>();
  99. var database = new Database(DatabaseType.MySql, Common.Tools.ConfigHelper.GetValue("Database:ConnectionString"));
  100. var connection = database.GetConnection();
  101. var categorySql = "";
  102. if (request.CategoryId > 0)
  103. categorySql += $" and lableId={request.CategoryId}";
  104. var teacherSql = "";
  105. if (request.TeacherId > 0)
  106. teacherSql += $" and TeacherId={request.TeacherId}";
  107. if (!string.IsNullOrEmpty(request.Press))
  108. teacherSql += $" and Press='{request.Press}'";
  109. if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
  110. {
  111. if (DateTime.TryParse(request.BeginTime, out var beginTime) && DateTime.TryParse(request.EndTime, out var endTime))
  112. teacherSql += $" and AddDate>='{request.BeginTime}' and AddDate<='{request.EndTime}'";
  113. }
  114. var maxYearSql = $"select max(AddDate) from tede_media where id in( SELECT MediaId FROM tede_system_lable_media where typevalue=1 {categorySql}) {teacherSql} ";
  115. var maxYear = await connection.ExecuteScalarAsync<string>(maxYearSql);
  116. var minYearSql = $"select min(AddDate) from tede_media where id in( SELECT MediaId FROM tede_system_lable_media where typevalue=1 {categorySql}) {teacherSql}";
  117. var minYear = await connection.ExecuteScalarAsync<string>(minYearSql);
  118. //
  119. if (DateTime.TryParse(maxYear, out var maxYearDate) && DateTime.TryParse(minYear, out var minYearDate))
  120. {
  121. var count = (maxYearDate.Year - minYearDate.Year);
  122. if (count > 0 && minYearDate.Year > 1)
  123. {
  124. for (int i = 0; i < count; i++)
  125. {
  126. var sql = $"select count(1) from tede_media where AddDate>'{minYearDate.Year + i}' and AddDate<'{minYearDate.Year + (i + 1)}' and id in( SELECT MediaId FROM tede_system_lable_media where typevalue=1 {categorySql}) {teacherSql}";
  127. var number = await connection.ExecuteScalarAsync<int>(sql);
  128. result.Add(new NavigationYearResult() { Year = (minYearDate.Year + i) + "年", Count = number });
  129. }
  130. }
  131. else
  132. {
  133. var sql = $"select count(1) from tede_media where AddDate>'{maxYearDate.Year}' and AddDate<'{maxYearDate.Year + 1}' and id in( SELECT MediaId FROM tede_system_lable_media where typevalue=1 {categorySql}) {teacherSql}";
  134. var number = await connection.ExecuteScalarAsync<int>(sql);
  135. result.Add(new NavigationYearResult() { Year = (maxYearDate.Year) + "年", Count = number });
  136. }
  137. }
  138. return result;
  139. }
  140. }
  141. }