tangxu
2024-03-20 7947ecd7933cc92f5d9565afb51aff6e744074c5
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using IStation.WinFrmUI;
using IStation.Untity;
 
 
namespace IStation.WinFrmUI.Product
{
    /// <summary>
    /// 设备分组管理控件
    /// </summary>
    public partial class ProductGroupMgrCtrl : XtraUserControl
    {
        public ProductGroupMgrCtrl()
        {
            InitializeComponent();
            this.treeList1.InitialDefaultSettings();
            this.treeList1.SelectImageList = CurrentViewModel.Image16Store;
            this.layoutControl1.SetupLayoutControl();
        }
 
        #region 当前视图
 
        public class CurrentViewModel
        {
            public CurrentViewModel() { }
            public CurrentViewModel(Model.ProductGroup rhs)
            {
                this.ID = rhs.ID;
                this.ParentID = TreeParentIdsHelper.GetLastParentID(rhs.ParentIds);
                if (this.ParentID < 1)
                {
                    this.ParentID = -1;
                }
                this.ParentIds = TreeParentIdsHelper.ToString(rhs.ParentIds);
                this.Name = rhs.Name;
                this.TagName = rhs.TagName;
                this.SortCode = rhs.SortCode;
                this.Description = rhs.Description;
                this.Model = rhs;
            }
 
            public void Reset(Model.ProductGroup rhs)
            {
                this.ID = rhs.ID;
                this.ParentID = TreeParentIdsHelper.GetLastParentID(rhs.ParentIds);
                if (this.ParentID < 1)
                {
                    this.ParentID = -1;
                }
                this.ParentIds = TreeParentIdsHelper.ToString(rhs.ParentIds);
                this.Name = rhs.Name;
                this.TagName = rhs.TagName;
                this.SortCode = rhs.SortCode;
                this.Description = rhs.Description;
                this.Model = rhs;
            }
 
            public long ID { get; set; }
            public long ParentID { get; set; }
            public string ParentIds { get; set; }
            public string Name { get; set; }
            public string TagName { get; set; }
            public int SortCode { get; set; }
            public string Description { get; set; }
            public int ImageIndex
            {
                get { return 0; }
            }
            public Model.ProductGroup Model { get; set; }
 
            /// <summary>
            /// 图标仓库
            /// </summary>
            public static ImageCollection Image16Store
            {
                get
                {
                    if (_image16Store == null)
                    {
                        _image16Store = new ImageCollection();
                        _image16Store.ImageSize = new Size(16, 16);
                        _image16Store.Images.Add(WinFrmUI.Properties.Resources.GroupC32, "Group");
                    }
                    return _image16Store;
                }
            }
            private static ImageCollection _image16Store;
        }
 
        #endregion
 
        /// <summary>
        /// 选择改变事件
        /// </summary>
        public event Action<Model.ProductGroup> SelectChangedEvent;
 
        private BindingList<CurrentViewModel> _allBindingList = null;//所有绑定列表
        private long _corpId;//当前客户
        private string _belongType;//所属类型
        private long _belongId;//所属标识
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData()
        {
            _allBindingList = null;
            this.SelectChangedEvent?.Invoke(null);
        }
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData(long corpId, string belongType, long belongId)
        {
            _corpId = corpId;
            _belongType = belongType;
            _belongId = belongId;
            _allBindingList = new BindingList<CurrentViewModel>();
            var all = new BLL.ProductGroup().GetByBelongTypeAndBelongID(_corpId, _belongType, _belongId);
            all?.ForEach(x =>
            {
                var vm = new CurrentViewModel(x);
                _allBindingList.Add(vm);
            });
 
            var group_none = new Model.ProductGroup();
            group_none.ID = 0;
            group_none.CorpID = _corpId;
            group_none.BelongType = _belongType;
            group_none.BelongID = _belongId;
            group_none.Name = "未分组";
            group_none.SortCode = int.MaxValue;
            _allBindingList.Add(new CurrentViewModel(group_none));
 
            this.treeList1.DataSource = _allBindingList;
            this.treeList1.RefreshDataSource();
            this.treeList1.ExpandAll();
            SelectChanged();
        }
 
