From d0ded5cd9bf5070a120bad58b5be21fe2ac6a4ff Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期六, 20 十二月 2025 16:41:09 +0800
Subject: [PATCH] test
---
IDViewer_2D/ExcelResultRecorder.cs | 198 +++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 198 insertions(+), 0 deletions(-)
diff --git a/IDViewer_2D/ExcelResultRecorder.cs b/IDViewer_2D/ExcelResultRecorder.cs
new file mode 100644
index 0000000..f9dad43
--- /dev/null
+++ b/IDViewer_2D/ExcelResultRecorder.cs
@@ -0,0 +1,198 @@
+锘縰sing SmartScanner.ViewModel;
+using OfficeOpenXml;
+using OfficeOpenXml.Style;
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Timers;
+
+namespace SmartScanner
+{
+ public static class ExcelResultRecorder
+ {
+ private static readonly string reportDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Reports");
+ private static readonly string reportFileName = $"DetectionResults_{DateTime.Now:yyyyMMdd}.xlsx";
+ private static readonly List<DetectionRecord> _pendingRecords = new List<DetectionRecord>();
+ private static readonly object _lock = new object();
+ private static Timer _autoFlushTimer;
+ static ExcelResultRecorder()
+ {
+ try
+ {
+ // 纭繚鎶ュ憡鐩綍瀛樺湪
+ if (!Directory.Exists(reportDirectory))
+ {
+ Directory.CreateDirectory(reportDirectory);
+ }
+ // 璁剧疆EPPlus璁稿彲璇佷笂涓嬫枃
+ //ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
+ ExcelPackage.License.SetNonCommercialPersonal("Lanbao");
+ InitializeAutoFlush();
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"ExcelResultRecorder鍒濆鍖栧け璐�: {ex.Message}");
+ throw;
+ }
+ }
+
+ public static void RecordDetectionResult(int sequence, string result, string qrCode = "N/A")
+ {
+ lock (_lock)
+ {
+ _pendingRecords.Add(new DetectionRecord
+ {
+ QrCode = qrCode,
+ Sequence = sequence,
+ Result = result,
+ Operator = UserManager.CurrentUser?.Username ?? "Unknown"
+ });
+
+ // 姣�10鏉¤褰曟垨姣忛殧20绉掕嚜鍔ㄤ繚瀛樹竴娆�
+ if (_pendingRecords.Count >= 10)
+ {
+ Task.Run(() => FlushRecords());
+ }
+ }
+ }
+
+ public static void FlushRecords()
+ {
+ lock (_lock)
+ {
+ if (_pendingRecords.Count == 0) return;
+
+ try
+ {
+ string filePath = Path.Combine(reportDirectory, reportFileName);
+
+ using (var package = new ExcelPackage(new FileInfo(filePath)))
+ {
+ ExcelWorksheet worksheet = package.Workbook.Worksheets["妫�娴嬬粨鏋�"] ??
+ package.Workbook.Worksheets.Add("妫�娴嬬粨鏋�");
+
+ // 鍒濆鍖栬〃澶达紙濡傛灉鏂囦欢鏄柊鍒涘缓鐨勶級
+ if (worksheet.Dimension == null)
+ {
+ CreateHeader(worksheet);
+ }
+
+ // 鎵惧埌绗竴涓┖琛�
+ int startRow = worksheet.Dimension?.End.Row + 1 ?? 2;
+ // 鎵归噺鍐欏叆鏁版嵁
+ for (int i = 0; i < _pendingRecords.Count; i++)
+ {
+ var record = _pendingRecords[i];
+ int currentRow = startRow + i;
+
+ worksheet.Cells[currentRow, 1].Value = record.QrCode;
+ worksheet.Cells[currentRow, 2].Value = $"鐐逛綅 {record.Sequence}";
+ worksheet.Cells[currentRow, 3].Value = record.Result;
+ worksheet.Cells[currentRow, 4].Value = record.DetectionTime.ToString("yyyy-MM-dd HH:mm:ss");
+ worksheet.Cells[currentRow, 5].Value = record.Operator;
+ // 鏍规嵁缁撴灉璁剧疆鍗曞厓鏍艰儗鏅壊
+ var resultCell = worksheet.Cells[currentRow, 3];
+ resultCell.Style.Fill.PatternType = ExcelFillStyle.Solid;
+ resultCell.Style.Fill.BackgroundColor.SetColor(
+ record.Result == "OK" ? Color.LightGreen : Color.Red);
+ }
+ // 鑷姩璋冩暣鍒楀
+ worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
+
+ // 淇濆瓨鏂囦欢
+ package.Save();
+ EnhancedLogViewModel.Instance.AddLog($"{_pendingRecords.Count}鏉℃娴嬬粨鏋滃凡淇濆瓨鑷硔reportFileName}");
+ }
+ _pendingRecords.Clear();
+ }
+ catch (Exception ex)
+ {
+ // 璁板綍閿欒骞朵繚鐣欐湭鍐欏叆鐨勮褰�
+ Console.WriteLine($"璁板綍妫�娴嬬粨鏋滃埌Excel澶辫触: {ex.Message}");
+ EnhancedLogViewModel.Instance.AddLog($"{_pendingRecords.Count}鏉℃娴嬬粨鏋滀繚瀛樺け璐ワ紝璇峰叧闂瓄reportFileName}鍚庨噸璇�", "ERROR");
+ }
+ }
+ }
+
+ private static void CreateHeader(ExcelWorksheet worksheet)
+ {
+ // 璁剧疆琛ㄥご
+ worksheet.Cells["A1"].Value = "浜岀淮鐮�";
+ worksheet.Cells["B1"].Value = "妫�娴嬬偣浣�";
+ worksheet.Cells["C1"].Value = "妫�娴嬬粨鏋�";
+ worksheet.Cells["D1"].Value = "妫�娴嬫椂闂�";
+ worksheet.Cells["E1"].Value = "鎿嶄綔浜哄憳";
+
+ // 璁剧疆琛ㄥご鏍峰紡
+ using (var range = worksheet.Cells["A1:E1"])
+ {
+ range.Style.Font.Bold = true;
+ range.Style.Border.Top.Style = ExcelBorderStyle.Thin;
+ range.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
+ range.Style.Border.Left.Style = ExcelBorderStyle.Thin;
+ range.Style.Border.Right.Style = ExcelBorderStyle.Thin;
+ range.Style.Fill.PatternType = ExcelFillStyle.Solid;
+ range.Style.Fill.BackgroundColor.SetColor(Color.LightGray);
+ range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
+ }
+ }
+ public static void OpenReportFolder()
+ {
+ try
+ {
+ if (Directory.Exists(reportDirectory))
+ {
+ System.Diagnostics.Process.Start("explorer.exe", reportDirectory);
+ }
+ else
+ {
+ Console.WriteLine("鎶ュ憡鐩綍涓嶅瓨鍦�");
+ }
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"鎵撳紑鎶ュ憡鏂囦欢澶瑰け璐�: {ex.Message}");
+ }
+ }
+
+ // 瀹氭椂淇濆瓨浠诲姟
+ public static void InitializeAutoFlush()
+ {
+ // 濡傛灉瀹氭椂鍣ㄥ凡瀛樺湪锛屽厛鍋滄骞堕噴鏀�
+ if (_autoFlushTimer != null)
+ {
+ _autoFlushTimer.Stop();
+ _autoFlushTimer.Dispose();
+ }
+
+ // 鍒涘缓鏂扮殑瀹氭椂鍣�
+ _autoFlushTimer = new Timer(20000);
+ _autoFlushTimer.Elapsed += (s, e) =>
+ {
+ try
+ {
+ Task.Run(() => FlushRecords());
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"瀹氭椂淇濆瓨璁板綍澶辫触: {ex.Message}");
+ }
+ };
+ _autoFlushTimer.AutoReset = true;
+ _autoFlushTimer.Start();
+ }
+
+ private class DetectionRecord
+ {
+ public string QrCode { get; set; }
+ public int Sequence { get; set; }
+ public string Result { get; set; }
+ public DateTime DetectionTime { get; } = DateTime.Now;
+ public string Operator { get; set; }
+ }
+ }
+}
--
Gitblit v1.9.3