Shuxia Ning
2025-02-13 2f1cbec203dcff25df7a5c2b51b13ec558f2c3db
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
using DevExpress.XtraEditors;
using IStation.Untity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
 
namespace IStation.WinFrmUI.Basic
{
    /// <summary>
    /// 
    /// </summary>
    public partial class SignalTypeGroupListCtrl : XtraUserControl
    {
        public SignalTypeGroupListCtrl()
        {
            InitializeComponent();
            this.treeList1.InitialDefaultSettings();
            this.treeList1.SelectImageList = ImageLib.Lib;
            this.layoutControl1.SetupLayoutControl();
        }
 
 
        public class CurrentViewModel
        {
            public CurrentViewModel() { }
 
            public CurrentViewModel(Model.Station rhs)
            {
                Reset(rhs);
            }
            public CurrentViewModel(Model.SignalTypeGroup rhs)
            {
                Reset(rhs);
            }
 
            public void Reset(Model.Station rhs)
            {
                this.ID = rhs.ID;
                this.Name = rhs.Name;
                this.ObjectType = IStation.ObjectType.Station;
                this.SortCode = rhs.SortCode;
                this.ImageIndex = ImageLib.Station;
                this.Model = rhs;
            }
 
            public void Reset(Model.SignalTypeGroup rhs)
            {
                this.ID = rhs.ID;
                this.ParentID = TreeParentIdsHelper.GetLastParentID(rhs.ParentIds);
                this.Name = rhs.Name;
                this.ObjectType = IStation.ObjectType.SignalTypeGroup;
                this.SortCode = rhs.SortCode;
                this.ImageIndex = ImageLib.Group;
                this.Model = rhs;
            }
 
            public long ID { get; set; }
            public long ParentID { get; set; }
            public string Name { get; set; }
            public string ObjectType { get; set; }
            public int SortCode { get; set; }
            public int ImageIndex { get; set; }
            public bool IsVirtual { get; set; }
            public object Model { get; set; }
 
        }
 
        /// <summary>
        /// 聚焦改变事件
        /// </summary> 
        public event Action<string, object> FocusedChangedEvent;
 
 
        private BLL.SignalTypeGroup _bll = new BLL.SignalTypeGroup();
        private List<CurrentViewModel> _allBindingList = null;//所有绑定列表
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void Clear()
        {
            _allBindingList = new List<CurrentViewModel>();
            this.treeList1.DataSource = _allBindingList;
            this.FocusedChangedEvent?.Invoke(string.Empty, null);
        }
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData()
        {
            WaitFrmHelper.ShowWaitForm("正在加载数据...");
 
            _allBindingList = new List<CurrentViewModel>();
 
            var signalTypeGroups = _bll.GetAll();
            if (signalTypeGroups != null && signalTypeGroups.Any())
            {
                foreach (var item in signalTypeGroups)
                {
                    var vm = new CurrentViewModel(item);
                    _allBindingList.Add(vm);
                }
            }
 
            _allBindingList.Add(new CurrentViewModel()
            {
                ID = SnowflakeIdHelper.NextId(),
                Name = "未分组",
                ObjectType = IStation.ObjectType.SignalTypeGroup,
                SortCode = int.MaxValue,
                ImageIndex = ImageLib.Question,
                IsVirtual = true,
                Model = new Model.SignalTypeGroup()
                {
                    ID = -1
                }
            });
 
 
            this.bindingSource1.DataSource = _allBindingList;
            WaitFrmHelper.HideWaitForm();
        }
 
        //全部展开
        private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.ExpandAll();
        }
 
