using Aitex.Core.RT.Event; using Aitex.Core.RT.Log; using Aitex.Core.UI.MVVM; using MECF.Framework.Common.DataCenter; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media.Imaging; namespace Aitex.Sorter.UI.ViewModel { /// /// 操作记录查询结果结构定义 /// public class SystemLogItem { /// /// 时间 /// public string Time { get; set; } /// /// ICON /// public object Icon { get; set; } /// /// CEID /// public object Ceid { get; set; } /// /// 类型:操作日志|事件|其他 /// public string LogType { get; set; } /// /// 针对腔体 /// public string TargetChamber { get; set; } /// /// 发起方 /// public string Initiator { get; set; } /// /// 详情 /// public string Detail { get; set; } } class PerPageItem { public string Value { get; set; } } class EventLogViewModel : UIViewModelBase { public bool SearchAlarmEvent { get; set; } public bool SearchWarningEvent { get; set; } public bool SearchInfoEvent { get; set; } public bool SearchOpeLog { get; set; } public bool SearchPMA { get; set; } public bool SearchPMB { get; set; } public bool SearchPMC { get; set; } public bool SearchPMD { get; set; } //public bool SearchCoolDown { get; set; } public bool SearchTM { get; set; } public bool SearchLL { get; set; } //public bool SearchBuf1 { get; set; } public bool SearchSystem { get; set; } public string SearchKeyWords { get; set; } public ICommand SearchCommand { get; set; } public ICommand ExportCommand { get; set; } public ICommand NavigateCommand { get; set; } public DateTime SearchBeginTime { get; set; } public DateTime SearchEndTime { get; set; } public string Keywords { get; set; } public ObservableCollection EventList { get; set; } public string SelectedEvent { get; set; } public ObservableCollection UserList { get; set; } public string SelectedUser { get; set; } public ObservableCollection SearchedResult { get; set; } public Func> QueryDBEventFunc { get; set; } public Func> QueryEventList { get; set; } public ObservableCollection PerPageItems { get; set; } public bool IsLoading { get; set; } private DateTime _pageStart; private DateTime _pageStop; private int _currentPage; private int _countPerPage; private int _totalPage; private PerPageItem _selectedPerPage; public PerPageItem SelectedPerPage { get { return _selectedPerPage; } set { _selectedPerPage = value; Task.Run(()=> DisplayResult(false)); } } public string PageInfo { get; set; } private List _resultEvent; public bool EnableFirst { get; set; } public bool EnablePrevious { get; set; } public bool EnableNext { get; set; } public bool EnableLast { get; set; } public EventLogViewModel() : base("EventLogViewModel") { PerPageItems = new ObservableCollection(); PerPageItems.Add(new PerPageItem(){Value = "All"}); PerPageItems.Add(new PerPageItem() { Value = "30" }); PerPageItems.Add(new PerPageItem() { Value = "300" }); PerPageItems.Add(new PerPageItem() { Value = "3000" }); PerPageItems.Add(new PerPageItem() { Value = "30000" }); _selectedPerPage = PerPageItems[0]; PageInfo = "0/0"; QueryDBEventFunc = (sql) => QueryDataClient.Instance.Service.QueryDBEvent(sql); QueryEventList = () => { List result = new List(); foreach (var eventName in Enum.GetNames(typeof(EventEnum))) result.Add(eventName); return result; }; var now = DateTime.Today; SearchBeginTime = now;// -new TimeSpan(1, 0, 0, 0); SearchEndTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999); SelectedUser = "All"; SearchKeyWords = string.Empty; SearchAlarmEvent = true; SearchWarningEvent = true; SearchInfoEvent = true; SearchOpeLog = false; SearchPMA = false; SearchPMB = false; SearchPMC = false; SearchPMD = false; //SearchCoolDown = false; SearchTM = false; SearchLL = false; //SearchBuf1 = false; SearchSystem = false; SearchCommand = new DelegateCommand((o) => Search(), (o) => true); ExportCommand = new DelegateCommand((o) => Export(), (o) => true); NavigateCommand = new DelegateCommand((o) => Navigate(o), (o) => true); } protected override void InvokeBeforeUpdateProperty(Dictionary data) { } public void Navigate(string args) { if (args == "first") _currentPage = 1; if (args == "previous") _currentPage--; if (args == "next") _currentPage++; if (args == "last") _currentPage = _totalPage; DisplayResult(false); } /// /// 预先载入数据 /// public void Preload() { //初始化所有的枚举事件类型 EventList = new ObservableCollection(); EventList.Add("All"); if (QueryEventList != null) { List evList = QueryEventList(); foreach (string ev in evList) EventList.Add(ev); } SelectedEvent = "All"; //所有属性更新 InvokePropertyChanged(); //第一次默认查询 if (SearchedResult == null) Search(); } /// /// 导出 /// void Export() { try { Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.DefaultExt = ".xls"; // Default file extension dlg.Filter = "Excel数据表格文件(*.xls)|*.xls"; // Filter files by extension Nullable result = dlg.ShowDialog();// Show open file dialog box if (result == true) // Process open file dialog box results { System.Data.DataSet ds = new System.Data.DataSet(); ds.Tables.Add(new System.Data.DataTable("系统运行日志")); ds.Tables[0].Columns.Add("类型"); ds.Tables[0].Columns.Add("时间"); ds.Tables[0].Columns.Add("腔体"); ds.Tables[0].Columns.Add("发起源"); ds.Tables[0].Columns.Add("内容描述"); foreach (var item in SearchedResult) { var row = ds.Tables[0].NewRow(); row[0] = item.LogType; row[1] = item.Time; row[2] = item.TargetChamber; row[3] = item.Initiator; row[4] = item.Detail; ds.Tables[0].Rows.Add(row); } ds.WriteXml(dlg.FileName); } } catch (Exception ex) { LOG.Write(ex); MessageBox.Show("导出系统日志发生错误", "导出失败", MessageBoxButton.OK, MessageBoxImage.Warning); } } private string GetSourceWhere() { return ""; } /* * * gid integer NOT NULL DEFAULT nextval('event_data_gid_seq'::regclass), event_id integer, event_enum text, type text, source text, description text, level text, occur_time timestamp without time zone, CONSTRAINT event_data_pkey PRIMARY KEY (gid) */ /// /// 查询 /// void Search() { if (IsLoading) return; IsLoading = true; InvokePropertyChanged("IsLoading"); Task.Factory.StartNew(() => { try { string sqlEvent = ""; string sqlOperationLog = ""; string sql = ""; if (SearchAlarmEvent || SearchWarningEvent || SearchInfoEvent) { sqlEvent = string.Format("SELECT \"event_id\", \"event_enum\", \"type\", \"occur_time\", \"level\",\"source\" , \"description\" FROM \"event_data\" where \"occur_time\" >='{0}' and \"occur_time\" <='{1}' ", SearchBeginTime.ToString("yyyyMMdd HHmmss"), SearchEndTime.ToString("yyyyMMdd HHmmss")); sqlEvent += GetSourceWhere(); sqlEvent += " and (FALSE "; if (SearchAlarmEvent) sqlEvent += " OR \"level\"='Alarm' "; if (SearchWarningEvent) sqlEvent += " OR \"level\"='Warning' "; if (SearchInfoEvent) sqlEvent += " OR \"level\"='Information' "; sqlEvent += " ) "; if (!string.IsNullOrWhiteSpace(SelectedEvent) && SelectedEvent != "All") sqlEvent += string.Format(" and lower(\"event_enum\")='{0}' ", SelectedEvent.ToLower()); if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' ", SearchKeyWords.ToLower()); } sql = sqlEvent; if (!string.IsNullOrEmpty(sqlOperationLog)) { if (string.IsNullOrEmpty(sql)) { sql = sqlOperationLog; } else { sql += " UNION ALL " + sqlOperationLog; } } if (!string.IsNullOrEmpty(sql) && QueryDBEventFunc != null) { sql += " order by \"occur_time\" asc ;"; _resultEvent = QueryDBEventFunc(sql); IsLoading = false; DisplayResult(true); Application.Current.Dispatcher.BeginInvoke(new Action(() => { InvokePropertyChanged("IsLoading"); })); } else { IsLoading = false; Application.Current.Dispatcher.BeginInvoke(new Action(() => { InvokePropertyChanged("IsLoading"); })); Application.Current.Dispatcher.BeginInvoke(new Action(() => { SearchedResult = new ObservableCollection(); InvokePropertyChanged("SearchedResult"); })); } } catch (Exception ex) { LOG.Write(ex); } }); } private void DisplayResult(bool firstTimeDisplay) { if (IsLoading) return; SearchedResult = new ObservableCollection(); if (_resultEvent == null || _resultEvent.Count == 0) { PageInfo = "0/0"; EnableFirst = EnableLast = EnableNext = EnablePrevious = false; Application.Current.Dispatcher.BeginInvoke(new Action(() => { InvokePropertyChanged(nameof(SearchedResult)); InvokePropertyChanged(nameof(PageInfo)); InvokePropertyChanged(nameof(EnableFirst)); InvokePropertyChanged(nameof(EnableLast)); InvokePropertyChanged(nameof(EnableNext)); InvokePropertyChanged(nameof(EnablePrevious)); })); return; } if (SelectedPerPage==null || SelectedPerPage.Value == "All" || string.IsNullOrEmpty(SelectedPerPage.Value)) { _countPerPage = _resultEvent.Count; } else { _countPerPage = int.Parse(SelectedPerPage.Value); } if (firstTimeDisplay) { _currentPage = 1; } _totalPage = _resultEvent.Count / _countPerPage + (_resultEvent.Count % _countPerPage > 0 ? 1 : 0); if (_currentPage < 1) _currentPage = 1; if (_currentPage > _totalPage) _currentPage = _totalPage; EnableFirst = _currentPage > 1; EnablePrevious = _currentPage > 1; EnableNext = _currentPage < _totalPage; EnableLast = _currentPage < _totalPage; PageInfo = $"{_currentPage}/{_totalPage}"; Application.Current.Dispatcher.BeginInvoke(new Action(() => { InvokePropertyChanged(nameof(SearchedResult)); InvokePropertyChanged(nameof(PageInfo)); InvokePropertyChanged(nameof(EnableFirst)); InvokePropertyChanged(nameof(EnableLast)); InvokePropertyChanged(nameof(EnableNext)); InvokePropertyChanged(nameof(EnablePrevious)); })); //2000一次,显示全部页面 int from = (_currentPage - 1) * _countPerPage; for (int i = 0; i < _countPerPage; i = i + 2000) { Application.Current.Dispatcher.BeginInvoke(new Action((index) => { string logTypeStr; for (int j = from+index; j - index-from < Math.Min(2000,_countPerPage) && j < _resultEvent.Count; j++) { EventItem ev = _resultEvent[j]; switch (ev.Level) { case EventLevel.Information: logTypeStr = "Info"; break; case EventLevel.Warning: logTypeStr = "Warning"; break; case EventLevel.Alarm: logTypeStr = "Alarm"; break; default: logTypeStr = "Undefine"; break; } SearchedResult.Add(new SystemLogItem() { Time = ((DateTime)ev.OccuringTime).ToString("yyyy/MM/dd HH:mm:ss.fff"), LogType = logTypeStr, Detail = ev.Description, Ceid = ev.Id, TargetChamber = ev.Source, Initiator = "", Icon = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.UI.Core;component/Resources/SystemLog/{0}.png", ev.Level.ToString()), UriKind.Absolute)) }); } InvokePropertyChanged("SearchedResult"); }), i); Thread.Sleep(300); } } private void OnPerPageChanged() { } } }