From 1fca2167068e9ef2c079ec742f20313fc6697ab1 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期一, 03 三月 2025 09:18:33 +0800
Subject: [PATCH] 完善代码;

---
 HMI.DataViewer/DataViewer.xaml.cs |  196 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 194 insertions(+), 2 deletions(-)

diff --git a/HMI.DataViewer/DataViewer.xaml.cs b/HMI.DataViewer/DataViewer.xaml.cs
index 7616abc..0b7ca90 100644
--- a/HMI.DataViewer/DataViewer.xaml.cs
+++ b/HMI.DataViewer/DataViewer.xaml.cs
@@ -1,6 +1,16 @@
-锘縰sing System.Windows;
+锘縰sing HMI.Base;
+using HMI.Core.Util;
+using HMI.DataViewer.Entity;
+using HMI.UI.Util;
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Data;
+using System.IO;
+using System.Linq;
+using System.Windows;
 using System.Windows.Controls;
-using HMI.Base;
+using System.Windows.Data;
 
 namespace HMI.DataViewer
 {
@@ -11,12 +21,194 @@
     {
         public DataViewer()
         {
+            DataContext = this;
             InitializeComponent();
         }
 
+        private string SourceFilePath;
+
+        private ObservableCollection<DataBase_Setting> settings = new ObservableCollection<DataBase_Setting>();
+        public ObservableCollection<DataBase_Setting> Settings
+        {
+            get => settings;
+            set { Set(ref settings, value); }
+        }
+
+        private ObservableCollection<QueryData> datas = new ObservableCollection<QueryData>();
+        public ObservableCollection<QueryData> Datas
+        {
+            get => datas;
+            set { Set(ref datas, value); }
+        }
+
+        private Dictionary<string, DataBase_Setting> Map = new Dictionary<string, DataBase_Setting>();
+
         private void Button_Click(object sender, RoutedEventArgs e)
         {
+            Button btn = sender as Button;
+            switch (btn.Name)
+            {
+                case "addBtn":
+                    OpenSettingModal(new DataBase_Setting());
+                    break;
+                case "rfhBtn":
+                    break;
+                case "delBtn":
+                    break;
+                case "srhBtn":
+                    SearchData();
+                    break;
+                case "resetBtn":
+                    ResetDataTable();
+                    break;
+                default:
+                    break;
+            }
+        }
 
+
+        private void OpenSettingModal(DataBase_Setting data)
+        {
+            DbSettingModal modal = new DbSettingModal(log)
+            {
+                Title = "鏂板鏁版嵁婧�",
+                Owner = Window.GetWindow(this)
+            };
+            if (modal.Open(-1, data, Settings, SourceFilePath) == true)
+            {
+                LoadData();
+            }
+        }
+
+        private void SearchData()
+        {
+            string program = programInput.Text;
+            if (!Map.ContainsKey(program))
+            {
+                ToastHelper.Error(Window.GetWindow(this), "鍙傛暟寮傚父锛岃鍒锋柊閲嶈瘯");
+                return;
+            }
+
+            var datas = Datas?.Where(d => !string.IsNullOrEmpty(d.Value)).ToList();
+            if (datas == null || datas.Count <= 0)
+            {
+                ToastHelper.Warning(Window.GetWindow(this), "璇疯缃煡璇㈡潯浠跺��");
+                return;
+            }
+
+            DataBase_Setting setting = Map[program];
+            
+
+            string fields = string.Join(",", Datas?.Select(d => $"[{d.Key}]").ToList());
+
+            string conditon = string.Join(" AND ", datas.Select(d => $"[{d.Key}] {d.Condition} '{d.Value}'").ToList());
+            string timeCol = setting.TimeCol;
+            if (string.IsNullOrEmpty(timeCol))
+            {
+                timeCol = "time";
+            }
+            if (!string.IsNullOrEmpty(startTimeInput.Text))
+            {
+                conditon += $" AND {timeCol} >= '{startTimeInput.Text}'";
+            }
+            if (!string.IsNullOrEmpty(endTimeInput.Text))
+            {
+                conditon += $" AND {timeCol} < '{endTimeInput.Text}'";
+            }
+            conditon = string.IsNullOrEmpty(conditon) ? string.Empty : $" WHERE {conditon}";
+            
+            SqliteHelper helper = null;
+            string sql = string.Empty;
+            try
+            {
+                helper = new SqliteHelper(setting.DBFilePath);
+                sql = $"SELECT {fields} FROM {setting.TableName} {conditon}";
+                DataTable dt = helper.GetDataTable(sql);
+
+                dataTable.ItemsSource = dt.DefaultView;
+            }
+            catch (Exception ex)
+            {
+                log.Error($"Select Data Failed. SQL: {sql}", ex);
+                ToastHelper.Error(Window.GetWindow(this), $"鏌ヨ澶辫触锛亄ex.Message}");
+
+            }
+            finally
+            {
+                helper?.Close();
+            }
+        }
+
+        private void ResetDataTable()
+        {
+            startTimeInput.SelectedDateTime = null;
+            endTimeInput.SelectedDateTime = null;
+            dataTable.ItemsSource = null;
+            foreach (var data in Datas)
+            {
+                data.Value = null;
+            }
+        }
+
+        public override void SetBaseFolder(string folder)
+        {
+            base.SetBaseFolder(folder);
+            SourceFilePath = Path.Combine(BaseFolder, "SourceData.setting");
+        }
+
+        private void PluginControl_Loaded(object sender, RoutedEventArgs e)
+        {
+            LoadData();
+            Loaded -= PluginControl_Loaded;
+        }
+
+        private void LoadData()
+        {
+            if (File.Exists(SourceFilePath))
+            {
+                List<string> names = new List<string>();
+                Settings = XmlHelper.DeserializeFromXml<ObservableCollection<DataBase_Setting>>(SourceFilePath);
+                Map.Clear();
+                foreach (var data in Settings)
+                {
+                    Map.Add(data.Program, data);
+                    names.Add(data.Program);
+                }
+                programInput.ItemsSource = names;
+            }
+        }
+
+        private void programInput_SelectionChanged(object sender, SelectionChangedEventArgs e)
+        {
+            var key = e.AddedItems[0].ToString();
+            if (!Map.ContainsKey(key))
+            {
+                ToastHelper.Error(Window.GetWindow(this), "鍙傛暟寮傚父锛岃鍒锋柊閲嶈瘯");
+                return;
+            }
+            DataBase_Setting setting = Map[key];
+            Datas = new ObservableCollection<QueryData>(
+                setting?.Condition?.Select(d => new QueryData { Key = d.Key, Name = d.Desc, Condition = "=" }).ToList());
+
+            var temp = dataTable.Columns[0];
+            dataTable.Columns.Clear();
+            dataTable.Columns.Add(temp);
+            foreach (var data in Datas)
+            {
+                AddDataGridTextColumn(data.Name, data.Key);
+            }
+        }
+
+        private void AddDataGridTextColumn(string header, string bindingPath)
+        {
+            DataGridTextColumn textColumn = new DataGridTextColumn
+            {
+                Header = header,
+                Binding = new Binding(bindingPath)
+                ,
+                Width = new DataGridLength(1, DataGridLengthUnitType.Star)
+            };
+            dataTable.Columns.Add(textColumn);
         }
     }
 }

--
Gitblit v1.9.1