DataPageViewModel.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using AutoMapper;
  2. using HandyControl.Controls;
  3. using LogForceTestApp.Modules.MainModule.Models;
  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.Threading.Tasks;
  13. namespace LogoForceTestApp.Modules.MainModule.ViewModels
  14. {
  15. public class DataPageViewModel:BindableBase
  16. {
  17. private readonly IMapper _mapper;
  18. private readonly IRepository _repository;
  19. //private readonly List<TestResultDto> _logModels;
  20. private readonly List<Information> _logModels;
  21. public DateTime SelectedStartDate { get; set; }
  22. public DateTime SelectedEndDate { get; set; }
  23. //public List<TestResultDto> LogModels { get; set; }
  24. public List<Information> LogModels { get; set; }
  25. public int PageIndex { get; set; } = 1;
  26. public int MaxCount { get; set; } = 1;
  27. public int PageCount { get; set; } = 20;
  28. public int AllPro { get; set; }
  29. public DelegateCommand QueryCommand { get; set; }
  30. public DataPageViewModel(IMapper mapper, IRepository repository)
  31. {
  32. _mapper = mapper;
  33. _repository = repository;
  34. SelectedStartDate = DateTime.Today;
  35. SelectedEndDate = DateTime.Today;
  36. //LogModels = new List<TestResultDto>();
  37. LogModels = new List<Information>();
  38. _mapper = mapper;
  39. _repository = repository;
  40. //_logModels = new List<TestResultDto>();
  41. _logModels = new List<Information>();
  42. InitData().Await();
  43. QueryCommand = new DelegateCommand(async () =>
  44. {
  45. await QueryData();
  46. });
  47. }
  48. private async Task InitData()
  49. {
  50. var dateTimeToday = DateTime.Today;
  51. //var logs = await _repository.GetAllQueryAsync<TestResult>(c => c.Datetime >= dateTimeToday);
  52. var logs = await _repository.GetAllQueryAsync<Information>(c => c.CreateTime >= dateTimeToday);
  53. //var dtos = _mapper.Map<List<TestResultDto>>(logs);
  54. var dtos = _mapper.Map<List<Information>>(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<TestResult>(c => c.Datetime >= SelectedStartDate && c.Datetime <= endTime));
  70. var logs = (await _repository.GetAllQueryAsync<Information>(c => c.CreateTime >= SelectedStartDate && c.CreateTime <= endTime));
  71. logs.Reverse();
  72. AllPro = logs.Count();//当日生产总量
  73. //var dtos = _mapper.Map<List<TestResultDto>>(logs);
  74. var dtos = _mapper.Map<List<Information>>(logs);
  75. _logModels.AddRange(dtos);
  76. LogModels = _logModels.Take(PageCount).ToList();
  77. MaxCount = _logModels.Count / PageCount + 1;
  78. }
  79. }
  80. }