lixiaojun
2025-01-20 6e1306ab578ed1ad79fc33b0bb7e496b897bf4a4
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
using DevExpress.Utils.DragDrop;
using DevExpress.XtraEditors;
using System.Windows.Controls;
using Yw;
using Yw.DAL.Basic;
using Yw.Vmo;
using Yw.WinFrmUI;
 
namespace HStation.WinFrmUI
{
    public partial class UserMgrPage : DocumentPage
    {
        public UserMgrPage()
        {
            InitializeComponent();
            this.gridView1.SetNormalView(30);
            //     this.gridView2.SetNormalView(30);
            this.gridView3.SetNormalView(30);
            this.PageTitle.Caption = "用户管理";
            this.PageTitle.SvgImageSize = new Size(24, 24);
        }
 
        private List<UserViewModel> _allBindingList = null;//数据绑定列表
 
        private List<UserRoleHaveViewModel> _userRoleHaveViewModelList = new();//角色和用户关系
 
        private List<UserAccountViewModel> _userLoginAccountViewModelList = new();
 
        /// <summary>
        /// 初始化数据源
        /// </summary>
        public override void InitialDataSource()
        {
            InitialData();
        }
 
        //初始化数据
        private async void InitialData()
        {
            var overlay = this.ShowOverlay();
 
            var allList = await BLLFactory<Yw.BLL.User>.Instance.GetByCorpID(GlobalParas._GlobalParas.CorpID);
            _allBindingList = new List<UserViewModel>();
            if (allList != null && allList.Count > 0)
            {
                foreach (var item in allList)
                {
                    var vm = new UserViewModel(item);
                    _allBindingList.Add(vm);
                }
            }
            this.UserViewModelBindingSource.DataSource = _allBindingList;
            this.UserViewModelBindingSource.ResetBindings(false);
            this.userRoleHaveViewModelBindingSource.DataSource = _userRoleHaveViewModelList;
            this.userRoleHaveViewModelBindingSource.ResetBindings(false);
            this.userAccountViewModelBindingSource.DataSource = _userLoginAccountViewModelList;
            this.userAccountViewModelBindingSource.ResetBindings(false);
            overlay.Close();
            SetDragEnable(this.barCkDrag.Checked);
        }
 
        /// <summary>
        /// 刷新数据
        /// </summary>
        public override void RefreshData()
        {
            base.RefreshData();
            InitialData();
        }
 
        //添加
        private void Add()
        {
            if (_allBindingList == null)
            {
                TipFormHelper.ShowError("数据初始化失败!");
                return;
            }
            var dlg = new AddUserDlg();
            dlg.ReloadDataEvent += (rhs) =>
            {
                _allBindingList.Add(new UserViewModel(rhs));
                this.UserViewModelBindingSource.ResetBindings(false);
                TipFormHelper.ShowSucceed("添加成功!");
            };
            dlg.ShowDialog();
        }
 
        //编辑
        private void Edit()
        {
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var dlg = new EditUserDlg();
            dlg.ReloadDataEvent += (rhs) =>
            {
                vm.Reset(rhs);
                this.gridView1.RefreshRow(this.gridView1.FocusedRowHandle);
                TipFormHelper.ShowSucceed("更新成功");
            };
            dlg.SetBindingData(vm.Vmo);
            dlg.ShowDialog();
        }
 
