duheng
2024-12-20 f1fac249aec3a5390152fa2a9a13e8da98283c3b
WinFrmUI/HStation.WinFrmUI.Assets.Core/14-tank/02-main/AddAssetsTankMainDlg.cs
@@ -1,154 +1,198 @@
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors;
using DevExpress.XtraScheduler.Outlook.Interop;
using HStation.Service.Assets;
using HStation.Vmo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Yw;
namespace HStation.WinFrmUI.Assets
namespace HStation.WinFrmUI
{
    public partial class AddAssetsTankMainDlg : DevExpress.XtraEditors.XtraForm
    {
        public AddAssetsTankMainDlg()
        {
            InitializeComponent();
            this.treeList1.InitialMultiColSettings();
            this.IconOptions.Icon = Yw.WinFrmUI.GlobalParas.AppIcon;
            this.layoutControl1.SetupLayoutControl();
            this.generalOkAndCancelCtrl1.OkEvent += GeneralOkAndCancelCtrl1_OkEvent;
        }
        private Vmo.AssetsTankMainVmo _TankVmo = null;
        private static AssetsTankMainVmo _last = null;
        private List<Vmo.AssetsTankCoefficientVmo> _AssetsTankCoefficient;
        /// <summary>
        /// 返回数据事件
        /// </summary>
        public event Action<HStation.Vmo.AssetsTankMainVmo> ReloadDataEvent;
        public async void SetBindingData(long SeriesID)
        private HStation.Vmo.AssetsTankSeriesVmo _series = null;
        private HStation.Vmo.AssetsTankMainVmo _vmo = null;
        /// <summary>
        /// 绑定数据
        /// </summary>
        public async void SetBindingData(HStation.Vmo.AssetsTankSeriesVmo series)
        {
            var bll = new BLL.AssetsTankCoefficient();
            _AssetsTankCoefficient = await bll.GetAll();
            this.TankCoefficientViewModelBindingSource.DataSource = _AssetsTankCoefficient;
            _TankVmo = new Vmo.AssetsTankMainVmo();
            _TankVmo.SeriesID = SeriesID;
            this.selectFlagsPopupCtrl1.SetBindingData<AssetsFlags>();
            if (series == null)
            {
                return;
            }
            _series = series;
            _vmo = new Vmo.AssetsTankMainVmo();
            _vmo.SeriesID = series.ID;
            var flags = await BLLFactory<Yw.BLL.SysFlag>.Instance.GetBySysType(HStation.Assets.DataType.TankMain);
            this.setFlagsEditCtrl1.SetBindingData(flags?.Select(x => x.Name).ToList(), null);
            if (_last != null)
            {
                this.txtName.EditValue = _last.Name;
                this.txtKeyWord.EditValue = KeyWordHelper.ToString(_last.KeyWords);
                this.setFlagsEditCtrl1.SetBindingData(flags?.Select(x => x.Name).ToList(), _last.Flags);
                this.txtTagName.EditValue = _last.TagName;
                this.txtDescription.EditValue = _last.Description;
            }
        }
        public event Func<Vmo.AssetsTankMainVmo, Task<bool>> ReloadDataEvent = null;
        //数据验证
        private bool Valid()
        //验证
        private async Task<bool> Valid()
        {
            this.dxErrorProvider1.ClearErrors();
            if (string.IsNullOrEmpty(TextEditName.Text.Trim()))
            if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                this.dxErrorProvider1.SetError(this.TextEditName, "必填项");
                this.dxErrorProvider1.SetError(this.txtName, "必填项");
                return false;
            }
            if (string.IsNullOrEmpty(TextEditMinorLoss.Text.Trim()))
            if (string.IsNullOrEmpty(this.txtMaxLevel.Text.Trim()))
            {
                this.dxErrorProvider1.SetError(this.TextEditMinorLoss, "必填项");
                this.dxErrorProvider1.SetError(this.txtMaxLevel, "必填项");
                return false;
            }
            if (string.IsNullOrEmpty(this.txtMinLevel.Text.Trim()))
            {
                this.dxErrorProvider1.SetError(this.txtMinLevel, "必填项");
                return false;
            }
            var tagname = this.txtTagName.Text.Trim();
            if (!string.IsNullOrEmpty(tagname))
            {
                if (await BLLFactory<HStation.BLL.AssetsTankMain>.Instance.IsExistTagName(tagname))
                {
                    this.dxErrorProvider1.SetError(this.txtTagName, "重复");
                    return false;
                }
            }
            return true;
        }
        //材料选择变化事件
        private void TextEditMaterial_SelectedIndexChanged(object sender, EventArgs e)
        //确定
        private async void GeneralOkAndCancelCtrl1_OkEvent()
        {
            if (_AssetsTankCoefficient == null)
                return;
            var select = GetCoefficientByMaterial(TextEditMaterial.Text);
            if (select == null)
            if (_series == null)
            {
                this.TextEditCoefficient.Text = string.Empty;
                return;
            }
            this.TextEditCoefficient.Text = select.MinorLoss.ToString();
        }
        //口径选择变化事件
        private void TextEditCaliber_SelectedIndexChanged(object sender, EventArgs e)
            if (_vmo == null)
        {
            if (_AssetsTankCoefficient == null)
                return;
            if (double.TryParse(TextEditCaliber.Text, out double caliber))
            {
                foreach (var item in _AssetsTankCoefficient)
                {
                    if (item.Caliber.HasValue)
                    {
                        if (Math.Abs(Convert.ToDouble(item.Caliber) - caliber) < 10)
                        {
                            this.TextEditCoefficient.Text = item.MinorLoss.ToString();
                            return;
                        }
                    }
                }
            }
        }
        //找到最相近的材料
        private AssetsTankCoefficientVmo GetCoefficientByMaterial(string name)
            if (!await Valid())
        {
            AssetsTankCoefficientVmo select = null;
            int maxMatchedChars = 0;
            foreach (var item in _AssetsTankCoefficient)
            {
                int matchedChars = GetIntersect(item.Material, name);
                if (matchedChars > maxMatchedChars)
                {
                    maxMatchedChars = matchedChars;
                    select = item;
                    return select;
                }
            }
            return select;
        }
        private int GetIntersect(string str1, string str2)
        {
            if (str1 == null || str2 == null) return 0;
            return string.Join("", str1.Intersect(str2)).Count();
        }
        //完成
        private async void BtnOk_ClickAsync(object sender, EventArgs e)
        {
            if (!(Valid()))
                return;
            _TankVmo.Description = DescriptionTextEdit.Text.Trim();
            _TankVmo.Name = TextEditName.Text.Trim();
            _TankVmo.KeyWord = this.TextEditKeyWord.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            _TankVmo.Flags = this.selectFlagsPopupCtrl1.SelectedFlags;
            if (double.TryParse(this.textEditDN.Text, out double DN))
            {
                _TankVmo.DN = DN;
            }
            if (double.TryParse(this.textEditMinLevel.Text, out double MinLevel))
            _vmo.Name = this.txtName.Text.Trim();
            //   _vmo.MinorLoss = double.Parse(this.txtMinorLoss.EditValue?.ToString());
            _vmo.KeyWords = HStation.Service.Assets.KeyWordHelper.ToList(this.txtKeyWord.Text.Trim());
            _vmo.Flags = this.setFlagsEditCtrl1.SelectedFlagList;
            _vmo.TagName = this.txtTagName.Text.Trim();
            _vmo.Description = this.txtDescription.Text.Trim();
            if (double.TryParse(this.txtDN.EditValue.ToString(), out double DN))
            {
                _TankVmo.MinLevel = MinLevel;
                _vmo.DN = DN;
            }
            if (double.TryParse(this.textEditMaxLevel.Text, out double MaxLevel))
            if (double.TryParse(this.txtMinLevel.Text, out double MinLevel))
            {
                _TankVmo.MaxLevel = MaxLevel;
                _vmo.MinLevel = MinLevel;
            }
            if (double.TryParse(this.textEditMinVol.Text, out double MinVol))
            if (double.TryParse(this.txtMaxLevel.Text, out double MaxLevel))
            {
                _TankVmo.MinVol = MinVol;
                _vmo.MaxLevel = MaxLevel;
            }
            _TankVmo.OverFlow = this.textEditVoerFlow.Checked;
            if (await this.ReloadDataEvent.Invoke(_TankVmo))
            if (double.TryParse(this.txtMinVol.Text, out double MinVol))
            {
                TipFormHelper.ShowSucceed("添加成功!");
                _vmo.MinVol = MinVol;
            }
            else
            _vmo.OverFlow = this.textEditVoerFlow.Checked;
            var id = await BLLFactory<HStation.BLL.AssetsTankMain>.Instance.Insert(_vmo);
            if (id < 1)
            {
                TipFormHelper.ShowError("添加失败!");
                TipFormHelper.ShowError("添加失败!");
                return;
            }
            var vmo = await BLLFactory<HStation.BLL.AssetsTankMain>.Instance.GetByID(id);
            _last = vmo;
            this.ReloadDataEvent?.Invoke(vmo);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        //获取所有系数列表
        private async Task<List<AssetsTankFactorVmo>> GetAllFactorList()
        {
            var vm = this.treeList1.GetCurrentViewModel(_AssetsTankCoefficient);
            if (vm == null)
                return;
            this.TextEditCoefficient.Text = vm.MinorLoss.ToString();
            if (_allFactorList == null)
            {
                _allFactorList = await BLLFactory<HStation.BLL.AssetsTankFactor>.Instance.GetAll();
                if (_allFactorList == null)
                {
                    _allFactorList = new List<AssetsTankFactorVmo>();
                }
            }
            return _allFactorList;
        }
        private List<AssetsTankFactorVmo> _allFactorList = null;
        //匹配系数
        private async void MatchingFactor()
        {
            if (this.txtMinLevel.EditValue == null)
            {
                var allFactorList = await GetAllFactorList();
                if (allFactorList != null && allFactorList.Count > 0)
                {
                    var material = this.txtMaxLevel.Text.Trim();
                    double? diameter = this.txtDN.EditValue == null ? null : double.Parse(this.txtDN.EditValue.ToString());
                    var factorList = allFactorList.ToList();
                    if (!string.IsNullOrEmpty(material))
                    {
                        factorList = factorList.Where(x => !string.IsNullOrEmpty(x.Material) && x.Material.Contains(material)).ToList();
                    }
                    if (diameter.HasValue)
                    {
                        factorList = factorList.Where(x => x.Caliber.HasValue && x.Caliber.Value == diameter.Value).ToList();
                    }
                    var factor = factorList.FirstOrDefault();
                    if (factor != null)
                    {
                        this.txtMinLevel.EditValue = factor.MinorLoss;
                    }
                }
            }
        }
        private void txtMaterial_EditValueChanged(object sender, EventArgs e)
        {
            MatchingFactor();
        }
        private void txtCaliber_EditValueChanged(object sender, EventArgs e)
        {
            MatchingFactor();
        }
    }
}