using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Log;
using MECF.Framework.Common.DataCenter;
using OpenSEMI.ClientBase;
namespace VirgoUI.Client.Models.DataLog.Event
{
public class SystemLogItem
{
///
/// 时间
///
public string Time { get; set; }
///
/// ICON
///
public object Icon { get; set; }
///
/// 类型:操作日志|事件|其他
///
public string LogType { get; set; }
///
/// 针对腔体
///
public string TargetChamber { get; set; }
///
/// 发起方
///
public string Initiator { get; set; }
///
/// 详情
///
public string Detail { get; set; }
}
public class EventViewModel : BaseModel
{
private int MenuPermission;
public EventViewModel()
{
MenuPermission = ClientApp.Instance.GetPermission("EventLog");
this.DisplayName = "Event";
this.QueryDBEventFunc = (sql) => QueryDataClient.Instance.Service.QueryDBEvent(sql);
this.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;
}
protected override void OnInitialize()
{
base.OnInitialize();
}
protected override void OnViewLoaded(object _view)
{
base.OnViewLoaded(_view);
this.view = (EventView)_view;
this.view.wfTimeFrom.Value = this.SearchBeginTime;
this.view.wfTimeTo.Value = this.SearchEndTime;
this.Preload();
}
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 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 void Preload()
{
//初始化所有的枚举事件类型
EventList = new ObservableCollection();
EventList.Add("All");
if (QueryEventList != null)
{
List evList = QueryEventList();
foreach (string ev in evList)
EventList.Add(ev);
}
SelectedEvent = "All";
//初始化所有的用户
//PreloadUsers();
//所有属性更新
//NotifyOfPropertyChange();
//第一次默认查询
if (SearchedResult == null)
Search();
}
///
/// 导出
///
public void Export()
{
if (MenuPermission != 3) return;
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()
{
//if (!SearchPMA) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorA);
//if (!SearchPMB) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorB);
//if (!SearchPMC) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorC);
//if (!SearchPMD) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorD);
//if (!SearchSystem) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.System);
//if (!SearchLL) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.Loadlock);
//if (!SearchTM) sqlEvent += string.Format(" and \"Source\"<>'{0}' and \"Source\"<>'{1}' and \"Source\"<>'{2}' ",
// ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
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)
*/
///
/// 查询
///
public void Search()
{
if (MenuPermission != 3) return;
Task.Factory.StartNew(() =>
{
try
{
this.SearchBeginTime = this.view.wfTimeFrom.Value;
this.SearchEndTime = this.view.wfTimeTo.Value;
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());
if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' or lower(\"type\") like '%{1}%'", SearchKeyWords.ToLower(), SearchKeyWords.ToLower());
}
if (SearchOpeLog)
{
//sqlOperationLog = string.Format(" SELECT \"UserName\" as \"Initiator\", 'UserOperation' as \"LogType\", \"Time\", \"ChamberId\" as \"TargetChamber\", \"Content\" as \"Description\" FROM \"OperationLog\" where \"Time\" >='{0}' and \"Time\" <='{1}' ", SearchBeginTime.ToString("yyyy/MM/dd HH:mm:ss"), SearchEndTime.ToString("yyyy/MM/dd HH:mm:ss"));
//if (!SearchPMA) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorA);
//if (!SearchPMB) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorB);
//if (!SearchPMC) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorC);
//if (!SearchPMD) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorD);
//if (!SearchSystem) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.System);
//if (!SearchLL) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.Loadlock);
//if (!SearchTM) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' and \"ChamberId\"<>'{1}' and \"ChamberId\"<>'{2}' ",
// ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
//if (!string.IsNullOrWhiteSpace(SelectedUser) && SelectedUser != "不限") sqlOperationLog += string.Format(" and lower(\"UserName\")='{0}' ", SelectedUser.ToLower());
//if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlOperationLog += string.Format(" and lower(\"Content\") 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\" DESC limit 2000;";
List lstEvent = QueryDBEventFunc(sql);
if (lstEvent == null)
return;
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
SearchedResult = new ObservableCollection();
string logTypeStr;
foreach (EventItem ev in lstEvent)
{
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 Aitex.Core.UI.View.Common.SystemLogItem()
{
Time = ((DateTime)ev.OccuringTime).ToString("yyyy/MM/dd HH:mm:ss.fff"),
LogType = logTypeStr,
Detail = ev.Description,
TargetChamber = ev.Source,
Initiator = "",
Icon = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.Common;component/Resources/SystemLog/{0}.png", ev.Level.ToString()), UriKind.Absolute))
});
}
NotifyOfPropertyChange("SearchedResult");
if (SearchedResult.Count >= 2000)
{
//MessageBox.Show("Only display max 2000 items,reset the query condition", "query too many result", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}));
}
else
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
SearchedResult = new ObservableCollection();
NotifyOfPropertyChange("SearchedResult");
}));
}
}
catch (Exception ex)
{
LOG.Write(ex);
}
});
}
private EventView view;
}
}