using System; using System.IO; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using Aitex.Core.RT.Log; using Aitex.Core.UI.ControlDataContext; using Aitex.Core.Utilities; using Aitex.Sorter.Common; using VirgoUI.Client.Models.Sys; using MECF.Framework.Common.DataCenter; using OpenSEMI.ClientBase; using ExcelLibrary.SpreadSheet; using MECF.Framework.Common.CommonData; namespace VirgoUI.Client.Models.DataLog.ProcessHistory { public class ProcessHistoryViewModel : BaseModel { public DateTime BeginDate { get; set; } public DateTime UIBeginTime { get; set; } public DateTime EndDate { get; set; } public DateTime UIEndTime { get; set; } public bool EnableExportList { get; set; } public bool EnableExportData { get; set; } public ObservableCollection ProcessList { get; set; } private HistoryProcessData selectedProcessData; public HistoryProcessData SelectedProcessData { get { return this.selectedProcessData; } set { this.selectedProcessData = value; this.UpdateData(this.selectedProcessData); if (this.selectedProcessData != null) { this.EnableExportData = true; this.NotifyOfPropertyChange("EnableExportData"); } else { this.EnableExportData = false; this.NotifyOfPropertyChange("EnableExportData"); } } } public ProcessDataChartDataItem ProcessChartData { get; set; } public List ProcessData { get; set; } public ProcessHistoryViewModel() { DisplayName = "Process History"; ProcessChartData = new ProcessDataChartDataItem(60000); this.EnableExportData = false; this.EnableExportList = false; this.NotifyOfPropertyChange("EnableExportData"); this.NotifyOfPropertyChange("EnableExportList"); } protected override void OnInitialize() { base.OnInitialize(); var now = DateTime.Now; EndDate = now; BeginDate = now; UIBeginTime = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, 0); UIEndTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999); } protected override void OnActivate() { base.OnActivate(); } public void UpdateRecipeList(DateTime from, DateTime to) { //ObservableCollection lst = new ObservableCollection(); //List result = Triton160UiSystem.Instance.WCF.Query.GetHistoryRecipeList(from, to); //if (result != null) // result.ForEach(lst.Add); //RecipeList = lst; //InvokePropertyChanged("RecipeList"); } public void UpdateProcessList(DateTime begin, DateTime end) { try { string sql = string.Format( "SELECT * FROM \"process_data\" where \"process_begin_time\" >= '{0}' and \"process_begin_time\" <= '{1}' order by \"process_begin_time\" ASC;", begin.ToString("yyyy/MM/dd HH:mm:ss.fff"), end.ToString("yyyy/MM/dd HH:mm:ss.fff")); List result = QueryDataClient.Instance.Service.QueryDBProcess(sql); Application.Current.Dispatcher.BeginInvoke(new Action(() => { this.ProcessList = new ObservableCollection(); if (result != null) { foreach (HistoryProcessData ev in result) { this.ProcessList.Add(ev); } } if (this.ProcessList.Count>0) { this.EnableExportList = true; this.NotifyOfPropertyChange("EnableExportList"); } else { this.EnableExportList = false; this.NotifyOfPropertyChange("EnableExportList"); } NotifyOfPropertyChange("ProcessList"); })); } catch (Exception e) { LOG.Write(e); } } public void UpdateData(HistoryProcessData dataLog) { if (dataLog == null) { ProcessChartData.ClearData(); return; } List keys = new List(); //Application.Current.Dispatcher.Invoke(new Action(() => //{ // Dictionary> dicItems = GetDataElements(CultureSupported.English); // keys = dicItems.Keys.ToList(); // ProcessChartData.UpdateColumns(dicItems); //})); ProcessData = QueryDataClient.Instance.Service.GetHistoryData(keys, dataLog.Guid , "Data"); Application.Current.Dispatcher.Invoke(new Action(() => { ProcessChartData.SetInfo(dataLog.RecipeName); ProcessChartData.UpdateData(ProcessData); })); } public void ExportLotList() { ObservableCollection DataLogList = this.ProcessList; if (DataLogList == null ||DataLogList.Count==0) { return; } Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.DefaultExt = ".xls"; // Default file extension dlg.Filter = "数据表格文件|*.xls"; // Filter files by extension dlg.FileName = string.Format("LotList{0}", BeginTime.ToString("yyyyMMdd")); Nullable result = dlg.ShowDialog();// Show open file dialog box if (result != true) // Process open file dialog box results return; if (File.Exists(dlg.FileName)) { File.Delete(dlg.FileName); } Workbook workbook = new Workbook(); Worksheet worksheet = new Worksheet(BeginTime.ToString("yyyyMMdd")); int col = 0; worksheet.Cells[0, col++] = new Cell("RecipeName"); worksheet.Cells[0, col++] = new Cell("ProcessBeginTime"); worksheet.Cells[0, col++] = new Cell("ProcessEndTime"); worksheet.Cells[0, col++] = new Cell("RecipeResult"); for (int i = 0; i < DataLogList.Count; i++) { int colCount = 0; worksheet.Cells[i + 1, colCount++] = new Cell(DataLogList[i].RecipeName); worksheet.Cells[i + 1, colCount++] = new Cell(DataLogList[i].StartTime); worksheet.Cells[i + 1, colCount++] = new Cell(DataLogList[i].EndTime); worksheet.Cells[i + 1, colCount++] = new Cell(DataLogList[i].Result); } workbook.Worksheets.Add(worksheet); workbook.Save(dlg.FileName); } public void ExportLotDetails() { if (this.SelectedProcessData == null) return; HistoryProcessData log = this.SelectedProcessData; if (log == null) { MessageBox.Show("No Data Selected, Please Select Lot From The Lot List"); return; } Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.DefaultExt = ".xls"; // Default file extension dlg.Filter = "数据表格文件|*.xls"; // Filter files by extension dlg.FileName = string.Format("{0}", log.RecipeName); Nullable result = dlg.ShowDialog();// Show open file dialog box if (result != true) // Process open file dialog box results return; if (File.Exists(dlg.FileName)) { File.Delete(dlg.FileName); } Workbook workbook = new Workbook(); Worksheet worksheet = new Worksheet(log.RecipeName); Dictionary colIndex = new Dictionary(); Dictionary rowIndex = new Dictionary(); int colCount = 0; int rowCount = 0; foreach (var item in this.ProcessData) { if (!rowIndex.ContainsKey(item.dateTime)) { rowCount++; rowIndex[item.dateTime] = rowCount; worksheet.Cells[rowCount, 0] = new Cell(item.dateTime); } if (!colIndex.ContainsKey(item.dbName)) { colCount++; colIndex[item.dbName] = colCount; worksheet.Cells[0, colCount] = new Cell(item.dbName); } worksheet.Cells[rowIndex[item.dateTime], colIndex[item.dbName]] = new Cell(item.value); } workbook.Worksheets.Add(worksheet); workbook.Save(dlg.FileName); } public void Query() { this.UpdateProcessList(BeginTime, EndTime); } private DateTime BeginTime { get { return new DateTime(BeginDate.Year, BeginDate.Month, BeginDate.Day, UIBeginTime.Hour, UIBeginTime.Minute, UIBeginTime.Second); } } private DateTime EndTime { get { return new DateTime(EndDate.Year, EndDate.Month, EndDate.Day, UIEndTime.Hour, UIEndTime.Minute, UIEndTime.Second, 999); } } } }