lixiaojun
2024-12-23 b9c79f595e5ad4684d731f968bf120ff8c52dbd8
WinFrmUI/HStation.WinFrmUI.Xhs.Core/03-simulation/05-function/SimulationFunctionCtrl.cs
@@ -1,5 +1,9 @@
using DevExpress.Utils.Extensions;
using DevExpress.CodeParser;
using DevExpress.DataAccess.Native;
using DevExpress.Utils.Extensions;
using DevExpress.Utils.Svg;
using DevExpress.XtraCharts;
using System.Collections.Concurrent;
using Yw.DAL.Basic;
using Yw.Vmo;
@@ -30,6 +34,10 @@
        /// </summary>
        public event Action<XhsProjectVmo, XhsProjectSiteVmo, XhsSchemeVmo, HydroWorkingVmo, bool> ProjectSiteWorkingCheckedEvent;
        /// <summary>
        /// 项目站工况增加事件
        /// </summary>
        public event Action<XhsProjectVmo, XhsProjectSiteVmo, XhsSchemeVmo, HydroWorkingVmo> ProjectSiteWorkingAppendEvent;
        /// <summary>
        /// 项目站工况更新事件
        /// </summary>
        public event Action<XhsProjectVmo, XhsProjectSiteVmo, XhsSchemeVmo, HydroWorkingVmo> ProjectSiteWorkingUpdateEvent;
@@ -41,6 +49,18 @@
        /// 创建项目站方案事件
        /// </summary>
        public event Action<XhsProjectVmo, XhsProjectSiteVmo, Yw.Model.HydroModelInfo, SvgImage> CreateProjectSiteSchemeEvent;
        /// <summary>
        /// 项目站方案增加事件
        /// </summary>
        public event Action<XhsProjectVmo, XhsProjectSiteVmo, XhsSchemeVmo> ProjectSiteSchemeAppendEvent;
        /// <summary>
        /// 项目站方案更新事件
        /// </summary>
        public event Action<XhsProjectVmo, XhsProjectSiteVmo, XhsSchemeVmo> ProjectSiteSchemeUpdateEvent;
        /// <summary>
        /// 项目站方案移除事件
        /// </summary>
        public event Action<XhsProjectVmo, XhsProjectSiteVmo, XhsSchemeVmo> ProjectSiteSchemeRemoveEvent;
        private XhsProjectVmo _project = null;//项目
        private XhsProjectSiteVmo _projectSite = null;//项目站
@@ -48,6 +68,7 @@
        private Dictionary<HydroWorkingVmo, bool> _allWorkingCheckedListDict = null;//所有工况选择列表字典
        private Dictionary<XhsSchemeVmo, Yw.Model.HydroModelInfo> _allSchemeHydroInfoDict = null;//所有方案水力信息列表
        private Dictionary<XhsSchemeVmo, Dictionary<HydroWorkingVmo, bool>> _allSchemeWorkingCheckedListDict = null;//所有方案工况选择列表字典
        private ConcurrentDictionary<string, bool> _gettingHydroInfoDict = new ConcurrentDictionary<string, bool>();//获取水力信息字典
        /// <summary>
        /// 绑定数据
@@ -87,7 +108,78 @@
                    });
                }
            });
            await ShowInfo();
            overlay.Close();
        }
        //项目站概况
        private async void elementProjectSiteInfo_Click(object sender, EventArgs e)
        {
            await ShowInfo();
        }
        //项目站水力模拟
        private async void elementProjectSiteSimulation_Click(object sender, EventArgs e)
        {
            if (_project == null)
            {
                return;
            }
            if (_projectSite == null)
            {
                return;
            }
            var hydroInfo = await GetHydroInfo(null, true);
            if (hydroInfo == null)
            {
                return;
            }
            this.ShowProjectSiteSimulationEvent?.Invoke(_project, _projectSite, null, hydroInfo, _allWorkingCheckedListDict, this.svgImg32[1]);
        }
        //创建项目站方案
        private async void accordionControl1_ContextButtonClick(object sender, DevExpress.Utils.ContextItemClickEventArgs e)
        {
            if (_project == null)
            {
                return;
            }
            if (_projectSite == null)
            {
                return;
            }
            var element = e.DataItem as DevExpress.XtraBars.Navigation.AccordionControlElement;
            if (element != this.elementProjectSiteSchemeList)
            {
                return;
            }
            var hydroInfo = await GetHydroInfo(null, true);
            if (hydroInfo == null)
            {
                return;
            }
            this.CreateProjectSiteSchemeEvent?.Invoke(_project, _projectSite, hydroInfo, this.svgImg32[3]);
        }
        //右键弹出
        private void accordionControl1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.popupMenu1.ShowPopup(MousePosition);
            }
        }
        //全部折叠
        private void barBtnCollpseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.accordionControl1.CollapseAll();
        }
        //全部展开
        private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.accordionControl1.ExpandAll();
        }
        #region 通用方法
