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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#region
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using CSFrameworkV5.Business;
using CSFrameworkV5.Business.BLL_Permission;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Library.CommonClass;
using CSFrameworkV5.Models;
using DevExpress.XtraEditors;
 
#endregion
 
namespace CSFrameworkV5.Library.PermissionForms
{
    public partial class frmUserRolePermission : frmBaseChild
    {
        private bllUserRole _BLL;
        private bllUser _BLLUser;
 
        private bool _DataChanged;
 
        public frmUserRolePermission()
        {
            InitializeComponent();
        }
 
        private void btnCancelRole_Click(object sender, EventArgs e)
        {
            if (Msg.AskQuestion("确定要取消操作吗?"))
                try
                {
                    frmWaitingEx.ShowMe(this);
                    DoLoadData(
                        ConvertEx.ToString(txt_CurrentUser
                            .EditValue)); //重新加载数据                
                    EnterEditMode(false);
                }
                finally
                {
                    frmWaitingEx.HideMe(this);
                }
        }
 
        private void btnEmpty_Click(object sender, EventArgs e)
        {
            txt_Account.EditValue = null;
            txt_Role.EditValue = null;
            txt_ExpireFrom.Text = "";
            txt_ExpireTo.Text = "";
        }
 
        private void btnFindRole_Click(object sender, EventArgs e)
        {
            var currentUser = ConvertEx.ToString(txt_CurrentUser.EditValue);
            LoadRole(currentUser, txtRoleText.Text);
        }
 
        private void btnQuery_Click(object sender, EventArgs e)
        {
            DoQuery();
            ShowSummaryPage(false); //显示Summary页面. 
            if (gvSummary.RowCount == 0) Msg.Warning("没有查到数据!");
        }
 
        private void btnSaveRole_Click(object sender, EventArgs e)
        {
            try
            {
                frmWaitingEx.ShowMe(this);
 
                var source = gcUserRoles.DataSource as DragDropDataSourceBase;
                var dt = source.GetChanges();
 
                if (dt != null)
                {
                    var success = _BLL.Update(dt);
                    if (success)
                        DoLoadData(
                            ConvertEx.ToString(txt_CurrentUser
                                .EditValue)); //重新加载数据                        
                }
 
                EnterEditMode(false);
                Msg.ShowInformation("保存成功!");
            }
            finally
            {
                frmWaitingEx.HideMe(this);
            }
        }
 
        private void btnViewUserRole_Click(object sender, EventArgs e)
        {
            try
            {
                frmWaitingEx.ShowMe(this);
 
                //显示用户名称
                var account = ConvertEx.ToString(txt_CurrentUser.EditValue);
                lcAccount.Text = "ID:" + account;
                DoLoadData(account);
            }
            finally
            {
                frmWaitingEx.HideMe(this);
            }
        }
 
        /// <summary>
        ///     删除记录
        /// </summary>
        /// <param name="sender"></param>
        public void DoDelete(IButtonInfo sender)
        {
            //删除查询表格的数据
            if (tcBusiness.SelectedTabPage == tpSummary)
            {
                if (gvSummary.RowCount == 0)
                {
                    Msg.Warning("表格内没有资料!");
                    return;
                }
 
                if (!Msg.AskQuestion("确定要删除吗?")) return;
 
                var account =
                    ConvertEx.ToString(
                        gvSummary.GetFocusedRowCellValue(col_Account));
                var roleID =
                    ConvertEx.ToString(
                        gvSummary.GetFocusedRowCellValue(col_RoleID));
 
                var success = _BLL.DeleteUserRole(account, roleID);
                if (success)
                {
                    gvSummary.DeleteRow(gvSummary.FocusedRowHandle);
                    gcSummary.RefreshDataSource();
 
                    //刷新明细表
                    account = ConvertEx.ToString(txt_CurrentUser.EditValue);
                    DoLoadData(account);
 
                    Msg.ShowInformation("删除成功!");
                }
            }
 
            //删除明细表格的数据
            if (tcBusiness.SelectedTabPage == tpRole)
                menuDeleteUserRole.PerformClick();
        }
 
