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
using DevExpress.XtraEditors;
using IStation.Untity;
using System.Collections.Generic;
using System.Linq;
 
namespace IStation.WinFrmUI.Basic
{
    public partial class EquipmentMonitorMappingMgrPage : DocumentPage
    {
        public EquipmentMonitorMappingMgrPage()
        {
            InitializeComponent();
            this.PageTitle.Caption = "测点关联";
            this.treeList1.InitialMultiColMultiSelectSettings();
            this.treeList1.SelectImageList = ImageLib.Lib;
 
            this.equipmentExTreeListCtrl1.StationChangedEvent += EquipmentExTreeListCtrl1_StationChangedEvent;
            this.equipmentExTreeListCtrl1.FocusedChangedEvent += EquipmentExTreeListCtrl1_FocusedChangedEvent;
        }
 
 
 
        public class CurrentViewModel
        {
            public CurrentViewModel() { }
 
            public CurrentViewModel(Model.MonitorPointGroup rhs)
            {
                this.ID = rhs.ID;
                this.ParentIds = rhs.ParentIds;
                this.ParentID = TreeParentIdsHelper.GetLastParentID(rhs.ParentIds);
                this.Name = rhs.Name;
                this.SortCode = rhs.SortCode;
                this.Description = rhs.Description;
                this.ImageIndex = ImageLib.Group;
                this.Model = rhs;
                this.IsGroup = true;
                if (this.ParentIds == null)
                {
                    this.ParentIds = new List<long>();
                }
            }
 
            public CurrentViewModel(Model.MonitorPoint rhs)
            {
                this.ID = rhs.ID;
                this.ParentID = rhs.GroupID;
                this.Name = rhs.Name;
                this.SortCode = rhs.SortCode;
                this.Description = rhs.Description;
                this.ImageIndex = ImageLib.MonitorPoint;
                this.Model = rhs;
                this.IsGroup = false;
                this.ParentIds = new List<long>();
            }
 
            public long ID { get; set; }
            public List<long> ParentIds { get; set; }
            public long ParentID { get; set; }
            public string Name { get; set; }
            public int SortCode { get; set; }
            public string Description { get; set; }
            public int ImageIndex { get; set; }
            public object Model { get; set; }
            public bool IsGroup { get; set; }
            public bool? Checked { get; set; } = false;
            public long MappingID { get; set; }
        }
 
        private BLL.EquipmentMonitorMapping _bll = new BLL.EquipmentMonitorMapping();
        private List<CurrentViewModel> _allBindingList = null;
        private Model.Equipment _model = null;
 
        /// <summary>
        /// 初始化数据
        /// </summary>
        public override void InitialDataSource()
        {
            this.equipmentExTreeListCtrl1.SetBindingData();
        }
 
 
        //泵站变换
        private void EquipmentExTreeListCtrl1_StationChangedEvent(Model.Station model)
        {
            _allBindingList = new List<CurrentViewModel>();
            if (model != null)
            {
                if (model != null)
                {
                    var groups = new BLL.MonitorPointGroup().GetByBelongTypeAndBelongID(IStation.ObjectType.Station, model.ID);
                    if (groups != null && groups.Any())
                    {
                        foreach (var item in groups)
                        {
                            var vm = new CurrentViewModel(item);
                            _allBindingList.Add(vm);
                        }
                    }
 
                    var monitorPoints = new BLL.MonitorPoint().GetByBelongTypeAndBelongID(IStation.ObjectType.Station, model.ID);
                    if (monitorPoints != null && monitorPoints.Any())
                    {
                        foreach (var item in monitorPoints)
                        {
                            var vm = new CurrentViewModel(item);
                            _allBindingList.Add(vm);
                        }
                    }
                }
            }
            this.currentViewModelBindingSource.DataSource = _allBindingList;
            this.currentViewModelBindingSource.ResetBindings(false);
        }
 
 
        //设备变换
        private void EquipmentExTreeListCtrl1_FocusedChangedEvent(Model.Equipment obj)
        {
            _model = obj;
            var mappings = _bll.GetByEquipmentID(obj.ID);
            if (_allBindingList != null && _allBindingList.Any())
            {
                _allBindingList.ForEach(x => x.Checked = false);
                if (mappings != null && mappings.Any())
                {
                    var ids = mappings.ToDictionary(x => x.MonitorPointID, x => x.ID);
                    var groups = _allBindingList.Where(x => x.IsGroup).OrderByDescending(x => x.ParentIds.Count).ToList();
                    foreach (var group in groups)
                    {
                        var children = _allBindingList.Where(x => x.ParentID == group.ID || x.ParentIds.Contains(group.ID));
                        if (children == null || !children.Any())
                        {
                            group.Checked = false;
                            continue;
                        }
 
                        var children_monitorPoints = children.Where(x => !x.IsGroup).ToList();
                        int children_monitorPoints_cekNum = 0;
                        children_monitorPoints?.ForEach(x =>
                        {
                            if (ids.Keys.Contains(x.ID))
                            {
                                x.Checked = true;
                                x.MappingID = ids[x.ID];
                                children_monitorPoints_cekNum++;
                            }
                        });
                        bool? cekBol = null;
                        if (children_monitorPoints.Count == children_monitorPoints_cekNum)
                        {
                            cekBol = true;
                        }
                        else if (children_monitorPoints_cekNum == 0)
                        {
                            cekBol = false;
                        }
 
                        if (cekBol != null)
                        {
                            var children_groups = children.Where(x => x.IsGroup).ToList();
                            if (children_groups != null && children_groups.Any())
                            {
                                bool? children_groups_cekBol = false;
                                var children_groups_cekNum = children_groups.Count(x => x.Checked.HasValue && x.Checked.Value);
                                if (children_groups_cekNum == 0)
                                {
                                    children_groups_cekBol = false;
                                }
                                else if (children_groups_cekNum == children_groups.Count)
                                {
                                    children_groups_cekBol = true;
                                }
                                if (children_groups_cekBol == null || cekBol.Value != children_groups_cekBol.Value)
                                {
                                    cekBol = null;
                                }
                            }
                        }
                        group.Checked = cekBol;
                    }
                }
            }
            this.currentViewModelBindingSource.ResetBindings(false);
        }
 
 
        //全部展开
        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 barCekSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.barCekSearch.Checked)
                this.treeList1.ShowFindPanel();
            else
                this.treeList1.HideFindPanel();
        }
 
        // 刷新数据
        private void barBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            InitialDataSource();
        }
 
        // 保存
        private void barBtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_model == null)
                return;
            this.treeList1.CloseEditor();
            var mappings = _allBindingList?.Where(x => !x.IsGroup && x.Checked.HasValue && x.Checked.Value)
                .Select(x => new Model.EquipmentMonitorMapping()
                {
                    //ID = x.MappingID,
                    EquipmentID = _model.ID,
                    MonitorPointID = x.ID
                }).ToList();
 
            var bol = _bll.SetOfMapping(_model.ID, mappings);
            if (!bol)
            {
                XtraMessageBox.Show("保存失败!");
                return;
            }
            XtraMessageBox.Show("保存成功!");
        }
 
 
 
    }
}