ningshuxia
2025-04-03 4917fb959e2befec07a693e72d7010c09494ec7c
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
using DevExpress.XtraEditors;
using IStation.WinFrmUI.Curve;
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
 
namespace IStation.WinFrmUI.Basic
{
    /// <summary>
    /// 
    /// </summary>
    public partial class PumpCurveTreeListCtrl : XtraUserControl
    {
        public PumpCurveTreeListCtrl()
        {
            InitializeComponent();
            treeList1.InitialDefaultSettings();
            treeList1.SelectImageList = ImageLib.Lib;
            layoutControl1.SetupLayoutControl();
            stationTreeListLookUpEdit1.SelectedChangedEvent += StationTreeListLookUpEdit1_SelectedChangedEvent; ;
        }
 
 
        public class CurrentViewModel
        {
            public CurrentViewModel() { }
            public CurrentViewModel(Model.Equipment equipment)
            {
                ID = equipment.ID.ToString();
                Name = equipment.Name;
                ParentID = equipment.BelongID.ToString();
                ObjectType = IStation.ObjectType.Equipment;
                Model = equipment;
                IsGroup = true;
                ImageIndex = ImageLib.Pump;
            }
 
            public CurrentViewModel(Model.PumpCurveMapping mapping)
            {
                ID = $"{mapping.CurveID}-{mapping.ID}";
                Name = mapping.OtherName;
                ParentID = mapping.PumpID.ToString();
                ObjectType = IStation.ObjectType.PumpCurveMapping;
                Model = mapping;
                IsGroup = false;
                IsWorkCurve = mapping.IsWorking;
                Reset(IsWorkCurve);
            }
 
            public CurrentViewModel(long mappingId, Model.PumpSpeedCurve curve)
            {
                ID = curve.ID.ToString();
                Name = curve.HZ + "hz";
                ParentID = $"{curve.PumpCurveID}-{mappingId}";
                ObjectType = IStation.ObjectType.PumpSpeedCurve;
                Model = curve;
                IsGroup = false;
                ImageIndex = ImageLib.PumpSpeedCurve;
            }
 
            public string ID { get; set; }
            public string ParentID { get; set; }
            public string Name { get; set; }
            public string ObjectType { get; set; }
            public object Model { get; set; }
            public bool IsGroup { get; set; }
            public int ImageIndex { get; set; }
            public bool IsWorkCurve { get; set; }
 
            public void Reset(bool isWorking)
            {
                if (ObjectType == IStation.ObjectType.PumpCurveMapping)
                {
                    IsWorkCurve = isWorking;
                    ImageIndex = IsWorkCurve ? ImageLib.WorkPumpCurve : ImageLib.PumpCurve;
                }
            }
        }
 
 
        /// <summary>
        /// 泵站改变事件
        /// </summary> 
        public event Action<Model.Station> StationChangedEvent;
 
        /// <summary>
        /// 聚焦改变事件
        /// </summary> 
        public event Action<string, object> FocusedChangedEvent;
 
        private BLL.Equipment _bll = new BLL.Equipment();
        private BindingList<CurrentViewModel> _allBindingList = null;//所有绑定列表
 
        private Model.Station _station = null;
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData()
        {
            stationTreeListLookUpEdit1.SetBindingData();
        }
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void Clear()
        {
            _allBindingList = new BindingList<CurrentViewModel>();
            treeList1.DataSource = _allBindingList;
            FocusedChangedEvent?.Invoke(string.Empty, null);
        }
 
