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

---
 HMI.LogViewer/LogViewer.xaml.cs |  142 +++++++++++++++++++++++++++++++----------------
 1 files changed, 93 insertions(+), 49 deletions(-)

diff --git a/HMI.LogViewer/LogViewer.xaml.cs b/HMI.LogViewer/LogViewer.xaml.cs
index 3920fbb..a2fc8f4 100644
--- a/HMI.LogViewer/LogViewer.xaml.cs
+++ b/HMI.LogViewer/LogViewer.xaml.cs
@@ -33,23 +33,26 @@
 
         private Timer autoRfhTimer;
 
-        private int currentPage = 1;
-        public int CurrentPage
+        private long pageSize = 50;
+
+        private long currentPage = 1;
+        public long CurrentPage
         {
             get => currentPage;
             set
             {
                 Set(ref currentPage, value);
+                Filtrate(value, pageSize);
             }
         }
 
-        private int maxPage = 0;
-        public int MaxPage
+        private long maxPage = 1;
+        public long MaxPage
         {
             get => maxPage;
             set
             {
-                Set(ref currentPage, value);
+                Set(ref maxPage, value);
             }
         }
 
@@ -65,6 +68,7 @@
             };
             autoRfhTimer.Elapsed += AutoRfhTimer_Elapsed;
             InitializeComponent();
+            QueryReset();
         }
 
         public override void InitFinish()
@@ -93,7 +97,6 @@
             //}
         }
 