        private void DoLoadData(string currentUser)
        {
            var dtAllRoles = _BLLUser.GetUserRoles4Picker(currentUser, "");
            var dtUserRoles = _BLLUser.GetUserRoles(currentUser);
 
            DragDropDataSourceBase availableRoles =
                new RoleDataSource(dtAllRoles);
            DragDropDataSourceBase userRoles =
                new UserRoleDataSource(dtUserRoles, currentUser, true);
 
            //观察数据变动情况
            userRoles.OnDataSourceChanged += userRoles_OnDataSourceChanged;
 
            lbAllRole.DisplayMember = "Name";
            lbAllRole.ValueMember = "Code";
            lbAllRole.DataSource = availableRoles;
 
            gcUserRoles.DataSource = userRoles;
        }
 
        private void DoQuery()
        {
            try
            {
                frmWaitingEx.ShowMe(this);
 
                //搜索数据            
                var dt = _BLL.SearchUserRole(
                    ConvertEx.ToString(txt_Account.EditValue),
                    ConvertEx.ToString(txt_Role.EditValue),
                    txt_ExpireFrom.DateTime, txt_ExpireTo.DateTime);
 
                gcSummary.DataSource = null; //绑定主表的Grid
                gcSummary.DataSource = dt;
            }
            finally
            {
                frmWaitingEx.HideMe(this);
            }
        }
 
        /// <summary>
        ///     查看选中记录的数据
        /// </summary>
        /// <param name="sender"></param>
        public void DoViewContent(IButtonInfo sender)
        {
            if (tcBusiness.SelectedTabPage == tpSummary)
            {
                if (gvSummary.RowCount == 0)
                {
                    Msg.Warning("表格内没有资料,请给用户分配角色。");
                    ShowDetailPage(false);
                }
                else
                {
                    var currentUser =
                        ConvertEx.ToString(
                            gvSummary.GetFocusedRowCellValue(col_Account));
                    if (!string.IsNullOrEmpty(currentUser))
                    {
                        LoadRole(currentUser, "");
                        txt_CurrentUser.EditValue = currentUser;
                        ShowDetailPage(false);
                        btnViewUserRole
                            .PerformClick(); //显示当前用户的内容                        
                    }
                }
            }
            else
            {
                btnViewUserRole.PerformClick();
            }
        }
 
        private void EnterEditMode(bool yes)
        {
            _DataChanged = yes;
 
            btnSaveRole.Enabled = yes;
            btnCancelRole.Enabled = yes;
            btnViewUserRole.Enabled = !yes;
            txt_CurrentUser.Enabled = !yes;
            _buttons.GetButtonByName(ButtonNameList.btnClose).Enable = !yes;
            _buttons.GetButtonByName(ButtonNameList.btnView).Enable = !yes;
        }
 
        private void frmUserRoleManagement_FormClosing(object sender,
            FormClosingEventArgs e)
        {
            if (_DataChanged) e.Cancel = !Msg.AskQuestion("您修改了数据没有保存,确定要退出吗?");
        }
 
        private void frmUserRoleManagement_Load(object sender, EventArgs e)
        {
            _BLL = new bllUserRole();
            _BLLUser = new bllUser();
 
            InitButtons();
 
            gvSummary.DoubleClick += gvSummary_DoubleClick;
 
            PermissionBinder.BoundUser(txt_Account);
            DataBinder.BindingLookupEditDataSource(txt_Role,
                new bllRole().GetLookupData(), tb_MyRole.RoleName,
                tb_MyRole.RoleID);
 
            txt_CurrentUser.Properties.PopupControl =
                ucCodeNamePicker1.PopupContainer;
            txt_CurrentUser.QueryPopUp += txt_CurrentUser_QueryPopUp;
 
            ucCodeNamePicker1.DoSetCaption("帐号", "姓名");
            ucCodeNamePicker1.DoSetField(tb_MyUser.Account, tb_MyUser.UserName);
            ucCodeNamePicker1.OnShowFiltredData +=
                ucCodeNamePicker1_OnShowFiltredData;
            ucCodeNamePicker1.OnRowClick += ucCodeNamePicker1_OnRowClick;
            ucCodeNamePicker1.DoSetSize(500);
            ucCodeNamePicker1.DoShowDefaultData(DataDictCache.Cache.User);
 
            lbAllRole.Items.Clear();
 
            DoQuery(); //查询
 
            EnterEditMode(false);
        }
 