        //泵站变换
        private void StationTreeListLookUpEdit1_SelectedChangedEvent(Model.Station obj)
        {
            _station = obj;
            StationChangedEvent?.Invoke(obj);
            SetBindingData(obj);
        }
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void SetBindingData(Model.Station model)
        {
            WaitFrmHelper.ShowWaitForm("正在加载数据...");
            _allBindingList = new BindingList<CurrentViewModel>();
            if (model != null)
            {
                var pumps = _bll.GetPumpListByBelongTypeAndBelongID(IStation.ObjectType.Station, model.ID);
                if (pumps != null && pumps.Any())
                {
                    var bllMapping = new BLL.PumpCurveMapping();
                    var bllSpeedCurve = new BLL.PumpSpeedCurve();
                    foreach (var pump in pumps)
                    {
                        _allBindingList.Add(new CurrentViewModel(pump));
 
                        var mappings = bllMapping.GetByPumpID(pump.ID);
                        if (mappings == null || mappings.Count < 1)
                            continue;
                        foreach (var mapping in mappings)
                        {
                            _allBindingList.Add(new CurrentViewModel(mapping));
                            var speedCurves = bllSpeedCurve.GetByPumpCurveID(mapping.CurveID);
                            if (speedCurves == null || speedCurves.Count < 1)
                                continue;
                            foreach (var speedCurve in speedCurves)
                            {
                                _allBindingList.Add(new CurrentViewModel(mapping.ID, speedCurve));
                            }
                        }
                    }
                }
            }
 
 
            treeList1.DataSource = _allBindingList;
            treeList1.RefreshDataSource();
 
            var firstNode = _allBindingList.Where(x => !x.IsGroup).FirstOrDefault();
            if (firstNode != null)
            {
                treeList1.FocusedNode = treeList1.FindNodeByKeyID(firstNode.ID);
            }
            WaitFrmHelper.HideWaitForm();
        }
 
        /// <summary>
        /// 修改曲线映射别名
        /// </summary> 
        public void UpdateOtherName(string name)
        {
            var row = treeList1.GetCurrentViewModel(_allBindingList);
            if (row == null)
                return;
            row.Name = name;
            treeList1.RefreshNode(treeList1.FocusedNode);
        }
 
        /// <summary>
        /// 修改工作曲线
        /// </summary> 
        public void UpdateWorkingCurve()
        {
            var row = treeList1.GetCurrentViewModel(_allBindingList);
            if (row == null)
                return;
            var existWorkPumpCurve = _allBindingList.Where(x => x.ParentID == row.ParentID && x.ID != row.ID && x.IsWorkCurve).FirstOrDefault();
            if (existWorkPumpCurve != null)
            {
                existWorkPumpCurve.Reset(false);
            }
            row.Reset(true);
            treeList1.RefreshDataSource();
        }
 
        /// <summary>
        /// 删除
        /// </summary> 
        public void DeletePumpMapping(long id, long mappingId)
        {
            var node = treeList1.FindNodeByKeyID($"{id}-{mappingId}");
            if (node == null) return;
            if (treeList1.GetDataRecordByNode(node) is CurrentViewModel vm)
            {
                _allBindingList.Remove(vm);
                var children = _allBindingList.Where(x => x.ParentID == vm.ID).ToList();
                if (children != null && children.Any())
                {
                    foreach (var item in children)
                    {
                        _allBindingList.Remove(item);
                    }
                }
                treeList1.RefreshDataSource();
            }
        }
 
        private CurrentViewModel _rightClickVm = null;
 
        #region TreeList
 
        //聚焦前
        private void treeList1_BeforeFocusNode(object sender, DevExpress.XtraTreeList.BeforeFocusNodeEventArgs e)
        {
            var vm = treeList1.GetDataRecordByNode(e.Node) as CurrentViewModel;
            if (vm == null)
                return;
            if (vm.IsGroup)
            {
                e.CanFocus = false;
            }
        }
 
        //聚焦改变
        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            var vm = treeList1.GetCurrentViewModel(_allBindingList);
            if (vm == null || vm.IsGroup)
                return;
            FocusedChangedEvent?.Invoke(vm.ObjectType, vm.Model);
        }
 
