duheng
2024-12-05 a31a34e7bf37b4ac3b75d43f2d64b90594d27374
WinFrmUI/HStation.WinFrmUI.Xhs.Core/04-pump/02-analy/SinglePumpAnalyDlg.cs
@@ -1,5 +1,6 @@
using HStation.WinFrmUI.PhartRelation;
using SqlSugar;
using Yw.WinFrmUI.Phart;
namespace HStation.WinFrmUI.Xhs
{
@@ -12,8 +13,12 @@
            InitChart();
        }
        public event Func<PumpMatchingViewModel, bool> ReloadDataEvent;
        #region Init
        private void InitChart()
        {
        {
            this.singlePumpAnalyInfoCtrl1.SetEvent += (id, hz, ex_ratio, qh, qe, qp) =>
            {
                this.pumpVariableSpeedChart1.Set(id, hz, ex_ratio, qh, qe, qp);
@@ -26,33 +31,210 @@
                ResetSectPointGrid();
            };
            this.singlePumpAnalyInfoCtrl1.SetDesignPointEvent += (q, h) =>
            {
                this.pumpVariableSpeedChart1.SetDesignPt(q,h);
            {
                this.pumpVariableSpeedChart1.SetEquipPt(new Yw.Geometry.Point2d(q, h));
                ResetSectPointGrid();
            };
            this.pumpVariableSpeedChart1.OnCalcQueryPoint += (id, pt) =>
            {
                this.singlePumpAnalyInfoCtrl1.SetQueryInfo(id, pt);
            };
            void ResetSectPointGrid()
            this.pumpVariableSpeedChart1.AddBySpeedEvent += () =>
            {
                var vm_list = this.pumpVariableSpeedChart1.GetList();
                ///this.singlePumpAnalyInfoCtrl1.SetSectPoint(vm_list);
            }
                AddBySpeed();
                ResetSectPointGrid();
            };
            this.pumpVariableSpeedChart1.AddByHzEvent += () =>
            {
                AddByHz();
                ResetSectPointGrid();
            };
            this.pumpVariableSpeedChart1.AddByPointEvent += () =>
            {
                AddByPoint();
                ResetSectPointGrid();
            };
            this.singlePumpAnalyInfoCtrl1.SaveEvent += (vm) =>
            {
                if (_vm == null)
                {
                    return;
                }
                _vm.CurrentHz = vm.CurrentHz;
                var bol = this.ReloadDataEvent?.Invoke(_vm);
                if (bol.HasValue && bol.Value)
                {
                    this.Close();
                }
            };
        }
        private void ResetSectPointGrid()
        {
            var vm_list = this.pumpVariableSpeedChart1.GetSectPointList();
            this.singlePumpAnalyInfoCtrl1.SetSectPointList(vm_list);
        }
        private void AddBySpeed()
        {
            var list = this.singlePumpAnalyInfoCtrl1.GetList();
            if (list == null || list.Count < 1)
            {
                return;
            }
            var index = list.Count;
            var vm_def = list.First();
            var dlg = new SetValueDlg();
            dlg.SetBindingData(vm_def.RatedSpeed);
            dlg.VerifyValueChanged += (speed) =>
            {
                var hz = Math.Round(speed / vm_def.RatedSpeed * 50, 1);
                if (hz > 50 || hz < 10)
                {
                    return false;
                }
                var vm = new SinglePumpAnalyViewModel(vm_def);
                vm.Id = Guid.NewGuid().ToString();
                vm.Name = $"{speed}rpm({hz}hz)";
                vm.Color = GetRandomColor(index);
                vm.CurrentHz = hz;
                vm.CurrentSpeed = speed;
                vm.IsDefault = false;
                vm.Calc();
                this.singlePumpAnalyInfoCtrl1.Add(vm);
                this.pumpVariableSpeedChart1.Add(vm.ToInfo());
                return true;
            };
            dlg.ShowDialog();
        }
        private void AddByHz()
        {
            var list = this.singlePumpAnalyInfoCtrl1.GetList();
            if (list == null || list.Count < 1)
            {
                return;
            }
            var index = list.Count;
            var vm_def = list.First();
            var dlg = new SetValueDlg();
            dlg.VerifyValueChanged += (hz) =>
            {
                if (hz > 50 || hz < 10)
                {
                    return false;
                }
                var speed = Math.Round(hz / 50 * vm_def.RatedSpeed);
                var vm = new SinglePumpAnalyViewModel(vm_def);
                vm.Id = Guid.NewGuid().ToString();
                vm.Name = $"{speed}rpm({hz}hz)";
                vm.Color = GetRandomColor(index);
                vm.CurrentHz = hz;
                vm.CurrentSpeed = speed;
                vm.IsDefault = false;
                vm.Calc();
                this.singlePumpAnalyInfoCtrl1.Add(vm);
                this.pumpVariableSpeedChart1.Add(vm.ToInfo());
                return true;
            };
            dlg.ShowDialog();
        }
        private void AddByPoint()
        {
            var list = this.singlePumpAnalyInfoCtrl1.GetList();
            if (list == null || list.Count < 1)
            {
                return;
            }
            var index = list.Count;
            var vm_def = list.First();
            var dlg = new SetPointDlg();
            dlg.SetBindingData();
            dlg.VerifyValueChanged += (x, y) =>
            {
                var pt = new Yw.Geometry.Point2d(x, y);
                var speed = PumpCalcHelper.GetSimuValue(vm_def.Qh, pt, vm_def.RatedSpeed);
                var hz = Math.Round(speed / vm_def.RatedSpeed * 50, 1);
                if (hz > 50 || hz < 20)
                {
                    return false;
                }
                var vm = new SinglePumpAnalyViewModel(vm_def);
                vm.Id = Guid.NewGuid().ToString();
                vm.Name = $"{speed}rpm({hz}hz)";
                vm.Color = GetRandomColor(index);
                vm.CurrentHz = hz;
                vm.CurrentSpeed = speed;
                vm.IsDefault = false;
                vm.Calc();
                this.singlePumpAnalyInfoCtrl1.Add(vm);
                this.pumpVariableSpeedChart1.Add(vm.ToInfo());
                return true;
            };
            dlg.ShowDialog();
        }
        private Color GetRandomColor(int count)
        {
            var color_list = new List<Color>()
            {
                Color.Red, Color.Blue, Color.Green,Color.DodgerBlue,
                Color.Fuchsia, Color.MidnightBlue,  Color.Maroon, Color.Aquamarine,
                Color.Bisque,Color.BurlyWood
            };
            if (count < color_list.Count)
            {
                return color_list[count];
            }
            var _random = new Random();
            int r = _random.Next(1, 256);
            int g = _random.Next(1, 256);
            int b = _random.Next(1, 256);
            return Color.FromArgb(r, g, b);
        }
        #endregion
        private PumpMatchingViewModel _vm = null;
        public void SetBindindData(PumpMatchingViewModel pump_matching)
        {
            InitChartData(pump_matching);
            _vm = pump_matching;
            SetChart(pump_matching);
        }
        private SinglePumpAnalyViewModel _working_vm = null;
        private async void InitChartData(PumpMatchingViewModel pump_matching)
        //设置图表数据
        private async void SetChart(PumpMatchingViewModel pump_math)
        {
            if (!long.TryParse(pump_matching.DbId, out long pump_main_id))
            var db_pump_mian_id = pump_math.DbId;
            var db_curve_id = pump_math.CurveDbId;
            if (pump_math.MatchingDbId != null)
            {
                db_pump_mian_id = pump_math.MatchingDbId;
                db_curve_id = pump_math.MatchingCurveDbId;
            }
            if (!long.TryParse(db_pump_mian_id, out long pump_main_id))
            {
                return;
            }
            if (!long.TryParse(db_curve_id, out long curve_id))
            {
                return;
            }
@@ -61,13 +243,11 @@
            {
                return;
            }
            var phart_list = await new BLL.XhsPumpMainPhartMappingExtensions().GetByPumpMainID(pump_main_id);
            if (phart_list == null || !phart_list.Any())
            var phart = await new BLL.XhsPumpMainPhartMappingExtensions().GetByID(curve_id);
            if (phart == null)
            {
                return;
            }
            var phart = phart_list.OrderBy(x => x.Importance).First();
            var diagram = phart.Diagram;
            if (diagram == null)
            {
@@ -99,10 +279,9 @@
            var qp = new Yw.Geometry.CubicSpline2d(points_qp);
            var def_vm = new SinglePumpAnalyViewModel();
            def_vm.Id = "def";
            def_vm.Name = "50";
            def_vm.Name = "额定曲线";
            def_vm.RatedSpeed = pump_main.RatedSpeed;
            def_vm.CurrentSpeed = pump_main.RatedSpeed;
            def_vm.CurrentHz = 50;
@@ -113,45 +292,54 @@
            def_vm.Qe = qe;
            def_vm.Qp = qp;
            def_vm.Calc();
            //_def_vm.QhCalc = qh;
            //_def_vm.QeCalc = qe;
            //_def_vm.QpCalc = qp;
            //_def_vm.SectQ = string.Empty;
            //_def_vm.SectH = string.Empty;
            //_def_vm.SectE = string.Empty;
            //_def_vm.SectP = string.Empty;
            var working_vm = new SinglePumpAnalyViewModel();
            working_vm.Id = "working";
            working_vm.Name = "设定曲线";
            working_vm.RatedSpeed = pump_main.RatedSpeed;
            working_vm.CurrentSpeed = Math.Round(pump_math.CurrentHz / 50 * pump_main.RatedSpeed, 1);
            working_vm.CurrentHz = pump_math.CurrentHz;
            working_vm.IsDefault = false;
            working_vm.Color = Color.Red;
            working_vm.ExtendRatio = 100;
            working_vm.Qh = qh;
            working_vm.Qe = qe;
            working_vm.Qp = qp;
            working_vm.Calc();
            //_def_vm.QueryQ = string.Empty;
            //_def_vm.QueryH = string.Empty;
            //_def_vm.QueryE = string.Empty;
            //_def_vm.QueryP = string.Empty;
            var calc_flow = pump_math.CalcuQ;
            var calc_head = pump_math.CalcuH;
            _working_vm = new SinglePumpAnalyViewModel();
            _working_vm.Id = "working";
            _working_vm.Name = pump_matching.CurrentHz.ToString();
            _working_vm.RatedSpeed = pump_main.RatedSpeed;
            _working_vm.CurrentSpeed = Math.Round(pump_matching.CurrentHz / 50 * pump_main.RatedSpeed, 1);
            _working_vm.CurrentHz = pump_matching.CurrentHz;
            _working_vm.IsDefault = false;
            _working_vm.Color = Color.Red;
            _working_vm.ExtendRatio = 100;
            _working_vm.Qh = qh;
            _working_vm.Qe = qe;
            _working_vm.Qp = qp;
            _working_vm.Calc();
            var list = new List<SinglePumpAnalyViewModel>() { def_vm, _working_vm };
            var list = new List<SinglePumpAnalyViewModel>() { def_vm, working_vm };
            var list_yw = list.Select(x => x.ToInfo()).ToList();
            this.singlePumpAnalyInfoCtrl1.SetBindingData(list);
            this.pumpVariableSpeedChart1.Add(list_yw);
            if (calc_flow.HasValue && calc_head.HasValue)
            {
                var flow = Math.Round(calc_flow.Value, 1);
                var head = Math.Round(calc_head.Value, 1);
                this.singlePumpAnalyInfoCtrl1.SetDesignPoint(flow, head);
                this.pumpVariableSpeedChart1.SetEquipPt(new Yw.Geometry.Point2d(flow, head));
                ResetSectPointGrid();
            }
        }
        private void barBtnSelPumpMian_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var dlg = new PumpMatchingDlg();
            dlg.SetBindingData(_vm);
            dlg.ReloadDataEvent += (vm) =>
            {
                if (vm == null)
                    return;
                SetBindindData(vm);
            };
            dlg.ShowDialog();
        }
    }
}