From ef52fd6fd07508883dce4eb002acda59f0169e59 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期四, 23 四月 2026 10:19:33 +0800
Subject: [PATCH] 优化;

---
 Plugin.TestResult/SingleCell.xaml.cs |  382 ++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 327 insertions(+), 55 deletions(-)

diff --git a/Plugin.TestResult/SingleCell.xaml.cs b/Plugin.TestResult/SingleCell.xaml.cs
index bae64dd..b9a6ab0 100644
--- a/Plugin.TestResult/SingleCell.xaml.cs
+++ b/Plugin.TestResult/SingleCell.xaml.cs
@@ -3,8 +3,10 @@
 using HMI.Core.Manager;
 using HMI.Core.Setting;
 using HMI.Core.Util;
+using HMI.UI.Util;
 using HMI.Util;
 using PLCHelper;
+using Plugin.TestResult;
 using Plugin.TestResult.Util;
 using Plugin.TestResult_WPF.Constant;
 using Plugin.TestResult_WPF.Model;
@@ -13,6 +15,7 @@
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
+using System.Dynamic;
 using System.IO;
 using System.Runtime.CompilerServices;
 using System.Windows;
@@ -49,7 +52,7 @@
         private InfoWindow window;
         private int maxSize;
 
-        private DataTable table;
+        public DataTable table { get; private set; }
 
         public SingleCell(DataTable table, string title, int index, int maxSize, string iniFile)
         {
@@ -296,47 +299,66 @@
 
         public void SetStatus(string status)
         {
-            var oldStatus = this.status;
-            this.status = status;
-            if (oldStatus != "testing" && status == "testing")
+            Dispatcher.Invoke(() =>
             {
-                UpdateProbeCount(ProbeCount + 1);
-                var now = DateTime.Now;
-                string dateStr = now.ToString("yyyyMMdd");
-                visualTestId = $"{title}_{now.ToString("yyyyMMddHHmmssfff")}";
-                helper = CheckSqlHelper(dateStr);
-            }
+                var oldStatus = this.status;
+                this.status = status;
+                if (oldStatus != "testing" && status == "testing")
+                {
+                    UpdateProbeCount(ProbeCount + 1);
+                    var now = DateTime.Now;
+                    string dateStr = now.ToString("yyyyMMdd");
+                    visualTestId = $"{title}_{now.ToString("yyyyMMddHHmmssfff")}";
+                    helper = CheckSqlHelper(dateStr);
+                    InsertARecourd();
+                }
 
-            if (oldStatus != "ok" && status == "ok")
-            {
-                SaveInfoToDB();
-            }
+                if (oldStatus != "ok" && status == "ok")
+                {
+                    SaveInfoToDB();
+                }
 
-            if (oldStatus != "ng" && status == "ng")
+                if (oldStatus != "ng" && status == "ng")
+                {
+                    SaveInfoToDB();
+                }
+            });
+        }
+
+        private void InsertARecourd()
+        {
+            try
             {
-                SaveInfoToDB();
+                string model = table?.CbProduct?.SelectedItem?.ToString();
+                var timestr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
+                var sql = $"INSERT INTO testResult (sn,model,station,position,result,time,resultId) VALUES ('{sn}','{model}','','{title}','{this.status}','{timestr}','{visualTestId}');";
+                helper.Execute(sql);
+            }
+            catch (Exception ex)
+            {
+                ViewControl.Logger.Error($"Cell[{title}] SaveToDB Error.", ex);
             }
         }
 
         public void WriteCSV(Info info)
         {
-            try
-            {
-                if (ParamSetting.CustomerCode != "LIXUN")
-                {
-                    return;
-                }
-                string fileName = this.sn?.Replace("/", "#")?.Replace("\\", "#") + ".csv";
-                string dir = Path.Combine(ParamSetting.DataLogFolder, "csv", this.title);
-                string title = $"{this.lang("Test.Name")},{this.lang("Test.Value")},{this.lang("Test.LimitDown")},{this.lang("Test.LimitUp")},{this.lang("Test.Unit")},{this.lang("Test.Result")},{this.lang("Test.TestTime")}";
-                string line = $"{info.step},{info.value},{info.limitDown},{info.limitUp},{info.unit},{info.result},{info.updateTime}";
+            //try
+            //{
+            //    if (ParamSetting.CustomerCode != "LIXUN")
+            //    {
+            //        return;
+            //    }
+            //    string fileName = this.sn?.Replace("/", "#")?.Replace("\\", "#") + ".csv";
+            //    string dir = Path.Combine(ParamSetting.DataLogFolder, "csv", this.title);
+            //    string title = $"{this.lang("Test.Name")},{this.lang("Test.Value")},{this.lang("Test.LimitDown")},{this.lang("Test.LimitUp")},{this.lang("Test.Unit")},{this.lang("Test.Result")},{this.lang("Test.TestTime")}";
+            //    string line = $"{info.step},{info.value},{info.limitDown},{info.limitUp},{info.unit},{info.result},{info.updateTime}";
 
-                CSVPrinter.Print(dir, fileName, title, line);
-                // ReverseCSV(Path.Combine(dir, fileName));
-            }
-            catch (Exception ex)
-            {
-            }
+            //    CSVPrinter.Print(dir, fileName, title, line);
+            //    // ReverseCSV(Path.Combine(dir, fileName));
+            //}
+            //catch (Exception ex)
+            //{
+            //}
         }
 
         public void SendToChart(Info info)