        //删除
        private async void Delete()
        {
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var result = XtraMessageBox.Show("请问确认删除当前数据吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes;
            if (!result)
            {
                return;
            }
            var bol = await BLLFactory<Yw.BLL.User>.Instance.DeleteByID(vm.ID);
            if (!bol)
            {
                TipFormHelper.ShowError("删除失败!");
                return;
            }
            _allBindingList.Remove(vm);
            this.UserViewModelBindingSource.ResetBindings(false);
            TipFormHelper.ShowSucceed("删除成功!");
        }
 
        //查看
        private void View()
        {
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var dlg = new ViewUserDlg();
            dlg.SetBindingData(vm);
            dlg.ShowDialog();
        }
 
        //上移
        private async void Up()
        {
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var rowHandle = this.gridView1.FocusedRowHandle;
            if (rowHandle == 0)
            {
                TipFormHelper.ShowWarn("上移失败!");
                return;
            }
            var current = this.gridView1.GetRow(rowHandle) as UserViewModel;
            if (current == null)
            {
                return;
            }
            var prevHandle = rowHandle - 1;
            var prev = this.gridView1.GetRow(prevHandle) as UserViewModel;
            if (prev == null)
            {
                return;
            }
            var sorters = new List<Yw.Vmo.Sorter>();
            sorters.Add(new Yw.Vmo.Sorter() { ID = current.ID, SortCode = prev.SortCode });
            sorters.Add(new Yw.Vmo.Sorter() { ID = prev.ID, SortCode = current.SortCode });
            var bol = await BLLFactory<Yw.BLL.User>.Instance.UpdateSorter(sorters);
            if (!bol)
            {
                TipFormHelper.ShowError("上移失败!");
                return;
            }
            var sortCode = current.SortCode;
            current.SortCode = prev.SortCode;
            prev.SortCode = sortCode;
            _allBindingList = _allBindingList.OrderBy(x => x.SortCode).ToList();
            this.UserViewModelBindingSource.DataSource = _allBindingList;
            this.UserViewModelBindingSource.ResetBindings(false);
            this.gridView1.FocusedRowHandle = prevHandle;
        }
 
        //下移
        private async void Down()
        {
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var rowHandle = this.gridView1.FocusedRowHandle;
            if (rowHandle == _allBindingList.Count - 1)
            {
                TipFormHelper.ShowWarn("下移失败!");
                return;
            }
            var current = this.gridView1.GetRow(rowHandle) as UserViewModel;
            if (current == null)
            {
                return;
            }
            var nextHandle = rowHandle + 1;
            var next = this.gridView1.GetRow(nextHandle) as UserViewModel;
            if (next == null)
            {
                return;
            }
            var sorters = new List<Yw.Vmo.Sorter>();
            sorters.Add(new Yw.Vmo.Sorter() { ID = current.ID, SortCode = next.SortCode });
            sorters.Add(new Yw.Vmo.Sorter() { ID = next.ID, SortCode = current.SortCode });
            var bol = await BLLFactory<Yw.BLL.User>.Instance.UpdateSorter(sorters);
            if (!bol)
            {
                TipFormHelper.ShowError("下移失败!");
                return;
            }
            var sortCode = current.SortCode;
            current.SortCode = next.SortCode;
            next.SortCode = sortCode;
            _allBindingList = _allBindingList.OrderBy(x => x.SortCode).ToList();
            this.UserViewModelBindingSource.DataSource = _allBindingList;
            this.UserViewModelBindingSource.ResetBindings(false);
            this.gridView1.FocusedRowHandle = nextHandle;
        }
 
        #region 当前
 
        //获取当前
        private UserViewModel GetCurrentViewModel()
        {
            if (_allBindingList == null)
            {
                TipFormHelper.ShowError("数据初始化错误!");
                return null;
            }
            if (_allBindingList.Count < 1)
            {
                TipFormHelper.ShowInfo("无数据!");
                return null;
            }
            var vm = this.gridView1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                TipFormHelper.ShowWarn("请选择数据行!");
                return null;
            }
            return vm;
        }
 
        #endregion 当前
 
        #region Ribbon
 
        //添加
        private void barBtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Add();
        }
 
