PhotoCaptureQueryViewModel.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. using System;
  4. using System.Collections.ObjectModel;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using TeamAAS_VP.Controls;
  11. using TeamAAS_VP.Interfaces;
  12. using TeamAAS_VP.Models;
  13. using TeamAAS_VP.Services;
  14. namespace TeamAAS_VP.ViewModels.Statistics
  15. {
  16. public class PhotoCaptureQueryViewModel : BindableBase
  17. {
  18. private readonly ISystemDatabaseService _systemDatabaseService;
  19. private readonly IProductService _productService;
  20. private readonly IConfigService _configService;
  21. public PhotoCaptureQueryViewModel(ISystemDatabaseService systemDatabaseService, IProductService productService, IConfigService configService)
  22. {
  23. _systemDatabaseService = systemDatabaseService;
  24. _productService = productService;
  25. _configService = configService;
  26. PageSize = 20; PageIndex = 1;
  27. StartDate = DateTime.Now.Date; EndDate = DateTime.Now.Date.AddDays(1);
  28. SearchCommand = new DelegateCommand(async () => await ExecuteSearchCommand());
  29. PrevPageCommand = new DelegateCommand(async () => { if (PageIndex > 1) { PageIndex--; await ExecuteSearchCommand(); } });
  30. NextPageCommand = new DelegateCommand(async () => { if (PageIndex * PageSize < TotalCount) { PageIndex++; await ExecuteSearchCommand(); } });
  31. ExportCommand = new DelegateCommand(async () => await ExecuteExportCommand());
  32. }
  33. private ObservableCollection<ProductModel> _AllProduct;
  34. public ObservableCollection<ProductModel> AllProduct
  35. {
  36. get { return _AllProduct; }
  37. set { SetProperty(ref _AllProduct, value); }
  38. }
  39. private ProductModel _SelectedProduct;
  40. public ProductModel SelectedProduct
  41. {
  42. get { return _SelectedProduct; }
  43. set { SetProperty(ref _SelectedProduct, value); }
  44. }
  45. private ObservableCollection<CameraInfo> _AllCamera;
  46. public ObservableCollection<CameraInfo> AllCamera
  47. {
  48. get { return _AllCamera; }
  49. set { SetProperty(ref _AllCamera, value); }
  50. }
  51. private CameraInfo _SelectedCamera;
  52. public CameraInfo SelectedCamera
  53. {
  54. get { return _SelectedCamera; }
  55. set { SetProperty(ref _SelectedCamera, value); }
  56. }
  57. private string _ProductSN;
  58. public string ProductSN
  59. {
  60. get { return _ProductSN; }
  61. set { SetProperty(ref _ProductSN, value); }
  62. }
  63. private DateTime? _StartDate;
  64. public DateTime? StartDate
  65. {
  66. get { return _StartDate; }
  67. set { SetProperty(ref _StartDate, value); }
  68. }
  69. private DateTime? _EndDate;
  70. public DateTime? EndDate
  71. {
  72. get { return _EndDate; }
  73. set { SetProperty(ref _EndDate, value); }
  74. }
  75. private ObservableCollection<PhotoCaptureRecord> _Results;
  76. public ObservableCollection<PhotoCaptureRecord> Results
  77. {
  78. get { return _Results; }
  79. set { SetProperty(ref _Results, value); }
  80. }
  81. private int _PageIndex;
  82. public int PageIndex
  83. {
  84. get { return _PageIndex; }
  85. set { SetProperty(ref _PageIndex, value); }
  86. }
  87. private int _PageSize;
  88. public int PageSize
  89. {
  90. get { return _PageSize; }
  91. set { SetProperty(ref _PageSize, value); }
  92. }
  93. private int _TotalCount;
  94. public int TotalCount
  95. {
  96. get { return _TotalCount; }
  97. set { SetProperty(ref _TotalCount, value); }
  98. }
  99. public DelegateCommand SearchCommand { get; private set; }
  100. public DelegateCommand PrevPageCommand { get; private set; }
  101. public DelegateCommand NextPageCommand { get; private set; }
  102. public DelegateCommand ExportCommand { get; private set; }
  103. private DelegateCommand _LoadedCommand;
  104. public DelegateCommand LoadedCommand =>
  105. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  106. void ExecuteLoadedCommand()
  107. {
  108. AllProduct = new ObservableCollection<ProductModel>(_productService.GetAllProducts());
  109. AllCamera = new ObservableCollection<CameraInfo>(_configService.GetAllCameras());
  110. }
  111. private async Task ExecuteSearchCommand()
  112. {
  113. try
  114. {
  115. var start = StartDate.HasValue ? StartDate.Value : (DateTime?)null;
  116. var end = EndDate.HasValue ? EndDate.Value : (DateTime?)null;
  117. var res = await _systemDatabaseService.QueryPhotoCapturesAsync(SelectedProduct?.Name, SelectedCamera?.CameraName, ProductSN, start, end);
  118. Results = new ObservableCollection<PhotoCaptureRecord>(res);
  119. TotalCount = Results.Count;
  120. }
  121. catch (Exception ex)
  122. {
  123. MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  124. }
  125. }
  126. private async Task ExecuteExportCommand()
  127. {
  128. try
  129. {
  130. var start = StartDate.HasValue ? StartDate.Value : (DateTime?)null;
  131. var end = EndDate.HasValue ? EndDate.Value : (DateTime?)null;
  132. var all = await _systemDatabaseService.QueryPhotoCapturesAsync(SelectedProduct?.Name, SelectedCamera?.CameraName, ProductSN, start, end);
  133. if (all == null || !all.Any()) { MessageBox.Show("没有找到符合条件的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information); return; }
  134. var dlg = new Microsoft.Win32.SaveFileDialog { FileName = $"PhotoCapture_{DateTime.Now:yyyyMMddHHmmss}.csv", DefaultExt = ".csv", Filter = "CSV Files (*.csv)|*.csv" };
  135. if (dlg.ShowDialog() != true) return;
  136. var path = dlg.FileName;
  137. using (var sw = new StreamWriter(path, false, Encoding.UTF8))
  138. {
  139. sw.WriteLine("Id,CaptureTime,RecipeName,ProcessName,CameraName,ProductSN,PositionIndex,IsSuccess,RobotPosition,PixelPosition,AbsolutePosition,ImagePath,OperatorName,Remarks");
  140. foreach (var r in all)
  141. {
  142. sw.WriteLine(string.Join(",",
  143. EscapeCsv(r.Id.ToString()),
  144. EscapeCsv(r.CaptureTime.ToString("yyyy-MM-dd HH:mm:ss")),
  145. EscapeCsv(r.RecipeName),
  146. EscapeCsv(r.ProcessName),
  147. EscapeCsv(r.CameraName),
  148. EscapeCsv(r.ProductSN),
  149. EscapeCsv(r.PositionIndex.ToString()),
  150. EscapeCsv(r.IsSuccess ? "1" : "0"),
  151. EscapeCsv(r.RobotPosition),
  152. EscapeCsv(r.PixelPosition),
  153. EscapeCsv(r.AbsolutePosition),
  154. EscapeCsv(r.ImagePath),
  155. EscapeCsv(r.OperatorName),
  156. EscapeCsv(r.Remarks)
  157. ));
  158. }
  159. }
  160. }
  161. catch (Exception ex)
  162. {
  163. MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  164. }
  165. }
  166. private string EscapeCsv(string s)
  167. {
  168. if (s == null) return "";
  169. if (s.Contains(",") || s.Contains("\"") || s.Contains("\r") || s.Contains("\n"))
  170. {
  171. return "\"" + s.Replace("\"", "\"\"") + "\"";
  172. }
  173. return s;
  174. }
  175. }
  176. }