@@ -378,30 +400,47 @@
                 allInfos.Add(info);
                 SendToChart(info);
                 WriteCSV(info);
-                if (queue.Count >= maxSize)
+
+                /// 鐩墠鍏堜笉闄愬埗鏁伴噺锛屽悗缁鏋滄暟鎹繃澶氬鑷存�ц兘闂鍐嶈�冭檻闄愬埗鏁伴噺
+                //if (queue.Count >= maxSize)
+                //{
+                //    queue.TryDequeue(out var first);
+                //    queue.Enqueue(info);
+                //    this.infos = new ObservableCollection<Info>(queue);
+                //    if (window != null) window.UpdateSource(this.infos);
+                //}
+                //else
+                //{
+                //    queue.Enqueue(info);
+                //    if (window != null)
+                //    {
+                //        window.Dispatcher.Invoke(() =>
+                //        {
+                //            this.infos.Add(info);
+                //        });
+                //    }
+                //    else
+                //    {
+                //        Dispatcher.Invoke(() =>
+                //        {
+                //            this.infos.Add(info);
+                //        });
+                //    }
+                //}
+
+                if (window != null)
                 {
-                    queue.TryDequeue(out var first);
-                    queue.Enqueue(info);
-                    this.infos = new ObservableCollection<Info>(queue);
-                    if (window != null) window.UpdateSource(this.infos);
+                    window.Dispatcher.Invoke(() =>
+                    {
+                        this.infos.Add(info);
+                    });
                 }
                 else
                 {
-                    queue.Enqueue(info);
-                    if (window != null)
+                    Dispatcher.Invoke(() =>
                     {
-                        window.Dispatcher.Invoke(() =>
-                        {
-                            this.infos.Add(info);
-                        });
-                    }
-                    else
-                    {
-                        Dispatcher.Invoke(() =>
-                        {
-                            this.infos.Add(info);
-                        });
-                    }
+                        this.infos.Add(info);
+                    });
                 }
 
                 Dispatcher.Invoke(() =>
@@ -578,7 +617,7 @@
             }
         }
 
-        private void NormalCSV(string filePath)
+        public void NormalCSV(string filePath)
         {
             try
             {
@@ -606,7 +645,7 @@
             }
         }
 
-        private void ReverseCSV(string filePath)
+        public void ReverseCSV_Bak(string filePath)
         {
             try
             {
@@ -739,6 +778,150 @@
             }
         }
 
