Procházet zdrojové kódy

检测保存csv文件存储数据达到Excel文件上限生成新文件

KWD před 2 měsíci
rodič
revize
1c4f657740
1 změnil soubory, kde provedl 118 přidání a 35 odebrání
  1. 118 35
      TeamAAS-VM/Core/Management.cs

+ 118 - 35
TeamAAS-VM/Core/Management.cs

@@ -5395,7 +5395,6 @@ namespace TeamAAS_VP.Core
             };
         }
 
-
         private Dictionary<string, double> GetAdjacentDiffDict(ConcurrentDictionary<string, double> keyData, string name)
         {
             var currentProduct = _productService.GetCurrentProduct();
@@ -5445,36 +5444,7 @@ namespace TeamAAS_VP.Core
             return diffDict;
         }
 
-
-        private async void DoYieldTime(object state)
-        {
-            try
-            {
-                //获取当前产品
-                var currentproduct = _productService.GetCurrentProduct();
-                CurrentProduct = _productService.GetCurrentProduct();
-                if (currentproduct == null) return;
-                TotalQuantity = await _systemDatabaseService.GetOverallProductionAsync(currentproduct.Name);
-
-                TotalQuantityToday = 0;
-                CurrentUPH = 0;
-                //NgNumber = 0;
-                //Yield = 0;
-                ObservableCollection<ProductModel> ProductList = new ObservableCollection<ProductModel>(_productService.GetAllProducts());
-                foreach (var product in ProductList)
-                {
-                    //TotalQuantity += await _systemDatabaseService.GetOverallProductionAsync(product.Name);
-                    TotalQuantityToday += await _systemDatabaseService.GetTodayProductionAsync(product.Name);
-                    CurrentUPH += await _systemDatabaseService.GetLastHourProductionAsync(product.Name);
-                    //NgNumber += await _systemDatabaseService.GetNGProductionAsync(product.Name);
-                }
-            }
-            catch (Exception)
-            {
-
-            }
-        }
-
+        #endregion
 
         #region vm启动检测
 
@@ -5690,6 +5660,11 @@ namespace TeamAAS_VP.Core
                     string fileName = $"{stationIndex}_{stationConfig.DeviceName}_{timestamp.ToString("yyyy-MM-dd")}.csv";
                     fullPath = Path.Combine(dirPath, fileName);
                 }
+
+                // ========== 新增:检查文件行数,如果达到上限则自动分片 ==========
+                fullPath = GetAvailableFilePath(fullPath);
+                // ============================================================
+
                 StringBuilder sb = new StringBuilder();
                 if (!File.Exists(fullPath))
                 {
@@ -5729,12 +5704,120 @@ namespace TeamAAS_VP.Core
                 return false;
             }
         }
+        /// <summary>
+        /// Excel 最大行数限制(Excel 2007+ 为 1048576 行,这里设为 1000000 留有余量)
+        /// </summary>
+        private const int MaxExcelRows = 1000000;
 
-        #endregion
+        /// <summary>
+        /// 获取可用的文件路径,如果当前文件行数达到上限,则自动生成新文件
+        /// </summary>
+        private string GetAvailableFilePath(string originalPath)
+        {
+            // 如果文件不存在,直接返回原路径
+            if (!File.Exists(originalPath))
+            {
+                return originalPath;
+            }
 
-        #endregion
+            // 获取当前文件的行数(包含表头)
+            int currentRowCount = GetFileRowCount(originalPath);
+
+            // 如果当前行数未达到上限,直接返回原路径
+            if (currentRowCount < MaxExcelRows)
+            {
+                return originalPath;
+            }
+
+            // 文件已达上限,生成新文件路径
+            string directory = Path.GetDirectoryName(originalPath);
+            string fileName = Path.GetFileNameWithoutExtension(originalPath);
+            string extension = Path.GetExtension(originalPath);
+
+            // 查找可用的序号
+            int fileIndex = 1;
+            string newPath;
+            do
+            {
+                string newFileName = $"{fileName}_{fileIndex:D4}{extension}";
+                newPath = Path.Combine(directory, newFileName);
+                fileIndex++;
+
+                // 如果新文件存在,检查它的行数是否也达到上限
+                if (File.Exists(newPath))
+                {
+                    int newFileRowCount = GetFileRowCount(newPath);
+                    if (newFileRowCount < MaxExcelRows)
+                    {
+                        // 这个文件可用,直接返回
+                        return newPath;
+                    }
+                    // 否则继续找下一个
+                }
+                else
+                {
+                    // 新文件不存在,可用
+                    return newPath;
+                }
+
+            } while (true);
+        }
+
+        /// <summary>
+        /// 获取 CSV 文件的行数(快速方法,只读取第一行和总行数)
+        /// </summary>
+        private int GetFileRowCount(string filePath)
+        {
+            try
+            {
+                // 使用 StreamReader 快速获取行数
+                using (var reader = new StreamReader(filePath, Encoding.UTF8))
+                {
+                    int lineCount = 0;
+                    while (reader.ReadLine() != null)
+                    {
+                        lineCount++;
+                    }
+                    return lineCount;
+                }
+            }
+            catch
+            {
+                // 如果读取失败,假设文件为0行(安全处理)
+                return 0;
+            }
+        }
 
         #endregion
+
+        private async void DoYieldTime(object state)
+        {
+            try
+            {
+                //获取当前产品
+                var currentproduct = _productService.GetCurrentProduct();
+                CurrentProduct = _productService.GetCurrentProduct();
+                if (currentproduct == null) return;
+                TotalQuantity = await _systemDatabaseService.GetOverallProductionAsync(currentproduct.Name);
+
+                TotalQuantityToday = 0;
+                CurrentUPH = 0;
+                //NgNumber = 0;
+                //Yield = 0;
+                ObservableCollection<ProductModel> ProductList = new ObservableCollection<ProductModel>(_productService.GetAllProducts());
+                foreach (var product in ProductList)
+                {
+                    //TotalQuantity += await _systemDatabaseService.GetOverallProductionAsync(product.Name);
+                    TotalQuantityToday += await _systemDatabaseService.GetTodayProductionAsync(product.Name);
+                    CurrentUPH += await _systemDatabaseService.GetLastHourProductionAsync(product.Name);
+                    //NgNumber += await _systemDatabaseService.GetNGProductionAsync(product.Name);
+                }
+            }
+            catch (Exception)
+            {
+
+            }
+        }
         // 用于等待“指定相机的下一张图到达”
         private readonly object _imageSyncLock = new object();
         private readonly Dictionary<Guid, TaskCompletionSource<ICogImage>> _nextImageTcsByCamera =
@@ -5877,7 +5960,6 @@ namespace TeamAAS_VP.Core
                 return (false, null, $"处理固定相机检测拍照时出错", pointIndex, procedureId);
             }
         }
-
         #region 压缩图片文件夹
         /// <summary>
         /// 压缩文件
@@ -6002,7 +6084,7 @@ namespace TeamAAS_VP.Core
         //                {
         //                    WriteIndented = true
         //                });
-                      
+
         //                // 确保目录存在
         //                if (!Directory.Exists(directoryPath))
         //                    Directory.CreateDirectory(directoryPath);
@@ -6045,6 +6127,7 @@ namespace TeamAAS_VP.Core
 
         //#endregion
 
+        #endregion
     }
 }