alielie
2025-03-03 ee9b94104cbb5597c6033d46ec356771d383b21b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using Microsoft.Win32;
using Panuon.WPF.UI;
using HMI.Core.Annotation;
using HMI.Core.Entity;
using HMI.Core.Exceptions;
using HMI.Core.Util;
using HMI.MenuManage.Entity;
 
namespace HMI.MenuManage
{
    /// <summary>
    /// ModalWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MenuModal : WindowX
    {
        public MenuModal()
        {
            InitializeComponent();
        }
 
        public MenuData Data { get; set; }
        public SqliteHelper sqliteHelper { get; set; }
 
        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();
                }
                catch (Exception ex)
                {
                    Panuon.WPF.UI.Toast.Show(this, "Error!", ToastPosition.Top);
                    e.Cancel = true;
                    return;
                }
                finally
                {
                    _isProcessing = false;
                    ButtonHelper.SetIsPending(okButton, false);
                }
            }
            base.OnClosing(e);
        }
 
        private bool ValidateForm()
        {
            try
            {
                FormValidateException ex = new FormValidateException();
                string segment = string.IsNullOrEmpty(Data.Id) ? "" : $" AND id!='{Data.Id}'";
                string sql = $"SELECT 1 FROM menu WHERE code='{TbCode.Text}'{segment};";
                if (string.IsNullOrEmpty(TbCode.Text))
                {
                    ex.Result.Add(new FormGroupValidateResult("请输入菜单编号", FgCode));
                }
                else if (sqliteHelper.HasData(sql))
                {
                    ex.Result.Add(new FormGroupValidateResult("菜单编号不能重复", FgCode));
                }
                else
                {
                    FgCode.ValidateResult = ValidateResult.None;
                    FgCode.Message = null;
                }
 
                if (string.IsNullOrEmpty(TbName.Text))
                {
                    ex.Result.Add(new FormGroupValidateResult("请输入菜单名称", FgName));
                }
                else
                {
                    FgName.ValidateResult = ValidateResult.None;
                    FgName.Message = null;
                }
 
                if(string.IsNullOrEmpty(TbIcon.Text))
                {
                    ex.Result.Add(new FormGroupValidateResult("请选择菜单图标", FgIcon));
                }
                else
                {
                    FgIcon.ValidateResult = ValidateResult.None;
                    FgIcon.Message = null;
                }
 
                if (string.IsNullOrEmpty(TbLevel.Text))
                {
                    ex.Result.Add(new FormGroupValidateResult("请选择菜单级别", FgLevel));
                }
                else
                {
                    FgLevel.ValidateResult = ValidateResult.None;
                    FgLevel.Message = null;
                }
 
                if (TbLevel.Text == "2")
                {
                    if (string.IsNullOrEmpty(TbType.Text))
                    {
                        ex.Result.Add(new FormGroupValidateResult("请选择菜单类型", FgType));
                    }
                    else
                    {
                        FgType.ValidateResult = ValidateResult.None;
                        FgType.Message = null;
                    }
 
                    if (string.IsNullOrEmpty(TbParentCode.Text))
                    {
                        ex.Result.Add(new FormGroupValidateResult("请选择上级菜单", FgParentCode));
                    }
                    else
                    {
                        FgParentCode.ValidateResult = ValidateResult.None;
                        FgParentCode.Message = null;
                    }
 
                    if (string.IsNullOrEmpty(TbDllPath.Text))
                    {
                        ex.Result.Add(new FormGroupValidateResult("请选择Dll路径", FgDllPath));
                    }
                    else
                    {
                        FgDllPath.ValidateResult = ValidateResult.None;
                        FgDllPath.Message = null;
                    }
 
                    if (string.IsNullOrEmpty(TbClassName.Text))
                    {
                        ex.Result.Add(new FormGroupValidateResult("请输入类名", FgClassName));
                    }
                    else
                    {
                        FgClassName.ValidateResult = ValidateResult.None;
                        FgClassName.Message = null;
                    }
 
                }
                else { 
 
                }
 
                if (!ex.Success) {
                    throw ex;
                }
            }
            catch (FormValidateException ex)
            {
                foreach (var data in ex.Result)
                {
                    var fg = (FormGroup)data.Sender;
                    fg.ValidateResult = ValidateResult.Error;
                    fg.Message = data.Message;
                }
                return false;
            }
 
            Data.Code = TbCode.Text;
            Data.Name = TbName.Text;
            Data.IconPath = TbIcon.Text;
            Data.Level = TbLevel.Text;
            Data.Type = TbType.Text;
            Data.ParentCode = TbParentCode.Text;
            Data.DllPath = TbDllPath.Text;
            Data.ClassName = TbClassName.Text;
            return true;
        }
 
        public bool? Open(MenuData data)
        {
            Data = data;
            TbCode.Text = Data.Code;
            TbName.Text = Data.Name;
            TbIcon.Text = Data.IconPath;
            TbLevel.SelectedValue = Data.Level;
            TbType.SelectedValue = Data.Type;
            TbParentCode.Text = Data.ParentCode;
            TbDllPath.Text = Data.DllPath;
            TbClassName.Text = Data.ClassName;
            return this.ShowDialog();
        }
 
        private void SaveOrUpdateData()
        {
            if (string.IsNullOrEmpty(Data.Id))
            {
                InsertMenu(Data);
            }
            else
            {
                UpdateMenu(Data);
            }
        }
 
        public void InsertMenu(MenuData data)
        {
            string sql = $@"INSERT INTO menu (code,name,iconPath,level,type,parentCode,dllPath,className) 
                VALUES
                ('{data.Code}','{data.Name}','{data.IconPath}','{data.Level}','{data.Type}','{data.ParentCode}',
                '{data.DllPath}','{data.ClassName}');";
            sqliteHelper.ExecuteSql(sql);
        }
 
        public void UpdateMenu(MenuData data)
        {
            string sql = $@"UPDATE menu SET code='{data.Code}',name='{data.Name}',
iconPath='{data.IconPath}',level='{data.Level}',type='{data.Type}',parentCode='{data.ParentCode}',
dllPath='{data.DllPath}',className='{data.ClassName}' WHERE id='{data.Id}';";
            sqliteHelper.ExecuteSql(sql);
        }
 
 
        private void IconSelect_Click(object sender, RoutedEventArgs e)
        {
            string BasePath = PathHelper.IconFolder;
 
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "(*.*)|*.*";
            openFileDialog.InitialDirectory = BasePath;
            openFileDialog.Title = "选择图片";
            openFileDialog.Multiselect = false;
 
            if (openFileDialog.ShowDialog() == true)
            {
                string FileName = openFileDialog.FileName;
                if (!FileName.StartsWith(BasePath))
                {
                    Panuon.WPF.UI.Toast.Show(this, "请将图标移动至程序Icon目录下!", ToastPosition.Top);
                }
                else
                {
                    TbIcon.Text = FileName.Replace(BasePath, ".");
                }
            }
 
        }
 
        private void DllSelect_Click(object sender, RoutedEventArgs e)
        {
            string pluginPath = PathHelper.PluginFolder();
 
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "(*.dll)|*.dll";
            openFileDialog.InitialDirectory = pluginPath;
            openFileDialog.Title = "选择文件";
            openFileDialog.Multiselect = false;
 
            if (openFileDialog.ShowDialog() == true)
            {
                string FileName = openFileDialog.FileName;
                if (!FileName.StartsWith(pluginPath))
                {
                    Panuon.WPF.UI.Toast.Show(this, "请将插件移动至程序Plugin目录下!", ToastPosition.Top);
                }
                else
                {
                    TbDllPath.Text = FileName.Replace(pluginPath, ".");
                    try
                    {
                        object[] infos = Assembly.LoadFrom(FileName).GetCustomAttributes(typeof(PluginClassNameAttribute), false);
                        if (infos != null && infos.Length > 0)
                        {
                            PluginClassNameAttribute attr = (PluginClassNameAttribute)infos[0];
                            TbClassName.Text = attr.ClassName;
                        }
                    }
                    catch (Exception ex)
                    {
 
                    }
                }
            }
        }
    }
}