OperationLogPageViewModel.cs 3.3 KB

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