ProcessHistoryViewModel.cs 9.9 KB

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