From e3868d4d61ded0e76fc8fe3fdacdd9e6d9113cf1 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期一, 13 十月 2025 08:50:15 +0800
Subject: [PATCH] 优化HMI;
---
HMI.MenuManage/MenuManage.xaml.cs | 140 +++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 125 insertions(+), 15 deletions(-)
diff --git a/HMI.MenuManage/MenuManage.xaml.cs b/HMI.MenuManage/MenuManage.xaml.cs
index fa38a3a..b812ce4 100644
--- a/HMI.MenuManage/MenuManage.xaml.cs
+++ b/HMI.MenuManage/MenuManage.xaml.cs
@@ -3,9 +3,13 @@
using HMI.MenuManage.Entity;
using HMI.MenuManage.Model;
using HMI.UI.Util;
+using log4net;
using System;
using System.Collections.Generic;
+using System.Collections.ObjectModel;
using System.ComponentModel;
+using System.Data;
+using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
@@ -18,14 +22,120 @@
public partial class MenuManage : PluginControl
{
#region 灞炴��
- private MenuManageModel Model;
+ public SqliteHelper sqliteHelper;
+
+ public int DataSourceCount { get => DataSource?.Count ?? 0; }
+
+ private ObservableCollection<MenuData> _DataSource;
+
+ public ObservableCollection<MenuData> DataSource
+ {
+ get => _DataSource;
+ set
+ {
+ Set(ref _DataSource, value);
+ Set(nameof(DataSourceCount));
+ }
+ }
+
+ private ObservableCollection<MenuData> _TreeDataSource;
+
+ public ObservableCollection<MenuData> TreeDataSource
+ {
+ get => _TreeDataSource;
+ set { Set(ref _TreeDataSource, value); }
+ }
#endregion
+
+ public static ILog PluginLog { get; private set; }
public MenuManage()
{
- Model = new MenuManageModel();
- this.DataContext = Model;
+ this.DataContext = this;
InitializeComponent();
+ if (!File.Exists(PathHelper.DatabasePath))
+ {
+ SqliteHelper.CreateDatabase(PathHelper.DatabasePath);
+ }
+ sqliteHelper = new SqliteHelper(PathHelper.DatabasePath);
+ sqliteHelper.ExecuteSql(@"CREATE TABLE IF NOT EXISTS menu (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ code TEXT NOT NULL,
+ name TEXT NOT NULL,
+ icon TEXT,
+ iconPath TEXT,
+ level TEXT,
+ type TEXT,
+ parentCode TEXT,
+ dllPath TEXT,
+ className TEXT,
+ sort REAL,
+ UNIQUE(code)
+ );");
+ }
+
+ public override void SetLogger(ILog logger)
+ {
+ base.SetLogger(logger);
+ PluginLog = logger;
+ }
+
+ public void LoadData()
+ {
+ ObservableCollection<MenuData> data = new ObservableCollection<MenuData>();
+ ObservableCollection<MenuData> treeData = new ObservableCollection<MenuData>();
+ DataTable dt = sqliteHelper.GetDataTable("SELECT m1.*,m2.name AS parentName FROM menu AS m1 LEFT JOIN menu AS m2 ON m1.parentCode = m2.code Order By m1.sort,m1.id;");
+ Dictionary<string, MenuData> cache = new Dictionary<string, MenuData>();
+ if (dt != null)
+ {
+ foreach (DataRow dr in dt.Rows)
+ {
+ var menu = new MenuData
+ {
+ Id = dr["id"].ToString(),
+ Code = dr["code"].ToString(),
+ Name = dr["name"].ToString(),
+ Icon = dr["icon"].ToString(),
+ Level = dr["level"].ToString(),
+ Type = dr["type"].ToString(),
+ ParentCode = dr["parentCode"].ToString(),
+ ParentName = dr["parentName"].ToString(),
+ DllPath = dr["dllPath"].ToString(),
+ ClassName = dr["className"].ToString(),
+ Sort = dr["sort"] == DBNull.Value ? 0 : double.Parse(dr["sort"].ToString()),
+ };
+ data.Add(menu);
+ if (menu.Level == "1")
+ {
+ treeData.Add(menu);
+ }
+ cache.Add(menu.Code, menu);
+ }
+
+ foreach (var menu in data)
+ {
+ if (!string.IsNullOrEmpty(menu.ParentCode) && menu.ParentCode != menu.Code)
+ {
+ cache[menu.ParentCode].Children.Add(menu);
+ }
+ }
+ }
+
+ TreeDataSource = treeData;
+ DataSource = data;
+ }
+
+ public void DeleteMenuById(string id)
+ {
+ string sql = $"DELETE FROM menu WHERE id = {id};";
+ sqliteHelper.ExecuteSql(sql);
+ }
+
+ public void DeleteMenuByIds(IEnumerable<string> ids)
+ {
+ List<string> sqls = ids.Select(id => $"DELETE FROM menu WHERE id = {id};")
+ .ToList();
+ sqliteHelper.ExecuteBatchSql(sqls);
}
private void Button_Click(object sender, RoutedEventArgs e)
@@ -38,13 +148,13 @@
MenuModal window = new MenuModal
{
Title = $"{LangUtil.Get("HMI.Menu.Add")}",
- sqliteHelper = Model.sqliteHelper,
+ sqliteHelper = sqliteHelper,
Owner = Window.GetWindow(this)
};
if (true == window.Open(new MenuData()))
{
MenuData data = window.Data;
- Model.LoadData();
+ LoadData();
}
}
catch (Exception ex)
@@ -57,7 +167,7 @@
{
try
{
- Model.LoadData();
+ LoadData();
}
catch (Exception ex)
{
@@ -69,7 +179,7 @@
{
try
{
- List<string> ids = Model.DataSource.Where(x => x.Selected).Select(x => x.Id).ToList();
+ List<string> ids = DataSource.Where(x => x.Selected).Select(x => x.Id).ToList();
if (ids == null || ids.Count <= 0)
{
ToastHelper.Warning(Window.GetWindow(this), LangUtil.Get("HMI.NoDeleteData"));
@@ -80,8 +190,8 @@
string.Format(LangUtil.Get("HMI.MakeSureDeletes"), ids.Count));
if (res == MessageBoxResult.OK)
{
- Model.DeleteMenuByIds(ids);
- Model.LoadData();
+ DeleteMenuByIds(ids);
+ LoadData();
}
}
catch (Exception ex)
@@ -98,13 +208,13 @@
MenuModal window = new MenuModal
{
Title = $"{LangUtil.Get("HMI.Menu.Edit")}",
- sqliteHelper = Model.sqliteHelper,
+ sqliteHelper = sqliteHelper,
Owner = Window.GetWindow(this)
};
if (true == window.Open(data))
{
var newData = window.Data;
- Model.LoadData();
+ LoadData();
}
}
catch (Exception ex)
@@ -122,8 +232,8 @@
string.Format(LangUtil.Get("HMI.MakeSureDeleteSingle"), data.Name));
if (res == MessageBoxResult.OK)
{
- Model.DeleteMenuById(data.Id);
- Model.LoadData();
+ DeleteMenuById(data.Id);
+ LoadData();
}
}
catch (Exception ex)
@@ -138,7 +248,7 @@
{
try
{
- Model?.sqliteHelper?.Close();
+ sqliteHelper?.Close();
}
catch (Exception ex)
{
@@ -148,7 +258,7 @@
private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
- Model.LoadData();
+ LoadData();
Loaded -= DataGrid_Loaded;
}
--
Gitblit v1.9.1