@@ -101,7 +193,10 @@
            Yw.Model.HydroModelInfo hydroInfo = null;
            if (!useCache)
            {
                hydroInfo = await GetHydroInfo(scheme);
                if (hydroInfo == null)
                {
                    hydroInfo = await GetHydroInfo(scheme);
                }
                if (hydroInfo == null)
                {
                    return default;
@@ -114,7 +209,15 @@
                {
                    if (_hydroInfo == null)
                    {
                        _hydroInfo = await GetHydroInfo(null);
                        hydroInfo = await GetHydroInfo(null);
                        if (hydroInfo == null)
                        {
                            return default;
                        }
                        if (_hydroInfo == null)
                        {
                            _hydroInfo = hydroInfo;
                        }
                    }
                    return _hydroInfo;
                }
@@ -132,9 +235,16 @@
                if (!_allSchemeHydroInfoDict.ContainsKey(scheme))
                {
                    hydroInfo = await GetHydroInfo(scheme);
                    _allSchemeHydroInfoDict.Add(scheme, hydroInfo);
                    return hydroInfo;
                    if (hydroInfo == null)
                    {
                        return default;
                    }
                    if (!_allSchemeHydroInfoDict.ContainsKey(scheme))
                    {
                        _allSchemeHydroInfoDict.Add(scheme, hydroInfo);
                    }
                }
                return _allSchemeHydroInfoDict[scheme];
            }
            if (_allSchemeHydroInfoDict.ContainsKey(scheme))
@@ -146,7 +256,7 @@
                _allSchemeHydroInfoDict.Add(scheme, hydroInfo);
            }
            return hydroInfo;
            return _allSchemeHydroInfoDict[scheme];
        }
        //获取水力信息
@@ -159,6 +269,18 @@
            if (_projectSite == null)
            {
                return default;
            }
            string key = $"{_project.ID}-{_projectSite.ID}-{scheme?.ID}";
            if (_gettingHydroInfoDict.TryGetValue(key, out bool getting))
            {
                if (getting)
                {
                    return default;
                }
            }
            if (!_gettingHydroInfoDict.TryAdd(key, true))
            {
                _gettingHydroInfoDict[key] = true;
            }
            HydroModelRelationVmo relation = null;
