EventViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using Aitex.Core.RT.Event;
  2. using MECF.Framework.Common.DataCenter;
  3. using Prism.Commands;
  4. using Prism.Mvvm;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Media.Imaging;
  13. using Venus_MainPages.Views;
  14. namespace Venus_MainPages.ViewModels
  15. {
  16. public class EventViewModel : BindableBase
  17. {
  18. #region 私有字段
  19. private int MenuPermission;
  20. #endregion
  21. #region 属性
  22. public bool SearchAlarmEvent { get; set; }
  23. public bool SearchWarningEvent { get; set; }
  24. public bool SearchInfoEvent { get; set; }
  25. public bool SearchOpeLog { get; set; }
  26. public bool SearchPMA { get; set; }
  27. public bool SearchPMB { get; set; }
  28. public bool SearchPMC { get; set; }
  29. public bool SearchPMD { get; set; }
  30. //public bool SearchCoolDown { get; set; }
  31. public bool SearchTM { get; set; }
  32. public bool SearchLL { get; set; }
  33. //public bool SearchBuf1 { get; set; }
  34. public bool SearchSystem { get; set; }
  35. public string SearchKeyWords { get; set; }
  36. public DateTime SearchBeginTime { get; set; }
  37. public DateTime SearchEndTime { get; set; }
  38. public string Keywords { get; set; }
  39. public ObservableCollection<string> EventList { get; set; }
  40. public string SelectedEvent { get; set; }
  41. public ObservableCollection<string> UserList { get; set; }
  42. public string SelectedUser { get; set; }
  43. public ObservableCollection<Aitex.Core.UI.View.Common.SystemLogItem> SearchedResult { get; set; }
  44. public Func<string, List<EventItem>> QueryDBEventFunc { get; set; }
  45. public Func<List<string>> QueryEventList { get; set; }
  46. #endregion
  47. #region 命令
  48. private DelegateCommand _SearchCommand;
  49. public DelegateCommand SearchCommand =>
  50. _SearchCommand ?? (_SearchCommand = new DelegateCommand(Search));
  51. private DelegateCommand _ExportCommand;
  52. public DelegateCommand ExportCommand =>
  53. _ExportCommand ?? (_ExportCommand = new DelegateCommand(Export));
  54. private DelegateCommand<object> _LoadCommand;
  55. public DelegateCommand<object> LoadCommand =>
  56. _LoadCommand ?? (_LoadCommand = new DelegateCommand<object>(OnLoad));
  57. #endregion
  58. public EventViewModel()
  59. {
  60. this.QueryDBEventFunc = (sql) => QueryDataClient.Instance.Service.QueryDBEvent(sql);
  61. this.QueryEventList = () =>
  62. {
  63. List<string> result = new List<string>();
  64. foreach (var eventName in Enum.GetNames(typeof(EventEnum)))
  65. result.Add(eventName);
  66. return result;
  67. };
  68. var now = DateTime.Today;
  69. SearchBeginTime = now;// -new TimeSpan(1, 0, 0, 0);
  70. SearchEndTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999);
  71. SelectedUser = "All";
  72. SearchKeyWords = string.Empty;
  73. SearchAlarmEvent = true;
  74. SearchWarningEvent = true;
  75. SearchInfoEvent = true;
  76. SearchOpeLog = false;
  77. SearchPMA = false;
  78. SearchPMB = false;
  79. SearchPMC = false;
  80. SearchPMD = false;
  81. //SearchCoolDown = false;
  82. SearchTM = false;
  83. SearchLL = false;
  84. //SearchBuf1 = false;
  85. SearchSystem = false;
  86. }
  87. #region 方法
  88. private void OnLoad(Object eventView)
  89. {
  90. this.view = (EventView)eventView;
  91. this.view.wfTimeFrom.Value = this.SearchBeginTime;
  92. this.view.wfTimeTo.Value = this.SearchEndTime;
  93. this.Preload();
  94. }
  95. /// <summary>
  96. /// 预先载入数据
  97. /// </summary>
  98. public void Preload()
  99. {
  100. //初始化所有的枚举事件类型
  101. EventList = new ObservableCollection<string>();
  102. EventList.Add("All");
  103. if (QueryEventList != null)
  104. {
  105. List<string> evList = QueryEventList();
  106. foreach (string ev in evList)
  107. EventList.Add(ev);
  108. }
  109. SelectedEvent = "All";
  110. //初始化所有的用户
  111. //PreloadUsers();
  112. //所有属性更新
  113. //NotifyOfPropertyChange();
  114. //第一次默认查询
  115. if (SearchedResult == null)
  116. Search();
  117. }
  118. /// <summary>
  119. /// 导出
  120. /// </summary>
  121. public void Export()
  122. {
  123. try
  124. {
  125. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  126. dlg.DefaultExt = ".xls"; // Default file extension
  127. dlg.Filter = "Excel数据表格文件(*.xls)|*.xls"; // Filter files by extension
  128. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  129. if (result == true) // Process open file dialog box results
  130. {
  131. System.Data.DataSet ds = new System.Data.DataSet();
  132. ds.Tables.Add(new System.Data.DataTable("系统运行日志"));
  133. ds.Tables[0].Columns.Add("类型");
  134. ds.Tables[0].Columns.Add("时间");
  135. ds.Tables[0].Columns.Add("腔体");
  136. ds.Tables[0].Columns.Add("发起源");
  137. ds.Tables[0].Columns.Add("内容描述");
  138. foreach (var item in SearchedResult)
  139. {
  140. var row = ds.Tables[0].NewRow();
  141. row[0] = item.LogType;
  142. row[1] = item.Time;
  143. row[2] = item.TargetChamber;
  144. row[3] = item.Initiator;
  145. row[4] = item.Detail;
  146. ds.Tables[0].Rows.Add(row);
  147. }
  148. ds.WriteXml(dlg.FileName);
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. //LOG.Write(ex);
  154. MessageBox.Show("导出系统日志发生错误", "导出失败", MessageBoxButton.OK, MessageBoxImage.Warning);
  155. }
  156. }
  157. private string GetSourceWhere()
  158. {
  159. //if (!SearchPMA) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorA);
  160. //if (!SearchPMB) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorB);
  161. //if (!SearchPMC) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorC);
  162. //if (!SearchPMD) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorD);
  163. //if (!SearchSystem) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.System);
  164. //if (!SearchLL) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.Loadlock);
  165. //if (!SearchTM) sqlEvent += string.Format(" and \"Source\"<>'{0}' and \"Source\"<>'{1}' and \"Source\"<>'{2}' ",
  166. // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
  167. return "";
  168. }
  169. /*
  170. *gid integer NOT NULL DEFAULT nextval('event_data_gid_seq'::regclass),
  171. event_id integer,
  172. event_enum text,
  173. type text,
  174. source text,
  175. description text,
  176. level text,
  177. occur_time timestamp without time zone,
  178. CONSTRAINT event_data_pkey PRIMARY KEY (gid)
  179. */
  180. /// <summary>
  181. /// 查询
  182. /// </summary>
  183. public void Search()
  184. {
  185. Task.Factory.StartNew(() =>
  186. {
  187. try
  188. {
  189. this.SearchBeginTime = this.view.wfTimeFrom.Value;
  190. this.SearchEndTime = this.view.wfTimeTo.Value;
  191. string sqlEvent = "";
  192. string sqlOperationLog = "";
  193. string sql = "";
  194. if (SearchAlarmEvent || SearchWarningEvent || SearchInfoEvent)
  195. {
  196. 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"));
  197. sqlEvent += GetSourceWhere();
  198. sqlEvent += " and (FALSE ";
  199. if (SearchAlarmEvent) sqlEvent += " OR \"level\"='Alarm' ";
  200. if (SearchWarningEvent) sqlEvent += " OR \"level\"='Warning' ";
  201. if (SearchInfoEvent) sqlEvent += " OR \"level\"='Information' ";
  202. sqlEvent += " ) ";
  203. if (!string.IsNullOrWhiteSpace(SelectedEvent) && SelectedEvent != "All") sqlEvent += string.Format(" and lower(\"event_enum\")='{0}' ", SelectedEvent.ToLower());
  204. //if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' ", SearchKeyWords.ToLower());
  205. if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' or lower(\"type\") like '%{1}%'", SearchKeyWords.ToLower(), SearchKeyWords.ToLower());
  206. }
  207. if (SearchOpeLog)
  208. {
  209. //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"));
  210. //if (!SearchPMA) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorA);
  211. //if (!SearchPMB) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorB);
  212. //if (!SearchPMC) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorC);
  213. //if (!SearchPMD) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorD);
  214. //if (!SearchSystem) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.System);
  215. //if (!SearchLL) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.Loadlock);
  216. //if (!SearchTM) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' and \"ChamberId\"<>'{1}' and \"ChamberId\"<>'{2}' ",
  217. // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
  218. //if (!string.IsNullOrWhiteSpace(SelectedUser) && SelectedUser != "不限") sqlOperationLog += string.Format(" and lower(\"UserName\")='{0}' ", SelectedUser.ToLower());
  219. //if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlOperationLog += string.Format(" and lower(\"Content\") like '%{0}%' ", SearchKeyWords.ToLower());
  220. }
  221. sql = sqlEvent;
  222. if (!string.IsNullOrEmpty(sqlOperationLog))
  223. {
  224. if (string.IsNullOrEmpty(sql))
  225. {
  226. sql = sqlOperationLog;
  227. }
  228. else
  229. {
  230. sql += " UNION ALL " + sqlOperationLog;
  231. }
  232. }
  233. if (!string.IsNullOrEmpty(sql) && QueryDBEventFunc != null)
  234. {
  235. sql += " order by \"occur_time\" DESC limit 2000;";
  236. List<EventItem> lstEvent = QueryDBEventFunc(sql);
  237. if (lstEvent == null)
  238. return;
  239. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  240. {
  241. SearchedResult = new ObservableCollection<Aitex.Core.UI.View.Common.SystemLogItem>();
  242. string logTypeStr;
  243. foreach (EventItem ev in lstEvent)
  244. {
  245. switch (ev.Level)
  246. {
  247. case EventLevel.Information: logTypeStr = "Info"; break;
  248. case EventLevel.Warning: logTypeStr = "Warning"; break;
  249. case EventLevel.Alarm: logTypeStr = "Alarm"; break;
  250. default: logTypeStr = "Undefine"; break;
  251. }
  252. SearchedResult.Add(new Aitex.Core.UI.View.Common.SystemLogItem()
  253. {
  254. ID = ev.Id,
  255. Time = ((DateTime)ev.OccuringTime).ToString("yyyy/MM/dd HH:mm:ss.fff"),
  256. LogType = logTypeStr,
  257. Detail = ev.Description,
  258. TargetChamber = ev.Source,
  259. Initiator = "",
  260. Icon = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.Common;component/Resources/SystemLog/{0}.png", ev.Level.ToString()), UriKind.Absolute))
  261. }); ;
  262. }
  263. RaisePropertyChanged("SearchedResult");
  264. if (SearchedResult.Count >= 2000)
  265. {
  266. //MessageBox.Show("Only display max 2000 items,reset the query condition", "query too many result", MessageBoxButton.OK, MessageBoxImage.Warning);
  267. }
  268. }));
  269. }
  270. else
  271. {
  272. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  273. {
  274. SearchedResult = new ObservableCollection<Aitex.Core.UI.View.Common.SystemLogItem>();
  275. RaisePropertyChanged("SearchedResult");
  276. }));
  277. }
  278. }
  279. catch (Exception ex)
  280. {
  281. //LOG.Write(ex);
  282. }
  283. });
  284. }
  285. private EventView view;
  286. #endregion
  287. }
  288. public class SystemLogItem
  289. {
  290. /// <summary>
  291. /// 时间
  292. /// </summary>
  293. public string Time { get; set; }
  294. /// <summary>
  295. /// ICON
  296. /// </summary>
  297. public object Icon { get; set; }
  298. /// <summary>
  299. /// 类型:操作日志|事件|其他
  300. /// </summary>
  301. public string LogType { get; set; }
  302. /// <summary>
  303. /// 针对腔体
  304. /// </summary>
  305. public string TargetChamber { get; set; }
  306. /// <summary>
  307. /// 发起方
  308. /// </summary>
  309. public string Initiator { get; set; }
  310. /// <summary>
  311. /// 详情
  312. /// </summary>
  313. public string Detail { get; set; }
  314. }
  315. }