From 7bf89fd93bc487e8445d6d85bb527251b7371a04 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期一, 12 五月 2025 13:14:11 +0800
Subject: [PATCH] 优化项目;

---
 HMI.DataViewer/DbSettingModal.xaml.cs |   69 ++++++++++++++++++++++++++++------
 1 files changed, 56 insertions(+), 13 deletions(-)

diff --git a/HMI.DataViewer/DbSettingModal.xaml.cs b/HMI.DataViewer/DbSettingModal.xaml.cs
index 2902aad..5ab9ed8 100644
--- a/HMI.DataViewer/DbSettingModal.xaml.cs
+++ b/HMI.DataViewer/DbSettingModal.xaml.cs
@@ -6,8 +6,10 @@
 using Microsoft.Win32;
 using Panuon.WPF.UI;
 using System;
+using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
+using System.IO;
 using System.Linq;
 using System.Windows.Controls;
 
@@ -87,7 +89,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;
                 }
@@ -133,7 +135,16 @@
             programInput.Text = Data.Program;
             dbFilePathInput.Text = Data.DBFilePath;
             tableNameInput.Text = Data.TableName;
-            
+            Conditions = data?.Condition ?? new ObservableCollection<QueryCondition>();
+
+            if (!string.IsNullOrEmpty(Data.DBFilePath) && File.Exists(Data.DBFilePath))
+            {
+                UpdateDbHelper(Data.DBFilePath);
+                UpdateTableSource();
+                tableNameInput.SelectedItem = Data.TableName;
+                UpdateColumnSource(Data.TableName);
+            }
+
             return this.ShowDialog();
         }
 
@@ -164,7 +175,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 +187,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 +257,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