Shuxia Ning
2024-07-17 fd681339c81201ed6fb3303647ecab89e3e6c0c1
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
using DevExpress.XtraEditors;
using NPOI.HSSF.UserModel;
using System.IO;
using Yw;
 
namespace IStation.Win
{
    public partial class ScheduleValidView : XtraUserControl
    {
        public ScheduleValidView()
        {
            InitializeComponent();
            this.bandedGridView1.BandPanelRowHeight = 35;
            this.bandedGridView1.RegistCustomDrawRowIndicator();
            this.bandedGridView1.SetNormalView();
 
            this.cmbSelectStation.SelectedIndex = _sel_index - 1;
        }
 
 
        private List<ScheduleValidViewModel> _schedule_valid_vm_list = null;
        private int _sel_index = 2;
 
        //导入文件
        private void barBtnImport_Click(object sender, EventArgs e)
        {
            var dlg = new OpenFileDialog();
            dlg.Filter = "EXCEL 文件(*.xls)|*.xls";
            if (dlg.ShowDialog() != DialogResult.OK)
                return;
 
            var fileName = dlg.FileName;
            int line = 0;
            try
            {
                //初始化文件 
                HSSFWorkbook theBook = null;
                using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    theBook = new HSSFWorkbook(file);
                }
 
                //检查表格是否符合
                NPOI.SS.UserModel.ISheet sheet1 = theBook.GetSheet("Sheet1");
                if (sheet1 == null)
                {
                    sheet1 = theBook.GetSheetAt(0);
                    if (sheet1 == null)
                    {
                        MessageBox.Show("请将数据放在Sheet1中");
                        return;
                    }
                }
 
                //标题行
                int title_line_index = 0;
                //时间
                int col_index_time = 0;
                //总流量
                int col_index_flow = 1;
                //总扬程
                int col_index_head = 2;
                //总功率
                int col_index_power = 3;
                //运行标志
                int col_index_flag_rpm_dict = 4;
                //运行标志
                int col_index_flag_flow_dict = 5;
                //运行标志
                int col_index_flag_head_dict = 6;
                //运行标志
                int col_index_flag_power_dict = 7;
 
 
                var row_title = sheet1.GetRow(title_line_index);
                if (row_title == null)
                {
                    MessageBox.Show("第一行第一列不能空,");
                    return;
                }
 
                //开始读取的行  
                int start_line = title_line_index + 1;
                var cell_0 = row_title.GetCell(0);
                if (cell_0 == null)
                {
                    MessageBox.Show("无法读取表头文件");
                    return;
                }
 
                _schedule_valid_vm_list = new List<ScheduleValidViewModel>();
 
                DateTime time;
                Dictionary<int, double> flag_rpm_dict, flag_flow_dict, flag_head_dict, flag_power_dict;
                double flow, head, power;
                NPOI.SS.UserModel.IRow rowtemp = null;
                NPOI.SS.UserModel.ICell cell;
 
                for (line = start_line; line < 100; line++)
                {
                    flow = head = power = -1;
                    flag_rpm_dict = new Dictionary<int, double>();
                    flag_flow_dict = new Dictionary<int, double>();
                    flag_head_dict = new Dictionary<int, double>();
                    flag_power_dict = new Dictionary<int, double>();
                    rowtemp = sheet1.GetRow(line);
                    if (rowtemp == null)
                        break;
 
                    cell = rowtemp.GetCell(col_index_time);
                    if (cell == null)
                        break;
                    if (!DateTime.TryParse(cell.StringCellValue, out time))
                    {
                        continue;
                    }
 
                    cell = rowtemp.GetCell(col_index_flow);
                    if (cell == null)
                        break;
                    if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                        flow = cell.NumericCellValue;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        flow = Convert.ToDouble(cell.StringCellValue);
                    else
                        continue;
 
                    if (flow < 0)
                        break;
 
                    cell = rowtemp.GetCell(col_index_head);
                    if (cell == null)
                        break;
                    if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                        head = cell.NumericCellValue;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        head = Convert.ToDouble(cell.StringCellValue);
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.Blank)
                        break;
                    if (head < 0)
                        break;
 
 
                    cell = rowtemp.GetCell(col_index_power);
                    if (cell == null)
                        break;
                    if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                        power = cell.NumericCellValue;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        power = Convert.ToDouble(cell.StringCellValue);
                    else
                        power = -1;
 
 
                    cell = rowtemp.GetCell(col_index_flag_rpm_dict);
                    if (cell == null)
                        break;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        flag_rpm_dict = JsonHelper.Json2Object<Dictionary<int, double>>(cell.StringCellValue);
 
                    cell = rowtemp.GetCell(col_index_flag_flow_dict);
                    if (cell == null)
                        break;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        flag_flow_dict = JsonHelper.Json2Object<Dictionary<int, double>>(cell.StringCellValue);
 
                    cell = rowtemp.GetCell(col_index_flag_head_dict);
                    if (cell == null)
                        break;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        flag_head_dict = JsonHelper.Json2Object<Dictionary<int, double>>(cell.StringCellValue);
 
                    cell = rowtemp.GetCell(col_index_flag_power_dict);
                    if (cell == null)
                        break;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        flag_power_dict = JsonHelper.Json2Object<Dictionary<int, double>>(cell.StringCellValue);
 
 
                    var vm = new ScheduleValidViewModel();
                    vm.Time = time;
                    vm.TotalFlowSrc = flow;
                    vm.TotalHeadSrc = head;
                    vm.TotalPowerSrc = power;
                    vm.FlagRpmDict = flag_rpm_dict;
                    if (flag_rpm_dict != null && flag_rpm_dict.Any())
                    {
                        var flags = flag_rpm_dict.Select(x => x.Key).ToList();
                        vm.RunFlagsSrc = Yw.Untity.IntListHelper.ToString(flags);
                    }
                    vm.FlagFlowDict = flag_flow_dict;
                    vm.FlagHeadDict = flag_head_dict;
                    vm.FlagPowerDict = flag_power_dict;
 
                    _schedule_valid_vm_list.Add(vm);
                }
 
