ProcessHistoryTwoViewModel.cs 15 KB

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