@@ -177,11 +299,13 @@
                return default;
            }
            var hydroInfo = await BLLFactory<Yw.BLL.HydroModelInfo>.Instance.GetByID(relation.ModelID);
            _gettingHydroInfoDict[key] = false;
            return hydroInfo;
        }
        /// <summary>
        /// 添加方案
        /// 触发项目站方案增加事件
        /// </summary>
        public void AppendScheme(XhsSchemeVmo scheme)
        {
@@ -235,11 +359,127 @@
            {
                _allSchemeWorkingCheckedListDict.Add(scheme, new Dictionary<HydroWorkingVmo, bool>());
            }
            this.ProjectSiteSchemeAppendEvent?.Invoke(_project, _projectSite, scheme);
        }
        /// <summary>
        /// 更新方案
        /// 触发项目站方案更新事件
        /// </summary>
        public void UpdateScheme(XhsSchemeVmo scheme)
        {
            if (_project == null)
            {
                return;
            }
            if (_projectSite == null)
            {
                return;
            }
            if (scheme == null)
            {
                return;
            }
            var elementScheme = this.elementProjectSiteSchemeList.Elements
                   .FirstOrDefault(x => (x.Tag is XhsSchemeVmo) && (x.Tag as XhsSchemeVmo).ID == scheme.ID);
            if (elementScheme == null)
            {
                return;
            }
            elementScheme.Text = scheme.Name;
            elementScheme.Hint = scheme.Description;
            elementScheme.Tag = scheme;
            if (_allSchemeHydroInfoDict != null && _allSchemeHydroInfoDict.Count > 0)
            {
                var schemeKey = _allSchemeHydroInfoDict.Keys.FirstOrDefault(x => x.ID == scheme.ID);
                if (schemeKey != null)
                {
                    var hydroInfoValue = _allSchemeHydroInfoDict[schemeKey];
                    _allSchemeHydroInfoDict.Remove(schemeKey);
                    _allSchemeHydroInfoDict.Add(scheme, hydroInfoValue);
                }
            }
            if (_allSchemeWorkingCheckedListDict != null && _allSchemeWorkingCheckedListDict.Count > 0)
            {
                var schemeKey = _allSchemeWorkingCheckedListDict.Keys.FirstOrDefault(x => x.ID == scheme.ID);
                if (schemeKey != null)
                {
                    var workingCheckedValue = _allSchemeWorkingCheckedListDict[schemeKey];
                    _allSchemeWorkingCheckedListDict.Remove(schemeKey);
                    _allSchemeWorkingCheckedListDict.Add(scheme, workingCheckedValue);
                }
            }
            this.ProjectSiteSchemeUpdateEvent?.Invoke(_project, _projectSite, scheme);
        }
        /// <summary>
        /// 移除方案
        /// </summary>
        public void RemoveScheme(XhsSchemeVmo scheme)
        {
            if (_project == null)
            {
                return;
            }
            if (_projectSite == null)
            {
                return;
            }
            if (scheme == null)
            {
                return;
            }
            var elementScheme = this.elementProjectSiteSchemeList.Elements
                   .FirstOrDefault(x => (x.Tag is XhsSchemeVmo) && (x.Tag as XhsSchemeVmo).ID == scheme.ID);
            if (elementScheme == null)
            {
                return;
            }
            if (elementScheme.Elements != null && elementScheme.Elements.Count > 0)
            {
                var workingElementList = elementScheme.Elements.ToList();
                foreach (var workingElement in workingElementList)
                {
                    if (workingElement.HeaderControl != null)
                    {
                        this.accordionControl1.Controls.Remove(workingElement.HeaderControl);
                    }
                    elementScheme.Elements.Remove(workingElement);
                }
            }
            this.elementProjectSiteSchemeList.Elements.Remove(elementScheme);
            if (this.elementProjectSiteSchemeList.Elements.Count < 1)
            {
                this.elementProjectSiteSchemeList.Style = DevExpress.XtraBars.Navigation.ElementStyle.Item;
            }
            if (_allSchemeHydroInfoDict != null && _allSchemeHydroInfoDict.Count > 0)
            {
                var schemeKey = _allSchemeHydroInfoDict.Keys.FirstOrDefault(x => x.ID == scheme.ID);
                if (schemeKey != null)
                {
                    _allSchemeHydroInfoDict.Remove(schemeKey);
                }
            }
            if (_allSchemeWorkingCheckedListDict != null && _allSchemeWorkingCheckedListDict.Count > 0)
            {
                var schemeKey = _allSchemeWorkingCheckedListDict.Keys.FirstOrDefault(x => x.ID == scheme.ID);
                if (schemeKey != null)
                {
                    _allSchemeWorkingCheckedListDict.Remove(schemeKey);
                }
            }
            this.ProjectSiteSchemeRemoveEvent?.Invoke(_project, _projectSite, scheme);
        }
        /// <summary>
        /// 添加工况
        /// 纯添加,不会触发事件
        /// 触发项目站工况增加事件
        /// </summary>
        public void AppendWorking(XhsSchemeVmo scheme, Yw.Vmo.HydroWorkingVmo working)
        {
@@ -282,6 +522,10 @@
                elementWorking.Click += async (sender, e) =>
                {
                    var hydroInfo = await GetHydroInfo(null, true);
                    if (hydroInfo == null)
                    {
                        return;
                    }
                    this.ShowProjectSiteWorkingEvent?.Invoke(_project, _projectSite, null, hydroInfo, working, this.svgImg32[4]);
                };
                this.elementProjectSiteSimulation.Elements.Add(elementWorking);
@@ -332,6 +576,10 @@
                elementWorking.Click += async (sender, e) =>
                {
                    var hydroInfo = await GetHydroInfo(scheme, true);
                    if (hydroInfo == null)
                    {
                        return;
                    }
                    this.ShowProjectSiteWorkingEvent?.Invoke(_project, _projectSite, scheme, hydroInfo, working, this.svgImg32[4]);
                };
                elementScheme.Elements.Add(elementWorking);
@@ -352,6 +600,8 @@
                    }
                }
            }
            this.ProjectSiteWorkingAppendEvent?.Invoke(_project, _projectSite, scheme, working);
        }
        /// <summary>
