AddScrewViewModel.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using Cognex.VisionPro.MultiLineFinder.Implementation.Internal;
  2. using Opc.Ua;
  3. using Prism.Events;
  4. using Prism.Mvvm;
  5. using Prism.Regions;
  6. using Prism.Services.Dialogs;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Input;
  15. using TeamAAS_VP.Core.PLCs;
  16. using TeamAAS_VP.Interfaces;
  17. using TeamAAS_VP.Models;
  18. using TeamAAS_VP.Views;
  19. namespace TeamAAS_VP.ViewModels
  20. {
  21. public class AddScrewViewModel : BindableBase,IDialogAware
  22. {
  23. IConfigService _configService;
  24. IEventAggregator _eventAggregator;
  25. IPlcService _plcService;
  26. private string _UserID;
  27. /// <summary>
  28. /// 员工工号
  29. /// </summary>
  30. public string UserID
  31. {
  32. get { return _UserID; }
  33. set { SetProperty(ref _UserID, value); }
  34. }
  35. private string _ScrewsizeCode;
  36. /// <summary>
  37. /// 螺丝条码
  38. /// </summary>
  39. public string ScrewsizeCode
  40. {
  41. get { return _ScrewsizeCode; }
  42. set { SetProperty(ref _ScrewsizeCode, value); }
  43. }
  44. private DelegateCommand _OkCommand;
  45. public DelegateCommand OkCommand =>
  46. _OkCommand ?? (_OkCommand = new DelegateCommand<string>(OkExecute));
  47. private DelegateCommand _CancelCommand;
  48. public event Action<IDialogResult> RequestClose;
  49. public DelegateCommand CancelCommand =>
  50. _CancelCommand ?? (_CancelCommand = new DelegateCommand<string>(CancelExecute));
  51. public string Title => "";
  52. public AddScrewViewModel(IConfigService configService, IEventAggregator eventAggregator, IPlcService plcService)
  53. {
  54. _configService = configService;
  55. _eventAggregator = eventAggregator;
  56. _plcService = plcService;
  57. }
  58. private async void OkExecute(string str)
  59. {
  60. try
  61. {
  62. var addressConfig = _configService.GetPlcAddresses();
  63. var plc = _plcService.GetPlcByNumber(addressConfig.PlcNo) as OpcUaClientPLC;
  64. string a = "";
  65. var stationConfig = _configService.GetDeviceInfo(0);
  66. if (ScrewsizeCode.Contains("|"))
  67. {
  68. string[] arr = ScrewsizeCode.Split('|');
  69. //for (int i = 0; i < 16; i++)
  70. //{
  71. // a += arr[i];
  72. //}
  73. a = arr[0];
  74. }
  75. else
  76. {
  77. a = ScrewsizeCode;
  78. }
  79. UserID = UserID.Replace("\r\n", "").Trim();
  80. if (string.IsNullOrWhiteSpace(a) && string.IsNullOrEmpty(UserID))
  81. {
  82. MessageBox.Show("请先扫描螺丝条码", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  83. //BarcodeTextBox.Focus(); // 重新聚焦
  84. return;
  85. }
  86. if (a == stationConfig.DataInfo.Screwsize1 || a == stationConfig.DataInfo.Screwsize2 || a == stationConfig.DataInfo.Screwsize3)
  87. {
  88. await plc.WriteNodeAsync<bool>(addressConfig.Out_PlcFail.Address, false);
  89. MessageBox.Show("螺丝型号匹配成功", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  90. //SendTaskMessage($"螺丝型号匹配成功", MessageLevel.Info);
  91. ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, stationConfig.DataInfo, true, UserID, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true);
  92. }
  93. else
  94. {
  95. await plc.WriteNodeAsync<bool>(addressConfig.Out_PlcFail.Address, true);
  96. MessageBox.Show("螺丝型号匹配失败", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  97. //SendTaskMessage($"螺丝型号匹配失败", MessageLevel.Error);
  98. ExportDataToCsv(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a, stationConfig.DataInfo, false, UserID, @"D:\添加螺丝记录\螺丝上料Log.csv", appendMode: true);
  99. }
  100. //DialogResult = true;
  101. //Close();
  102. }
  103. catch (Exception ex)
  104. {
  105. MessageBox.Show("螺丝螺丝条码异常", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
  106. return;
  107. }
  108. }
  109. private void CancelExecute(string str)
  110. {
  111. //DialogResult = false;
  112. //Close();
  113. }
  114. public static void ExportDataToCsv(string dateTime, string model, CTQ_Data DataInfo, bool isMatch,string UserId, string savePath, bool appendMode = false)
  115. {
  116. string directory = global::System.IO.Path.GetDirectoryName(savePath);
  117. // 如果目录路径不为空且不存在,则创建
  118. if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
  119. {
  120. Directory.CreateDirectory(directory);
  121. }
  122. // 构建CSV内容
  123. StringBuilder csvContent = new StringBuilder();
  124. // 处理表头:仅当文件不存在/非追加模式时,才写入表头
  125. if (!appendMode || !File.Exists(savePath))
  126. {
  127. //csvContent.AppendLine("日期时间,目标螺丝型号1,螺丝型号2,螺丝型号3,添加螺丝型号,添加螺丝员工,是否匹配");
  128. csvContent.AppendLine("DateTime,Screw1,Screw2,Screw3,AddScrew,UserID,Results");
  129. }
  130. // 处理数据行
  131. string matchStr = isMatch ? "OK" : "NG";
  132. // 如果型号中包含逗号,需要用引号包裹
  133. string safeModel = model;
  134. if (safeModel.Contains(","))
  135. {
  136. safeModel = $"\"{safeModel}\"";
  137. }
  138. csvContent.AppendLine($"{dateTime},{DataInfo.Screwsize1},{DataInfo.Screwsize2},{DataInfo.Screwsize3},{model},{UserId},{matchStr}");
  139. // 写入文件:UTF-8带BOM编码,Excel打开无乱码
  140. if (appendMode && File.Exists(savePath))
  141. {
  142. // 追加模式:在文件末尾新增内容
  143. File.AppendAllText(savePath, csvContent.ToString(), Encoding.UTF8);
  144. }
  145. else
  146. {
  147. // 覆盖模式:新建/替换文件
  148. File.WriteAllText(savePath, csvContent.ToString(), Encoding.UTF8);
  149. }
  150. }
  151. public bool CanCloseDialog()
  152. {
  153. return true;
  154. }
  155. public void OnDialogClosed()
  156. {
  157. }
  158. public void OnDialogOpened(IDialogParameters parameters)
  159. {
  160. }
  161. }
  162. }