        private void gvSummary_DoubleClick(object sender, EventArgs e)
        {
            if (gvSummary.RowCount > 0) DoViewContent(null);
        }
 
        private void gvUserRoles_DoubleClick(object sender, EventArgs e)
        {
            //双击表格,打开修改窗体
            menuEditUserRole.PerformClick();
        }
 
        /// <summary>
        ///     初始化数据窗体的按钮
        /// </summary>
        public override void InitButtons()
        {
            base.InitButtons();
 
            var list = new ArrayList();
            list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnView, "查看",
                ToolBarGroup.数据操作,
                Globals.LoadBitmap("32_View.png"), new Size(57, 28), true, true,
                DoViewContent));
            list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnDelete,
                "删除", ToolBarGroup.数据操作,
                Globals.LoadBitmap("32_Del.png"), new Size(57, 28), true, true,
                DoDelete));
            var ii = (IButtonInfo[])list.ToArray(typeof(IButtonInfo));
 
            Buttons.AddRange(ii);
        }
 
        private void lbAllRole_DoubleClick(object sender, EventArgs e)
        {
            //双击移动项目到表格中
 
            Assertion.AssertEditorEmpty(txt_CurrentUser, "请选择用户!", true);
 
            var source = lbAllRole.DataSource as DragDropDataSourceBase;
            var dest = gcUserRoles.DataSource as DragDropDataSourceBase;
 
            if (source.Count > 0)
            {
                var currentUser = ConvertEx.ToString(txt_CurrentUser.EditValue);
 
                var tag = lbAllRole.SelectedItem as ItemExtend;
                UserRoleItem U;
 
                if (frmUserRoleEditor.ExecuteAdd(currentUser, tag, out U))
                {
                    dest.AddToListAndTable(U);
                    source.Remove(tag);
                }
            }
 
            gcUserRoles.DataSource = null; //刷新表格
            gcUserRoles.DataSource = dest;
 
            lbAllRole.DataSource = null;
            lbAllRole.DataSource = source;
        }
 
        private void LoadRole(string currentUser, string searchText)
        {
            var dtAllRoles =
                _BLLUser.GetUserRoles4Picker(currentUser, searchText);
            DragDropDataSourceBase availableRoles =
                new RoleDataSource(dtAllRoles);
            lbAllRole.DisplayMember = "Name";
            lbAllRole.ValueMember = "Code";
            lbAllRole.DataSource = availableRoles;
        }
 
        private void menuDeleteUserRole_Click(object sender, EventArgs e)
        {
            //将表格的数据移动到可选角色
            if (gvUserRoles.RowCount == 0) return;
 
            var source = gcUserRoles.DataSource as DragDropDataSourceBase;
            var dest = lbAllRole.DataSource as DragDropDataSourceBase;
            if (source.Count > 0)
            {
                var o = gvUserRoles.GetRow(gvUserRoles.FocusedRowHandle);
                var tag = o as ItemExtend;
                dest.AddToListAndTable(tag);
                source.Remove(tag);
            }
 
            gcUserRoles.RefreshDataSource(); //刷新表格
 
            lbAllRole.DataSource = null;
            lbAllRole.DataSource = dest;
        }
 
        private void menuEditUserRole_Click(object sender, EventArgs e)
        {
            //弹出菜单,打开修改窗体
            if (gvUserRoles.FocusedRowHandle >= 0)
            {
                var R =
                    gvUserRoles.GetRow(gvUserRoles.FocusedRowHandle) as
                        UserRoleItem;
                if (frmUserRoleEditor.ExecuteEdit(R))
                {
                    EnterEditMode(true);
                    gvUserRoles.UpdateCurrentRow();
                }
            }
        }
 
        private void ShowDetailPage(bool disableSummaryPage)
        {
            tpRole.PageEnabled = true;
            tcBusiness.SelectedTabPage = tpRole;
            tpSummary.PageEnabled = !disableSummaryPage;
        }
 
        private void ShowSummaryPage(bool disableDetailPage)
        {
            tpSummary.PageEnabled = true;
            tcBusiness.SelectedTabPage = tpSummary;
            tpRole.PageEnabled = !disableDetailPage;
        }
 
        private void txt_CurrentUser_EditValueChanged(object sender,
            EventArgs e)
        {
            btnViewUserRole.PerformClick();
        }
 
        private void txt_CurrentUser_QueryPopUp(object sender,
            CancelEventArgs e)
        {
            ucCodeNamePicker1.DoShowDefaultData(DataDictCache.Cache.User);
        }
 
        private void ucCodeNamePicker1_OnRowClick(DataRow clickedRow)
        {
            txt_CurrentUser.EditValue = clickedRow[tb_MyUser.Account];
            ucCodeNamePicker1.Hide();
            txt_CurrentUser.ClosePopup();
            btnFindRole.PerformClick();
        }
 
        private DataTable ucCodeNamePicker1_OnShowFiltredData(string code,
            string name)
        {
            var dt = DataDictCache.Cache.User.Copy();
            var filter = "";
            if (!string.IsNullOrEmpty(code))
            {
                if (filter != "") filter = filter + " OR ";
 
                filter = " Account LIKE '%" + code + "%'";
            }
 
            if (!string.IsNullOrEmpty(name))
            {
                if (filter != "") filter = filter + " OR ";
 
                filter = filter + " UserName LIKE '%" + name + "%'";
            }
 
            dt.DefaultView.RowFilter = filter;
            dt = dt.DefaultView.ToTable();
            return dt;
        }
 
        private void userRoles_OnDataSourceChanged(DataTable sourceTable,
            ItemExtend item, DataRowState state)
        {
            if (state == DataRowState.Added || state == DataRowState.Deleted)
                EnterEditMode(true);
        }
 
        #region DragDrop拖放操作
 
        private void lbAllRole_DragDrop(object sender, DragEventArgs e)
        {
            //拖拉用户的角色到可选角色.           
            var dest = lbAllRole.DataSource as DragDropDataSourceBase;
            var source = gcUserRoles.DataSource as DragDropDataSourceBase;
 
            if (e.Data.GetDataPresent(typeof(UserRoleItem)))
            {
                var tag = e.Data.GetData(typeof(UserRoleItem)) as UserRoleItem;
                dest.AddToListAndTable(tag);
                source.Remove(tag);
            }
 
            lbAllRole.DataSource = null;
            lbAllRole.DataSource = dest;
 
            lbAllRole.SelectedIndex = lbAllRole.ItemCount - 1; //目的地定位到最后一条记录
            lbAllRole.Focus();
            lbAllRole.Refresh();
 
            gcUserRoles.RefreshDataSource();
        }
 
        private void OnList_DragOver(object sender, DragEventArgs e)
        {
            //拖放到目标控件上触发的事件.
 
            var list = sender as ImageListBoxControl;
            var source = list.DataSource as DragDropDataSourceBase;
            UserRoleItem tag = null;
 
            if (e.Data.GetDataPresent(typeof(UserRoleItem)))
                tag = e.Data.GetData(typeof(UserRoleItem)) as UserRoleItem;
 
            if (e.Data.GetDataPresent(typeof(ItemExtend)))
                tag = e.Data.GetData(typeof(ItemExtend)) as UserRoleItem;
 
            if (tag == null || source.IndexOf(tag) >= 0)
                e.Effect = DragDropEffects.None;
            else
                e.Effect = DragDropEffects.Move;
        }
 
        private void OnList_MouseDown(object sender, MouseEventArgs e)
        {
            //点鼠标右键,一次性拖放多个对象.
 
            var list = sender as ImageListBoxControl;
 
            if (e.Button == MouseButtons.Right && list.SelectedItems.Count > 1)
                list.DoDragDrop(list.SelectedItems, DragDropEffects.Move);
        }
 
        private void OnList_MouseMove(object sender, MouseEventArgs e)
        {
            //移动鼠标,进入拖拉状态.
 
            var list = sender as ImageListBoxControl;
            if (MouseButtons.Left == e.Button && list.SelectedItem != null)
                list.DoDragDrop(list.SelectedItem, DragDropEffects.Move);
 
            if (MouseButtons.Right == e.Button)
                list.DoDragDrop(list.SelectedItems, DragDropEffects.Move);
        }
 
        private void gvUserRoles_MouseDown(object sender, MouseEventArgs e)
        {
            //点鼠标右键,一次性拖放多个对象.          
            if (e.Button == MouseButtons.Right &&
                gvUserRoles.SelectedRowsCount > 1)
            {
                var iRows = gvUserRoles.GetSelectedRows();
                var rows = new List<DataRow>();
 
                foreach (var i in iRows) rows.Add(gvUserRoles.GetDataRow(i));
 
                gcUserRoles.DoDragDrop(rows, DragDropEffects.Move);
            }
        }
 
        private void gvUserRoles_MouseMove(object sender, MouseEventArgs e)
        {
            //移动鼠标,进入拖拉状态.            
            if (MouseButtons.Left == e.Button &&
                gvUserRoles.FocusedRowHandle >= 0)
            {
                var o = gvUserRoles.GetRow(gvUserRoles.FocusedRowHandle);
                gcUserRoles.DoDragDrop(o, DragDropEffects.Move);
            }
 
            if (e.Button == MouseButtons.Right &&
                gvUserRoles.SelectedRowsCount >= 1)
            {
                var iRows = gvUserRoles.GetSelectedRows();
                var rows = new List<DataRow>();
 
                foreach (var i in iRows) rows.Add(gvUserRoles.GetDataRow(i));
 
                gcUserRoles.DoDragDrop(rows, DragDropEffects.Move);
            }
        }
 
        private void gcUserRoles_DragOver(object sender, DragEventArgs e)
        {
            //拖放到目标控件上触发的事件.
 
            //没有用户,不支持拖放
            if (txt_CurrentUser.Text == "") return;
 
            var source = gcUserRoles.DataSource as DragDropDataSourceBase;
            ItemExtend tag = null;
 
            if (e.Data.GetDataPresent(
                    typeof(BaseListBoxControl.SelectedItemCollection)))
            {
                BaseListBoxControl.SelectedItemCollection listSelected;
                listSelected =
                    e.Data.GetData(
                            typeof(BaseListBoxControl.SelectedItemCollection))
                        as
                        BaseListBoxControl.SelectedItemCollection;
                tag = listSelected[0] as ItemExtend;
            }
 
            if (e.Data.GetDataPresent(typeof(ItemExtend)))
                tag = e.Data.GetData(typeof(ItemExtend)) as ItemExtend;
 
            if (e.Data.GetDataPresent(typeof(UserRoleItem)))
                tag = e.Data.GetData(typeof(UserRoleItem)) as ItemExtend;
 
            if (tag == null || source.IndexOf(tag) >= 0)
                e.Effect = DragDropEffects.None;
            else
                e.Effect = DragDropEffects.Move;
        }
 
        private void gcUserRoles_DragDrop(object sender, DragEventArgs e)
        {
            //拖拉可选角色到组的角色.
            var dest = gcUserRoles.DataSource as DragDropDataSourceBase;
            var source = lbAllRole.DataSource as DragDropDataSourceBase;
 
            ItemExtend tag = null;
            UserRoleItem U = null;
 
            if (e.Data.GetDataPresent(typeof(ItemExtend)))
                tag = e.Data.GetData(typeof(ItemExtend)) as ItemExtend;
 
            if (e.Data.GetDataPresent(typeof(UserRoleItem)))
                tag = e.Data.GetData(typeof(UserRoleItem)) as UserRoleItem;
 
            if (tag != null)
            {
                var currentUser = ConvertEx.ToString(txt_CurrentUser.EditValue);
                if (frmUserRoleEditor.ExecuteAdd(currentUser, tag, out U))
                {
                    dest.AddToListAndTable(U);
                    source.Remove(tag);
                }
 
                gcUserRoles.DataSource = null;
                gcUserRoles.DataSource = dest;
 
                gvUserRoles.MoveLast(); //目的地定位到最后一条记录
                lbAllRole.Refresh();
            }
        }
 
        #endregion
    }
}