ProcessHistoryViewModel.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Windows;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.UI.ControlDataContext;
  9. using Aitex.Core.Utilities;
  10. using Aitex.Sorter.Common;
  11. using VirgoUI.Client.Models.Sys;
  12. using MECF.Framework.Common.DataCenter;
  13. using OpenSEMI.ClientBase;
  14. using ExcelLibrary.SpreadSheet;
  15. using MECF.Framework.Common.CommonData;
  16. namespace VirgoUI.Client.Models.DataLog.ProcessHistory
  17. {
  18. public class ProcessHistoryViewModel : BaseModel
  19. {
  20. public DateTime BeginDate { get; set; }
  21. public DateTime UIBeginTime { get; set; }
  22. public DateTime EndDate { get; set; }
  23. public DateTime UIEndTime { get; set; }
  24. public bool EnableExportList { get; set; }
  25. public bool EnableExportData { get; set; }
  26. public ObservableCollection<HistoryProcessData> ProcessList { get; set; }
  27. private HistoryProcessData selectedProcessData;
  28. public HistoryProcessData SelectedProcessData
  29. {
  30. get { return this.selectedProcessData; }
  31. set
  32. {
  33. this.selectedProcessData = value;
  34. this.UpdateData(this.selectedProcessData);
  35. if (this.selectedProcessData != null)
  36. {
  37. this.EnableExportData = true;
  38. this.NotifyOfPropertyChange("EnableExportData");
  39. }
  40. else
  41. {
  42. this.EnableExportData = false;
  43. this.NotifyOfPropertyChange("EnableExportData");
  44. }
  45. }
  46. }
  47. public ProcessDataChartDataItem ProcessChartData { get; set; }
  48. public List<HistoryDataItem> ProcessData { get; set; }
  49. public ProcessHistoryViewModel()
  50. {
  51. DisplayName = "Process History";
  52. ProcessChartData = new ProcessDataChartDataItem(60000);
  53. this.EnableExportData = false;
  54. this.EnableExportList = false;
  55. this.NotifyOfPropertyChange("EnableExportData");
  56. this.NotifyOfPropertyChange("EnableExportList");
  57. }
  58. protected override void OnInitialize()
  59. {
  60. base.OnInitialize();
  61. var now = DateTime.Now;
  62. EndDate = now;
  63. BeginDate = now;
  64. UIBeginTime = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, 0);
  65. UIEndTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999);
  66. }
  67. protected override void OnActivate()
  68. {
  69. base.OnActivate();
  70. }
  71. public void UpdateRecipeList(DateTime from, DateTime to)
  72. {
  73. //ObservableCollection<string> lst = new ObservableCollection<string>();
  74. //List<string> result = Triton160UiSystem.Instance.WCF.Query.GetHistoryRecipeList(from, to);
  75. //if (result != null)
  76. // result.ForEach(lst.Add);
  77. //RecipeList = lst;
  78. //InvokePropertyChanged("RecipeList");
  79. }
  80. public void UpdateProcessList(DateTime begin, DateTime end)
  81. {
  82. try
  83. {
  84. string sql = string.Format(
  85. "SELECT * FROM \"process_data\" where \"process_begin_time\" >= '{0}' and \"process_begin_time\" <= '{1}' order by \"process_begin_time\" ASC;",
  86. begin.ToString("yyyy/MM/dd HH:mm:ss.fff"), end.ToString("yyyy/MM/dd HH:mm:ss.fff"));
  87. List<HistoryProcessData> result = QueryDataClient.Instance.Service.QueryDBProcess(sql);
  88. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  89. {
  90. this.ProcessList = new ObservableCollection<HistoryProcessData>();
  91. if (result != null)
  92. {
  93. foreach (HistoryProcessData ev in result)
  94. {
  95. this.ProcessList.Add(ev);
  96. }
  97. }
  98. if (this.ProcessList.Count>0)
  99. {
  100. this.EnableExportList = true;
  101. this.NotifyOfPropertyChange("EnableExportList");
  102. }
  103. else
  104. {
  105. this.EnableExportList = false;
  106. this.NotifyOfPropertyChange("EnableExportList");
  107. }
  108. NotifyOfPropertyChange("ProcessList");
  109. }));
  110. }
  111. catch (Exception e)
  112. {
  113. LOG.Write(e);
  114. }
  115. }
  116. public void UpdateData(HistoryProcessData dataLog)
  117. {
  118. if (dataLog == null)
  119. {
  120. ProcessChartData.ClearData();
  121. return;
  122. }
  123. List<string> keys = new List<string>();
  124. //Application.Current.Dispatcher.Invoke(new Action(() =>
  125. //{
  126. // Dictionary<string, Tuple<string, string, bool>> dicItems = GetDataElements(CultureSupported.English);
  127. // keys = dicItems.Keys.ToList();
  128. // ProcessChartData.UpdateColumns(dicItems);
  129. //}));
  130. ProcessData = QueryDataClient.Instance.Service.GetHistoryData(keys, dataLog.Guid , "Data");
  131. Application.Current.Dispatcher.Invoke(new Action(() =>
  132. {
  133. ProcessChartData.SetInfo(dataLog.RecipeName);
  134. ProcessChartData.UpdateData(ProcessData);
  135. }));
  136. }
  137. public void ExportLotList()
  138. {
  139. ObservableCollection<HistoryProcessData> DataLogList = this.ProcessList;
  140. if (DataLogList == null ||DataLogList.Count==0)
  141. {
  142. return;
  143. }
  144. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  145. dlg.DefaultExt = ".xls"; // Default file extension
  146. dlg.Filter = "数据表格文件|*.xls"; // Filter files by extension
  147. dlg.FileName = string.Format("LotList{0}", BeginTime.ToString("yyyyMMdd"));
  148. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  149. if (result != true) // Process open file dialog box results
  150. return;
  151. if (File.Exists(dlg.FileName))
  152. {
  153. File.Delete(dlg.FileName);
  154. }
  155. Workbook workbook = new Workbook();
  156. Worksheet worksheet = new Worksheet(BeginTime.ToString("yyyyMMdd"));
  157. int col = 0;
  158. worksheet.Cells[0, col++] = new Cell("RecipeName");
  159. worksheet.Cells[0, col++] = new Cell("ProcessBeginTime");
  160. worksheet.Cells[0, col++] = new Cell("ProcessEndTime");
  161. worksheet.Cells[0, col++] = new Cell("RecipeResult");
  162. for (int i = 0; i < DataLogList.Count; i++)
  163. {
  164. int colCount = 0;
  165. worksheet.Cells[i + 1, colCount++] = new Cell(DataLogList[i].RecipeName);
  166. worksheet.Cells[i + 1, colCount++] = new Cell(DataLogList[i].StartTime);
  167. worksheet.Cells[i + 1, colCount++] = new Cell(DataLogList[i].EndTime);
  168. worksheet.Cells[i + 1, colCount++] = new Cell(DataLogList[i].Result);
  169. }
  170. workbook.Worksheets.Add(worksheet);
  171. workbook.Save(dlg.FileName);
  172. }
  173. public void ExportLotDetails()
  174. {
  175. if (this.SelectedProcessData == null)
  176. return;
  177. HistoryProcessData log = this.SelectedProcessData;
  178. if (log == null)
  179. {
  180. MessageBox.Show("No Data Selected, Please Select Lot From The Lot List");
  181. return;
  182. }
  183. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  184. dlg.DefaultExt = ".xls"; // Default file extension
  185. dlg.Filter = "数据表格文件|*.xls"; // Filter files by extension
  186. dlg.FileName = string.Format("{0}", log.RecipeName);
  187. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  188. if (result != true) // Process open file dialog box results
  189. return;
  190. if (File.Exists(dlg.FileName))
  191. {
  192. File.Delete(dlg.FileName);
  193. }
  194. Workbook workbook = new Workbook();
  195. Worksheet worksheet = new Worksheet(log.RecipeName);
  196. Dictionary<string, int> colIndex = new Dictionary<string, int>();
  197. Dictionary<DateTime, int> rowIndex = new Dictionary<DateTime, int>();
  198. int colCount = 0;
  199. int rowCount = 0;
  200. foreach (var item in this.ProcessData)
  201. {
  202. if (!rowIndex.ContainsKey(item.dateTime))
  203. {
  204. rowCount++;
  205. rowIndex[item.dateTime] = rowCount;
  206. worksheet.Cells[rowCount, 0] = new Cell(item.dateTime);
  207. }
  208. if (!colIndex.ContainsKey(item.dbName))
  209. {
  210. colCount++;
  211. colIndex[item.dbName] = colCount;
  212. worksheet.Cells[0, colCount] = new Cell(item.dbName);
  213. }
  214. worksheet.Cells[rowIndex[item.dateTime], colIndex[item.dbName]] = new Cell(item.value);
  215. }
  216. workbook.Worksheets.Add(worksheet);
  217. workbook.Save(dlg.FileName);
  218. }
  219. public void Query()
  220. {
  221. this.UpdateProcessList(BeginTime, EndTime);
  222. }
  223. private DateTime BeginTime
  224. {
  225. get
  226. {
  227. return new DateTime(BeginDate.Year, BeginDate.Month, BeginDate.Day,
  228. UIBeginTime.Hour, UIBeginTime.Minute, UIBeginTime.Second);
  229. }
  230. }
  231. private DateTime EndTime
  232. {
  233. get
  234. {
  235. return new DateTime(EndDate.Year, EndDate.Month, EndDate.Day,
  236. UIEndTime.Hour, UIEndTime.Minute, UIEndTime.Second, 999);
  237. }
  238. }
  239. }
  240. }