Shuxia Ning
2024-10-08 cf4967a0aebab18c5a37137f3e4c61b2d73a54bb
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
using DevExpress.Utils;
using DevExpress.XtraEditors;
using IStation.Untity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
 
 
namespace IStation.WinFrmUI.Scatl.Monitor
{
    /// <summary>
    /// 分析测点树列表控件
    /// </summary>
    public partial class MultiSelectAnaMonitorPointTreeListCtrl : XtraUserControl
    {
        public MultiSelectAnaMonitorPointTreeListCtrl()
        {
            InitializeComponent();
            this.treeList1.InitialDefaultSettings();
            this.treeList1.SelectImageList = CurrentViewModel.Image16Store;
            this.layoutControl1.SetupLayoutControl();
        }
 
        #region 当前视图
 
        public class CurrentViewModel
        {
            public CurrentViewModel() { }
 
            public CurrentViewModel(Model.Scatl.LogicTreeEx rhs)
            {
                this.ID = $"{rhs.LogicType}_{rhs.LogicId}";
                this.Name = rhs.LogicName;
                this.DispName = rhs.LogicName;
                this.ObjectType = rhs.LogicType;
                this.ObjectID = rhs.LogicId;
                this.SortCode = rhs.SortCode;
                this.Description = rhs.Description;
                this.ImageIndex = Get16ObjectTypeImageIndex(rhs.LogicType);
                this.Model = rhs;
            }
 
            public CurrentViewModel(Model.Scatl.MonitorPointGroup rhs)
            {
                this.ID = $"{IStation.Scatl.ObjectType.MonitorPointGroup_监测点组}_{rhs.Id}";
                if (rhs.ParentIds != null && rhs.ParentIds.Count > 0)
                {
                    this.ParentID = $"{IStation.Scatl.ObjectType.MonitorPointGroup_监测点组}_{TreeParentIdsHelper.GetLastParentID(rhs.ParentIds)}";
                }
                else
                {
                    this.ParentID = $"{rhs.BelongType}_{rhs.BelongId}";
                }
                this.Name = rhs.Name;
                this.DispName = rhs.Name;
                this.ObjectType = IStation.Scatl.ObjectType.MonitorPointGroup_监测点组;
                this.ObjectID = rhs.Id;
                this.SortCode = rhs.SortCode;
                this.Description = rhs.Description;
                this.ImageIndex = Get16ObjectTypeImageIndex(this.ObjectType);
                this.Model = rhs;
            }
 
            public CurrentViewModel(Model.Scatl.MonitorPoint rhs)
            {
                this.ID = $"{IStation.Scatl.ObjectType.MonitorPoint_监测点}_{rhs.Id}";
                if (rhs.GroupId < 1)
                {
 
                }
                else
                {
                    this.ParentID = $"{IStation.Scatl.ObjectType.MonitorPointGroup_监测点组}_{rhs.GroupId}";
                }
 
                this.Name = rhs.Name;
                this.DispName = rhs.Name;
                this.ObjectType = IStation.Scatl.ObjectType.MonitorPoint_监测点;
                this.ObjectID = rhs.Id;
                this.SortCode = rhs.SortCode;
                this.Description = rhs.Description;
                this.ImageIndex = Get16ObjectTypeImageIndex(this.ObjectType);
                this.Model = rhs;
            }
 