+        public void ReverseCSV(string filePath)
+        {
+            try
+            {
+                if (allInfos == null || allInfos.Count < 4) return;
+
+                Dictionary<string, int> columnMap = new Dictionary<string, int>();
+                int colPoint = 0;
+                List<string> titleRow = new List<string>();
+                List<string[]> datas = new List<string[]>();
+                List<string> timeRow = new List<string>();
+
+                void AddTitle(string title)
+                {
+                    if (titleRow.IndexOf(title) < 0)
+                    {
+                        titleRow.Add(title);
+                    }
+                }
+
+                // 鍏堣缃〃澶�
+                for (int i = 0; i < allInfos.Count; i++)
+                {
+                    var info = allInfos[i];
+                    if (IgnoreTestItem.Contains(info.step.ToLower()))
+                    {
+                        continue;
+                    }
+                    string testTime = info.updateTime;
+                    int rowIndex = timeRow.IndexOf(testTime);
+                    if (rowIndex < 0)
+                    {
+                        timeRow.Add(testTime);
+                    }
+
+                    string title = $"{info.step}{(string.IsNullOrEmpty(info.unit) ? "" : $"({info.unit})")}";
+                    AddTitle("娴嬭瘯鏃堕棿");
+                    AddTitle(title);
+                    if (!string.IsNullOrEmpty(info.limitDown))
+                    {
+                        string key2 = $"{info.step}涓嬮檺";
+                        AddTitle(key2);
+                    }
+
+                    if (!string.IsNullOrEmpty(info.limitUp))
+                    {
+                        string key2 = $"{info.step}涓婇檺";
+                        AddTitle(key2);
+                    }
+
+                    if (!string.IsNullOrEmpty(info.limitUp))
+                    {
+                        string key2 = $"{info.step}娴嬭瘯缁撴灉";
+                        AddTitle(key2);
+                    }
+
+                    //if (!string.IsNullOrEmpty(info.updateTime))
+                    //{
+                    //    string key2 = $"{info.step}娴嬭瘯鏃堕棿";
+                    //    AddTitle(key2);
+                    //}
+                }
+
+                for (int i = 0; i < allInfos.Count; i++)
+                {
+                    var info = allInfos[i];
+                    if (IgnoreTestItem.Contains(info.step.ToLower()))
+                    {
+                        continue;
+                    }
+                    string testTime = info.updateTime;
+                    int row = timeRow.IndexOf(testTime);
+                    if (row < 0)
+                    {
+                        continue;
+                    }
+
+
+
+                    string title = $"{info.step}{(string.IsNullOrEmpty(info.unit) ? "" : $"({info.unit})")}";
+                    int col = titleRow.IndexOf(title);
+
+                    if (datas.Count <= row)
+                    {
+                        datas.Add(new string[titleRow.Count]);
+                    }
+                    var arr = datas[row];
+                    arr[col] = info.value;
+
+
+                    if (!string.IsNullOrEmpty(info.limitDown))
+                    {
+                        string key2 = $"{info.step}涓嬮檺";
+                        int col2 = titleRow.IndexOf(key2);
+                        arr[col2] = info.limitDown;
+                    }
+
+                    if (!string.IsNullOrEmpty(info.limitUp))
+                    {
+                        string key2 = $"{info.step}涓婇檺";
+                        int col2 = titleRow.IndexOf(key2);
+                        arr[col2] = info.limitUp;
+                    }
+
+                    if (!string.IsNullOrEmpty(info.limitUp))
+                    {
+                        string key2 = $"{info.step}娴嬭瘯缁撴灉";
+                        int col2 = titleRow.IndexOf(key2);
+                        arr[col2] = info.result;
+                    }
+
+                    if (!string.IsNullOrEmpty(info.updateTime))
+                    {
+                        string key2 = "娴嬭瘯鏃堕棿";// $"{info.step}娴嬭瘯鏃堕棿";
+                        int col2 = titleRow.IndexOf(key2);
+                        arr[col2] = info.updateTime;
+                    }
+                }
+
+
+                FileInfo fi = new FileInfo(filePath);
+                if (!fi.Directory.Exists)
+                {
+                    fi.Directory.Create();  //鏂囦欢涓嶅瓨鍦紝鍒涘缓鏂囦欢
+                }
+
+                using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
+                using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
+                {
+                    string title = string.Join(",", titleRow);
+                    sw.WriteLine(title);
+                    foreach (var info in datas)
+                    {
+                        string row = string.Join(",", info);
+                        sw.WriteLine(row);
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                ViewControl.Logger.Error($"Cell[{title}] SaveInfoToCSV Error.", ex);
+            }
+        }
+
         /// <summary>
         /// 淇濆瓨娴嬭瘯鏁版嵁鍒癈SV
         /// </summary>
@@ -813,8 +996,10 @@
             {
                 try
                 {
+                    string model = table?.CbProduct?.SelectedItem?.ToString();
                     var timestr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
-                    var sql = $"INSERT INTO testResult (sn,station,position,result,time,resultId) VALUES ('{sn}','','{title}','{this.status}','{timestr}','{visualTestId}');";
+                    //var sql = $"INSERT INTO testResult (sn,model,station,position,result,time,resultId) VALUES ('{sn}','{model}','','{title}','{this.status}','{timestr}','{visualTestId}');";
+                    var sql = $"UPDATE testResult SET time = '{timestr}', result = '{this.status}' WHERE resultId = '{visualTestId}'";
                     helper.Execute(sql);
                     helper.Dispose();
                     helper = null;
@@ -850,11 +1035,43 @@
                 case "exportCSVItem":
                     TryExportCSV();
                     break;
+                case "manualDebugItem":
+                    OpenManualDebugWindow();
+                    break;
                 default:
                     break;
             }
         }
 
+        /// <summary>
+        /// 鎵撳紑鎵嬪姩璋冭瘯绐楀彛
+        /// </summary>
+        private void OpenManualDebugWindow()
+        {
+            string manualDebugSeq = ParameterManager.GetCommonParameterByName("ManualDebugSeqPath")?.Value;
+            if (string.IsNullOrEmpty(manualDebugSeq))
+            {
+                MsgBoxHelper2.Error("璇峰厛璁剧疆鎵嬪姩璋冭瘯搴忓垪璺緞[ManualDebugSeqPath]!");
+                return;
+            }
+            string modelPath = table.parent.testModelFile;
+            int dutNo = table.index * table.cellCount + this.index;
+            var win = new ManualDebugWindow(manualDebugSeq, modelPath)
+            {
+                Title = $"{this.title}鎵嬪姩璋冭瘯",
+                Owner = Window.GetWindow(this),
+                OvenIndex = this.table.index,
+                OvenName = this.table.title,
+
+                CellIndex = dutNo,
+                CellName = this.title
+            };
+            win.ShowDialog();
+        }
+
+        /// <summary>
+        /// 灏濊瘯瀵煎嚭CSV
+        /// </summary>
         private void TryExportCSV()
         {
             System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog
@@ -863,7 +1080,7 @@
             };
             if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
-                this.SaveInfoToCSV(sfd.FileName);
+                this.NormalCSV(sfd.FileName);
             }
         }
 
