using HandyControl.Data; using Microsoft.Win32; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using StartServerWPF.Modules.Main.Models; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows.Input; namespace StartServerWPF.Modules.Main.ViewModels { public class LogManagementViewModel : BindableBase { IEventAggregator _ea; private readonly IDialogService _dialogService; private readonly MainViewModel _mainViewModel; public LogManagementViewModel( IEventAggregator ea, IDialogService dialogService, MainViewModel mainViewModel) { this._dialogService = dialogService; this._mainViewModel = mainViewModel; } private string _title = "日志查看"; public string Title => _title; public ICommand LoadedCommand => new DelegateCommand(()=> { InitInfo(); }); public ICommand UploadCommand { get; set; } public DelegateCommand QueryDataCommand => new(QueryData); public DelegateCommand ResetQueryDataCommand => new(InitInfo); private void InitInfo() { StartTime = DateTime.Today; EndTime = DateTime.Now; QueryData(); } public void Refresh() { QueryData(); } public void QueryData() { SelectIndexLogType = "全部"; DataList = new List(); string fileName = _mainViewModel.sc.vpnInfo.SystemLogPath+ "SerLog_" + EndTime.ToString("yyyyMMdd") + ".txt"; if (!File.Exists(fileName)) return; Task.Run(() => { _queryDataList = new List(); var lines= File.ReadAllLines(fileName); foreach (var item in lines) { var str= item.Trim().Split(" "); _queryDataList.Add(new LogMessageModel() { OriginTime = Convert.ToDateTime(str[0]), LogType = str[1], AppName = str[2], State = str[3] }); } _totalDataList = _queryDataList; int index = _queryDataList.Count; MaxCountPage = (index % countPerPage == 0 ? index / countPerPage : index / countPerPage + 1); DataList = _totalDataList.Take(countPerPage).ToList(); }); } private string _SelectIndexLogType; public string SelectIndexLogType { get => _SelectIndexLogType; set { if (_queryDataList != null) { _totalDataList = _queryDataList.Where(l => l.LogType == value).ToList(); int index = _totalDataList.Count; MaxCountPage = (index % countPerPage == 0 ? index / countPerPage : index / countPerPage + 1); DataList = _totalDataList.Take(countPerPage).ToList(); } SetProperty(ref _SelectIndexLogType, value); } } private DateTime _StartTime; public DateTime StartTime { get => _StartTime; set => SetProperty(ref _StartTime, value); } private DateTime _EndTime; public DateTime EndTime { get => _EndTime; set => SetProperty(ref _EndTime, value); } private List _DataList; public List DataList { get { return _DataList; } set { SetProperty(ref _DataList, value); } } ///// ///// 所有数据 ///// private List _totalDataList; private List _queryDataList; /// /// 页码 /// private int _pageIndex = 1; /// /// 页码 /// public int PageIndex { get => _pageIndex; set => SetProperty(ref _pageIndex, value); } /// /// 页码 /// private int _MaxCountPage = 15; /// /// 页码 /// public int MaxCountPage { get => _MaxCountPage; set => SetProperty(ref _MaxCountPage, value); } private int countPerPage = 15; /// /// 页码改变命令 /// public DelegateCommand> PageUpdatedCmd => new(PageUpdated); /// /// 页码改变 /// private void PageUpdated(FunctionEventArgs info) { DataList = _totalDataList.Skip((info.Info - 1) * countPerPage).Take(countPerPage).ToList(); } } }