TrackLogRepository.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Datory;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Page;
  8. using GxPress.Common.Tools;
  9. using GxPress.Repository.Interface.TrackLog;
  10. using GxPress.Request.TrackLog;
  11. using Microsoft.Extensions.Options;
  12. namespace GxPress.Repository.Implement.TrackLog
  13. {
  14. public class TrackLogRepository : ITrackLogRepository
  15. {
  16. private readonly Repository<Entity.tede2.TrackLog.TrackLog> _repository;
  17. private readonly IMapper _mapper;
  18. private readonly string _connectionString;
  19. private readonly string _databaseTypestr;
  20. public TrackLogRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  21. {
  22. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  23. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  24. var databaseType =
  25. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  26. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  27. _repository = new Repository<Entity.tede2.TrackLog.TrackLog>(database);
  28. _mapper = mapper;
  29. }
  30. public IDatabase Database => _repository.Database;
  31. public string TableName => _repository.TableName;
  32. public List<TableColumn> TableColumns => _repository.TableColumns;
  33. public async Task<PagedList<Entity.tede2.TrackLog.TrackLog>> GetAllAsync(TrackLogSearchRequest request)
  34. {
  35. var result = new PagedList<Entity.tede2.TrackLog.TrackLog>();
  36. var query = Q.NewQuery();
  37. if (!string.IsNullOrEmpty(request.KeyWord))
  38. query.WhereLike(nameof(Entity.tede2.TrackLog.TrackLog.Remark), $"%{request.KeyWord}%");
  39. if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
  40. {
  41. if (DateTime.TryParse(request.BeginTime, out var beginTime) && DateTime.TryParse(request.EndTime, out var endTime))
  42. {
  43. query.WhereDate(nameof(Entity.tede2.TrackLog.TrackLog.CreatedDate), ">=", beginTime);
  44. query.WhereDate(nameof(Entity.tede2.TrackLog.TrackLog.CreatedDate), "<=", endTime);
  45. }
  46. }
  47. result.Total = await _repository.CountAsync(query);
  48. result.Items = await _repository.GetAllAsync(query.OrderByDesc(nameof(Entity.tede2.TrackLog.TrackLog.CreatedDate)).ForPage(request.Page, request.PerPage));
  49. return result;
  50. }
  51. public async Task<bool> InsertAsync(Entity.tede2.TrackLog.TrackLog model)
  52. {
  53. return await _repository.InsertAsync(model) > 0;
  54. }
  55. public async Task<bool> InsertAsync(string creator, int adminId, string ip, string remark)
  56. {
  57. var model = new Entity.tede2.TrackLog.TrackLog()
  58. {
  59. Creator = creator,
  60. AdminId = adminId,
  61. Ip = ip,
  62. Remark = remark
  63. };
  64. return await _repository.InsertAsync(model) > 0;
  65. }
  66. }
  67. }