From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点

---
 HMI.DataViewer/DbSettingModal.xaml.cs |  297 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 297 insertions(+), 0 deletions(-)

diff --git a/HMI.DataViewer/DbSettingModal.xaml.cs b/HMI.DataViewer/DbSettingModal.xaml.cs
new file mode 100644
index 0000000..66051b3
--- /dev/null
+++ b/HMI.DataViewer/DbSettingModal.xaml.cs
@@ -0,0 +1,297 @@
+锘縰sing HMI.Core.Util;
+using HMI.DataViewer.Entity;
+using HMI.UI.DpUtil;
+using HMI.UI.Util;
+using log4net;
+using Microsoft.Win32;
+using Panuon.WPF.UI;
+using System;
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+using System.IO;
+using System.Linq;
+using System.Windows.Controls;
+
+namespace HMI.DataViewer
+{
+    /// <summary>
+    /// ModalWindow.xaml 鐨勪氦浜掗�昏緫
+    /// </summary>
+    public partial class DbSettingModal : WindowX
+    {
+        public DbSettingModal(ILog log)
+        {
+            this.log = log;
+            this.DataContext = this;
+            InitializeComponent();
+        }
+        private ILog log;
+        public string FilePath { get; private set; }
+        public int DataIndex { get; private set; }
+        public DataBase_Setting Data { get; private set; }
+        public ObservableCollection<DataBase_Setting> Datasource { get; private set; }
+
+        private SqliteHelper helper;
+        public SqliteHelper Helper
+        {
+            get => helper;
+            set
+            {
+                if (helper == value) return;
+                helper?.Close();
+                helper = value;
+            }
+        }
+
+        private ObservableCollection<string> columns = new ObservableCollection<string>();
+        public ObservableCollection<string> Columns
+        {
+            get => columns;
+            set { Set(ref columns, value); }
+        }
+
+        private ObservableCollection<QueryCondition> conditions =
+            new ObservableCollection<QueryCondition>();
+        public ObservableCollection<QueryCondition> Conditions
+        {
+            get => conditions;
+            set { Set(ref conditions, value); }
+        }
+
+
+        private bool _isProcessing;
+
+        protected override void OnClosing(CancelEventArgs e)
+        {
+            //Press Continue Button
+            if (DialogResult == true)
+            {
+                if (_isProcessing)
+                {
+                    e.Cancel = true;
+                    return;
+                }
+                var okButton = WindowXModalDialog.GetOKButton(this);
+                try
+                {
+                    _isProcessing = true;
+                    ButtonHelper.SetIsPending(okButton, true);
+                    if (!ValidateForm())
+                    {
+                        e.Cancel = true;
+                        return;
+                    }
+
+                    SaveOrUpdateData();
+                    Helper?.Close();
+                }
+                catch (Exception ex)
+                {
+                    log.Error("IPManage Form Submit Failed.", ex);
+                    ToastHelper.Error(this, $"{this.lang("App.App.UnhandledException")}: {ex.Message}");
+                    e.Cancel = true;
+                    return;
+                }
+                finally
+                {
+                    _isProcessing = false;
+                    ButtonHelper.SetIsPending(okButton, false);
+                }
+            }
+            base.OnClosing(e);
+        }
+
+        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;
+        }
+
+        private bool ValidateForm()
+        {
+            bool flag = FormGroupHelper.ValidateForm(formPanel);
+            if (flag)
+            {
+                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;
+            }
+            return flag;
+        }
+
+        public bool? Open(int index, DataBase_Setting data,
+            ObservableCollection<DataBase_Setting> dataSource,
+            string filePath)
+        {
+            FilePath = filePath;
+            DataIndex = index;
+            Data = data;
+            Datasource = dataSource;
+
+            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();
+        }
+
+        private void SaveOrUpdateData()
+        {
+            if (DataIndex == -1)
+            {
+                InsertData(Data);
+            }
+            else
+            {
+                UpdateData(DataIndex, Data);
+            }
+        }
+
+        public void InsertData(DataBase_Setting data)
+        {
+            Datasource.Add(data);
+            XmlHelper.SerializeToXml(FilePath, Datasource);
+        }
+
+        public void UpdateData(int index, DataBase_Setting data)
+        {
+            Datasource[index] = data;
+            XmlHelper.SerializeToXml(FilePath, Datasource);
+        }
+
+        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
+        {
+            Button btn = sender as Button;
+            switch (btn.Name)
+            {
+                case "searchBtn":
+                    OpenSelectDbDialog();
+                    break;
+                case "selectAllBtn":
+                    SelectAll();
+                    break;
+                case "addBtn":
+                    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.ConfigFolder;
+
+            OpenFileDialog openFileDialog = new OpenFileDialog();
+            openFileDialog.Filter = "(*.db)|*.db";
+            openFileDialog.InitialDirectory = BasePath;
+            openFileDialog.Title = this.lang("Dv.SelectDbFile");
+            openFileDialog.Multiselect = false;
+
+            if (openFileDialog.ShowDialog() == true)
+            {
+                string FileName = openFileDialog.FileName;
+                dbFilePathInput.Text = FileName;
+                UpdateDbHelper(FileName);
+                UpdateTableSource();
+            }
+        }
+
+        private void SelectAll()
+        {
+            columnTable.SelectAll();
+            AddCondition();
+        }
+
+        private void AddCondition()
+        {
+            if (columnTable.SelectedItems == null || columnTable.SelectedItems.Count <= 0) return;
+            var temp = new ObservableCollection<QueryCondition>();
+            foreach (var column in columnTable.SelectedItems)
+            {
+                temp.Add(new QueryCondition { Key = column.ToString(), Desc = column.ToString() });
+            }
+            Conditions = temp;
+        }
+
+        private void RemoveCondition()
+        {
+            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)
+        {
+            RadioButton btn = sender as RadioButton;
+            var condition = btn.DataContext as QueryCondition;
+            if (condition.IsTimeCol == true)
+            {
+                condition.IsTimeCol = false;
+            }
+            else
+            {
+                condition.IsTimeCol = true;
+                foreach (var c in Conditions)
+                {
+                    if (c != condition) c.IsTimeCol = false;
+                }
+            }
+        }
+    }
+}

--
Gitblit v1.9.1