PhotoCaptureQueryViewModel.cs 7.7 KB

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