DataLogView.xaml.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using Aitex.Triton160.Common;
  16. using Aitex.Triton160.UI.ViewModel;
  17. using DataAnalysisControl.Core;
  18. using Aitex.Core.Util;
  19. using ExcelLibrary.SpreadSheet;
  20. using Triton160.UI.ViewModel;
  21. namespace Aitex.Triton160.UI.Views
  22. {
  23. /// <summary>
  24. /// Interaction logic for DataLogView.xaml
  25. /// </summary>
  26. public partial class DataLogView : UserControl
  27. {
  28. private DataLogViewModel _viewModel;
  29. private DateTime BeginTime
  30. {
  31. get
  32. {
  33. var BeginDate = dateBegin.SelectedDate.HasValue ? dateBegin.SelectedDate.Value : DateTime.Now;
  34. return new DateTime(BeginDate.Year, BeginDate.Month, BeginDate.Day,
  35. timeBegin.Value.Value.Hour, timeBegin.Value.Value.Minute, timeBegin.Value.Value.Second);
  36. }
  37. }
  38. private DateTime EndTime
  39. {
  40. get
  41. {
  42. var EndDate = dateEnd.SelectedDate.HasValue ? dateEnd.SelectedDate.Value : DateTime.Now;
  43. return new DateTime(EndDate.Year, EndDate.Month, EndDate.Day,
  44. timeEnd.Value.Value.Hour, timeEnd.Value.Value.Minute, timeEnd.Value.Value.Second, 999);
  45. }
  46. }
  47. public DataLogView()
  48. {
  49. InitializeComponent();
  50. _viewModel = new DataLogViewModel();
  51. DataContext = _viewModel;
  52. var now = DateTime.Now;
  53. dateBegin.DisplayDate = now - new TimeSpan(1, 0, 0, 0);
  54. dateEnd.DisplayDate = now;
  55. dateBegin.SelectedDate = now;
  56. dateEnd.SelectedDate = now;
  57. timeBegin.Value = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, 0);
  58. timeEnd.Value = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999);
  59. dataGrid_LotList.CanUserAddRows = false;
  60. _viewModel.UpdateRecipeList(BeginTime, EndTime);
  61. IsVisibleChanged += DataLogView_IsVisibleChanged;
  62. }
  63. void DataLogView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  64. {
  65. if (!IsLoaded)
  66. return;
  67. if (IsVisible)
  68. _viewModel.UpdateRecipeList(BeginTime, EndTime);
  69. }
  70. private void buttonQuery_Click(object sender, RoutedEventArgs e)
  71. {
  72. _viewModel.UpdateDataLogList(BeginTime, EndTime, recipeName.Text,LotTextBox.BarcodeText.Trim());
  73. List<DataLogItem> DataLogList = _viewModel.DataLogList;
  74. ButtonExportList.IsEnabled = DataLogList != null && DataLogList.Count > 0;
  75. }
  76. private void dateBegin_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
  77. {
  78. if (!IsLoaded)
  79. return;
  80. _viewModel.UpdateRecipeList(BeginTime, EndTime);
  81. }
  82. private void timeBegin_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  83. {
  84. if (!IsLoaded)
  85. return;
  86. _viewModel.UpdateRecipeList(BeginTime, EndTime);
  87. }
  88. private void dateEnd_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
  89. {
  90. if (!IsLoaded)
  91. return;
  92. _viewModel.UpdateRecipeList(BeginTime, EndTime);
  93. }
  94. private void timeEnd_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  95. {
  96. if (!IsLoaded)
  97. return;
  98. _viewModel.UpdateRecipeList(BeginTime, EndTime);
  99. }
  100. private void dataGrid_DataLogList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  101. {
  102. DataLogItem log = (dataGrid_LotList.SelectedItem) as DataLogItem;
  103. _viewModel.UpdateData(log );
  104. ButtonExportData.IsEnabled = log != null ;
  105. }
  106. private void buttonLotListExport_Click(object sender, RoutedEventArgs e)
  107. {
  108. List<DataLogItem> DataLogList = _viewModel.DataLogList;
  109. if (DataLogList == null ||DataLogList.Count==0)
  110. {
  111. return;
  112. }
  113. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  114. dlg.DefaultExt = ".xls"; // Default file extension
  115. dlg.Filter = "数据表格文件|*.xls"; // Filter files by extension
  116. dlg.FileName = string.Format("LotList{0}", BeginTime.ToString("yyyyMMdd"));
  117. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  118. if (result != true) // Process open file dialog box results
  119. return;
  120. if (File.Exists(dlg.FileName))
  121. {
  122. File.Delete(dlg.FileName);
  123. }
  124. Workbook workbook = new Workbook();
  125. Worksheet worksheet = new Worksheet(BeginTime.ToString("yyyyMMdd"));
  126. int col = 0;
  127. worksheet.Cells[0, col++] = new Cell(Aitex.Triton160.UI.Properties.Resources.LotName);
  128. worksheet.Cells[0, col++] = new Cell(Aitex.Triton160.UI.Properties.Resources.RecipeName);
  129. worksheet.Cells[0, col++] = new Cell(Aitex.Triton160.UI.Properties.Resources.ProcessBeginTime);
  130. worksheet.Cells[0, col++] = new Cell(Aitex.Triton160.UI.Properties.Resources.ProcessEndTime);
  131. worksheet.Cells[0, col++] = new Cell(Aitex.Triton160.UI.Properties.Resources.RecipeResult);
  132. for (int i=0; i<DataLogList.Count;i++)
  133. {
  134. int colCount = 0;
  135. worksheet.Cells[i+1, colCount++] = new Cell(DataLogList[i].LotName);
  136. worksheet.Cells[i+1, colCount++] = new Cell(DataLogList[i].RecipeName);
  137. worksheet.Cells[i+1, colCount++] = new Cell(DataLogList[i].ProcessBeginTime);
  138. worksheet.Cells[i+1, colCount++] = new Cell(DataLogList[i].ProcessEndTime);
  139. worksheet.Cells[i+1, colCount++] = new Cell(DataLogList[i].RecipeResult);
  140. }
  141. workbook.Worksheets.Add(worksheet);
  142. workbook.Save(dlg.FileName);
  143. }
  144. private void buttonLotDetailsExport_Click(object sender, RoutedEventArgs e)
  145. {
  146. DataLogItem log = (dataGrid_LotList.SelectedItem) as DataLogItem;
  147. if (log == null)
  148. {
  149. MessageBox.Show(Aitex.Triton160.UI.Properties.Resources.NoDataSelectedPleaseSelectLotFromTheLotList);
  150. return;
  151. }
  152. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  153. dlg.DefaultExt = ".xls"; // Default file extension
  154. dlg.Filter = "数据表格文件|*.xls"; // Filter files by extension
  155. dlg.FileName = string.Format("{0}", log.DataLogName);
  156. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  157. if (result != true) // Process open file dialog box results
  158. return;
  159. if (File.Exists(dlg.FileName))
  160. {
  161. File.Delete(dlg.FileName);
  162. }
  163. Workbook workbook = new Workbook();
  164. Worksheet worksheet = new Worksheet(log.DataLogName);
  165. Dictionary<string, int> colIndex = new Dictionary<string, int>();
  166. Dictionary<DateTime, int> rowIndex = new Dictionary<DateTime, int>();
  167. int colCount = 0;
  168. int rowCount = 0;
  169. foreach (var item in _viewModel.ProcessData)
  170. {
  171. if (!rowIndex.ContainsKey(item.dateTime))
  172. {
  173. rowCount++;
  174. rowIndex[item.dateTime] = rowCount;
  175. worksheet.Cells[rowCount, 0] = new Cell(item.dateTime);
  176. }
  177. if (!colIndex.ContainsKey(item.dbName))
  178. {
  179. colCount++;
  180. colIndex[item.dbName] = colCount;
  181. worksheet.Cells[0, colCount] = new Cell(item.dbName);
  182. }
  183. worksheet.Cells[rowIndex[item.dateTime], colIndex[item.dbName]] = new Cell(item.value);
  184. }
  185. workbook.Worksheets.Add(worksheet);
  186. workbook.Save(dlg.FileName);
  187. }
  188. }
  189. }