ningshuxia
2025-03-31 d321346f204a69b1929cc39098a5d88f44509e7b
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
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using MathNet.Numerics;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace IStation.WinFrmUI.Monitor
{
    public partial class MonitorDataIntegrationPage : DocumentPage
    {
        public MonitorDataIntegrationPage()
        {
            InitializeComponent();
            this.PageTitle.Caption = "数据整合";
            this.stationListCtrl1.FocusedChangedEvent += StationListCtrl1_FocusedChangedEvent;
            this.monitorDataSourcesTreeList1.FocusedChangedEvent += MonitorDataSourcesListCtrl1_FocusedChangedEvent;
            this.gridView1.SetNormalView();
            this.colTime.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.colTime.DisplayFormat.FormatString = "G";
        }
 
        public class CurrentViewModel
        {
            [Display(Name = "年月")]
            public string YearMonth { get; set; }
 
            [Display(Name = "时间")]
            public DateTime Time { get; set; }
 
            [Display(Name = "运行工况")]
            public string RunFlags { get; set; }
 
            [Display(Name = "转速列表")]
            public string RpmList { get; set; }
 
            [Display(Name = "扬程列表")]
            public string HeadList { get; set; }
 
            [Display(Name = "总流量")]
            public double TotalFlow { get; set; }
 
            [Display(Name = "总功率")]
            public double TotalPower { get; set; }
        }
 
        private BLL.StationSignalRecordPacket _bll = new BLL.StationSignalRecordPacket();
        private Model.Station _station = null;
        private Model.MonitorDataSources _monitorDataSources = null;
        private List<CurrentViewModel> _allBindingList = null;
        private Dictionary<string, Dictionary<string, Tuple<int, Dictionary<string, Dictionary<string, Tuple<double, double>>>>>> _statistical_dict = null;
 
        /// <summary>
        /// 清空数据
        /// </summary>
        public void Clear()
        {
            _allBindingList = new List<CurrentViewModel>();
            this.currentViewModelBindingSource.DataSource = _allBindingList;
        }
 
        /// <summary>
        /// 初始化数据
        /// </summary>
        public override void InitialDataSource()
        {
            this.stationListCtrl1.SetBindingData();
            this.monitorDataSourcesTreeList1.SetBindingData();
        }
 
        //泵站变换
        private void StationListCtrl1_FocusedChangedEvent(Model.Station obj)
        {
            _station = obj;
            SetBindingData(_monitorDataSources, _station);
        }
 
        //来源变换
        private void MonitorDataSourcesListCtrl1_FocusedChangedEvent(Model.MonitorDataSources obj)
        {
            _monitorDataSources = obj;
            SetBindingData(_monitorDataSources, _station);
        }
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData(Model.MonitorDataSources monitorDataSources, Model.Station station)
        {
            _allBindingList = new List<CurrentViewModel>();
            this.currentViewModelBindingSource.DataSource = _allBindingList;
            if (monitorDataSources == null || station == null)
            {
                return;
            }
            if (this.barCekLoad.Checked)
            {
                return;
            }
            var packets = _bll.Get(_monitorDataSources.ID, _station.ID);
            SetBindingData(packets);
        }
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData(List<Model.StationSignalRecordPacket> packets)
        {
            WaitFrmHelper.ShowWaitForm();
            _allBindingList = new List<CurrentViewModel>();
            _statistical_dict = new Dictionary<string, Dictionary<string, Tuple<int, Dictionary<string, Dictionary<string, Tuple<double, double>>>>>>();
            if (packets != null && packets.Any())
            {
                var enginePumps = new BLL.Equipment().GetEnginePumpListByBelongTypeAndBelongID(IStation.ObjectType.Station, _station.ID);
                foreach (var packet in packets)
                {
                    var station_signal_records = packet.StationSignalRecords;
                    if (station_signal_records == null || !station_signal_records.Any())
                        continue;
                    var year_month = $"{packet.Year}-{packet.Month}";
                    foreach (var record in station_signal_records)
                    {
                        var pump_signal_records = record.PumpSignalRecords;
                        if (pump_signal_records == null || !pump_signal_records.Any())
                            continue;
                        pump_signal_records = pump_signal_records.OrderBy(x => x.Flag).ToList();
 
                        var total_power = pump_signal_records.Sum(x => x.InstantaneousPower);
                        var run_flags = pump_signal_records.Select(x => x.Flag).ToList();
                        var rpm_list = pump_signal_records.Select(x =>
                        {
                            if (x.Rpm == 0 && (x.Flag == 15 || x.Flag == 16))
                            {
                                return 590;
                            }
                            return x.Rpm;
                        }).ToList();
                        var head_list = pump_signal_records.Select(x =>
                        {
                            return Math.Round(x.Head, 2);
                        }).ToList();
 
 
                        var vm = new CurrentViewModel();
                        vm.YearMonth = year_month;
                        vm.Time = record.Time;
                        vm.RunFlags = IStation.Untity.IntListHelper.ToString(run_flags);
                        vm.RpmList = IStation.Untity.DoubleListHelper.ToString(rpm_list);
                        vm.HeadList = IStation.Untity.DoubleListHelper.ToString(head_list);
                        vm.TotalFlow = Math.Round(record.TotalFlow, 1);
                        vm.TotalPower = Math.Round(total_power, 1);
                        _allBindingList.Add(vm);
                    }
                }
            }
            if (_allBindingList.Any())
            {
                var list = _allBindingList;
                var group_ym = list.GroupBy(x => x.YearMonth);
                var max_flow_diff = 0d;
                var vm_list = new List<CurrentViewModel>();
                foreach (var item_ym in group_ym)
                {
                    _statistical_dict.Add(item_ym.Key, new Dictionary<string, Tuple<int, Dictionary<string, Dictionary<string, Tuple<double, double>>>>>());
                    var group_run_flag = item_ym.GroupBy(x => x.RunFlags);
                    foreach (var item_run_flag in group_run_flag)
                    {
                        var count = item_run_flag.Count();
                        var group_rpm = item_run_flag.GroupBy(x => x.RpmList);
                        var dict = new Dictionary<string, Dictionary<string, Tuple<double, double>>>();
                        foreach (var item_rpm in group_rpm)
                        {
                            dict.Add(item_rpm.Key, new Dictionary<string, Tuple<double, double>>());
                            var group_head = item_rpm.GroupBy(x => x.HeadList);
                            foreach (var item_head in group_head)
                            {
                                var flow_diff = item_head.Max(x => x.TotalFlow) - item_head.Min(x => x.TotalFlow);
                                var power_diff = item_head.Max(x => x.TotalPower) - item_head.Min(x => x.TotalPower);
                                dict[item_rpm.Key][item_head.Key] = new Tuple<double, double>(flow_diff, power_diff);
                                if (item_head.Count() > 1)
                                {
                                    vm_list.AddRange(item_head);
                                }
                                if (flow_diff > max_flow_diff)
                                {
                                    max_flow_diff = flow_diff;
                                }
                            }
                        }
                        _statistical_dict[item_ym.Key].Add(item_run_flag.Key, new Tuple<int, Dictionary<string, Dictionary<string, Tuple<double, double>>>>(count, dict));
                    }
                }
 
               // this.currentViewModelBindingSource.DataSource = vm_list;
                // XtraMessageBox.Show($"{max_Info}  / {max_flow_diff}");
            }
 
 
            this.currentViewModelBindingSource.DataSource = _allBindingList; 
            WaitFrmHelper.HideWaitForm();
        }
 
        //整合
        private void barBtnIntegration_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_monitorDataSources == null || _station == null)
                return;
            var dlg = new SetTimeStepDlg();
            dlg.ReloadDataEvent += (timeStep) =>
            {
                Task.Run(() =>
                {
                    this.Invoke(new Action(() =>
                    {
                        AlertTool.ShowInfo(Application.OpenForms[0], "提示", "开始整合!");
                    }));
                    var packets = _bll.AnalysisAndSave(_monitorDataSources.ID, _station.ID, timeStep);
                    this.Invoke(new Action(() =>
                    {
                        SetBindingData(packets);
                        AlertTool.ShowInfo(Application.OpenForms[0], "提示", "整合完成!");
                    }));
 
                });
 
            };
            dlg.ShowDialog();
        }
 
        //
        private void barCekLoad_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetBindingData(_monitorDataSources, _station);
        }
 
        //自定义组名
        private void gridView1_CustomDrawGroupRow(object sender, DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
        {
            if (_statistical_dict == null && !_statistical_dict.Any())
            {
                return;
            }
            var vm = this.gridView1.GetRow(e.RowHandle) as CurrentViewModel;
            var grid = e.Info as GridGroupRowInfo;
            if (grid.Level == 0)
            {
                // var name = $"{vm.RunFlags} (时间点:{_statistical_dict[vm.YearMonth][vm.RunFlags].Item1}) ";
                var max_flow_diff = _statistical_dict[vm.YearMonth][vm.RunFlags].Item2.Max(x => x.Value.Max(y => y.Value.Item1));
                // var name = $"{vm.RunFlags} (数量:{_statistical_dict[vm.YearMonth][vm.RunFlags].Item1} 最大流量差:{max_flow_diff:N1})  ";
                var name = $"{vm.RunFlags} (流量差:{max_flow_diff:N1})  ";
                grid.GroupText = name;
            }
            else if (grid.Level == 1)
            {
                var tuple = _statistical_dict[vm.YearMonth][vm.RunFlags].Item2[vm.RpmList];
                var max_flow_diff = tuple.Max(x => x.Value.Item1);
                var name = $"{vm.RpmList} (流量差:{max_flow_diff:N1})";
                grid.GroupText = name;
            }
            else if (grid.Level == 2)
            {
                var tuple = _statistical_dict[vm.YearMonth][vm.RunFlags].Item2[vm.RpmList][vm.HeadList];
                var name = $"{vm.HeadList} (流量差:{tuple.Item1:N1},功率差:{tuple.Item2})";
                grid.GroupText = name;
            }
        }
 
        #region 菜单 
 
        //全部展开
        private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView1.ExpandAllGroups();
        }
 
        //全部折叠
        private void barBtnCollapseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView1.CollapseAllGroups();
        }
 
        //检索
        private void barCekSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.barCekSearch.Checked)
                this.gridView1.ShowFindPanel();
            else
                this.gridView1.HideFindPanel();
        }
 
        // 刷新数据
        private void barBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            InitialDataSource();
        }
 
        #endregion
 
 
 
 
    }
}