@@ -885,5 +1102,60 @@
             return S7Context.WriteData(RunTimeVariablePool.DEFAULT_PLC_NAME,
                 $"Oven{table.index + 1}_Cell{index + 1}_Light_NG", on, out msg);
         }
+
+        private void OnPreviewMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
+        {
+
+            ContextMenu contextMenu = new ContextMenu();
+            contextMenu.Closed += ContextMenu_Closed;
+
+            //<  />
+            //<
+            //< MenuItem IsCheckable = "False" x: Name = "manualDebugItem" Click = "MenuItem_Click" Header = "鎵嬪姩璋冭瘯" >
+            //    < MenuItem.Style >
+            //        < Style TargetType = "MenuItem" BasedOn = "{StaticResource {x:Static pu:StyleKeys.MenuItemStyle}}" >
+
+            //        </ Style >
+            //    </ MenuItem.Style >
+            //</ MenuItem >
+
+            var item = new MenuItem
+            {
+                IsCheckable = false,
+                IsEnabled = !table.IsTesting,
+                Name = "clearSNRItem",
+                Header = this.lang("Tr.ClearSNR"),
+            };
+            item.Click += MenuItem_Click;
+            contextMenu.Items.Add(item);
+
+            item = new MenuItem
+            {
+                IsCheckable = false,
+                Name = "exportCSVItem",
+                Header = this.lang("App.ExportCSV"),
+            };
+            item.Click += MenuItem_Click;
+            contextMenu.Items.Add(item);
+
+            if (table.parent.inManualDebugMode)
+            {
+                item = new MenuItem
+                {
+                    IsCheckable = false,
+                    Name = "manualDebugItem",
+                    Header = this.lang("鎵嬪姩璋冭瘯"),
+                };
+                item.Click += MenuItem_Click;
+                contextMenu.Items.Add(item);
+            }
+
+            this.ContextMenu = contextMenu;
+        }
+
+        private void ContextMenu_Closed(object sender, RoutedEventArgs e)
+        {
+            this.ContextMenu = null;
+        }
     }
 }

--
Gitblit v1.9.1