        //鼠标点击
        private void treeList1_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                var hitInfo = treeList1.CalcHitInfo(e.Location);
                _rightClickVm = treeList1.GetDataRecordByNode(hitInfo.Node) as CurrentViewModel;
                if (_rightClickVm == null)
                    return;
                if (_rightClickVm.IsGroup)
                {
                    popMenu.ShowPopup(MousePosition);
                }
                else if (_rightClickVm.ObjectType == IStation.ObjectType.PumpCurveMapping)
                {
                    var mapping = _rightClickVm.Model as Model.PumpCurveMapping;
                    var exist = new BLL.PumpSpeedCurve().IsExistByPumpCurveID(mapping.CurveID);
                    if (!exist)
                        return;
                    popCurve.ShowPopup(MousePosition);
                }
            }
        }
        #endregion
 
        #region 右键菜单
 
        //泵详细信息
        private void barBtnPumpDetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var vm = _rightClickVm;
            if (vm == null || !vm.IsGroup)
                return;
            var pump = vm.Model as Model.Equipment<Model.Pump>;
            var dlg = new PumpInfoDlg();
            dlg.SetBindingData(pump);
            dlg.ShowDialog();
        }
 
        //表格导入
        private void barBtnImportByExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var vm = _rightClickVm;
            if (vm == null || !vm.IsGroup)
                return;
            var pump = vm.Model as Model.Equipment;
            var dlg = new ImportCurveByExcelDlg();
            dlg.ReloadDataEvent += (curveCode, eCurveSourceFrom, featCurveExpressGroup) =>
            {
                var pumpCurveExMapping = new Model.PumpCurveExMapping();
                pumpCurveExMapping.CurveCode = curveCode;
                pumpCurveExMapping.SourceFrom = eCurveSourceFrom;
                pumpCurveExMapping.CreateMethod = Model.eCurveCreateMethod.Excel导入;
                pumpCurveExMapping.CurveInfo = featCurveExpressGroup;
                pumpCurveExMapping.ReliabilityStatus = Model.PumpCurve.eReliabilityStatus.Normal;
                pumpCurveExMapping.CreateTime = DateTime.Now;
 
                pumpCurveExMapping.PumpID = pump.ID;
                pumpCurveExMapping.OtherName = curveCode;
                pumpCurveExMapping.IsWorking = false;
                var mappingId = new BLL.PumpCurve().InsertEx(pumpCurveExMapping);
                if (mappingId < 1)
                {
                    return false;
                }
                var mapping = new BLL.PumpCurveMapping().GetByID(mappingId);
 
                _allBindingList.Add(new CurrentViewModel(mapping));
                treeList1.FocusedNode = treeList1.FocusedNode;
                return true;
            };
            dlg.ShowDialog();
        }
 
        //图片导入
        private void barBtnImportByImage_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var vm = _rightClickVm;
            if (vm == null || !vm.IsGroup)
                return;
            var pump = vm.Model as Model.Equipment;
            var dlg = new ImportCurveByPictureDlg();
            dlg.ReloadDataEvent += (curveCode, eCurveSourceFrom, featCurveExpressGroup) =>
            {
                var pumpCurveExMapping = new Model.PumpCurveExMapping();
                pumpCurveExMapping.CurveCode = curveCode;
                pumpCurveExMapping.SourceFrom = eCurveSourceFrom;
                pumpCurveExMapping.CreateMethod = Model.eCurveCreateMethod.图片拟合;
                pumpCurveExMapping.CurveInfo = featCurveExpressGroup;
                pumpCurveExMapping.ReliabilityStatus = Model.PumpCurve.eReliabilityStatus.Normal;
                pumpCurveExMapping.CreateTime = DateTime.Now;
 
                pumpCurveExMapping.PumpID = pump.ID;
                pumpCurveExMapping.OtherName = curveCode;
                pumpCurveExMapping.IsWorking = false;
                var mappingId = new BLL.PumpCurve().InsertEx(pumpCurveExMapping);
                if (mappingId < 1)
                {
                    return false;
                }
                var mapping = new BLL.PumpCurveMapping().GetByID(mappingId);
 
                _allBindingList.Add(new CurrentViewModel(mapping));
                treeList1.FocusedNode = treeList1.FocusedNode;
                return true;
            };
            dlg.ShowDialog();
        }
 
        //单点导入
        private void barBtnImportByOnePoint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var vm = _rightClickVm;
            if (vm == null || !vm.IsGroup)
                return;
            var pump = vm.Model as Model.Equipment;
            var dlg = new ImportCurveByOnePointDlg();
            dlg.ReloadDataEvent += (curveCode, eCurveSourceFrom, featCurveExpressGroup) =>
            {
                var pumpCurveExMapping = new Model.PumpCurveExMapping();
                pumpCurveExMapping.CurveCode = curveCode;
                pumpCurveExMapping.SourceFrom = eCurveSourceFrom;
                pumpCurveExMapping.CreateMethod = Model.eCurveCreateMethod.单点生成;
                pumpCurveExMapping.CurveInfo = featCurveExpressGroup;
                pumpCurveExMapping.ReliabilityStatus = Model.PumpCurve.eReliabilityStatus.Normal;
                pumpCurveExMapping.CreateTime = DateTime.Now;
 
                pumpCurveExMapping.PumpID = pump.ID;
                pumpCurveExMapping.OtherName = curveCode;
                pumpCurveExMapping.IsWorking = false;
                var mappingId = new BLL.PumpCurve().InsertEx(pumpCurveExMapping);
                if (mappingId < 1)
                {
                    return false;
                }
                var mapping = new BLL.PumpCurveMapping().GetByID(mappingId);
 
                _allBindingList.Add(new CurrentViewModel(mapping));
                treeList1.FocusedNode = treeList1.FocusedNode;
                return true;
            };
            dlg.ShowDialog();
        }
 
        //公式导入
        private void barBtnImportByExpress_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var vm = _rightClickVm;
            if (vm == null || !vm.IsGroup)
                return;
            var pump = vm.Model as Model.Equipment;
            var dlg = new ImportCurveByCurveExpressDlg();
            dlg.ReloadDataEvent += (curveCode, eCurveSourceFrom, featCurveExpressGroup) =>
            {
                var pumpCurveExMapping = new Model.PumpCurveExMapping();
                pumpCurveExMapping.CurveCode = curveCode;
                pumpCurveExMapping.SourceFrom = eCurveSourceFrom;
                pumpCurveExMapping.CreateMethod = Model.eCurveCreateMethod.曲线表达式;
                pumpCurveExMapping.CurveInfo = featCurveExpressGroup;
                pumpCurveExMapping.ReliabilityStatus = Model.PumpCurve.eReliabilityStatus.Normal;
                pumpCurveExMapping.CreateTime = DateTime.Now;
 
                pumpCurveExMapping.PumpID = pump.ID;
                pumpCurveExMapping.OtherName = curveCode;
                pumpCurveExMapping.IsWorking = false;
                var mappingId = new BLL.PumpCurve().InsertEx(pumpCurveExMapping);
                if (mappingId < 1)
                {
                    return false;
                }
                var mapping = new BLL.PumpCurveMapping().GetByID(mappingId);
 
                _allBindingList.Add(new CurrentViewModel(mapping));
                treeList1.FocusedNode = treeList1.FocusedNode;
                return true;
            };
            dlg.ShowDialog();
        }
 
        //查看
        private void barBtnMultiCurveView_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var mapping = _rightClickVm.Model as Model.PumpCurveMapping;
            var model = new BLL.PumpCurve().GetExMappingByID(mapping.PumpID, mapping.CurveID);
            if (model == null)
                return;
            var dlg = new MultiCurveDlg();
            dlg.SetBindingData(model);
            dlg.ShowDialog();
        }
 
        #endregion
 
        #region 菜单事件
 
        //全部展开
        private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            treeList1.ExpandAll();
        }
 
        //全部折叠 
        private void barBtnCollapseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            treeList1.CollapseAll();
        }
 
        //检索
        private void barCkSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (barCkSearch.Checked)
                treeList1.ShowFindPanel();
            else
                treeList1.HideFindPanel();
        }
 
 
        #endregion
 
 
        private void barExportInp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_station == null)
                return;
            var path = Path.Combine(SettingsD.File.RootDirectory, "陈行", _station.Name + "_inp");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var pumps = new BLL.Equipment().GetPumpListByBelongTypeAndBelongID(IStation.ObjectType.Station, _station.ID);
            foreach (var pump in pumps)
            {
                var curve = new BLL.PumpCurve().GetDefaultWorkingByPumpID(pump.ID);
                if (curve == null)
                    continue;
                var fitPointsQH = curve.CurveInfo.CurveQH.GetFitPoints(100);
                var fitPointsQE = curve.CurveInfo.CurveQE.GetFitPoints(100);
 
                var curveQH = curve.CurveInfo.CurveQH;
                var curveQE = curve.CurveInfo.CurveQE;
                var filePath = path + "\\" + pump.Name + ".crv";
                // ExportInpHelper.INPDefaultFile(filePath, pump.Name + "-" + curve.SourceFrom.GetDisplayText(), curveQH, curveQE);
                ExportCurveHelper.INPFile(filePath, curveQH, curveQE, 100);
            }
 
            XtraMessageBox.Show("over");
        }
    }
}