From 1afe548ac40cc7f975917ef9bf2e156595cf81f3 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期四, 16 四月 2026 08:58:04 +0800
Subject: [PATCH] 优化;

---
 HMI.DataViewer/DbSettingModal.xaml.cs |   79 ++++++++++++++++++++++++++++++++-------
 1 files changed, 64 insertions(+), 15 deletions(-)

diff --git a/HMI.DataViewer/DbSettingModal.xaml.cs b/HMI.DataViewer/DbSettingModal.xaml.cs
index 2902aad..66051b3 100644
--- a/HMI.DataViewer/DbSettingModal.xaml.cs
+++ b/HMI.DataViewer/DbSettingModal.xaml.cs
@@ -8,6 +8,7 @@
 using System;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
+using System.IO;
 using System.Linq;
 using System.Windows.Controls;
 
@@ -87,7 +88,7 @@
                 catch (Exception ex)
                 {
                     log.Error("IPManage Form Submit Failed.", ex);
-                    ToastHelper.Error(this, $"鏈煡寮傚父锛亄ex.Message}");
+                    ToastHelper.Error(this, $"{this.lang("App.App.UnhandledException")}: {ex.Message}");
                     e.Cancel = true;
                     return;
                 }
@@ -100,10 +101,15 @@
             base.OnClosing(e);
         }
 
-        public bool ValidateDBAddress(object value, out string err)
+        public bool ValidateName(object value, out string err)
         {
             err = null;
-
+            var target = Datasource.FirstOrDefault(e => e.Program == value.ToString());
+            if (target != null && target != Data)
+            {
+                err = this.lang("App.MustBeUnique", LangUtil.Get("Dv.Datasource.Name")); ;
+                return false;
+            }
             return true;
         }
 
@@ -115,6 +121,7 @@
                 Data.Program = programInput.Text;
                 Data.DBFilePath = dbFilePathInput.Text;
                 Data.TableName = tableNameInput.Text;
+                Data.Pattern = TbPattern.Text;
                 Data.TimeCol = Conditions.FirstOrDefault(c => c.IsTimeCol)?.Key;
                 Data.Condition = Conditions;
             }
@@ -133,7 +140,17 @@
             programInput.Text = Data.Program;
             dbFilePathInput.Text = Data.DBFilePath;
             tableNameInput.Text = Data.TableName;
-            
+            TbPattern.Text = Data.Pattern;
+
+            if (!string.IsNullOrEmpty(Data.DBFilePath) && File.Exists(Data.DBFilePath))
+            {
+                UpdateDbHelper(Data.DBFilePath);
+                UpdateTableSource();
+                tableNameInput.SelectedItem = Data.TableName;
+                UpdateColumnSource(Data.TableName);
+            }
+            Conditions = data?.Condition ?? new ObservableCollection<QueryCondition>();
+
             return this.ShowDialog();
         }
 
@@ -164,7 +181,8 @@
         private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
         {
             Button btn = sender as Button;
-            switch (btn.Name) {
+            switch (btn.Name)
+            {
                 case "searchBtn":
                     OpenSelectDbDialog();
                     break;
@@ -175,31 +193,56 @@
                     AddCondition();
                     break;
                 case "delBtn":
+                    RemoveCondition();
                     break;
                 default:
                     break;
             }
 
-            
+
+        }
+
+        private void UpdateDbHelper(string filePath)
+        {
+            if (Helper == null)
+            {
+                Helper = new SqliteHelper(filePath);
+            }
+            else if (Helper.DataBaseFile != filePath)
+            {
+                //Helper.Close();
+                Helper = new SqliteHelper(filePath);
+            }
+        }
+
+        private void UpdateTableSource()
+        {
+            string[] tableNames = Helper.GetAllTableInFile();
+            tableNameInput.ItemsSource = tableNames;
+        }
+
+        private void UpdateColumnSource(string tableName)
+        {
+            string[] cols = Helper.GetColumnNameFromTable(tableName);
+            Columns = new ObservableCollection<string>(cols);
         }
 
         private void OpenSelectDbDialog()
         {
-            string BasePath = PathHelper.IconFolder;
+            string BasePath = PathHelper.ConfigFolder;
 
             OpenFileDialog openFileDialog = new OpenFileDialog();
             openFileDialog.Filter = "(*.db)|*.db";
             openFileDialog.InitialDirectory = BasePath;
-            openFileDialog.Title = "閫夋嫨鏁版嵁搴撴枃浠�";
+            openFileDialog.Title = this.lang("Dv.SelectDbFile");
             openFileDialog.Multiselect = false;
 
             if (openFileDialog.ShowDialog() == true)
             {
                 string FileName = openFileDialog.FileName;
                 dbFilePathInput.Text = FileName;
-                Helper = new SqliteHelper(FileName);
-                string[] tableNames = Helper.GetAllTableInFile();
-                tableNameInput.ItemsSource = tableNames;
+                UpdateDbHelper(FileName);
+                UpdateTableSource();
             }
         }
 
@@ -220,11 +263,17 @@
             Conditions = temp;
         }
 
-
-        private void ComboxSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
+        private void RemoveCondition()
         {
-            string[] cols = Helper.GetColumnNameFromTable(e.AddedItems[0].ToString());
-            Columns = new ObservableCollection<string>(cols);
+            if (conditionTable.SelectedItems == null || conditionTable.SelectedItems.Count <= 0) return;
+            Conditions = new ObservableCollection<QueryCondition>(
+                Conditions.Where(d => !conditionTable.SelectedItems.Contains(d)).ToList());
+        }
+
+        private void ComboxSelectionChanged(object sender, SelectionChangedEventArgs e)
+        {
+            UpdateColumnSource(e.AddedItems[0].ToString());
+            //Conditions = new ObservableCollection<QueryCondition>();
         }
 
         private void RadioButton_Click(object sender, System.Windows.RoutedEventArgs e)

--
Gitblit v1.9.1