FileJobLogRepository.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using AipDatabase.API.Infrastructures;
  2. using AipDatabase.API.Interfaces;
  3. using AipDatabase.API.Models;
  4. using Dapper;
  5. using Microsoft.Data.SqlClient;
  6. using Microsoft.IdentityModel.Tokens;
  7. using System.Data;
  8. namespace AipDatabase.API.Repositories
  9. {
  10. public class FileJobLogRepository : IFileJobLogRepository
  11. {
  12. private readonly ILogger<FileJobLogRepository> _log;
  13. private readonly IDatabaseFactory _databaseFactory;
  14. private readonly SqlHelper<FileJobLogRepository> _sqlHelper;
  15. public FileJobLogRepository(ILogger<FileJobLogRepository> log, IDatabaseFactory databaseFactory)
  16. {
  17. _log = log;
  18. _databaseFactory = databaseFactory;
  19. _sqlHelper = new SqlHelper<FileJobLogRepository>();
  20. }
  21. public async Task<Pagination<GatewayLog>> FindGatewayLogs(int pagePerCount, int pageSize, int page, string startDate, string endDate, string? searchType, string? searchText)
  22. {
  23. try
  24. {
  25. string whereSql = "";
  26. if (!searchType.IsNullOrEmpty() && !searchText.IsNullOrEmpty())
  27. {
  28. whereSql = $" AND {searchType} Like '%{searchText}%'";
  29. if ("FileName".Equals(searchType) || "FileOwner".Equals(searchType))
  30. {
  31. whereSql += $" OR New{searchType} LIKE '%{searchText}%'";
  32. }
  33. }
  34. using var connection = _databaseFactory.GetDbConnection();
  35. var parameters1 = new
  36. {
  37. startDate,
  38. endDate,
  39. };
  40. string sql = string.Empty;
  41. string tmpSql = _sqlHelper.GetSqlFromEmbeddedResource("FileJobLog.FindGatewayLogsTatal");
  42. sql = tmpSql + whereSql;
  43. _log.LogInformation(sql);
  44. Int64 totalCount = await connection.QueryFirstAsync<Int64>(
  45. sql,
  46. parameters1,
  47. commandType: CommandType.Text
  48. );
  49. _log.LogInformation($"FileJobLog.FindGatewayLogsTatal: {totalCount} EA.");
  50. Pagination<GatewayLog> result = new Pagination<GatewayLog>(page, pageSize, pagePerCount, (int)totalCount);
  51. var parameters2 = new
  52. {
  53. startDate,
  54. endDate,
  55. startRow = result.startRow,
  56. endRow = result.endRow
  57. };
  58. tmpSql = _sqlHelper.GetSqlFromEmbeddedResource("FileJobLog.FindGatewayLogs");
  59. sql = "SELECT A.* FROM (" + tmpSql + whereSql + " ) A WHERE RowNum BETWEEN @startRow AND @endRow";
  60. _log.LogInformation(sql);
  61. IEnumerable<GatewayLog> logResult = await connection.QueryAsync<GatewayLog>(
  62. sql,
  63. parameters2,
  64. commandType: CommandType.Text
  65. );
  66. result.lists = logResult.ToList();
  67. return result;
  68. }
  69. catch (Exception)
  70. {
  71. throw;
  72. }
  73. }
  74. }
  75. }