1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#region
 
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using CSFrameworkV5.Business.BLL_Permission;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Library.UserControls;
using CSFrameworkV5.Models;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
 
#endregion
 
namespace CSFrameworkV5.Library.PermissionForms
{
    /// <summary>
    ///     编辑菜单资料
    /// </summary>
    public partial class frmEditorMenu : frmEditorBase
    {
        private TreeListNode _FocusedNode;
        private DataTable _MenuData;
        private DataTable _MenuPicker;
        private RoleActionsView _RoleActionsView;
        private UpdateType _UpdateType = UpdateType.None;
        private bool Changed;
 
        private frmEditorMenu()
        {
            InitializeComponent();
        }
 
        private void BindingEditors(DataRowView rv)
        {
            txtFormNamespace.Text =
                ConvertEx.ToString(rv.Row[tb_MyMenu.FormNamespace]);
            txtMenuCaption.Text =
                ConvertEx.ToString(rv.Row[tb_MyMenu.MenuCaption]);
            txtMenuName.Text = ConvertEx.ToString(rv.Row[tb_MyMenu.MenuName]);
            txtMenuType.SelectedIndex =
                GetIndex(ConvertEx.ToString(rv.Row[tb_MyMenu.MenuType]));
            txtModuleID.EditValue =
                ConvertEx.ToString(rv.Row[tb_MyMenu.ModuleID]);
            txtParameters.Text =
                ConvertEx.ToString(rv.Row[tb_MyMenu.Parameters]);
            txtParentMenuName.EditValue =
                ConvertEx.ToString(rv.Row[tb_MyMenu.ParentMenuName]);
        }
 
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
 
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!ValidateInput()) return;
 
            DataRow current = null;
 
            if (_UpdateType == UpdateType.Add)
            {
                current = _MenuData.NewRow();
                _MenuData.Rows.Add(current);
            }
 
            if (_UpdateType == UpdateType.Modify)
            {
                var rv =
                    (DataRowView)_FocusedNode.TreeList.GetDataRecordByNode(
                        _FocusedNode);
                current = rv.Row;
            }
 
            //生成当前菜单的功能点数据。
            ActionCreateCustomNameData(pcActions, txtMenuName.Text,
                _RoleActionsView.ActionCustomNameData);
 
            if (current != null) FillRow(current);
 