                this.scheduleValidViewModelBindingSource.DataSource = _schedule_valid_vm_list;
                this.scheduleValidViewModelBindingSource.ResetBindings(false);
                this.gridControl1.RefreshDataSource();
                this.bandedGridView1.BestFitColumns();
            }
            catch (System.Exception err)
            {
                MessageBox.Show(string.Format("读取Excel出错!错误原因:{0},行号: {1}", err.Message, line), "提示信息",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 
        //调度
        private void barBtnSchedule_Click(object sender, EventArgs e)
        {
            if (_schedule_valid_vm_list == null || !_schedule_valid_vm_list.Any())
            {
                XtraMessageBox.Show("无数据!");
                return;
            }
 
            try
            {
                //WaitHelper.ShowWaitForm();
                //var station = new Service.Station().Get();
                //List<Model.Pump> pumps; 
                //if (_sel_index == 1)
                //{
                //    pumps = station.Station1; 
                //}
                //else
                //{
                //    pumps = station.Station2; 
                //}
 
                //var current_open_pump_list = new List<int>();
                //var must_open_pump_list = new List<int>();
                //var must_not_open_pump_list = new List<int>();
                //var schedule_helper = new Algorithm.ScheduleHelper();
 
                //foreach (var vm in _schedule_valid_vm_list)
                //{
                //    if (vm.FlagRpmDict == null || !vm.FlagRpmDict.Any())
                //        continue;
                //    current_open_pump_list = vm.FlagRpmDict.Select(x => x.Key).ToList();
                //    if (current_open_pump_list == null || current_open_pump_list.Count < 1)
                //        continue;
                //    var target_flow = vm.TotalFlowSrc;
                //    var target_head = vm.TotalHeadSrc;
 
                //    var opt_schedule = schedule_helper.GetOptAnaCombine(pumps,  target_flow, target_head);
                //    if (opt_schedule != null)
                //    {
                //        vm.TotalFlowSchedule = opt_schedule.Flow;
                //        vm.TotalHeadSchedule = opt_schedule.Head;
                //        vm.TotalPowerSchedule = opt_schedule.Power;
                //        vm.RunFlagsSchedule = opt_schedule.Remark;
 
                //        vm.TotalFlowScheduleSrcDiff = opt_schedule.Flow - target_flow;
                //        vm.TotalPowerScheduleSrcDiff = opt_schedule.Power - vm.TotalPowerSrc;
                //    }
 
                //    var opt_src = schedule_helper.GetOptAnaCombineByWorking(pumps, vm.FlagRpmDict, vm.FlagHeadDict, target_flow, target_head);
                //    if (opt_src != null)
                //    {
                //        vm.TotalFlowCalc = opt_src.Flow;
                //        vm.TotalPowerCalc = opt_src.Power;
 
                //        vm.TotalFlowCalcSrcDiff = opt_src.Flow - target_flow;
                //        vm.TotalPowerCalcSrcDiff = opt_src.Power - vm.TotalPowerSrc;
                //    }
                //    vm.Round();
                //}
 
                this.scheduleValidViewModelBindingSource.DataSource = _schedule_valid_vm_list;
                this.scheduleValidViewModelBindingSource.ResetBindings(false);
                this.gridControl1.RefreshDataSource();
                this.bandedGridView1.BestFitColumns();
            }
            //catch (Exception ex)
            //{
            //    XtraMessageBox.Show(ex.Message);
            //}
            finally
            {
                WaitHelper.HideWaitForm();
            }
 
        }
 
        //选中泵站变换
        private void cmbSelectStation_SelectedIndexChanged(object sender, EventArgs e)
        {
            _sel_index = this.cmbSelectStation.SelectedIndex + 1;
        }
 
 
    }
}