12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- using AutoMapper;
- using HandyControl.Controls;
- using HandyControl.Data;
- using Prism.Commands;
- using Prism.Mvvm;
- using Repository;
- using Repository.Entiies;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace LogoForceTestApp.Modules.MainModule.ViewModels
- {
- public class LogPageViewModel : BindableBase
- {
- private readonly List<LogModel> _logModels;
- private readonly IMapper _mapper;
- public List<LogModel> LogModels { get; set; }
- public int PageIndex { get; set; } = 1;
- public int MaxCount { get; set; } = 1;
- public int PageCount { get; set; } = 20;
- public DateTime SelectedStartDate { get; set; }
- public DateTime SelectedEndDate { get; set; }
- public DelegateCommand<FunctionEventArgs<int>> PageUpdatedCmd => new(PageUpdated);
- public DelegateCommand QueryCommand { get; set; }
- /// <summary>
- /// 页码改变
- /// </summary>
- private void PageUpdated(FunctionEventArgs<int> info)
- {
- LogModels = _logModels.Skip((info.Info - 1) * PageCount).Take(PageCount).Reverse().ToList();
- }
- public LogPageViewModel(IMapper mapper)
- {
- SelectedStartDate = DateTime.Today;
- SelectedEndDate = DateTime.Today;
- LogModels = new List<LogModel>();
- _mapper = mapper;
- _logModels = new List<LogModel>();
- InitData().Await();
- QueryCommand = new DelegateCommand(async () =>
- {
- await QueryData();
- });
-
- }
- private async Task InitData()
- {
- var logRepository = new LogRepository();
- var dateTimeToday = DateTime.Today;
- var logs= await logRepository.GetAllQueryAsync<Log>(c => c.Timestamp >= dateTimeToday);
- var dtos=_mapper.Map<List<LogModel>>(logs);
- dtos.Reverse();
- _logModels.AddRange(dtos);
- LogModels = _logModels.Take(PageCount).ToList();
- MaxCount = _logModels.Count / PageCount + 1;
- }
- private async Task QueryData()
- {
- if (SelectedEndDate < SelectedStartDate || SelectedStartDate == default)
- {
- MessageBox.Show("请选择正确的日期","提示");
- return;
- }
-
- _logModels.Clear();
- var logRepository = new LogRepository();
- var endTime=SelectedEndDate.AddDays(1);
- var logs = (await logRepository.GetAllQueryAsync<Log>(c => c.Timestamp >= SelectedStartDate&&c.Timestamp<=endTime));
- logs.Reverse();
- var dtos = _mapper.Map<List<LogModel>>(logs);
- _logModels.AddRange(dtos);
- LogModels = _logModels.Take(PageCount).ToList();
- MaxCount = _logModels.Count / PageCount + 1;
- }
-
- }
- public class LogModel : BindableBase
- {
- public int Id { get; set; }
- public DateTime Timestamp { get; set; }
- public string Properties { get; set; }
- }
- }
|