using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using TeamAAS_VP.Controls; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Models; using TeamAAS_VP.Services; namespace TeamAAS_VP.ViewModels.Statistics { public class PhotoCaptureQueryViewModel : BindableBase { private readonly ISystemDatabaseService _systemDatabaseService; private readonly IProductService _productService; private readonly IConfigService _configService; public PhotoCaptureQueryViewModel(ISystemDatabaseService systemDatabaseService, IProductService productService, IConfigService configService) { _systemDatabaseService = systemDatabaseService; _productService = productService; _configService = configService; PageSize = 20; PageIndex = 1; StartDate = DateTime.Now.Date; EndDate = DateTime.Now.Date.AddDays(1); SearchCommand = new DelegateCommand(async () => await ExecuteSearchCommand()); PrevPageCommand = new DelegateCommand(async () => { if (PageIndex > 1) { PageIndex--; await ExecuteSearchCommand(); } }); NextPageCommand = new DelegateCommand(async () => { if (PageIndex * PageSize < TotalCount) { PageIndex++; await ExecuteSearchCommand(); } }); ExportCommand = new DelegateCommand(async () => await ExecuteExportCommand()); } private ObservableCollection _AllProduct; public ObservableCollection AllProduct { get { return _AllProduct; } set { SetProperty(ref _AllProduct, value); } } private ProductModel _SelectedProduct; public ProductModel SelectedProduct { get { return _SelectedProduct; } set { SetProperty(ref _SelectedProduct, value); } } private ObservableCollection _AllCamera; public ObservableCollection AllCamera { get { return _AllCamera; } set { SetProperty(ref _AllCamera, value); } } private CameraInfo _SelectedCamera; public CameraInfo SelectedCamera { get { return _SelectedCamera; } set { SetProperty(ref _SelectedCamera, value); } } private string _ProductSN; public string ProductSN { get { return _ProductSN; } set { SetProperty(ref _ProductSN, value); } } private DateTime? _StartDate; public DateTime? StartDate { get { return _StartDate; } set { SetProperty(ref _StartDate, value); } } private DateTime? _EndDate; public DateTime? EndDate { get { return _EndDate; } set { SetProperty(ref _EndDate, value); } } private ObservableCollection _Results; public ObservableCollection Results { get { return _Results; } set { SetProperty(ref _Results, value); } } private int _PageIndex; public int PageIndex { get { return _PageIndex; } set { SetProperty(ref _PageIndex, value); } } private int _PageSize; public int PageSize { get { return _PageSize; } set { SetProperty(ref _PageSize, value); } } private int _TotalCount; public int TotalCount { get { return _TotalCount; } set { SetProperty(ref _TotalCount, value); } } public DelegateCommand SearchCommand { get; private set; } public DelegateCommand PrevPageCommand { get; private set; } public DelegateCommand NextPageCommand { get; private set; } public DelegateCommand ExportCommand { get; private set; } private DelegateCommand _LoadedCommand; public DelegateCommand LoadedCommand => _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand)); void ExecuteLoadedCommand() { AllProduct = new ObservableCollection(_productService.GetAllProducts()); AllCamera = new ObservableCollection(_configService.GetAllCameras()); } private async Task ExecuteSearchCommand() { try { var start = StartDate.HasValue ? StartDate.Value : (DateTime?)null; var end = EndDate.HasValue ? EndDate.Value : (DateTime?)null; var res = await _systemDatabaseService.QueryPhotoCapturesAsync(SelectedProduct?.Name, SelectedCamera?.CameraName, ProductSN, start, end); Results = new ObservableCollection(res); TotalCount = Results.Count; } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } private async Task ExecuteExportCommand() { try { var start = StartDate.HasValue ? StartDate.Value : (DateTime?)null; var end = EndDate.HasValue ? EndDate.Value : (DateTime?)null; var all = await _systemDatabaseService.QueryPhotoCapturesAsync(SelectedProduct?.Name, SelectedCamera?.CameraName, ProductSN, start, end); if (all == null || !all.Any()) { MessageBox.Show("没有找到符合条件的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } var dlg = new Microsoft.Win32.SaveFileDialog { FileName = $"PhotoCapture_{DateTime.Now:yyyyMMddHHmmss}.csv", DefaultExt = ".csv", Filter = "CSV Files (*.csv)|*.csv" }; if (dlg.ShowDialog() != true) return; var path = dlg.FileName; using (var sw = new StreamWriter(path, false, Encoding.UTF8)) { sw.WriteLine("Id,CaptureTime,RecipeName,ProcessName,CameraName,ProductSN,IsSuccess,RobotPosition,PixelPosition,AbsolutePosition,ImagePath,OperatorName,Remarks"); foreach (var r in all) { sw.WriteLine(string.Join(",", EscapeCsv(r.Id.ToString()), EscapeCsv(r.CaptureTime.ToString("yyyy-MM-dd HH:mm:ss")), EscapeCsv(r.RecipeName), EscapeCsv(r.ProcessName), EscapeCsv(r.CameraName), EscapeCsv(r.ProductSN), EscapeCsv(r.IsSuccess ? "1" : "0"), EscapeCsv(r.RobotPosition), EscapeCsv(r.PixelPosition), EscapeCsv(r.AbsolutePosition), EscapeCsv(r.ImagePath), EscapeCsv(r.OperatorName), EscapeCsv(r.Remarks) )); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } private string EscapeCsv(string s) { if (s == null) return ""; if (s.Contains(",") || s.Contains("\"") || s.Contains("\r") || s.Contains("\n")) { return "\"" + s.Replace("\"", "\"\"") + "\""; } return s; } } }