ProcessHistoryTwoViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Sorter.Common;
  3. using Caliburn.Micro;
  4. using Caliburn.Micro.Core;
  5. using MECF.Framework.Common.CommonData;
  6. using MECF.Framework.Common.DataCenter;
  7. using MECF.Framework.Common.Utilities;
  8. using MECF.Framework.UI.Client.CenterViews.DataLogs.Event;
  9. using MECF.Framework.UI.Client.ClientBase;
  10. using OpenSEMI.ClientBase;
  11. using OpenSEMI.ClientBase.Command;
  12. using SciChart.Charting.Common.Extensions;
  13. using SciChart.Core.Extensions;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Collections.ObjectModel;
  17. using System.ComponentModel;
  18. using System.Data;
  19. using System.Diagnostics.Eventing.Reader;
  20. using System.Linq;
  21. using System.Windows.Forms;
  22. using System.Windows.Input;
  23. namespace MECF.Framework.UI.Client.CenterViews.DataLogs.ProcessHistory
  24. {
  25. public class ProcessHistoryTwoViewModel : UiViewModelBase
  26. {
  27. public ProcessHistoryTwoViewModel()
  28. {
  29. QueryCommand = new BaseCommand<object>(QueryLots);
  30. BatchDetailCommand = new BaseCommand<object>(BatchDetail);
  31. ProcessDetailCommand = new BaseCommand<object>(ProcessDetail);
  32. ProcessExportCommand = new BaseCommand<object>(ProcessExport);
  33. ExportProcessTableCommand = new BaseCommand<object>(ExportProcessTable);
  34. ProcessJobInfoCommand = new BaseCommand<object>(ProcessJobInfo);
  35. // DisplayGraphCommand = new BaseCommand<object>(DisplayGraph);
  36. InitTime();
  37. }
  38. public ObservableCollection<ProcessHistoryLot> ProcessHistoryLots { get; set; } = new ObservableCollection<ProcessHistoryLot>();
  39. private int _processHistoryLotSelectIndex;
  40. public int ProcessHistoryLotSelectIndex
  41. {
  42. get => _processHistoryLotSelectIndex;
  43. set
  44. {
  45. _processHistoryLotSelectIndex = value;
  46. NotifyOfPropertyChange(nameof(ProcessHistoryLotSelectIndex));
  47. }
  48. }
  49. private DateTime _searchBeginTime;
  50. public DateTime SearchBeginTime
  51. {
  52. get { return _searchBeginTime; }
  53. set
  54. {
  55. _searchBeginTime = value;
  56. NotifyOfPropertyChange("SearchBeginTime");
  57. }
  58. }
  59. private DateTime _searchEndTime;
  60. public DateTime SearchEndTime
  61. {
  62. get { return _searchEndTime; }
  63. set
  64. {
  65. _searchEndTime = value;
  66. NotifyOfPropertyChange("SearchEndTime");
  67. }
  68. }
  69. private string keyWord;
  70. public string KeyWord
  71. {
  72. get { return keyWord; }
  73. set
  74. {
  75. keyWord = value;
  76. NotifyOfPropertyChange("KeyWord");
  77. }
  78. }
  79. private ProcessHistoryTwoView view;
  80. public ICommand QueryCommand { get; set; }
  81. public ICommand BatchDetailCommand { get; set; }
  82. public ICommand ProcessDetailCommand { get; set; }
  83. public ICommand ProcessExportCommand { get; set; }
  84. public ICommand ExportProcessTableCommand { get; set; }
  85. public ICommand ProcessJobInfoCommand { get; set; }
  86. // public ICommand DisplayGraphCommand { get; set; }
  87. protected override void OnViewLoaded(object _view)
  88. {
  89. base.OnViewLoaded(_view);
  90. this.view = (ProcessHistoryTwoView)_view;
  91. this.view.wfTimeFrom.Content = this.SearchBeginTime;
  92. this.view.wfTimeTo.Content = this.SearchEndTime;
  93. QueryLots(new object());
  94. ProcessHistoryLots.FirstOrDefault().IsChecked = true;
  95. }
  96. void InitTime()
  97. {
  98. SearchBeginTime = DateTime.Now.Date;
  99. SearchEndTime = DateTime.Now.Date.AddDays(1).Date;
  100. }
  101. void QueryLots(object e)
  102. {
  103. this.SearchBeginTime = Convert.ToDateTime(this.view.wfTimeFrom.Content);
  104. this.SearchEndTime = Convert.ToDateTime(this.view.wfTimeTo.Content);
  105. if (SearchBeginTime > SearchEndTime)
  106. {
  107. MessageBox.Show("Time range invalid, start time should be early than end time");
  108. return;
  109. }
  110. ProcessHistoryLots.Clear();
  111. QueryLot(SearchBeginTime, SearchEndTime, KeyWord);//.OrderByDescending(lot => lot.StartTime).ToArray();
  112. }
  113. void BatchDetail(object o)
  114. {
  115. if (ProcessHistoryLotSelectIndex != -1 && ProcessHistoryLots.Count > 0)
  116. {
  117. DateTime startTime = ProcessHistoryLots[ProcessHistoryLotSelectIndex].StartTime;
  118. DateTime endTime = ProcessHistoryLots[ProcessHistoryLotSelectIndex].EndTime;
  119. var batchID = ProcessHistoryLots[ProcessHistoryLotSelectIndex].BatchID;
  120. List<string> layoutData = ProcessHistoryLots[ProcessHistoryLotSelectIndex].LayoutData.Split(',').ToList();
  121. List<string> waferData = ProcessHistoryLots[ProcessHistoryLotSelectIndex].WaferData.Split(',').ToList();
  122. var windowManager = IoC.Get<IWindowManager>();
  123. BatchDetailViewModel batchDetailViewModel = new BatchDetailViewModel(startTime, endTime, batchID, layoutData, layoutData.Count, waferData);
  124. (windowManager as WindowManager)?.ShowDialogWithTitle(batchDetailViewModel, null, "Batch Detail");
  125. }
  126. else
  127. {
  128. var windowManager = IoC.Get<IWindowManager>();
  129. BatchDetailViewModel batchDetailViewModel = new BatchDetailViewModel();
  130. (windowManager as WindowManager)?.ShowDialogWithTitle(batchDetailViewModel, null, "Batch Detail");
  131. }
  132. }
  133. public void SelectDate(string SelectType)
  134. {
  135. var windowManager = Caliburn.Micro.Core.IoC.Get<Caliburn.Micro.IWindowManager>();
  136. if (SelectType == "Start")
  137. {
  138. this.SearchBeginTime = Convert.ToDateTime(this.view.wfTimeFrom.Content);
  139. SelectDateViewModel selectdateViewModel = new SelectDateViewModel(SearchBeginTime);
  140. var result = (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(selectdateViewModel, null, "Start Time");
  141. if (result == true)
  142. {
  143. this.view.wfTimeFrom.Content = selectdateViewModel.SearchDate;
  144. }
  145. }
  146. else
  147. {
  148. this.SearchEndTime = Convert.ToDateTime(this.view.wfTimeTo.Content);
  149. SelectDateViewModel selectdateViewModel = new SelectDateViewModel(SearchEndTime);
  150. var result = (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(selectdateViewModel, null, "End Time");
  151. if (result == true)
  152. {
  153. this.view.wfTimeTo.Content = selectdateViewModel.SearchDate;
  154. }
  155. }
  156. }
  157. void ProcessDetail(object o)
  158. {
  159. //if (ProcessHistoryLots.Count > 0)
  160. //{
  161. var windowManager = IoC.Get<IWindowManager>();
  162. var param = ProcessHistoryLots.Where(x => x.IsChecked).ToList();
  163. object processDetailViewModel = new ProcessDetailViewModel<double>(param);
  164. (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(processDetailViewModel, null, "Process Detail");
  165. //}
  166. }
  167. void ProcessJobInfo(object o)
  168. {
  169. var windowManager = IoC.Get<IWindowManager>();
  170. var param = ProcessHistoryLots.Where(x => x.IsChecked).FirstOrDefault();
  171. ProcessDetailV2ViewModel processDetailViewModel = new ProcessDetailV2ViewModel();
  172. processDetailViewModel.PJInfo = param;
  173. processDetailViewModel.TableCount = ProcessHistoryLots.Count;
  174. processDetailViewModel.SelectItemIndex = ProcessHistoryLots.IndexOf(param) + 1;
  175. (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(processDetailViewModel, null, "ProcessJob Info");
  176. }
  177. void ExportProcessTable(object o)
  178. {
  179. try
  180. {
  181. if (ProcessHistoryLots == null || ProcessHistoryLots.Count == 0)
  182. {
  183. MessageBox.Show($"Table is null");
  184. }
  185. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  186. dlg.DefaultExt = ".xlsx"; // Default file extension
  187. dlg.FileName = $"Process_{SearchBeginTime.ToString("yyyy_MM_dd_HH_mm_ss")}~{SearchEndTime.ToString("yyyy_MM_dd_HH_mm_ss")}";
  188. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  189. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  190. if (result == true) // Process open file dialog box results
  191. {
  192. System.Data.DataSet ds = new System.Data.DataSet();
  193. SaveStepsToTable(ds);
  194. if (!ExcelHelper.ExportToExcel(dlg.FileName, ds, out string reason, true, false))
  195. {
  196. MessageBox.Show($"Export failed, {reason}");
  197. return;
  198. }
  199. MessageBox.Show($"Export succeed, file save as {dlg.FileName}");
  200. }
  201. }
  202. catch (Exception ex)
  203. {
  204. LOG.Write(ex);
  205. MessageBox.Show("导出 Process Table发生错误", "导出失败");
  206. }
  207. }
  208. private void SaveStepsToTable(System.Data.DataSet ds)
  209. {
  210. ds.Tables.Add(new System.Data.DataTable("Process"));
  211. ds.Tables[0].Columns.Add("StartTime");
  212. ds.Tables[0].Columns.Add("EndTime");
  213. ds.Tables[0].Columns.Add("BatchId");
  214. ds.Tables[0].Columns.Add("JobName");
  215. ds.Tables[0].Columns.Add("RecipeName");
  216. ds.Tables[0].Columns.Add("LayoutName");
  217. ds.Tables[0].Columns.Add("Form");
  218. ds.Tables[0].Columns.Add("Wafer");
  219. foreach (var item in ProcessHistoryLots)
  220. {
  221. var row = ds.Tables[0].NewRow();
  222. row["BatchId"] = item.BatchID + " ";
  223. row["JobName"] = item.JobName;
  224. row["RecipeName"] = item.RecipeName;
  225. row["Form"] = item.Form;
  226. row["StartTime"] = item.StartTime;
  227. row["EndTime"] = item.EndTime;
  228. row["Wafer"] = item.WaferCount;
  229. row["LayoutName"] = item.LayoutName;
  230. ds.Tables[0].Rows.Add(row);
  231. }
  232. }
  233. public void SelectChangedHandle(object obj)
  234. {
  235. if (obj != null && obj is ProcessHistoryLot)
  236. {
  237. var item = obj as ProcessHistoryLot;
  238. item.IsChecked = true;
  239. }
  240. }
  241. void ProcessExport(object o)
  242. {
  243. var windowManager = IoC.Get<IWindowManager>();
  244. var param = ProcessHistoryLots.Where(x => x.IsChecked).ToList();
  245. object processExportViewModel = new ProcessExportAllViewModel<double>(param);
  246. (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(processExportViewModel, null, "Process Export");
  247. }
  248. //void DisplayGraph(object o)
  249. //{
  250. // ClientApp.Instance.SwitchPage("DataLog", "DataHistory", null);
  251. //}
  252. public void QueryLot(DateTime from, DateTime to, string key)
  253. {
  254. string sql = $"SELECT * FROM \"pj_data\" where \"start_time\" >= '{from:yyyy/MM/dd HH:mm:ss.fff}' and \"start_time\" <= '{to:yyyy/MM/dd HH:mm:ss.fff}' order by \"start_time\" DESC;";
  255. if (!string.IsNullOrWhiteSpace(key)) sql += string.Format(" and lower(\"batch_id\")='{0}' ", key.ToLower());
  256. DataTable dbData = QueryDataClient.Instance.Service.QueryData(sql);
  257. if (dbData != null && dbData.Rows.Count > 0)
  258. {
  259. for (int i = 0; i < dbData.Rows.Count; i++)
  260. {
  261. ProcessHistoryLot item = new ProcessHistoryLot();
  262. item.PjId = dbData.Rows[i]["guid"].ToString();
  263. item.BatchID = dbData.Rows[i]["batch_id"].ToString();
  264. item.JobName = dbData.Rows[i]["job_name"].ToString();
  265. item.RecipeName = dbData.Rows[i]["recipe_name"].ToString();
  266. item.LayoutName = dbData.Rows[i]["layout_name"].ToString();
  267. item.LayoutData = dbData.Rows[i]["layout_data"].ToString();
  268. item.WaferData = dbData.Rows[i]["wafer_data"].ToString();
  269. item.Form = dbData.Rows[i]["form"].ToString();
  270. if (!dbData.Rows[i]["total_wafer_count"].Equals(DBNull.Value))
  271. {
  272. item.WaferCount = (int)dbData.Rows[i]["total_wafer_count"];
  273. }
  274. if (!dbData.Rows[i]["start_time"].Equals(DBNull.Value))
  275. {
  276. item.StartTime = (DateTime)dbData.Rows[i]["start_time"];
  277. }
  278. if (!dbData.Rows[i]["end_time"].Equals(DBNull.Value))
  279. {
  280. item.EndTime = (DateTime)dbData.Rows[i]["end_time"];
  281. }
  282. ProcessHistoryLots.Add(item);
  283. }
  284. }
  285. }
  286. }
  287. public class ProcessHistoryLot : NotifiableItem
  288. {
  289. private bool _IsChecked;
  290. public bool IsChecked
  291. {
  292. get => _IsChecked;
  293. set
  294. {
  295. _IsChecked = value;
  296. InvokePropertyChanged(nameof(IsChecked));
  297. }
  298. }
  299. private string _pjId;
  300. public string PjId
  301. {
  302. get => _pjId;
  303. set
  304. {
  305. _pjId = value;
  306. InvokePropertyChanged(nameof(PjId));
  307. }
  308. }
  309. private string _batchID;
  310. public string BatchID
  311. {
  312. get => _batchID;
  313. set
  314. {
  315. _batchID = value;
  316. InvokePropertyChanged(nameof(BatchID));
  317. }
  318. }
  319. private string _jobName;
  320. public string JobName
  321. {
  322. get => _jobName;
  323. set
  324. {
  325. _jobName = value;
  326. InvokePropertyChanged(nameof(JobName));
  327. }
  328. }
  329. private string _recipeName;
  330. public string RecipeName
  331. {
  332. get => _recipeName;
  333. set
  334. {
  335. _recipeName = value;
  336. InvokePropertyChanged(nameof(RecipeName));
  337. }
  338. }
  339. private string _layoutName;
  340. public string LayoutName
  341. {
  342. get => _layoutName;
  343. set
  344. {
  345. _layoutName = value;
  346. InvokePropertyChanged(nameof(LayoutName));
  347. }
  348. }
  349. private string _layoutData;
  350. public string LayoutData
  351. {
  352. get => _layoutData;
  353. set
  354. {
  355. _layoutData = value;
  356. InvokePropertyChanged(nameof(LayoutData));
  357. }
  358. }
  359. private string _waferData;
  360. public string WaferData
  361. {
  362. get => _waferData;
  363. set
  364. {
  365. _waferData = value;
  366. InvokePropertyChanged(nameof(WaferData));
  367. }
  368. }
  369. private string _form;
  370. public string Form
  371. {
  372. get => _form;
  373. set
  374. {
  375. _form = value;
  376. InvokePropertyChanged(nameof(Form));
  377. }
  378. }
  379. private DateTime _startTime;
  380. public DateTime StartTime
  381. {
  382. get => _startTime;
  383. set
  384. {
  385. _startTime = value;
  386. InvokePropertyChanged(nameof(StartTime));
  387. }
  388. }
  389. private DateTime _endTime;
  390. public DateTime EndTime
  391. {
  392. get => _endTime;
  393. set
  394. {
  395. _endTime = value;
  396. InvokePropertyChanged(nameof(EndTime));
  397. }
  398. }
  399. private int _waferCount;
  400. public int WaferCount
  401. {
  402. get => _waferCount;
  403. set
  404. {
  405. _waferCount = value;
  406. InvokePropertyChanged(nameof(WaferCount));
  407. }
  408. }
  409. }
  410. }