        //全部折叠 
        private void barBtnCollapseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.CollapseAll();
        }
 
        //检索
        private void barCkSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.barCkSearch.Checked)
                this.treeList1.ShowFindPanel();
            else
                this.treeList1.HideFindPanel();
        }
 
        //聚焦改变
        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
                return;
            if (vm.IsVirtual)
            {
                SetBarBtnEnabled(false, false);
            }
            else
            {
                var isStation = vm.ObjectType == IStation.ObjectType.Station;
                SetBarBtnEnabled(isStation, !isStation);
            }
 
            this.FocusedChangedEvent?.Invoke(vm.ObjectType, vm.Model);
        }
 
        //按钮使用状态
        private void SetBarBtnEnabled(bool isStation, bool isSignalType)
        {
            this.barBtnAdd.Enabled = isStation;
            this.barBtnAddChildren.Enabled = isSignalType;
            this.barBtnEdit.Enabled = isSignalType;
            this.barBtnDelete.Enabled = isSignalType;
            this.barBtnUpdateSortCode.Enabled = isSignalType;
            this.barBtnDetail.Enabled = isSignalType;
        }
 
        //添加
        private void barBtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_allBindingList == null)
                return;
            var vmStation = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (vmStation == null)
            {
                XtraMessageBox.Show("请选择数据行!");
                return;
            }
            WaitFrmHelper.ShowWaitForm();
            var dlg = new AddSignalTypeGroupDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData();
            dlg.ReloadDataEvent += (rhs) =>
            {
                rhs.SortCode = _allBindingList.Count(x => x.ParentID == vmStation.ID) + 1;
                rhs.ID = _bll.Insert(rhs);
                if (rhs.ID > 0)
                {
                    var vm = new CurrentViewModel(rhs);
                    _allBindingList.Add(vm);
                    this.bindingSource1.ResetBindings(false);
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        //添加子项
        private void barBtnAddChildren_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_allBindingList == null)
                return;
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                XtraMessageBox.Show("请选择数据行!");
                return;
            }
            if (currentVm.IsVirtual)
            {
                XtraMessageBox.Show("虚拟组无法添加子项!");
                return;
            }
 
            WaitFrmHelper.ShowWaitForm();
            var dlg = new AddSignalTypeGroupDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData();
            dlg.ReloadDataEvent += (rhs) =>
            {
                var parentGroup = currentVm.Model as Model.SignalTypeGroup;
                rhs.ParentIds = TreeParentIdsHelper.GetChildParentIds(parentGroup.ID, parentGroup.ParentIds);
                rhs.SortCode = _allBindingList.Count(x => x.ParentID == parentGroup.ID) + 1;
                rhs.ID = _bll.Insert(rhs);
                if (rhs.ID > 0)
                {
                    var vm = new CurrentViewModel(rhs);
                    _allBindingList.Add(vm);
                    this.bindingSource1.ResetBindings(false);
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        //编辑
        private void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                XtraMessageBox.Show("请选择数据行!");
                return;
            }
            if (currentVm.IsVirtual)
            {
                XtraMessageBox.Show("虚拟组无法编辑!");
                return;
            }
            var group = currentVm.Model as Model.SignalTypeGroup;
            WaitFrmHelper.ShowWaitForm();
            var dlg = new EditSignalTypeGroupDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData(group);
            dlg.ReloadDataEvent += (rhs) =>
            {
                var bol = _bll.Update(rhs);
                if (bol)
                {
                    currentVm.Reset(rhs);
                    this.treeList1.RefreshNode(this.treeList1.FocusedNode);
                }
                return bol;
            };
            dlg.ShowDialog();
        }
 
        //删除
        private void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                XtraMessageBox.Show("请选择数据行!");
                return;
            }
            if (currentVm.IsVirtual)
            {
                XtraMessageBox.Show("虚拟组无法删除!");
                return;
            }
            if (XtraMessageBox.Show($"确认删除数据行?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                return;
            var result = _bll.DeleteByID(currentVm.ID, out string msg);
            if (result)
            {
                if (string.IsNullOrEmpty(msg))
                {
                    XtraMessageBox.Show($"删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    XtraMessageBox.Show($"删除成功!\n警告:{msg}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                XtraMessageBox.Show($"删除失败!\n原因:{msg}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _allBindingList.Remove(currentVm);
            this.bindingSource1.ResetBindings(false);
        }
 
        //详细信息
        private void barBtnDetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraMessageBox.Show("待补充!");
        }
 
        //排序码
        private void barBtnUpdateSortCode_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                XtraMessageBox.Show("请选择数据行!");
                return;
            }
            if (currentVm.IsVirtual)
            {
                XtraMessageBox.Show("虚拟组无法修改!");
                return;
            }
            var dlg = new SetSortCodeDlg();
            dlg.SetBindingData(currentVm.SortCode);
            dlg.ReloadDataEvent += (rhs) =>
            {
                var model = currentVm.Model as Model.SignalTypeGroup;
                model.SortCode = rhs;
                var result = _bll.Update(model);
                if (result)
                {
                    currentVm.Reset(model);
                    this.bindingSource1.ResetBindings(false);
                }
                return result;
            };
            dlg.ShowDialog();
        }
 
    }
}