            public string ID { get; set; }
            public string ParentID { get; set; }
            public string DispName { get; set; }
            public string Name { get; set; }
            public string ObjectType { get; set; }
            public long ObjectID { get; set; }
            public int SortCode { get; set; }
            public string Description { get; set; }
            public int ImageIndex { get; set; }
            public object Model { get; set; }
            public bool? Checked { 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.Scatl.Properties.Resources.Logic32, "Logic");
                        _image16Store.Images.Add(WinFrmUI.Scatl.Properties.Resources.Station32, "Station");
                        _image16Store.Images.Add(WinFrmUI.Scatl.Properties.Resources.Group32, "Group");
                        _image16Store.Images.Add(WinFrmUI.Scatl.Properties.Resources.MonitorPoint32, "MonitorPoint");
                    }
                    return _image16Store;
                }
            }
            private static ImageCollection _image16Store;
 
 
            public static Image Get16ObjectTypeImage(string objectType)
            {
                Image img = null;
                switch (objectType)
                {
                    case IStation.Scatl.ObjectType.LogicArea_业务区域: img = Image16Store.Images["Logic"]; break;
                    case IStation.Scatl.ObjectType.Station_泵站: img = Image16Store.Images["Station"]; break;
                    case IStation.Scatl.ObjectType.MonitorPointGroup_监测点组: img = Image16Store.Images["Group"]; break;
                    case IStation.Scatl.ObjectType.MonitorPoint_监测点: img = Image16Store.Images["MonitorPoint"]; break;
                }
                return img;
            }
 
            //获取
            public static int Get16ObjectTypeImageIndex(string objectType)
            {
                var img = Get16ObjectTypeImage(objectType);
                return Image16Store.Images.IndexOf(img);
            }
        }
 
        #endregion
 
        /// <summary>
        /// check 改变后
        /// </summary>
        public event Action<List<Model.Scatl.MonitorPointExSignalList>> CheckedChangedEvent;
 
        private List<Model.Scatl.LogicCatalog> _allCatalogList = null;//所有业务类别列表
        private BindingList<CurrentViewModel> _allBindingList = null;//所有绑定列表
 
 
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData(long projectId, Model.Scatl.Monitor.eSourceType sourceType, List<long> monitorPointIds = null)
        {
            _allCatalogList = new BLL.Scatl.LogicCatalog().GetAll(projectId);
            if (_allCatalogList == null)
                _allCatalogList = new List<Model.Scatl.LogicCatalog>();
            var logicTree = new BLL.Scatl.LogicTree().GetAllEx(projectId);
            var logicGroup = logicTree.GroupBy(x => x.CatalogId).ToList();
            logicTree = logicGroup?.OrderByDescending(x => x.Count()).First().Select(x => x).ToList();
            var group_list = new BLL.Scatl.MonitorPointGroup().GetAll(projectId);
            var point_list = new BLL.Scatl.MonitorPoint().GetExSignalList(projectId);
            point_list = point_list?.Where(x => x.SourceType == sourceType).ToList();
            this.SetBindingData(logicTree, group_list, point_list, monitorPointIds);
        }
 
 
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData(
              List<Model.Scatl.LogicTreeEx> all_logic_list,
            List<Model.Scatl.MonitorPointGroup> all_group_list,
            List<Model.Scatl.MonitorPointExSignalList> all_point_list,
            List<long> MonitorPointIds = null)
        {
            _allBindingList = new BindingList<CurrentViewModel>();
            if (all_point_list == null)
                all_point_list = new List<Model.Scatl.MonitorPointExSignalList>();
            if (all_logic_list != null && all_logic_list.Count > 0)
            {
                foreach (var logic in all_logic_list)
                {
                    var monitorPoints = all_point_list.Where(x => x.BelongType == logic.LogicType && x.BelongId == logic.LogicId).ToList();
                    if (monitorPoints == null || monitorPoints.Count() < 1)
                        continue;
                    var groups = all_group_list?.Where(x => x.BelongType == logic.LogicType && x.BelongId == logic.LogicId).ToList();
                    if (groups == null || groups.Count() < 1)
                    {
                        AddMoniorNodeList(monitorPoints, MonitorPointIds);
                    }
                    else
                    {
                        groups = groups.Where(x => monitorPoints.Exists(point => point.GroupId == x.Id)).ToList();
                        if (groups == null || groups.Count < 1)
                        {
                            AddMoniorNodeList(monitorPoints, MonitorPointIds);
                        }
                        else
                        {
                            foreach (var g in groups)
                            {
                                var g_moinotrPoints = monitorPoints.FindAll(x => x.GroupId == g.Id);
                                if (g_moinotrPoints == null || g_moinotrPoints.Count < 1)
                                    continue;
                                var v_grp_monitor = new CurrentViewModel(g);
                                var grp_check_status = AddMoniorNodeList(g_moinotrPoints, MonitorPointIds);
                                v_grp_monitor.Checked = grp_check_status;
                                _allBindingList.Add(v_grp_monitor);
                            }
                            //未分组的
                            var point_list0 = from x in monitorPoints where x.GroupId == 0 orderby x.SortCode select x;
                            AddMoniorNodeList(point_list0, MonitorPointIds);
                        }
                    }
                }
            }
 
            this.treeList1.DataSource = _allBindingList;
            this.treeList1.ForceInitialize();
            CheckedChanged();
        }
 
        private bool? AddMoniorNodeList(
            IEnumerable<Model.Scatl.MonitorPointExSignalList> all_point_list,
            List<long> MonitorPointIds = null)
        {
            if (all_point_list == null || all_point_list.Count() == 0)
                return false;
            var point_list = all_point_list.OrderBy(x => x.SortCode).ToList();
            int check_num = 0;
            foreach (var point in point_list)
            {
                var vm = new CurrentViewModel(point);
                if (MonitorPointIds != null && MonitorPointIds.Count > 0)
                {
                    if (MonitorPointIds.Contains(point.Id))
                    {
                        check_num++;
                        vm.Checked = true;
                    }
                }
                _allBindingList.Add(vm);
            }
            if (check_num == 0)
                return false;
            if (check_num == all_point_list.Count())
                return true;
            return null;
        }
 
 
        //全部展开
        private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.ExpandAll();
        }
 
        //全部折叠
        private void barBtnCollpseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.CollapseAll();
        }
 
        //检索
        private void barBtnSearch_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;
        }
        //显示ID
        private void barBtnDisplayID_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var isDisp = barBtnDisplayID.Checked;
            if (_allBindingList == null)
                return;
            foreach (var m in _allBindingList)
            {
                if (isDisp)
                    m.DispName = string.Format("{0} ({1})", m.Name, m.ObjectID);
                else
                    m.DispName = m.Name;
            }
            this.treeList1.DataSource = _allBindingList;
            this.treeList1.RefreshDataSource();
        }
 
        //树线
        private void barCkTreeLine_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.OptionsView.ShowTreeLines = this.barCkTreeLine.Checked ? DefaultBoolean.True : DefaultBoolean.False;
        }
 
        //选中节点后触发
        private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            SetCheckedChildNodes(e.Node, e.Node.CheckState);
            SetCheckedParentNodes(e.Node, e.Node.CheckState);
            CheckedChanged();
        }
 
        //选中事件
        private void CheckedChanged()
        {
            if (_allBindingList == null)
                return;
            var list = _allBindingList.Where(x => x.ObjectType == IStation.Scatl.ObjectType.MonitorPoint_监测点 && x.Checked == true).Select(x => x.Model as Model.Scatl.MonitorPointExSignalList).ToList();
            this.CheckedChangedEvent?.Invoke(list);
        }
 
 
        private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
        {
            e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
        }
 
 
        /// 设置子节点的状态
        private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
        {
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                node.Nodes[i].CheckState = check;
                SetCheckedChildNodes(node.Nodes[i], check);
            }
        }
 
 
 
 
        /// 设置父节点的状态
        private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
        {
            if (node.ParentNode != null)
            {
                bool b = false;
                CheckState state;
                for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
                {
                    state = node.ParentNode.Nodes[i].CheckState;
                    if (!check.Equals(state))
                    {
                        b = !b;
                        break;
                    }
                }
                node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
                SetCheckedParentNodes(node.ParentNode, check);
            }
        }
 
    }
}