LogPageViewModel.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using AutoMapper;
  2. using HandyControl.Controls;
  3. using HandyControl.Data;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using Repository;
  7. using Repository.Entiies;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. namespace LogoForceTestApp.Modules.MainModule.ViewModels
  13. {
  14. public class LogPageViewModel : BindableBase
  15. {
  16. private readonly List<LogModel> _logModels;
  17. private readonly IMapper _mapper;
  18. public List<LogModel> LogModels { get; set; }
  19. public int PageIndex { get; set; } = 1;
  20. public int MaxCount { get; set; } = 1;
  21. public int PageCount { get; set; } = 20;
  22. public DateTime SelectedStartDate { get; set; }
  23. public DateTime SelectedEndDate { get; set; }
  24. public DelegateCommand<FunctionEventArgs<int>> PageUpdatedCmd => new(PageUpdated);
  25. public DelegateCommand QueryCommand { get; set; }
  26. /// <summary>
  27. /// 页码改变
  28. /// </summary>
  29. private void PageUpdated(FunctionEventArgs<int> info)
  30. {
  31. LogModels = _logModels.Skip((info.Info - 1) * PageCount).Take(PageCount).Reverse().ToList();
  32. }
  33. public LogPageViewModel(IMapper mapper)
  34. {
  35. SelectedStartDate = DateTime.Today;
  36. SelectedEndDate = DateTime.Today;
  37. LogModels = new List<LogModel>();
  38. _mapper = mapper;
  39. _logModels = new List<LogModel>();
  40. InitData().Await();
  41. QueryCommand = new DelegateCommand(async () =>
  42. {
  43. await QueryData();
  44. });
  45. }
  46. private async Task InitData()
  47. {
  48. var logRepository = new LogRepository();
  49. var dateTimeToday = DateTime.Today;
  50. var logs= await logRepository.GetAllQueryAsync<Log>(c => c.Timestamp >= dateTimeToday);
  51. var dtos=_mapper.Map<List<LogModel>>(logs);
  52. dtos.Reverse();
  53. _logModels.AddRange(dtos);
  54. LogModels = _logModels.Take(PageCount).ToList();
  55. MaxCount = _logModels.Count / PageCount + 1;
  56. }
  57. private async Task QueryData()
  58. {
  59. if (SelectedEndDate < SelectedStartDate || SelectedStartDate == default)
  60. {
  61. MessageBox.Show("请选择正确的日期","提示");
  62. return;
  63. }
  64. _logModels.Clear();
  65. var logRepository = new LogRepository();
  66. var endTime=SelectedEndDate.AddDays(1);
  67. var logs = (await logRepository.GetAllQueryAsync<Log>(c => c.Timestamp >= SelectedStartDate&&c.Timestamp<=endTime));
  68. logs.Reverse();
  69. var dtos = _mapper.Map<List<LogModel>>(logs);
  70. _logModels.AddRange(dtos);
  71. LogModels = _logModels.Take(PageCount).ToList();
  72. MaxCount = _logModels.Count / PageCount + 1;
  73. }
  74. }
  75. public class LogModel : BindableBase
  76. {
  77. public int Id { get; set; }
  78. public DateTime Timestamp { get; set; }
  79. public string Properties { get; set; }
  80. }
  81. }