@@ -523,10 +773,8 @@
            this.ProjectSiteWorkingRemoveEvent?.Invoke(_project, _projectSite, scheme, working);
        }
        #endregion
        //项目站概况
        private async void elementProjectSiteInfo_Click(object sender, EventArgs e)
        //显示信息
        private async Task ShowInfo()
        {
            if (_project == null)
            {
@@ -544,51 +792,47 @@
            this.ShowProjectSiteInfoEvent?.Invoke(_project, _projectSite, hydroInfo, this.svgImg32[0]);
        }
        //项目站水力模拟
        private async void elementProjectSiteSimulation_Click(object sender, EventArgs e)
        /// <summary>
        /// 保存
        /// </summary>
        public async Task<Yw.Model.HydroModelInfo> SaveModel(XhsSchemeVmo scheme, Yw.Model.HydroModelInfo hydroInfo)
        {
            if (_project == null)
            {
                return;
            }
            if (_projectSite == null)
            {
                return;
            }
            var hydroInfo = await GetHydroInfo(null, true);
            if (hydroInfo == null)
            {
                return;
                return default;
            }
            this.ShowProjectSiteSimulationEvent?.Invoke(_project, _projectSite, null, hydroInfo, _allWorkingCheckedListDict, this.svgImg32[1]);
            var id = await BLLFactory<Yw.BLL.HydroModelInfo>.Instance.Save(hydroInfo);
            if (id < 1)
            {
                return default;
            }
            var db = await BLLFactory<Yw.BLL.HydroModelInfo>.Instance.GetByID(id);
            if (scheme == null)
            {
                _hydroInfo = db;
                return _hydroInfo;
            }
            if (_allSchemeHydroInfoDict == null || _allSchemeHydroInfoDict.Count < 1)
            {
                return default;
            }
            var schemeKey = _allSchemeHydroInfoDict.Keys.FirstOrDefault(x => x.ID == scheme.ID);
            if (schemeKey == null)
            {
                return default;
            }
            _allSchemeHydroInfoDict[schemeKey] = db;
            return db;
        }
        //创建项目站方案
        private async void accordionControl1_ContextButtonClick(object sender, DevExpress.Utils.ContextItemClickEventArgs e)
        /// <summary>
        /// 刷新
        /// </summary>
        public async Task<Yw.Model.HydroModelInfo> RefreshModel(XhsSchemeVmo scheme)
        {
            if (_project == null)
            {
                return;
            }
            if (_projectSite == null)
            {
                return;
            }
            var element = e.DataItem as DevExpress.XtraBars.Navigation.AccordionControlElement;
            if (element != this.elementProjectSiteSchemeList)
            {
                return;
            }
            var hydroInfo = await GetHydroInfo(null, true);
            if (hydroInfo == null)
            {
                return;
            }
            this.CreateProjectSiteSchemeEvent?.Invoke(_project, _projectSite, hydroInfo, this.svgImg32[3]);
            return await GetHydroInfo(scheme, false);
        }
        #endregion
    }
}