ProcessHistoryTwoViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. if (ProcessHistoryLots != null && ProcessHistoryLots.FirstOrDefault() != null)
  95. {
  96. ProcessHistoryLots.FirstOrDefault().IsChecked = true;
  97. }
  98. }
  99. void InitTime()
  100. {
  101. SearchBeginTime = DateTime.Now.Date;
  102. SearchEndTime = DateTime.Now.Date.AddDays(1).Date;
  103. }
  104. void QueryLots(object e)
  105. {
  106. this.SearchBeginTime = Convert.ToDateTime(this.view.wfTimeFrom.Content);
  107. this.SearchEndTime = Convert.ToDateTime(this.view.wfTimeTo.Content);
  108. if (SearchBeginTime > SearchEndTime)
  109. {
  110. MessageBox.Show("Time range invalid, start time should be early than end time");
  111. return;
  112. }
  113. ProcessHistoryLots.Clear();
  114. QueryLot(SearchBeginTime, SearchEndTime, KeyWord);//.OrderByDescending(lot => lot.StartTime).ToArray();
  115. }
  116. void BatchDetail(object o)
  117. {
  118. if (ProcessHistoryLotSelectIndex != -1 && ProcessHistoryLots.Count > 0)
  119. {
  120. DateTime startTime = ProcessHistoryLots[ProcessHistoryLotSelectIndex].StartTime;
  121. DateTime endTime = ProcessHistoryLots[ProcessHistoryLotSelectIndex].EndTime;
  122. var batchID = ProcessHistoryLots[ProcessHistoryLotSelectIndex].BatchID;
  123. List<string> layoutData = ProcessHistoryLots[ProcessHistoryLotSelectIndex].LayoutData.Split(',').ToList();
  124. List<string> waferData = ProcessHistoryLots[ProcessHistoryLotSelectIndex].WaferData.Split(',').ToList();
  125. var windowManager = IoC.Get<IWindowManager>();
  126. BatchDetailViewModel batchDetailViewModel = new BatchDetailViewModel(startTime, endTime, batchID, layoutData, layoutData.Count, waferData);
  127. (windowManager as WindowManager)?.ShowDialogWithTitle(batchDetailViewModel, null, "Batch Detail");
  128. }
  129. else
  130. {
  131. var windowManager = IoC.Get<IWindowManager>();
  132. BatchDetailViewModel batchDetailViewModel = new BatchDetailViewModel();
  133. (windowManager as WindowManager)?.ShowDialogWithTitle(batchDetailViewModel, null, "Batch Detail");
  134. }
  135. }
  136. public void SelectDate(string SelectType)
  137. {
  138. var windowManager = Caliburn.Micro.Core.IoC.Get<Caliburn.Micro.IWindowManager>();
  139. if (SelectType == "Start")
  140. {
  141. this.SearchBeginTime = Convert.ToDateTime(this.view.wfTimeFrom.Content);
  142. SelectDateViewModel selectdateViewModel = new SelectDateViewModel(SearchBeginTime);
  143. var result = (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(selectdateViewModel, null, "Start Time");
  144. if (result == true)
  145. {
  146. this.view.wfTimeFrom.Content = selectdateViewModel.SearchDate;
  147. }
  148. }
  149. else
  150. {
  151. this.SearchEndTime = Convert.ToDateTime(this.view.wfTimeTo.Content);
  152. SelectDateViewModel selectdateViewModel = new SelectDateViewModel(SearchEndTime);
  153. var result = (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(selectdateViewModel, null, "End Time");
  154. if (result == true)
  155. {
  156. this.view.wfTimeTo.Content = selectdateViewModel.SearchDate;
  157. }
  158. }
  159. }
  160. void ProcessDetail(object o)
  161. {
  162. //if (ProcessHistoryLots.Count > 0)
  163. //{
  164. var windowManager = IoC.Get<IWindowManager>();
  165. var param = ProcessHistoryLots.Where(x => x.IsChecked).ToList();
  166. object processDetailViewModel = new ProcessDetailViewModel<double>(param);
  167. (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(processDetailViewModel, null, "Process Detail");
  168. //}
  169. }
  170. void ProcessJobInfo(object o)
  171. {
  172. var windowManager = IoC.Get<IWindowManager>();
  173. var param = ProcessHistoryLots.Where(x => x.IsChecked).FirstOrDefault();
  174. ProcessDetailV2ViewModel processDetailViewModel = new ProcessDetailV2ViewModel();
  175. processDetailViewModel.PJInfo = param;
  176. processDetailViewModel.TableCount = ProcessHistoryLots.Count;
  177. processDetailViewModel.SelectItemIndex = ProcessHistoryLots.IndexOf(param) + 1;
  178. (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(processDetailViewModel, null, "ProcessJob Info");
  179. }
  180. void ExportProcessTable(object o)
  181. {
  182. try
  183. {
  184. if (ProcessHistoryLots == null || ProcessHistoryLots.Count == 0)
  185. {
  186. MessageBox.Show($"Table is null");
  187. }
  188. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  189. dlg.DefaultExt = ".xlsx"; // Default file extension
  190. dlg.FileName = $"Process_{SearchBeginTime.ToString("yyyy_MM_dd_HH_mm_ss")}~{SearchEndTime.ToString("yyyy_MM_dd_HH_mm_ss")}";
  191. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  192. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  193. if (result == true) // Process open file dialog box results
  194. {
  195. System.Data.DataSet ds = new System.Data.DataSet();
  196. SaveStepsToTable(ds);
  197. if (!ExcelHelper.ExportToExcel(dlg.FileName, ds, out string reason, true, false))
  198. {
  199. MessageBox.Show($"Export failed, {reason}");
  200. return;
  201. }
  202. MessageBox.Show($"Export succeed, file save as {dlg.FileName}");
  203. }
  204. }
  205. catch (Exception ex)
  206. {
  207. LOG.Write(ex);
  208. MessageBox.Show("导出 Process Table发生错误", "导出失败");
  209. }
  210. }
  211. private void SaveStepsToTable(System.Data.DataSet ds)
  212. {
  213. ds.Tables.Add(new System.Data.DataTable("Process"));
  214. ds.Tables[0].Columns.Add("StartTime");
  215. ds.Tables[0].Columns.Add("EndTime");
  216. ds.Tables[0].Columns.Add("BatchId");
  217. ds.Tables[0].Columns.Add("JobName");
  218. ds.Tables[0].Columns.Add("RecipeName");
  219. ds.Tables[0].Columns.Add("LayoutName");
  220. ds.Tables[0].Columns.Add("Form");
  221. ds.Tables[0].Columns.Add("Wafer");
  222. foreach (var item in ProcessHistoryLots)
  223. {
  224. var row = ds.Tables[0].NewRow();
  225. row["BatchId"] = item.BatchID + " ";
  226. row["JobName"] = item.JobName;
  227. row["RecipeName"] = item.RecipeName;
  228. row["Form"] = item.Form;
  229. row["StartTime"] = item.StartTime;
  230. row["EndTime"] = item.EndTime;
  231. row["Wafer"] = item.WaferCount;
  232. row["LayoutName"] = item.LayoutName;
  233. ds.Tables[0].Rows.Add(row);
  234. }
  235. }
  236. public void SelectChangedHandle(object obj)
  237. {
  238. if (obj != null && obj is ProcessHistoryLot)
  239. {
  240. var item = obj as ProcessHistoryLot;
  241. item.IsChecked = true;
  242. }
  243. }
  244. void ProcessExport(object o)
  245. {
  246. var windowManager = IoC.Get<IWindowManager>();
  247. var param = ProcessHistoryLots.Where(x => x.IsChecked).ToList();
  248. object processExportViewModel = new ProcessExportAllViewModel<double>(param);
  249. (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(processExportViewModel, null, "Process Export");
  250. }
  251. //void DisplayGraph(object o)
  252. //{
  253. // ClientApp.Instance.SwitchPage("DataLog", "DataHistory", null);
  254. //}
  255. public void QueryLot(DateTime from, DateTime to, string key)
  256. {
  257. 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;";
  258. if (!string.IsNullOrWhiteSpace(key)) sql += string.Format(" and lower(\"batch_id\")='{0}' ", key.ToLower());
  259. DataTable dbData = QueryDataClient.Instance.Service.QueryData(sql);
  260. if (dbData != null && dbData.Rows.Count > 0)
  261. {
  262. for (int i = 0; i < dbData.Rows.Count; i++)
  263. {
  264. ProcessHistoryLot item = new ProcessHistoryLot();
  265. item.PjId = dbData.Rows[i]["guid"].ToString();
  266. item.BatchID = dbData.Rows[i]["batch_id"].ToString();
  267. item.JobName = dbData.Rows[i]["job_name"].ToString();
  268. item.RecipeName = dbData.Rows[i]["recipe_name"].ToString();
  269. item.LayoutName = dbData.Rows[i]["layout_name"].ToString();
  270. item.LayoutData = dbData.Rows[i]["layout_data"].ToString();
  271. item.WaferData = dbData.Rows[i]["wafer_data"].ToString();
  272. item.Form = dbData.Rows[i]["form"].ToString();
  273. if (!dbData.Rows[i]["total_wafer_count"].Equals(DBNull.Value))
  274. {
  275. item.WaferCount = (int)dbData.Rows[i]["total_wafer_count"];
  276. }
  277. if (!dbData.Rows[i]["start_time"].Equals(DBNull.Value))
  278. {
  279. item.StartTime = (DateTime)dbData.Rows[i]["start_time"];
  280. }
  281. if (!dbData.Rows[i]["end_time"].Equals(DBNull.Value))
  282. {
  283. item.EndTime = (DateTime)dbData.Rows[i]["end_time"];
  284. }
  285. ProcessHistoryLots.Add(item);
  286. }
  287. }
  288. }
  289. }
  290. public class ProcessHistoryLot : NotifiableItem
  291. {
  292. private bool _IsChecked;
  293. public bool IsChecked
  294. {
  295. get => _IsChecked;
  296. set
  297. {
  298. _IsChecked = value;
  299. InvokePropertyChanged(nameof(IsChecked));
  300. }
  301. }
  302. private string _pjId;
  303. public string PjId
  304. {
  305. get => _pjId;
  306. set
  307. {
  308. _pjId = value;
  309. InvokePropertyChanged(nameof(PjId));
  310. }
  311. }
  312. private string _batchID;
  313. public string BatchID
  314. {
  315. get => _batchID;
  316. set
  317. {
  318. _batchID = value;
  319. InvokePropertyChanged(nameof(BatchID));
  320. }
  321. }
  322. private string _jobName;
  323. public string JobName
  324. {
  325. get => _jobName;
  326. set
  327. {
  328. _jobName = value;
  329. InvokePropertyChanged(nameof(JobName));
  330. }
  331. }
  332. private string _recipeName;
  333. public string RecipeName
  334. {
  335. get => _recipeName;
  336. set
  337. {
  338. _recipeName = value;
  339. InvokePropertyChanged(nameof(RecipeName));
  340. }
  341. }
  342. private string _layoutName;
  343. public string LayoutName
  344. {
  345. get => _layoutName;
  346. set
  347. {
  348. _layoutName = value;
  349. InvokePropertyChanged(nameof(LayoutName));
  350. }
  351. }
  352. private string _layoutData;
  353. public string LayoutData
  354. {
  355. get => _layoutData;
  356. set
  357. {
  358. _layoutData = value;
  359. InvokePropertyChanged(nameof(LayoutData));
  360. }
  361. }
  362. private string _waferData;
  363. public string WaferData
  364. {
  365. get => _waferData;
  366. set
  367. {
  368. _waferData = value;
  369. InvokePropertyChanged(nameof(WaferData));
  370. }
  371. }
  372. private string _form;
  373. public string Form
  374. {
  375. get => _form;
  376. set
  377. {
  378. _form = value;
  379. InvokePropertyChanged(nameof(Form));
  380. }
  381. }
  382. private DateTime _startTime;
  383. public DateTime StartTime
  384. {
  385. get => _startTime;
  386. set
  387. {
  388. _startTime = value;
  389. InvokePropertyChanged(nameof(StartTime));
  390. }
  391. }
  392. private DateTime _endTime;
  393. public DateTime EndTime
  394. {
  395. get => _endTime;
  396. set
  397. {
  398. _endTime = value;
  399. InvokePropertyChanged(nameof(EndTime));
  400. }
  401. }
  402. private int _waferCount;
  403. public int WaferCount
  404. {
  405. get => _waferCount;
  406. set
  407. {
  408. _waferCount = value;
  409. InvokePropertyChanged(nameof(WaferCount));
  410. }
  411. }
  412. }
  413. }