duheng
2024-11-17 a2a57963e160a319276c5c8499f16c9809056e4c
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
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
using HStation.WinFrmUI.Assets;
using System.ComponentModel;
using Yw;
 
namespace HStation.WinFrmUI.Xhs.PumpProduct
{
    /// <summary>
    ///
    /// </summary>
    public partial class PumpProductSeriesTreeListCtrl : XtraUserControl
    {
        public PumpProductSeriesTreeListCtrl()
        {
            InitializeComponent();
            this.treeList1.InitialDefaultSettings();
            this.treeList1.SelectImageList = ImageLib.Lib;
        }
 
        /// <summary>
        /// 聚焦改变事件
        /// </summary>
        public event Action<long, bool> FocusedChangedEvent;
 
        private BindingList<CurrentTreeViewModel> _allBindingList = null;//所有绑定列表
 
        private BLL.AssetsPumpSeries _seriesbll = null;
        private BLL.AssetsPumpGroup _groupbll = null;
        private BLL.AssetsPumpType _typebll = null;
        private BLL.AssetsPumpSeriesTypMapping _typeSeriesMapBll = null;
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void Clear()
        {
            _allBindingList = new BindingList<CurrentTreeViewModel>();
            this.treeList1.DataSource = _allBindingList;
            this.FocusedChangedEvent?.Invoke(default, default);
        }
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public async void SetBindingData()
        {
            _seriesbll = new BLL.AssetsPumpSeries();
            _groupbll = new BLL.AssetsPumpGroup();
            _typebll = new BLL.AssetsPumpType();
            _typeSeriesMapBll = new BLL.AssetsPumpSeriesTypMapping();
            _allBindingList = new BindingList<CurrentTreeViewModel>();
            var allType = await _typebll.GetAll();
            _allBindingList.Add(new CurrentTreeViewModel
            {
                ID = 1,
                Name = "泵列表",
                ImageIndex = ImageLib.Listview
            });
            foreach (var item in allType)
            {
                var type = new CurrentTreeViewModel(item);
                _allBindingList.Add(type);
                var allseriesid = await _typeSeriesMapBll.GetSeriesIDByTypeID(item.ID);
                if (allseriesid.Count > 0 && allseriesid != null)
                {
                    var allseries = await _seriesbll.GetByIds(allseriesid);
                    foreach (var series in allseries)
                    {
                        var vm = new CurrentTreeViewModel(series, item.ID);
                        _allBindingList.Add(vm);
                        var allgroup = await _groupbll.GetBySeriesID(series.ID);
                        foreach (var group in allgroup)
                        {
                            var groupvm = new CurrentTreeViewModel(group);
                            _allBindingList.Add(groupvm);
                        }
                    }
                }
            }
            this.treeList1.DataSource = _allBindingList;
            this.treeList1.RefreshDataSource();
            TreeListNode firstParentNode = treeList1.Nodes[0]; // 假设这是你要展开的父节点
            firstParentNode.Expand();
        }
 
        //聚焦改变
        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (vm != null)
            {
                var series = vm.Model as Vmo.AssetsPumpSeriesVmo;
                if (series != null)
                {
                    this.FocusedChangedEvent?.Invoke(series.ID, true);
                }
                else
                {
                    var group = vm.Model as Vmo.AssetsPumpGroupVmo;
                    if (group != null)
                    {
                        this.FocusedChangedEvent?.Invoke(group.ID, false);
                    }
                }
            }
        }
 
        #region 菜单事件
 
