| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- 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.Interfaces;
- using TeamAAS_VP.Models;
- namespace TeamAAS_VP.ViewModels.Statistics
- {
- public class NozzleChangeRecordQueryViewModel : BindableBase
- {
- private readonly ISystemDatabaseService _systemDatabaseService;
- public NozzleChangeRecordQueryViewModel(ISystemDatabaseService systemDatabaseService)
- {
- _systemDatabaseService = systemDatabaseService;
- 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 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 string _NozzleBatchNumber;
- public string NozzleBatchNumber
- {
- get { return _NozzleBatchNumber; }
- set { SetProperty(ref _NozzleBatchNumber, value); }
- }
- private ObservableCollection<NozzleChangeRecord> _Results;
- public ObservableCollection<NozzleChangeRecord> 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 async Task ExecuteSearchCommand()
- {
- try
- {
- var start = StartDate.HasValue ? StartDate.Value : (DateTime?)null;
- var end = EndDate.HasValue ? EndDate.Value : (DateTime?)null;
- var res = await _systemDatabaseService.QueryNozzleChangeRecordsPagedAsync(start, end, NozzleBatchNumber, PageIndex, PageSize);
- Results = new ObservableCollection<NozzleChangeRecord>(res.Items);
- TotalCount = res.TotalCount;
- }
- 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.QueryNozzleChangeRecordsAsync(start, end, NozzleBatchNumber);
- if (all == null || !all.Any()) { MessageBox.Show("没有找到符合条件的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information); return; }
- var dlg = new Microsoft.Win32.SaveFileDialog { FileName = $"NozzleChange_{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("ChangeTime,NozzleBatchNumber,CurrentLockCount,OperatorName");
- foreach (var r in all)
- {
- sw.WriteLine($"{r.ChangeTime:yyyy-MM-dd HH:mm:ss},{r.NozzleBatchNumber},{r.CurrentLockCount},{r.OperatorName}");
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- }
- }
|