            Changed = true;
            Close();
        }
 
        public static bool Execute(RoleActionsView roleActionsView,
            UpdateType updateType, TreeListNode focusedNode)
        {
            var form = new frmEditorMenu();
            form._MenuData = roleActionsView.MenuData;
            form._RoleActionsView = roleActionsView;
            form._FocusedNode = focusedNode;
            form.InitEditor(updateType, focusedNode);
            form.ShowDialog();
            return form.Changed;
        }
 
        private void FillRow(DataRow current)
        {
            current[tb_MyMenu.DataSetID] = Loginer.CurrentUser.DBID;
            current[tb_MyMenu.FormNamespace] = txtFormNamespace.Text;
            current[tb_MyMenu.MenuCaption] = txtMenuCaption.Text;
            current[tb_MyMenu.MenuName] = txtMenuName.Text;
            current[tb_MyMenu.MenuType] = txtMenuType.Text
                .Substring(0, txtMenuType.Text.IndexOf("-") - 1).Trim();
            current[tb_MyMenu.ModuleID] = txtModuleID.EditValue;
            current[tb_MyMenu.Parameters] = txtParameters.Text;
            current[tb_MyMenu.ParentMenuName] = txtParentMenuName.EditValue;
            current[tb_MyMenu.Actions] = ActionGetValue(pcActions);
        }
 
        private void frmMenuEditor_Load(object sender, EventArgs e)
        {
            _MenuPicker = new bllPermission().GetMenuPicker(true);
 
            txtModuleID.Properties.DisplayMember =
                sys_ModuleFileList.ModuleName;
            txtModuleID.Properties.ValueMember = sys_ModuleFileList.ModuleID;
            txtModuleID.Properties.DataSource =
                new bllPermission().GetModuleData();
 
            OnModuleIDChanged();
        }
 
        private int GetIndex(string menuType)
        {
            if (menuType.Trim() == "") return 0;
 
            for (var i = 0; i < txtMenuType.Properties.Items.Count - 1; i++)
                if (txtMenuType.Properties.Items[i].ToStringEx()
                        .IndexOf(menuType) >= 0)
                    return i;
 
            return 0;
        }
 
        public void InitEditor(UpdateType updateType, TreeListNode focusedNode)
        {
            _UpdateType = updateType;
            var rv =
                (DataRowView)focusedNode.TreeList.GetDataRecordByNode(
                    focusedNode);
 
            ActionInitPanel(pcActions, _RoleActionsView.ActionsData);
            ActionShowMenuFuns(pcActions, rv.Row,
                _RoleActionsView.ActionCustomNameData);
 
            //新增菜单
            if (UpdateType.Add == updateType)
            {
                txtMenuType.SelectedIndex = 1; //DataForm - 数据窗体菜单
 
                //新增菜单的父级预设为当前结点
                if (focusedNode != null)
                {
                    txtModuleID.EditValue = rv.Row[tb_MyMenu.ModuleID];
                    txtParentMenuName.EditValue = rv.Row[tb_MyMenu.MenuName];
                }
            }
            else
            {
                BindingEditors(rv);
 
                txtParentMenuName.Enabled = false;
                txtModuleID.Enabled = false;
            }
 
            var editMode = updateType == UpdateType.Add ||
                           UpdateType.Modify == updateType;
            panelControl1.Enabled = editMode;
            pcActions.Enabled = editMode;
            btnOK.Enabled = editMode;
            btnCancel.Enabled = true; //btnOK.Enabled;
        }
 
        private void OnModuleIDChanged()
        {
            if (_MenuData != null && txtModuleID.EditValue != null)
            {
                var temp = _MenuData.Copy();
                var top = temp.NewRow();
                top[tb_MyMenu.MenuName] = "";
                top[tb_MyMenu.MenuCaption] = "<顶级菜单>";
                temp.Rows.InsertAt(top, 0);
 
                temp.DefaultView.RowFilter = "ModuleID=" +
                                             txtModuleID.EditValue
                                                 .ToStringEx() +
                                             " or MenuName=''";
 
                txtParentMenuName.Properties.DisplayMember =
                    tb_MyMenu.MenuCaption;
                txtParentMenuName.Properties.ValueMember = tb_MyMenu.MenuName;
                txtParentMenuName.Properties.DataSource = temp.DefaultView;
            }
        }
 
        private void txtModuleID_EditValueChanged(object sender, EventArgs e)
        {
            OnModuleIDChanged();
        }
 
        private bool ValidateInput()
        {
            //if (txtModuleID.Text == "")
            //{
            //    Msg.Warning("所属模块不能为空!");
            //    txtModuleID.Focus();
            //    return false;
            //}
 
            if (txtMenuName.Text == "")
            {
                Msg.Warning("菜单名称不能为空!");
                txtMenuName.Focus();
                return false;
            }
 
            if (txtMenuCaption.Text == "")
            {
                Msg.Warning("菜单的标题不能为空!");
                txtMenuCaption.Focus();
                return false;
            }
 
            if (RoleActionsView.IsExistsInCache(_MenuData, txtMenuName.Text,
                    tb_MyMenu.MenuName, _UpdateType))
            {
                Msg.Warning("菜单名称已经存在!");
                txtMenuName.Focus();
                return false;
            }
 
            return true;
        }
 
        #region 功能点动态面板操作相关的方法。
 
        /// <summary>
        ///     初始化功能权限控制面板
        /// </summary>
        public static void ActionInitPanel(PanelControl pcActions,
            DataTable actionData)
        {
            var ROW = 13;
            var rowTop = 3;
            var colLeft = 2;
            var count = 0;
 
            pcActions.BeginInit();
            pcActions.Controls.Clear();
 
            foreach (DataRow actionRow in actionData.Rows)
            {
                var value =
                    ConvertEx.ToInt(actionRow[tb_MyActions.ActionValue]);
                if (value == 0) continue;
 
                var checkEdit = new ucCheckEdit();
                checkEdit.Name = "ucCheckEdit_" + value.ToStringEx();
                checkEdit.IsChecked = false;
                checkEdit.Location = new Point(colLeft, rowTop);
                checkEdit.MaximumSize = new Size(2333, 21);
                checkEdit.MinimumSize = new Size(29, 21);
                checkEdit.Size = new Size(88, 21);
                checkEdit.TabIndex = 0;
                checkEdit.CheckText =
                    ConvertEx.ToString(
                        actionRow[tb_MyActions.ActionName]); //显示预设的名称
                checkEdit.Tag = actionRow; //标记
                checkEdit.InnerEditor.Properties.ReadOnly = true;
                checkEdit.InnerEditor.Enabled = true;
                checkEdit.InnerCheckEdit.Enabled = true;
                checkEdit.InnerCheckEdit.Properties.ReadOnly = true;
 
                pcActions.Controls.Add(checkEdit);
 
                rowTop += 21 + 2;
                count++;
                if (count > ROW)
                {
                    colLeft += checkEdit.Width - 8;
                    rowTop = 3;
                    count = 0;
                }
            }
 
            pcActions.EndInit();
        }
 
        /// <summary>
        ///     显示菜单的功能点
        /// </summary>
        public static void ActionShowMenuFuns(PanelControl pcActions,
            DataRow rowMenuData,
            DataTable actionCustomNameData)
        {
            var Auths = ConvertEx.ToInt(rowMenuData[tb_MyMenu.Actions]); //菜单的权限
            var menu =
                ConvertEx.ToString(rowMenuData[tb_MyMenu.MenuName]); //菜单名称
 
            int value;
            DataRow rowAction;
 
            foreach (Control c in pcActions.Controls)
                if (c is ucCheckEdit)
                {
                    rowAction = c.Tag as DataRow; //功能点资料行
                    value = ConvertEx.ToInt(
                        rowAction[tb_MyActions.ActionValue]); //取功能点的权限值
 
                    (c as ucCheckEdit).IsChecked =
                        (value & Auths) == value; //逻辑“与”运算。
                    (c as ucCheckEdit).Enabled =
                        (value & Auths) ==
                        value; //当前菜单有此功能权限才显示                    
 
                    //显示自定义的功能点名称
                    var filter = "MenuName='" + menu + "' and TagValue=" +
                                 value.ToStringEx();
 
                    var rows = actionCustomNameData.Select(filter);
                    if (rows.Length > 0)
                        (c as ucCheckEdit).CheckText =
                            ConvertEx.ToString(
                                rows[0][tb_MyFormTagName.TagName]); //取自定义名称
                    else
                        (c as ucCheckEdit).CheckText =
                            ConvertEx.ToString(
                                rowAction[tb_MyActions.ActionName]); //预设名称
                }
        }
 
        /// <summary>
        ///     显示菜单的功能点
        /// </summary>
        public static int ActionGetValue(PanelControl pcActions)
        {
            var value = 0;
            var i = 0;
            DataRow rowAction;
            foreach (Control c in pcActions.Controls)
                if (c is ucCheckEdit && (c as ucCheckEdit).IsChecked)
                {
                    rowAction = c.Tag as DataRow; //功能点资料行
                    i = ConvertEx.ToInt(
                        rowAction[tb_MyActions.ActionValue]); //取功能点的权限值
                    value = value + i; //累加
                }
 
            return value;
        }
 
        /// <summary>
        ///     创建功能自定义名称
        /// </summary>
        public static void ActionCreateCustomNameData(PanelControl pcActions,
            string menuName,
            DataTable actionCustomNameData)
        {
            //取窗体的权限
            var auths = 0;
            var val = 0;
            ucCheckEdit ce;
            DataRow row;
            string filter;
 
            foreach (Control ctr in pcActions.Controls)
            {
                if (!(ctr is ucCheckEdit)) continue;
 
                ce = ctr as ucCheckEdit;
                row = ce.Tag as DataRow; //功能点的数据行
                val = ConvertEx.ToInt(row[tb_MyActions.ActionValue]);
 
                if (ce.IsChecked) auths += val; //累加权限数值
 
                filter = "DataSetID='" + Loginer.CurrentUser.DBID +
                         "' AND MenuName='" + menuName + "' and TagValue=" +
                         val.ToStringEx();
                var rows = actionCustomNameData.Select(filter);
                if (rows.Length > 0) //有自定义功能点名称
                {
                    var tagName =
                        ConvertEx.ToString(rows[0][tb_MyFormTagName.TagName]);
                    if (ce.IsChecked &&
                        tagName.ToUpper() != ce.CheckText.ToUpper())
                        rows[0][tb_MyFormTagName.TagName] =
                            ce.CheckText; //修改新功能点名称
 
                    if (ce.IsChecked == false) //删除了功能名称
                        rows[0].Delete();
                }
                else
                {
                    var authName =
                        ConvertEx.ToString(row[tb_MyActions.ActionName]);
                    if (authName.ToUpper() != ce.CheckText.ToUpper())
                    {
                        var newrow = actionCustomNameData.NewRow();
                        newrow[tb_MyFormTagName.DataSetID] =
                            Loginer.CurrentUser.DBID;
                        newrow[tb_MyFormTagName.MenuName] = menuName;
                        newrow[tb_MyFormTagName.TagValue] = val;
                        newrow[tb_MyFormTagName.TagName] = ce.CheckText; //新的名称
                        actionCustomNameData.Rows.Add(newrow);
                    }
                }
            }
        }
 
        #endregion
    }
}