using Microsoft.EntityFrameworkCore; using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; namespace MembranePoreTester.Views { public partial class HistoryWindow : Window { public int? SelectedStation { get; set; } // 传入当前工位ID public HistoryWindow() { InitializeComponent(); } private void Query_Click(object sender, RoutedEventArgs e) { using var db = new AppDbContext(); var query = new List(); // 获取泡点法记录 var bubblePoints = db.BubblePointRecords .Select(b => new { b.Id, b.StationId, Type = "泡点法", b.TestDate, b.SampleType, b.SampleSpec, b.Tester, Result = $"最大孔径: {b.MaxPoreSize:F3} μm" }); query.AddRange(bubblePoints); // 获取孔分布记录 var poreDistributions = db.PoreDistributionRecords .Select(p => new { p.Id, p.StationId, Type = "孔分布", p.TestDate, p.SampleType, p.SampleSpec, p.Tester, Result = $"平均孔径: {p.AveragePoreSize:F3} μm" }); query.AddRange(poreDistributions); // 过滤工位 if (cmbStation.SelectedIndex > 0) { int station = cmbStation.SelectedIndex; query = query.Where(x => ((dynamic)x).StationId == station).ToList(); } // 过滤类型 if (cmbType.SelectedIndex == 1) query = query.Where(x => ((dynamic)x).Type == "泡点法").ToList(); else if (cmbType.SelectedIndex == 2) query = query.Where(x => ((dynamic)x).Type == "孔分布").ToList(); // 过滤日期范围 if (dpStart.SelectedDate != null) query = query.Where(x => ((dynamic)x).TestDate >= dpStart.SelectedDate).ToList(); if (dpEnd.SelectedDate != null) query = query.Where(x => ((dynamic)x).TestDate <= dpEnd.SelectedDate.Value.AddDays(1)).ToList(); dgHistory.ItemsSource = query.OrderByDescending(x => ((dynamic)x).TestDate).ToList(); } private void ExportSelected_Click(object sender, RoutedEventArgs e) { if (dgHistory.SelectedItem == null) { MessageBox.Show("请先选中一条记录"); return; } dynamic selected = dgHistory.SelectedItem; int id = selected.Id; string type = selected.Type; var saveFileDialog = new SaveFileDialog { Filter = "Excel文件|*.xlsx", FileName = $"{type}_{id}.xlsx" }; if (saveFileDialog.ShowDialog() == true) { if (type == "泡点法") { using var db = new AppDbContext(); var entity = db.BubblePointRecords.Find(id); if (entity != null) { ExportHelper.ExportBubblePoint(entity, saveFileDialog.FileName); } } else { using var db = new AppDbContext(); var entity = db.PoreDistributionRecords .Include(p => p.DataPoints) .FirstOrDefault(p => p.Id == id); if (entity != null) { ExportHelper.ExportPoreDistribution(entity, saveFileDialog.FileName); } } } } private void LoadToCurrentStation_Click(object sender, RoutedEventArgs e) { if (dgHistory.SelectedItem == null) return; if (SelectedStation == null) { MessageBox.Show("未指定当前工位"); return; } dynamic selected = dgHistory.SelectedItem; int id = selected.Id; string type = selected.Type; // 通过事件或回调将记录加载到当前工位 // 这里需要与主窗口交互,简单起见,我们使用静态事件 LoadRecordEvent?.Invoke(this, new LoadRecordEventArgs { RecordId = id, Type = type, TargetStation = SelectedStation.Value }); DialogResult = true; Close(); } private void Close_Click(object sender, RoutedEventArgs e) => Close(); public static event EventHandler LoadRecordEvent; private void Window_Loaded(object sender, RoutedEventArgs e) { Query_Click(sender, e); } } public class LoadRecordEventArgs : EventArgs { public int RecordId { get; set; } public string Type { get; set; } public int TargetStation { get; set; } } }