From 5ddadba291ea2d9dba78259973594a4664b94f57 Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期四, 08 一月 2026 16:45:35 +0800
Subject: [PATCH] 简化相机逻辑,支持手动数据处理

---
 LB_SmartVision/SQL/RecordProductDataRepository.cs |  419 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 419 insertions(+), 0 deletions(-)

diff --git a/LB_SmartVision/SQL/RecordProductDataRepository.cs b/LB_SmartVision/SQL/RecordProductDataRepository.cs
new file mode 100644
index 0000000..4c5915e
--- /dev/null
+++ b/LB_SmartVision/SQL/RecordProductDataRepository.cs
@@ -0,0 +1,419 @@
+锘縰sing LB_SmartVisionCommon;
+using MySql.Data.MySqlClient;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace LB_SmartVision.SQL
+{
+    /// <summary>
+    /// 鏁版嵁璁块棶灞�
+    /// </summary>
+    public class RecordProductDataRepository
+    {
+        private static object recordProductDataRepositoryObject = new object();
+        /// <summary>
+        /// 娣诲姞鏁版嵁
+        /// </summary>
+        /// <param name="record">鐢熶骇鏁版嵁璁板綍</param>
+        /// <returns>鏄惁娣诲姞鎴愬姛</returns>
+        public static bool AddRecord(RecordProductData record)
+        {
+            Stopwatch stopwatch = Stopwatch.StartNew();
+            lock (recordProductDataRepositoryObject)
+            {
+                stopwatch.Start();
+                try
+                {
+                    using (var connection = DatabaseRecordProductDataHelper.GetConnection())
+                    {
+                        connection.Open();
+                        string sql = @"
+                                     INSERT INTO RecordProductData 
+                                     (ProductName, ProductSN, InspectionOperator, NGType, NGSize,DetectionTime,CameraInspection)
+                                     VALUES 
+                                     (@ProductName, @ProductSN, @InspectionOperator, @NGType, @NGSize, @DetectionTime, @CameraInspection)";
+                        using (var cmd = new MySqlCommand(sql, connection))
+                        {
+                            cmd.Parameters.AddWithValue("@ProductName", record.ProductName);
+                            cmd.Parameters.AddWithValue("@ProductSN", record.ProductSN);
+                            cmd.Parameters.AddWithValue("@InspectionOperator", record.InspectionOperator);
+                            cmd.Parameters.AddWithValue("@NGType", record.NGType);
+                            cmd.Parameters.AddWithValue("@NGSize", record.NGSize);
+                            cmd.Parameters.AddWithValue("@DetectionTime", record.DetectionTime);
+                            cmd.Parameters.AddWithValue("@CameraInspection", record.CameraInspection);
+                            int rowsAffected = cmd.ExecuteNonQuery();
+                            stopwatch.Stop();
+                            Task.Factory.StartNew(() =>
+                            {
+                                LogHelper.Info($"娣诲姞淇℃伅鑰楁椂: {stopwatch.ElapsedMilliseconds}ms");
+                            });
+                            return rowsAffected > 0;
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    AsyncLogHelper.Error($"娣诲姞璁板綍澶辫触: {ex.Message}");
+                    return false;
+                }
+            }
+        }
+        /// <summary>
+        /// 鏌ヨ鎵�鏈夋暟鎹�
+        /// </summary>
+        /// <returns>List<RecordProductData></returns>
+        public static List<RecordProductData> GetAllRecords()
+        {
+            lock (recordProductDataRepositoryObject)
+            {
+                var records = new List<RecordProductData>();
+                try
+                {
+                    using (var connection = DatabaseRecordProductDataHelper.GetConnection())
+                    {
+                        connection.Open();
+                        string sql = "SELECT * FROM RecordProductData ORDER BY CreatedDate DESC";
+                        using (var cmd = new MySqlCommand(sql, connection))
+                        using (var reader = cmd.ExecuteReader())
+                        {
+                            while (reader.Read())
+                            {
+                                records.Add(MapReaderToRecord(reader));
+                            }
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    AsyncLogHelper.Error($"鏌ヨ璁板綍澶辫触: {ex.Message}");
+                }
+                return records;
+            }
+        }
+        /// <summary>
+        /// 鏍规嵁浜у搧缂栧彿鏌ヨ
+        /// </summary>
+        /// <param name="productSN">浜у搧缂栧彿</param>
+        /// <returns>List<RecordProductData></returns>
+        public static List<RecordProductData> GetRecordsByProductNumber(string productSN)
+        {
+            lock (recordProductDataRepositoryObject)
+            {
+                var records = new List<RecordProductData>();
+                try
+                {
+                    using (var connection = DatabaseRecordProductDataHelper.GetConnection())
+                    {
+                        connection.Open();
+                        string sql = "SELECT * FROM RecordProductData WHERE productSN = @productSN ORDER BY CreatedDate DESC";
+                        using (var cmd = new MySqlCommand(sql, connection))
+                        {
+                            cmd.Parameters.AddWithValue("@productSN", productSN);
+                            using (var reader = cmd.ExecuteReader())
+                            {
+                                while (reader.Read())
+                                {
+                                    records.Add(MapReaderToRecord(reader));
+                                }
+                            }
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    AsyncLogHelper.Error($"鏍规嵁浜у搧缂栧彿鏌ヨ澶辫触: {ex.Message}");
+                }
+                return records;
+            }
+        }
+        /// <summary>
+        /// 鏍规嵁鎿嶄綔鑰呮煡璇�
+        /// </summary>
+        /// <param name="inspectionOperator">鎿嶄綔鑰�</param>
+        /// <returns>List<RecordProductData></returns>
+        public static List<RecordProductData> GetRecordsByOperator(string inspectionOperator)
+        {
+            lock (recordProductDataRepositoryObject)
+            {
+                var records = new List<RecordProductData>();
+                try
+                {
+                    using (var connection = DatabaseRecordProductDataHelper.GetConnection())
+                    {
+                        connection.Open();
+                        string sql = "SELECT * FROM RecordProductData WHERE InspectionOperator = @InspectionOperator ORDER BY CreatedDate DESC";
+                        using (var cmd = new MySqlCommand(sql, connection))
+                        {
+                            cmd.Parameters.AddWithValue("@InspectionOperator", inspectionOperator);
+                            using (var reader = cmd.ExecuteReader())
+                            {
+                                while (reader.Read())
+                                {
+                                    records.Add(MapReaderToRecord(reader));
+                                }
+                            }
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    AsyncLogHelper.Error($"鏍规嵁鎿嶄綔鑰呮煡璇㈠け璐�: {ex.Message}");
+                }
+                return records;
+            }
+        }
+        /// <summary>
+        /// 鏍规嵁鏃堕棿娈垫煡璇� - 浣跨敤CreatedDate
+        /// </summary>
+        /// <param name="startTime">璧峰鏃堕棿</param>
+        /// <param name="endTime">缁撴潫鏃堕棿</param>
+        /// <returns>List<RecordProductData></returns>
+        public static List<RecordProductData> GetRecordsByTimeRange(DateTime startTime, DateTime endTime)
+        {
+            lock (recordProductDataRepositoryObject)
+            {
+                var records = new List<RecordProductData>();
+                try
+                {
+                    using (var connection = DatabaseRecordProductDataHelper.GetConnection())
+                    {
+                        connection.Open();
+                        string sql = @"
+                                     SELECT * FROM RecordProductData 
+                                     WHERE CreatedDate BETWEEN @StartTime AND @EndTime 
+                                     ORDER BY CreatedDate DESC";
+                        using (var cmd = new MySqlCommand(sql, connection))
+                        {
+                            cmd.Parameters.AddWithValue("@StartTime", startTime);
+                            cmd.Parameters.AddWithValue("@EndTime", endTime.AddDays(1).AddSeconds(-1)); // 鍖呭惈缁撴潫鏃ユ湡鐨勫叏澶�
+                            using (var reader = cmd.ExecuteReader())
+                            {
+                                while (reader.Read())
+                                {
+                                    records.Add(MapReaderToRecord(reader));
+                                }
+                            }
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    AsyncLogHelper.Error($"鏍规嵁鏃堕棿娈垫煡璇㈠け璐�: {ex.Message}");
+                }
+                return records;
+            }
+        }
+
+        /// <summary>
+        /// 缁勫悎鏌ヨ锛氫骇鍝佺紪鍙� + 鏃堕棿娈�
+        /// </summary>
+        /// <param name="productSN">浜у搧缂栧彿</param>
+        /// <param name="startTime">璧峰鏃堕棿</param>
+        /// <param name="endTime">缁撴潫鏃堕棿</param>
+        /// <returns>List<RecordProductData></returns>
+        public static List<RecordProductData> GetRecordsByProductAndTimeRange(string productSN, DateTime startTime, DateTime endTime)
+        {
+            lock (recordProductDataRepositoryObject)
+            {
+                var records = new List<RecordProductData>();
+                try
+                {
+                    using (var connection = DatabaseRecordProductDataHelper.GetConnection())
+                    {
+                        connection.Open();
+                        string sql = @"
+                                     SELECT * FROM RecordProductData 
+                                     WHERE productSN = @productSN 
+                                     AND CreatedDate BETWEEN @StartTime AND @EndTime 
+                                     ORDER BY CreatedDate DESC";
+                        using (var cmd = new MySqlCommand(sql, connection))
+                        {
+                            cmd.Parameters.AddWithValue("@productSN", productSN);
+                            cmd.Parameters.AddWithValue("@StartTime", startTime);
+                            cmd.Parameters.AddWithValue("@EndTime", endTime.AddDays(1).AddSeconds(-1));
+                            using (var reader = cmd.ExecuteReader())
+                            {
+                                while (reader.Read())
+                                {
+                                    records.Add(MapReaderToRecord(reader));
+                                }
+                            }
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    AsyncLogHelper.Error($"缁勫悎鏌ヨ澶辫触: {ex.Message}");
+                }
+                return records;
+            }
+        }
+        /// <summary>
+        /// 缁勫悎鏌ヨ锛氭搷浣滆�� + 鏃堕棿娈�
+        /// </summary>
+        /// <param name="inspectionOperator">鎿嶄綔鑰�</param>
+        /// <param name="startTime">璧峰鏃堕棿</param>
+        /// <param name="endTime">缁撴潫鏃堕棿</param>
+        /// <returns>List<RecordProductData></returns>
+        public static List<RecordProductData> GetRecordsByOperatorAndTimeRange(string inspectionOperator, DateTime startTime, DateTime endTime)
+        {
+            lock (recordProductDataRepositoryObject)
+            {
+                var records = new List<RecordProductData>();
+                try
+                {
+                    using (var connection = DatabaseRecordProductDataHelper.GetConnection())
+                    {
+                        connection.Open();
+                        string sql = @"
+                                     SELECT * FROM RecordProductData 
+                                     WHERE InspectionOperator = @InspectionOperator 
+                                     AND CreatedDate BETWEEN @StartTime AND @EndTime 
+                                     ORDER BY CreatedDate DESC";
+                        using (var cmd = new MySqlCommand(sql, connection))
+                        {
+                            cmd.Parameters.AddWithValue("@InspectionOperator", inspectionOperator);
+                            cmd.Parameters.AddWithValue("@StartTime", startTime);
+                            cmd.Parameters.AddWithValue("@EndTime", endTime.AddDays(1).AddSeconds(-1));
+                            using (var reader = cmd.ExecuteReader())
+                            {
+                                while (reader.Read())
+                                {
+                                    records.Add(MapReaderToRecord(reader));
+                                }
+                            }
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    AsyncLogHelper.Error($"缁勫悎鏌ヨ澶辫触: {ex.Message}");
+                }
+                return records;
+            }
+        }
+        /// <summary>
+        /// 楂樼骇缁勫悎鏌ヨ锛氭敮鎸佸涓潯浠�
+        /// </summary>
+        /// <param name="productSN">浜у搧缂栧彿</param>
+        /// <param name="operatorName">鎿嶄綔鑰�</param>
+        /// <param name="startTime">璧峰鏃堕棿</param>
+        /// <param name="endTime">缁撴潫鏃堕棿</param>
+        /// <returns>List<RecordProductData></returns>
+        public static List<RecordProductData> GetRecordsByMultipleConditions(
+            string productSN = null,
+            string operatorName = null,
+            DateTime? startTime = null,
+            DateTime? endTime = null)
+        {
+            lock (recordProductDataRepositoryObject)
+            {
+                var records = new List<RecordProductData>();
+                try
+                {
+                    using (var connection = DatabaseRecordProductDataHelper.GetConnection())
+                    {
+                        connection.Open();
+                        var sql = "SELECT * FROM RecordProductData WHERE 1=1";
+                        var parameters = new List<MySqlParameter>();
+                        if (!string.IsNullOrEmpty(productSN))
+                        {
+                            sql += " AND ProductSN = @ProductSN";
+                            parameters.Add(new MySqlParameter("@ProductSN", productSN));
+                        }
+                        if (!string.IsNullOrEmpty(operatorName))
+                        {
+                            sql += " AND MeasurementOperator = @InspectionOperator";
+                            parameters.Add(new MySqlParameter("@InspectionOperator", operatorName));
+                        }
+                        if (startTime.HasValue)
+                        {
+                            sql += " AND CreatedDate >= @StartTime";
+                            parameters.Add(new MySqlParameter("@StartTime", startTime.Value));
+                        }
+                        if (endTime.HasValue)
+                        {
+                            sql += " AND CreatedDate <= @EndTime";
+                            parameters.Add(new MySqlParameter("@EndTime", endTime.Value.AddDays(1).AddSeconds(-1)));
+                        }
+                        sql += " ORDER BY CreatedDate DESC";
+                        using (var cmd = new MySqlCommand(sql, connection))
+                        {
+                            foreach (var param in parameters)
+                            {
+                                cmd.Parameters.Add(param);
+                            }
+                            using (var reader = cmd.ExecuteReader())
+                            {
+                                while (reader.Read())
+                                {
+                                    records.Add(MapReaderToRecord(reader));
+                                }
+                            }
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    AsyncLogHelper.Error($"楂樼骇缁勫悎鏌ヨ澶辫触: {ex.Message}");
+                }
+                return records;
+            }
+        }
+        /// <summary>
+        /// 缁熻鏌ヨ锛氳幏鍙栨煇涓椂闂存鍐呯殑璁板綍鏁伴噺
+        /// </summary>
+        /// <param name="startTime">璧峰鏃堕棿</param>
+        /// <param name="endTime">缁撴潫鏃堕棿</param>
+        /// <returns>璁板綍鏁伴噺</returns>
+        public static int GetRecordCountByTimeRange(DateTime startTime, DateTime endTime)
+        {
+            lock (recordProductDataRepositoryObject)
+            {
+                try
+                {
+                    using (var connection = DatabaseRecordProductDataHelper.GetConnection())
+                    {
+                        connection.Open();
+                        string sql = @"
+                                     SELECT COUNT(*) FROM RecordProductData 
+                                     WHERE CreatedDate BETWEEN @StartTime AND @EndTime";
+                        using (var cmd = new MySqlCommand(sql, connection))
+                        {
+                            cmd.Parameters.AddWithValue("@StartTime", startTime);
+                            cmd.Parameters.AddWithValue("@EndTime", endTime.AddDays(1).AddSeconds(-1));
+                            return Convert.ToInt32(cmd.ExecuteScalar());
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    AsyncLogHelper.Error($"缁熻鏌ヨ澶辫触: {ex.Message}");
+                    return 0;
+                }
+            }
+        }
+        /// <summary>
+        /// 杈呭姪鏂规硶锛氬皢鏁版嵁璇诲彇鍣ㄦ槧灏勫埌RecordProductData瀵硅薄
+        /// </summary>
+        /// <param name="reader">MySqlDataReader reader</param>
+        /// <returns>RecordProductData</returns>
+        private static RecordProductData MapReaderToRecord(MySqlDataReader reader)
+        {
+            return new RecordProductData
+            {
+                ProductName = reader["ProductName"]?.ToString() ?? string.Empty,
+                ProductSN = reader["ProductSN"]?.ToString() ?? string.Empty,
+                InspectionOperator = reader["InspectionOperator"]?.ToString() ?? string.Empty,
+                NGType = reader["NGType"]?.ToString() ?? string.Empty,
+                NGSize = reader["NGSize"]?.ToString() ?? string.Empty,
+                DetectionTime = reader["DetectionTime"]?.ToString() ?? string.Empty,
+                CameraInspection = reader["CameraInspection"]?.ToString() ?? string.Empty,
+            };
+        }
+    }
+}

--
Gitblit v1.9.3