OperationLogPageViewModel.cs 3.3 KB

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