        /// <summary>
        ///  选择改变项
        /// </summary>
        public void SelectChanged()
        {
            var row = this.treeList1.GetFocusedRow() as CurrentViewModel;
            this.SelectChangedEvent?.Invoke(row?.Model);
        }
 
        #region 基础方法 
 
        //添加根
        public void AddRoot()
        {
            if (_allBindingList == null)
                return;
            var items = _allBindingList.Where(x => x.ID > 0 && x.ParentID == -1).ToList();
            int sort_code = items.Count > 0 ? items.Max(x => x.SortCode) + 1 : 1;
            WaitFrmHelper.ShowWaitForm();
            var dlg = new AddProductGroupDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData(_corpId, _belongType, _belongId, sort_code);
            dlg.ReloadDataEvent += (rhs) =>
            {
                var vm = new CurrentViewModel(rhs);
                _allBindingList.Add(vm);
                this.treeList1.RefreshDataSource();
                XtraMessageBox.Show("添加成功!");
            };
            dlg.ShowDialog();
        }
 
        //添加子项
        public void AddChild()
        {
            var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                XtraMessageBox.Show("请选择数据行!");
                return;
            }
            if (vm.ID < 1)
            {
                XtraMessageBox.Show("请选择实际分组!");
                return;
            }
            var items = _allBindingList.Where(x => x.ParentID == vm.ID).ToList();
            var sort_code = items.Count > 0 ? items.Max(x => x.SortCode) + 1 : 1;
            var parent_id_list = TreeParentIdsHelper.GetChildParentIds(vm.ID, vm.Model.ParentIds);
            WaitFrmHelper.ShowWaitForm();
            var dlg = new AddProductGroupDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData(_corpId, _belongType, _belongId, sort_code, parent_id_list);
            dlg.ReloadDataEvent += (rhs) =>
            {
                var vm_rhs = new CurrentViewModel(rhs);
                _allBindingList.Add(vm_rhs);
                this.treeList1.RefreshDataSource();
                XtraMessageBox.Show("添加成功!");
            };
            dlg.ShowDialog();
        }
 