-
         private void UserControl_Loaded(object sender, RoutedEventArgs e)
         {
             Loaded -= UserControl_Loaded;
@@ -103,6 +106,10 @@
         {
             Refresh100items();
         }
+
+        //private void RefreshTable() { 
+        //    if(CurrentPage == )
+        //}
 
         private void Button_Click(object sender, RoutedEventArgs e)
         {
@@ -114,6 +121,7 @@
                     break;
                 case "resetBtn":
                     QueryReset();
+                    CurrentPage = 1;
                     break;
                 case "exportBtn":
                     DoExport();
@@ -144,9 +152,10 @@
                     return;
                 }
 
-                queryBtn.IsEnabled = false;
-                autoRfhInput.IsEnabled = false;
-                Filtrate();
+                AutoRfh = false;
+                //queryBtn.IsEnabled = false;
+                //autoRfhInput.IsEnabled = false;
+                CurrentPage = 1;
             }
             catch (Exception ex)
             {
@@ -162,31 +171,60 @@
 
         private void QueryReset()
         {
+            var today = DateTime.Now;
+            var start = DateTime.Parse(today.ToString("yyyy-MM-dd 00:00:00")).AddDays(-7);
+            var end = DateTime.Parse(today.ToString("yyyy-MM-dd 00:00:00")).AddDays(1);
+
             levelInput.Text = null;
-            startTimeInput.SelectedDateTime = null;
-            endTimeInput.SelectedDateTime = null;
-            startTimeInput.Text = null;
-            table.ItemsSource = null;
+            startTimeInput.SelectedDateTime = start;
+            endTimeInput.SelectedDateTime = end;
         }
 
-        private void Filtrate()
+        private void Filtrate(long page, long size)
         {
-            string str = LoadSql();
-            DataTable dt = new DataTable();
-            var files = GetOldDbFiles(PathHelper.LogViewerDir, new DateTime?[] { startTimeInput.SelectedDateTime,
+            try
+            {
+                var files = GetOldDbFiles(PathHelper.LogViewerDir, new DateTime?[] { startTimeInput.SelectedDateTime,
                 endTimeInput.SelectedDateTime });
-            foreach (string file in files)
-            {
-                using (SqliteHelper sql = new SqliteHelper(file))
+                if (files.Length <= 0)
                 {
-                    dt.Merge(sql.GetDataTable(str));
+                    table.ItemsSource = new DataTable().DefaultView;
+                    return;
                 }
+
+                using (Sqlite3Helper helper = new Sqlite3Helper(files[0]))
+                {
+                    for (int i = 0; i < files.Length; i++)
+                    {
+                        var attach = $"ATTACH DATABASE '{files[i]}' AS 'db{i}'";
+                        helper.Execute(attach);
+                    }
+
+                    string sql = string.Join(" UNION ALL ", GetSqls(files));
+
+                    string sortName = "time", sortDirection = "DESC";
+
+                    string baseSql = $"SELECT level,time,source,text FROM ({sql}) as _temp";
+
+                    string countSql = $"SELECT COUNT(1) FROM ({sql}) as __temp";
+
+                    long total = (long)helper.SelectOne(countSql);
+                    long pages = (total/pageSize)+ ((total % pageSize) > 0 ? 1 : 0);
+
+                    sql = $"{baseSql} ORDER BY {sortName} {sortDirection} LIMIT {size} OFFSET {(page - 1) * size} ";
+
+                    DataTable dt = helper.GetDataTable(sql);
+                    table.ItemsSource = dt.DefaultView;
+
+                    MaxPage = pages;
+                }
+
             }
-            if (dt.Rows.Count > 0)
+            catch (Exception ex)
             {
-                dt.DefaultView.Sort = "time Desc";
+                log.Error("Select Log Error.", ex);
+                MsgBoxHelper2.Error($"鏌ヨ澶辫触: {ex.Message}");
             }
-            table.ItemsSource = dt.DefaultView;
         }
 
         public static string[] GetOldDbFiles(string folderPath, DateTime?[] range)
@@ -217,35 +255,40 @@
         }
 
         /// <summary>
-        /// 鍔犺浇鐣岄潰鍒ゆ柇鏁版嵁搴撹鍙�
+        /// 
         /// </summary>
-        /// <returns> str鏁版嵁搴撶殑璇彞 </returns>
-        private string LoadSql()
+        /// <param name="files"></param>
+        /// <returns></returns>
+        private string[] GetSqls(string[] files)
         {
-            string sql = " select level,time,source,text from log";
-            List<string> datas = new List<string>();
-            if (!string.IsNullOrEmpty(levelInput.Text))
+            string[] sqls = new string[files.Length];
+            for (int i = 0; i < files.Length; i++)
             {
-                datas.Add($"level='{levelInput.Text}'");
-            }
-            if (!string.IsNullOrEmpty(startTimeInput.Text))
-            {
-                datas.Add($"time>='{startTimeInput.Text}'");
-            }
-            if (!string.IsNullOrEmpty(endTimeInput.Text))
-            {
-                datas.Add($"time<='{endTimeInput.Text}'");
-            }
-            if (!string.IsNullOrEmpty(sourceInput.Text))
-            {
-                datas.Add($"source='{sourceInput.Text}'");
-            }
+                string sql = $"SELECT level,time,source,text FROM db{i}.log";
+                List<string> datas = new List<string>();
+                if (!string.IsNullOrEmpty(levelInput.Text))
+                {
+                    datas.Add($"level='{levelInput.Text}'");
+                }
+                if (!string.IsNullOrEmpty(startTimeInput.Text))
+                {
+                    datas.Add($"time>='{startTimeInput.Text}'");
+                }
+                if (!string.IsNullOrEmpty(endTimeInput.Text))
+                {
+                    datas.Add($"time<'{endTimeInput.Text}'");
+                }
+                if (!string.IsNullOrEmpty(sourceInput.Text))
+                {
+                    datas.Add($"source='{sourceInput.Text}'");
+                }
 
-            string condition = string.Join(" AND ", datas);
-            condition = string.IsNullOrEmpty(condition) ? string.Empty : $" WHERE {condition}";
-
-            sql = $"{sql}{condition} order by time desc";
-            return sql;
+                string condition = string.Join(" AND ", datas);
+                condition = string.IsNullOrEmpty(condition) ? string.Empty : $" WHERE {condition}";
+                sql = $"{sql}{condition}";
+                sqls[i] = sql;
+            }
+            return sqls;
         }
 
         /// <summary>
@@ -267,6 +310,7 @@
                         this.Dispatcher.BeginInvoke((Action)delegate ()
                         {
                             table.ItemsSource = dt.DefaultView;
+                            MaxPage = 1;
                         });
                     }
                 }

--
Gitblit v1.9.1