        //编辑
        private void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit();
        }
 
        //删除
        private void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Delete();
        }
 
        //详细信息
        private void barBtnInfo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            View();
        }
 
        //上移
        private void barBtnUp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Up();
        }
 
        //下移
        private void barBtnDown_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Down();
        }
 
        //查询
        private void barBtnSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView1.OptionsFind.AlwaysVisible = !this.gridView1.OptionsFind.AlwaysVisible;
        }
 
        //刷新
        private void barBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.RefreshData();
        }
 
        #endregion Ribbon
 
        private async void dragDropEvents1_DragDrop(object sender, DevExpress.Utils.DragDrop.DragDropEventArgs e)
        {
            var indexs = e.Data as int[];
            if (indexs == null || indexs.Length < 1)
            {
                e.Handled = true;
                return;
            }
            var sourceIndex = indexs[0];
            var sourceObj = this.gridView1.GetRow(sourceIndex) as UserViewModel;
            if (sourceObj == null)
            {
                e.Handled = true;
                return;
            }
 
            var destIndex = this.gridView1.GetRowHandleByCP(e.Location);
            if (destIndex < 0)
            {
                e.Handled = true;
                return;
            }
 
            var destObj = this.gridView1.GetRow(destIndex) as UserViewModel;
            if (destObj == null)
            {
                e.Handled = true;
                return;
            }
 
            var sorters = new List<Yw.Vmo.Sorter>();
 
            if (e.InsertType == InsertType.Before)
            {
                sorters.Add(new Yw.Vmo.Sorter() { ID = sourceObj.ID, SortCode = destObj.SortCode });
                _allBindingList.ForEach(x =>
                {
                    if (x.SortCode >= destObj.SortCode)
                    {
                        if (x != sourceObj)
                        {
                            sorters.Add(new Yw.Vmo.Sorter() { ID = x.ID, SortCode = x.SortCode + 1 });
                        }
                    }
                });
            }
            else if (e.InsertType == InsertType.After)
            {
                sorters.Add(new Yw.Vmo.Sorter() { ID = sourceObj.ID, SortCode = destObj.SortCode + 1 });
                _allBindingList.ForEach(x =>
                {
                    if (x.SortCode > destObj.SortCode)
                    {
                        if (x != sourceObj)
                        {
                            sorters.Add(new Yw.Vmo.Sorter() { ID = x.ID, SortCode = x.SortCode + 1 });
                        }
                    }
                });
            }
            else
            {
                e.Handled = true;
                return;
            }
 
            var bll = BLLFactory<Yw.BLL.User>.Instance;
            var bol = await bll.UpdateSorter(sorters);
            if (!bol)
            {
                e.Handled = true;
                return;
            }
 
            _allBindingList.ForEach(x =>
            {
                var sorter = sorters.Find(t => t.ID == x.ID);
                if (sorter != null)
                {
                    x.SortCode = sorter.SortCode;
                }
            });
        }
 
        private void barCkDrag_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetDragEnable(this.barCkDrag.Checked);
        }
 
        //设置拖拽可用性
        private void SetDragEnable(bool allowArag)
        {
            var be = this.behaviorManager1.GetBehavior<DevExpress.Utils.DragDrop.DragDropBehavior>(this.gridView1);
            be.Properties.AllowDrag = allowArag;
        }
 
        /// <summary>
        /// 编辑管理类型
        /// </summary>
        private void BtnEditAdminType_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var row = this.GetCurrentViewModel();
            if (row == null)
                return;
            var dlg = new UpdateAdminTypeDlg();
            dlg.SetBingingData(row.ID, row.AdminType);
            dlg.ShowDialog();
        }
 
        private async void flyoutPanel1_ButtonClick(object sender, DevExpress.Utils.FlyoutPanelButtonClickEventArgs e)
        {
            if (e.Button.Caption.Equals("关闭"))
            {
                flyoutPanel1.HidePopup();
            }
            if (e.Button.Caption.Equals("保存"))
            {
                var vm = GetCurrentViewModel();
                if (vm == null)
                {
                    return;
                }
                var userRoleInput = new SetUserRoleInputVmo();
                userRoleInput.UserID = vm.ID;
                userRoleInput.RoleIds = new List<long>();
                foreach (var item in _userRoleHaveViewModelList)
                {
                    if (item.Have)
                    {
                        userRoleInput.RoleIds.Add(item.ID);
                    }
                }
                var bol = await BLLFactory<Yw.BLL.UserRoleMapping>.Instance.Set(userRoleInput);
                if (bol)
                {
                    TipFormHelper.ShowSucceed("设置成功!");
                }
                else
                {
                    TipFormHelper.ShowError("设置失败!");
                }
                flyoutPanel1.HidePopup();
            }
        }
 
        //单元格点击事件
        private async void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            if (e.Column == this.colRole)
            {
                _userRoleHaveViewModelList.Clear();
                var allUserHaveList = await BLLFactory<Yw.BLL.UserRoleMapping>.Instance.GetAuthorizeRoleList(vm.ID);
                foreach (var item in allUserHaveList)
                {
                    _userRoleHaveViewModelList.Add(new UserRoleHaveViewModel(item));
                }
                this.userRoleHaveViewModelBindingSource.ResetBindings(false);
                flyoutPanel1.ShowPopup();
            }
            if (e.Column == this.colAccount)
            {
                _userLoginAccountViewModelList.Clear();
                var allLoginAccountList = await BLLFactory<Yw.BLL.UserLoginAccountStd>.Instance.GetByUserID(vm.ID);
                foreach (var item in allLoginAccountList)
                {
                    _userLoginAccountViewModelList.Add(new UserAccountViewModel(item));
                }
                this.userAccountViewModelBindingSource.ResetBindings(false);
                flyoutPanel2.ShowPopup();
            }
        }
 
        /// <summary>
        /// 账户单元格点击事件
        /// </summary>
        private async void gridView3_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            var vm = this.gridView3.GetCurrentViewModel(_userLoginAccountViewModelList);
            if (vm == null)
            {
                return;
            }
            if (e.Column == this.colResetPwd)
            {
                if (XtraMessageBox.Show($"确认重置密码吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) != DialogResult.OK)
                    return;
                var userVm = GetCurrentViewModel();
                var loginAccount = await BLLFactory<Yw.BLL.UserLoginAccount>.Instance.GetByUserID(userVm.ID);
                if (loginAccount == null || loginAccount.Count == 0)
                    return;
                var bll = new Yw.BLL.UserLoginAccount();
                if (!await bll.ResetSystemLoginPwd(vm.ID))
                {
                    XtraMessageBox.Show("重置失败!");
                    return;
                }
                // var model = bll.GetByID(row.UserID);
                // row.Reset(model);
                this.gridView1.RefreshRow(this.gridView1.FocusedRowHandle);
                XtraMessageBox.Show("重置成功!");
            }
            if (e.Column == this.colEditPwd)
            {
                var dlg = new UpdatePwdDlg();
                dlg.SetBindingData(vm.ID);
                dlg.ShowDialog();
            }
        }
 
        private void flyoutPanel2_ButtonClick(object sender, DevExpress.Utils.FlyoutPanelButtonClickEventArgs e)
        {
            if (e.Button.Caption.Equals("关闭"))
            {
                flyoutPanel2.HidePopup();
            }
        }
 
        //使用状态
        private void BtnEditStatus_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var dlg = new SetUseStatusDlg();
            dlg.SetBindingData((Yw.Model.eUseStatus)vm.UseStatus);
            dlg.ReloadDataEvent += async (status) =>
            {
                var bol = await BLLFactory<Yw.BLL.User>.Instance.UpdateUseStatus(vm.ID, (int)status);
                if (bol)
                {
                    vm.UseStatus = (eUseStatus)status;
                    this.gridView1.RefreshRow(this.gridView1.FocusedRowHandle);
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
    }
}