using AutomaticApp.Common; using AutomaticApp.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace AutomaticApp.ViewModels { /// /// 系统日志数据记录 /// internal class LogViewModel : NotifyBase { string Save_Path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"数据记录\"; public static ErrorMessageModel ErrorMessageModel { get; set; } = new ErrorMessageModel(); private DateTime selectedDate; public DateTime SelectedDate { get { return selectedDate; } set { selectedDate = value; this.DoNotify(); } } int pageNum = 0; List logs = new List(); private int totalPage; public int TotalPage { get { return totalPage; } set { totalPage = value; this.DoNotify(); } } private string logMsg; public string LogMsg { get { return logMsg; } set { logMsg = value; this.DoNotify(); } } public CommandBase ReadTxtLogCommand { get; set; }//读取日志信息 public CommandBase ForwordLogCommand { get; set; }//读取日志信息 public CommandBase NextLogCommand { get; set; }//读取日志信息 public LogViewModel() { SelectedDate = DateTime.Now; ReadTxtLog(null); this.ReadTxtLogCommand = new CommandBase(); this.ReadTxtLogCommand.DoExcute = new Action(ReadTxtLog); this.ReadTxtLogCommand.DoCanExcute = new Func((o) => true); this.ForwordLogCommand = new CommandBase(); this.ForwordLogCommand.DoExcute = new Action(ForwordLog); this.ForwordLogCommand.DoCanExcute = new Func((o) => true); this.NextLogCommand = new CommandBase(); this.NextLogCommand.DoExcute = new Action(NextLog); this.NextLogCommand.DoCanExcute = new Func((o) => true); } public void ReadTxtLog(object o) { //设置当前页为1 pageNum = 1; logs = tools.ReadFromTXT("解析数据.txt", Save_Path + SelectedDate.ToString("yyyy_MM_dd") + @"\").Split("\r\n").ToList(); ErrorMessageModel._MessageCon = ""; if (logs.Count < 100) { TotalPage = 1; for (int i = 0; i < logs.Count; i++) { ErrorMessageModel._MessageCon += logs[i] + "\r\n"; } } else { TotalPage = (int)Math.Ceiling((double)logs.Count / 100); for (int i = 0; i < 100; i++) { ErrorMessageModel._MessageCon += logs[i] + "\r\n"; } } LogMsg = string.Format("共计{0}页,当前第{1}页", TotalPage, pageNum); } public void ForwordLog(object o) { if (pageNum == 1) return; pageNum -= 1; ErrorMessageModel._MessageCon = ""; for (int i = 0; i < 100; i++) { ErrorMessageModel._MessageCon += logs[i + (pageNum - 1) * 100] + "\r\n"; } LogMsg = string.Format("共计{0}页,当前第{1}页", TotalPage, pageNum); } public void NextLog(object o) { if (pageNum == Math.Ceiling((double)logs.Count / 100)) return; pageNum += 1; ErrorMessageModel._MessageCon = ""; for (int i = 0; i < ((pageNum == Math.Ceiling((double)logs.Count / 100)) ? (logs.Count - (pageNum - 1) * 100) : 100); i++) { ErrorMessageModel._MessageCon += logs[i + (pageNum - 1) * 100] + "\r\n"; } LogMsg = string.Format("共计{0}页,当前第{1}页", TotalPage, pageNum); } } }