AddStandDialog.xaml.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using Microsoft.Win32;
  3. using System.Windows;
  4. using System.ComponentModel;
  5. using System.IO;
  6. using MeterVision.model;
  7. using System.Collections.Generic;
  8. using OfficeOpenXml;
  9. using MeterVision.db;
  10. using System.Threading.Tasks;
  11. using MeterVision.Dlg;
  12. namespace MeterVision.Stand
  13. {
  14. public partial class AddStandDialog : Window
  15. {
  16. //新增的TStand
  17. public TStand mTStand { get; set; }
  18. private List<TStandDetail> mStandDetailList;
  19. public AddStandDialog()
  20. {
  21. InitializeComponent();
  22. }
  23. private void BtnClose_Click(object sender, RoutedEventArgs e)
  24. {
  25. this.Close();
  26. }
  27. private void BtnImportExcel_Click(object sender, RoutedEventArgs e)
  28. {
  29. // 打开文件选择对话框
  30. var openFileDialog = new OpenFileDialog
  31. {
  32. //Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx",
  33. Filter = "Excel Files (*.xlsx)|*.xlsx",
  34. Title = "选择Excel文件"
  35. };
  36. if (openFileDialog.ShowDialog() == true)
  37. {
  38. // 显示选择的文件名
  39. txtExcelFile.Text = openFileDialog.FileName;
  40. if(string.IsNullOrWhiteSpace(txtStandName.Text.Trim()))
  41. {
  42. txtStandName.Text = ThisApp.GetFileNameWithoutExtension(txtExcelFile.Text.Trim());
  43. }
  44. }
  45. }
  46. // 当单选框的状态发生变化时,动态显示或隐藏Excel相关区域
  47. private void rbImportExcel_Checked(object sender, RoutedEventArgs e)
  48. {
  49. if (ExcelImportPanel != null)
  50. {
  51. ExcelImportPanel.Visibility = Visibility.Visible;
  52. }
  53. }
  54. private void rbCustomTemplate_Checked(object sender, RoutedEventArgs e)
  55. {
  56. if (ExcelImportPanel != null)
  57. {
  58. ExcelImportPanel.Visibility = Visibility.Collapsed;
  59. }
  60. }
  61. private async void BtnOK_Click(object sender, RoutedEventArgs e)
  62. {
  63. bool blGetData = false;
  64. string standName = txtStandName.Text.Trim();
  65. if (standName == string.Empty)
  66. {
  67. MessageBox.Show("请输入模板名称!");
  68. return;
  69. }
  70. mTStand = new TStand()
  71. {
  72. StandId = Guid.NewGuid().ToString(),
  73. StandName = standName,
  74. CreateTime = ThisApp.GetNowTime_yyyyMMddHHmmss(),
  75. StandType = rbCustomTemplate.IsChecked == true ? 1 : 2
  76. };
  77. if(rbCustomTemplate.IsChecked == true)
  78. {
  79. blGetData = true;
  80. }
  81. else if (rbImportExcel.IsChecked == true)
  82. {
  83. string filePath = txtExcelFile.Text.Trim();
  84. if (string.IsNullOrWhiteSpace(filePath))
  85. {
  86. MessageBox.Show(Application.Current.MainWindow, "请选择Excel文件!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  87. return;
  88. }
  89. mTStand.StandFile = txtExcelFile.Text.Trim();
  90. //ImportExcel(filePath);
  91. bool blLoad = await LoadTStandDetailList();
  92. blGetData = blLoad;
  93. } //IsChecked
  94. //开始插入数据库操作
  95. if (blGetData)
  96. {
  97. bool blInsert = await InsertDataBase(mTStand, mStandDetailList);
  98. if (blInsert)
  99. {
  100. this.DialogResult = true;
  101. this.Close();
  102. }
  103. }
  104. }
  105. //插入数据库
  106. private async Task<bool> InsertDataBase(TStand stand,List<TStandDetail> standDetails)
  107. {
  108. bool blInsert = false;
  109. string titleInfo = "正在插入数据库,请稍候...";
  110. WaitWindow waitWindow = new WaitWindow(titleInfo)
  111. {
  112. Owner = this,
  113. WindowStartupLocation = WindowStartupLocation.CenterOwner
  114. };
  115. waitWindow.Show();
  116. try
  117. {
  118. await Task.Run(() =>
  119. {
  120. if (stand.StandType == 2)
  121. {
  122. blInsert = DBStand.InsertStandAndDetails(stand, standDetails);
  123. if (blInsert) {
  124. //获取不重复的站点
  125. List<StationInfo> stationInfos = DBStand.GetUniqueStationInfo(stand.StandId);
  126. blInsert = DBStation.InsertTStations(stationInfos);
  127. }
  128. }
  129. else
  130. {
  131. blInsert = DBStand.InsertTStand(stand);
  132. }
  133. });
  134. }
  135. catch(Exception ex)
  136. {
  137. MessageBox.Show($"插入数据失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  138. }
  139. finally
  140. {
  141. waitWindow.Close();
  142. }
  143. return blInsert;
  144. }
  145. //加载列表数据
  146. private async Task<bool> LoadTStandDetailList()
  147. {
  148. string titleInfo = "正在获取数据,请稍候...";
  149. WaitImportDlg waitWindow = new WaitImportDlg(titleInfo)
  150. {
  151. Owner = this,
  152. WindowStartupLocation = WindowStartupLocation.CenterOwner
  153. };
  154. waitWindow.Show();
  155. try
  156. {
  157. await Task.Run(() =>
  158. {
  159. ImportExcel(mTStand.StandFile,waitWindow);
  160. });
  161. // 如果mStandDetailList不为空,则表示导入成功
  162. return mStandDetailList != null && mStandDetailList.Count > 0;
  163. }
  164. catch (Exception ex)
  165. {
  166. MessageBox.Show(this, $"获取Excel数据失败:{ex.Message}", "错误",
  167. MessageBoxButton.OK, MessageBoxImage.Error);
  168. return false;
  169. }
  170. finally
  171. {
  172. waitWindow.Close();
  173. }
  174. }
  175. public void ImportExcel(string filePath,WaitImportDlg importDlg)
  176. {
  177. mStandDetailList = new List<TStandDetail>();
  178. Console.WriteLine($"正在读取文件: {filePath}");
  179. using (var package = new ExcelPackage(new FileInfo(filePath)))
  180. {
  181. Console.WriteLine($"工作表数量: {package.Workbook.Worksheets.Count}");
  182. //检查是否存在工作表
  183. if (package.Workbook.Worksheets.Count == 0)
  184. {
  185. throw new InvalidOperationException("Excel文件中没有工作表。");
  186. }
  187. // 获取第一个工作表
  188. var worksheet = package.Workbook.Worksheets[1];
  189. // 获取总行数
  190. int rowCount = worksheet.Dimension?.Rows ?? 0;
  191. Console.WriteLine($"行数: {rowCount}");
  192. importDlg.PrgCount = rowCount;
  193. // 遍历每一行
  194. for (int row = 2; row <= rowCount; row++) // 假设第一行是标题行
  195. {
  196. importDlg.PrgIndex = row;
  197. //判断图像是否符合要求的规格
  198. string srcImage = worksheet.Cells[row, 1].Text.Trim();
  199. //为了导入excel表格中的内容,先暂时不判断文件是否满足要求
  200. /*if (!ThisApp.IsImageDimensionsValid(srcImage))
  201. {
  202. continue;
  203. }*/
  204. var standDetail = new TStandDetail
  205. {
  206. StandDetailId = Guid.NewGuid().ToString(),
  207. CreateTime = ThisApp.GetNowTime_yyyyMMddHHmmss(),
  208. StandId = mTStand.StandId,
  209. // 读取图像路径(假设图像路径在A列)
  210. SrcImage = worksheet.Cells[row, 1].Text.Trim(),
  211. // 读取标准答案(假设标准答案在B列)
  212. StandValue = worksheet.Cells[row, 2].Text.Trim(),
  213. DeviceSn = worksheet.Cells[row, 3].Text.Trim(),
  214. StationId = worksheet.Cells[row, 4].Text.Trim(),
  215. SampleTime = worksheet.Cells[row, 5].Text.Trim(),
  216. ENumCount = ThisApp.ConvertToInt(worksheet.Cells[row, 6].Text.Trim()),
  217. ELastUnit = ThisApp.ConvertToInt(worksheet.Cells[row, 7].Text.Trim())
  218. };
  219. // 添加到结果列表
  220. mStandDetailList.Add(standDetail);
  221. }
  222. }
  223. }
  224. //-------------------------------------------------------------
  225. }
  226. }