        //检索
        private void barCkSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.barCkSearch.Checked)
                this.layoutControlItemSearch.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            else
                this.layoutControlItemSearch.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
        }
 
        #endregion 菜单事件
 
        //添加分类
        private void barBtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_allBindingList == null)
                return;
            var dlg = new AddAssetsPumpTypeDlg();
            dlg.ReloadDataEvent += async (rhs) =>
            {
                var bll = new BLL.AssetsPumpType();
                var id = await bll.Insert(rhs);
                if (id > 0)
                {
                    rhs.ID = id;
                    _allBindingList.Add(new CurrentTreeViewModel(rhs));
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        /// <summary>
        /// 临时  (添加泵型号)
        /// </summary>
    /*    private void get()
        {
            if (_allBindingList == null)
                return;
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            var dlg = new AddPumpProductSeriesDlg();
            dlg.ReloadDataEvent += async (model) =>
            {
                var allchildList = await _seriesbll.GetAll();
                var id = await _seriesbll.Insert(model);
                if (id > 0)
                {
                    var vm = await _seriesbll.GetByID(id);
                    _allBindingList.Add(new CurrentTreeViewModel(vm));
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }*/
 
        //全部折叠
        private void barBtnCollapseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.CollapseAll();
        }
 
        //全部展开
        private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.ExpandAll();
        }
 
        //编辑
        private async void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                TipFormHelper.ShowWarn("请选择数据行!");
                return;
            }
            if (currentVm.IsGroup == false)
            {
                var series = currentVm.Model as Model.AssetsPumpSeries;
                var dlg = new EditPumpProductSeriesDlg();
                var model = await new BLL.AssetsPumpSeries().GetByID(currentVm.ID);
                if (model == null)
                    return;
                dlg.SetBindingData(model);
                dlg.ReloadDataEvent += async (model) =>
               {
                   if (await _seriesbll.Update(model))
                   {
                       currentVm.Reset(model);
                       this.treeList1.RefreshDataSource();
                       return true;
                   }
                   return false;
               };
                dlg.ShowDialog();
            }
            else
            {
                var dlg = new EditPumpProductGroupDlg();
                var GroupDto = await new BLL.AssetsPumpGroup().GetByID(currentVm.ID);
                if (GroupDto == null)
                    return;
                dlg.SetBindingData(GroupDto);
                dlg.ReloadDataEvent += async (model) =>
               {
                   if (await _groupbll.Update(model))
                   {
                       currentVm.Reset(model);
                       this.treeList1.RefreshDataSource();
                       return true;
                   }
                   return false;
               };
                dlg.ShowDialog();
            }
        }
 
        //删除
        private async void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                MessageBoxHelper.ShowWarning("请选择数据行!");
                return;
            }
            if (MessageBoxHelper.IsClickOk($"确认删除数据行?", "提示"))
                return;
            if (currentVm.IsGroup)
            {
                var groupresult = await _groupbll.DeleteEx(currentVm.ID);
                if (groupresult)
                {
                    _allBindingList.Remove(currentVm);
                }
                else
                {
                    MessageBoxHelper.ShowError($"删除失败!");
                    return;
                }
            }
            else
            {
                var result = await BLLFactory<BLL.AssetsPumpSeries>.Instance.DeleteEx(currentVm.ID);
                if (result)
                {
                    _allBindingList.Remove(currentVm);
                    var children = _allBindingList.Where(x => x.ParentID == currentVm.ID).ToList();
                    foreach (var child in children)
                    {
                        _allBindingList.Remove(child);
                    }
                    MessageBoxHelper.ShowSuccess($"删除成功!");
                    this.treeList1.Refresh();
                    return;
                }
                else
                {
                    MessageBoxHelper.ShowError($"删除失败!");
                    return;
                }
            }
        }
 
        //添加型号组
        private void barBtnAddChild_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                MessageBoxHelper.ShowWarning("请选择数据行!");
                return;
            }
            if (_allBindingList == null)
                return;
            var dlg = new AddPumpProductGroupDlg();
            dlg.SetBindingData(currentVm.ID);
            dlg.ReloadDataEvent += async (model) =>
            {
                var allchildList = await BLLFactory<BLL.AssetsPumpGroup>.Instance.GetAll();
                model.SortCode = allchildList.Count == 0 ? 1 : allchildList.Max(x => x.SortCode) + 1;
                var id = await _groupbll.Insert(model);
                if (id > 0)
                {
                    var vm = await BLLFactory<BLL.AssetsPumpGroup>.Instance.GetByID(id);
                    _allBindingList.Add(new CurrentTreeViewModel(vm));
                    this.treeList1.Refresh();
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        public long GetCurrentID()
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                MessageBoxHelper.ShowWarning("请选择数据行!");
                return default;
            }
            if (currentVm.IsGroup)
            {
                return currentVm.ParentID;
            }
            else if (currentVm.IsType)
            {
                MessageBoxHelper.ShowWarning("请选择系列再进行添加操作!");
                return default;
            }
            else
            {
                return currentVm.ID;
            }
        }
 
        public long GetCurrentGroupID()
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                MessageBoxHelper.ShowWarning("请选择数据行!");
                return default;
            }
            if (currentVm.IsGroup)
            {
                return currentVm.ID;
            }
            else
            {
                return default;
            }
        }
 
        //右击显示菜单
        private void treeList1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
                if (vm != null)
                {
                    Point screenPointType = Cursor.Position;
                    if (vm.ID == 1)
                    {
                        popupMenuListView.ShowPopup(screenPointType);
                        return;
                    }
                    if (vm.IsType)
                    {
                        popupMenuType.ShowPopup(screenPointType);
                    }
                    else if (!vm.IsGroup)
                    {
                        popupMenuSeries.ShowPopup(screenPointType);
                    }
                    else
                    {
                        popupMenuGroup.ShowPopup(screenPointType);
                    }
                }
            }
        }
 
        #region 分类菜单
 
        private void BarBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.SetBindingData();
        }
 
        #endregion 分类菜单
 
        #region 类型菜单
 
        //添加泵系列
        private void BarButtonAddAssetsPumpSeries_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_allBindingList == null)
                return;
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                MessageBoxHelper.ShowWarning("请选择数据行!");
                return;
            }
            var dlg = new AddPumpProductSeriesDlg();
            dlg.ReloadDataEvent += async (model) =>
            {
                //var allchildList = await _seriesbll.GetAll();
 
                var id = await _seriesbll.Insert(model);
                if (id > 0)
                {
                    var typemapid = await _typeSeriesMapBll.Insert(
                        new Vmo.AssetsPumpTypeSeriesMap
                        {
                            SeriesID = id,
                            TypeID = currentVm.ID
                        }
                        );
                    var vm = await _seriesbll.GetByID(id);
                    _allBindingList.Add(new CurrentTreeViewModel(vm, currentVm.ID));
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        //删除分类
        private async void BarButtonDeleteAssetsPumpType_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                MessageBoxHelper.ShowWarning("请选择数据行!");
                return;
            }
            if (MessageBoxHelper.IsClickOk($"确认删除数据行?", "提示"))
                return;
            var typeresult = await _typebll.DeleteByID(currentVm.ID);
            if (typeresult)
            {
                _allBindingList.Remove(currentVm);
                TipFormHelper.ShowSucceed("删除成功!");
                return;
            }
            else
            {
                TipFormHelper.ShowSucceed("删除失败!");
            }
        }
 
        //编辑分类
        private void BarButtonEditAssetsPumpType_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                TipFormHelper.ShowWarn("请选择数据行!");
                return;
            }
            var dlg = new EditAssetsPumpTypeDlg();
            dlg.SetBindingData(currentVm.ID);
            dlg.ReloadDataEvent += async (rhs) =>
            {
                var bll = new BLL.AssetsPumpType();
                if (await bll.Update(rhs))
                {
                    currentVm.Reset(rhs);
                    this.treeList1.RefreshDataSource();
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        #endregion 类型菜单
 
        #region 型号菜单
 
        public event Action<object, DevExpress.XtraBars.ItemClickEventArgs> AddAssetsPumpMain;
 
        private void BarButtonAddAssetsPumpMain_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddAssetsPumpMain.Invoke(null, null);
        }
 
        #endregion 型号菜单
    }
}