From 559890c482f5973f06aafdfec9550fee5ba3c7f2 Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 09 十一月 2026 17:02:15 +0800
Subject: [PATCH] feat: CAN 模拟器 TCP 化 + 多设备并发调试 + 后台生命周期管理
---
HMI.MenuManage/MenuManage.xaml.cs | 157 ++++++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 140 insertions(+), 17 deletions(-)
diff --git a/HMI.MenuManage/MenuManage.xaml.cs b/HMI.MenuManage/MenuManage.xaml.cs
index 8ffc3c0..3ab7a4c 100644
--- a/HMI.MenuManage/MenuManage.xaml.cs
+++ b/HMI.MenuManage/MenuManage.xaml.cs
@@ -3,8 +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;
@@ -17,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 IIF(m1.level=1,m1.sort,m2.sort),IIF(m1.level=1,0,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)
@@ -37,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)
@@ -56,7 +167,7 @@
{
try
{
- Model.LoadData();
+ LoadData();
}
catch (Exception ex)
{
@@ -68,19 +179,19 @@
{
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.Menu.NoDeleteData"));
+ ToastHelper.Warning(Window.GetWindow(this), LangUtil.Get("HMI.NoDeleteData"));
return;
}
var res = MsgBoxHelper.WarnMessage(Window.GetWindow(this),
- string.Format(LangUtil.Get("HMI.Menu.MakeSureDeletes"), ids.Count));
+ string.Format(LangUtil.Get("HMI.MakeSureDeletes"), ids.Count));
if (res == MessageBoxResult.OK)
{
- Model.DeleteMenuByIds(ids);
- Model.LoadData();
+ DeleteMenuByIds(ids);
+ LoadData();
}
}
catch (Exception ex)
@@ -97,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)
@@ -118,11 +229,11 @@
{
var data = (MenuData)((Button)sender).DataContext;
var res = MsgBoxHelper.WarnMessage(Window.GetWindow(this),
- string.Format(LangUtil.Get("HMI.Menu.MakeSureDeleteSingle"), data.Name));
+ 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)
@@ -133,9 +244,21 @@
}
}
+ public override void DoClose(object sender, EventArgs e)
+ {
+ try
+ {
+ sqliteHelper?.Close();
+ }
+ catch (Exception ex)
+ {
+ log.Warn("Do Close Failed.", ex);
+ }
+ }
+
private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
- Model.LoadData();
+ LoadData();
Loaded -= DataGrid_Loaded;
}
--
Gitblit v1.9.1