        //编辑
        public void Edit()
        {
            var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                XtraMessageBox.Show("请选择数据行!");
                return;
            }
            if (vm.ID < 1)
            {
                XtraMessageBox.Show("请选择实际分组!");
                return;
            }
            WaitFrmHelper.ShowWaitForm();
            var dlg = new EditProductGroupDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData(vm.Model);
            dlg.ReloadDataEvent += (rhs) =>
            {
                vm.Reset(rhs);
                this.treeList1.RefreshNode(this.treeList1.FocusedNode);
                XtraMessageBox.Show("更新成功!");
            };
            dlg.ShowDialog();
        }
 
        //删除
        public void Delete()
        {
            var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                XtraMessageBox.Show("请选择数据行!");
                return;
            }
            if (vm.ID < 1)
            {
                XtraMessageBox.Show("请选择实际分组!");
                return;
            }
            XtraMessageBox.Show("正在开发中,敬请期待!");
        }
 
        //详细信息
        public void View()
        {
            var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                XtraMessageBox.Show("请选择数据行!");
                return;
            }
            if (vm.ID < 1)
            {
                XtraMessageBox.Show("请选择实际分组!");
                return;
            }
            WaitFrmHelper.ShowWaitForm();
            var dlg = new ViewProductGroupDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData(vm.Model);
            dlg.ShowDialog();
        }
 
        //更新 TagName
        public void UpdateTagName()
        {
            var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                XtraMessageBox.Show("请选择数据行!");
                return;
            }
            if (vm.ID < 1)
            {
                XtraMessageBox.Show("请选择实际分组!");
                return;
            }
            WaitFrmHelper.ShowWaitForm();
            var dlg = new SetTagNameDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData(vm.TagName);
            dlg.ReloadDataEvent += (rhs) =>
            {
                var bll = new BLL.ProductGroup();
                var result = bll.UpdateTagName(vm.Model.CorpID, vm.ID, rhs);
                if (result)
                {
                    var model = bll.GetByID(vm.Model.CorpID, vm.ID);
                    vm.Reset(model);
                    this.treeList1.RefreshNode(this.treeList1.FocusedNode);
                }
                return result;
            };
            dlg.ShowDialog();
        }
 
        //更新排序码
        public void UpdateSortCode()
        {
            var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                XtraMessageBox.Show("请选择数据行!");
                return;
            }
            if (vm.ID < 1)
            {
                XtraMessageBox.Show("请选择实际分组!");
                return;
            }
            var dlg = new SetSortCodeDlg();
            dlg.SetBindingData(vm.SortCode);
            dlg.ReloadDataEvent += (rhs) =>
            {
                var bll = new BLL.ProductGroup();
                var result = bll.UpdateSortCode(vm.Model.CorpID, vm.ID, rhs);
                if (result)
                {
                    var model = bll.GetByID(vm.Model.CorpID, vm.ID);
                    vm.Reset(model);
                    this.treeList1.RefreshNode(this.treeList1.FocusedNode);
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        /// <summary>
        /// 获取当前分组
        /// </summary>
        public Model.ProductGroup GetFocused()
        {
            var vm = this.treeList1.GetFocusedRow() as CurrentViewModel;
            return vm?.Model;
        }
 
        #endregion
 
        #region 事件处理器
 
 
        //全部展开(menu)
        private void barBtnExpandAllInMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.ExpandAll();
        }
 
        //全部折叠(menu)
        private void barBtnCollpseAllInMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.CollapseAll();
        }
 
        //检索(menu)
        private void barBtnSearchInMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.layoutControlItem1.Visibility == DevExpress.XtraLayout.Utils.LayoutVisibility.Always)
                this.layoutControlItem1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            else
                this.layoutControlItem1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
        }
 
        //树线
        private void barCkTreeLine_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.OptionsView.ShowTreeLines = this.barCkTreeLine.Checked ? DefaultBoolean.True : DefaultBoolean.False;
        }
 
        //拖拽开关
        private void barCkDragSort_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var be = this.behaviorManager1.GetBehavior<DevExpress.Utils.DragDrop.DragDropBehavior>(this.treeList1);
            be.Properties.AllowDrag = this.barCkDragSort.Checked;
        }
 
        //根节点添加分组
        private void barBtnAddRoot_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddRoot();
        }
 
        //节点下添加分组
        private void barBtnAddChild_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddChild();
        }
 
        //节点编辑
        private void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit();
        }
 
        //删除
        private void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Delete();
        }
 
        //详细信息
        private void barBtnDetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            View();
        }
 
        //更新标签
        private void barBtnTagName_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UpdateTagName();
        }
 
        //更新排序
        private void barBtnSortCode_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UpdateSortCode();
        }
 
        //全部折叠 (node)
        private void btnCollpaseAllInNode_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.FocusedNode?.Collapse();
        }
 
        //全部展开 (node)
        private void btnExpandAllInNode_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.FocusedNode?.Expand();
        }
 
        //右键菜单弹出
        private void treeList1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                var node = this.treeList1.GetNodeAt(e.X, e.Y);
                if (node == null)
                {
                    this.pmRoot.ShowPopup(MousePosition);
                    return;
                }
            }
        }
 
        //右键菜单弹出
        private void treeList1_RowCellClick(object sender, DevExpress.XtraTreeList.RowCellClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (e.Column == this.colName)
                {
                    SelectChanged();
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                this.pmNode.ShowPopup(MousePosition);
            }
        }
 
        #endregion
 
    }
 
}