lixiaojun
2024-12-19 1dd158434a41627a6684cd630b2696fb83b1e3d6
WinFrmUI/HStation.WinFrmUI.Assets.Core/16-flowmeter/02-main/AddAssetsFlowmeterMainDlg.cs
@@ -1,141 +1,174 @@
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 AddAssetsFlowmeterMainDlg : DevExpress.XtraEditors.XtraForm
    {
        public AddAssetsFlowmeterMainDlg()
        {
            InitializeComponent();
            this.treeList1.InitialMultiColSettings();
            this.IconOptions.Icon = Yw.WinFrmUI.GlobalParas.AppIcon;
            this.layoutControl1.SetupLayoutControl();
            this.generalOkAndCancelCtrl1.OkEvent += GeneralOkAndCancelCtrl1_OkEvent;
        }
        private Vmo.AssetsFlowmeterMainVmo _FlowmeterVmo = null;
        private static AssetsFlowmeterMainVmo _last = null;
        private List<Vmo.AssetsFlowmeterCoefficientVmo> _AssetsFlowmeterCoefficient;
        /// <summary>
        /// 返回数据事件
        /// </summary>
        public event Action<HStation.Vmo.AssetsFlowmeterMainVmo> ReloadDataEvent;
        public async void SetBindingData(long SeriesID)
        private HStation.Vmo.AssetsFlowmeterSeriesVmo _series = null;
        private HStation.Vmo.AssetsFlowmeterMainVmo _vmo = null;
        /// <summary>
        /// 绑定数据
        /// </summary>
        public async void SetBindingData(HStation.Vmo.AssetsFlowmeterSeriesVmo series)
        {
            var bll = new BLL.AssetsFlowmeterCoefficient();
            _AssetsFlowmeterCoefficient = await bll.GetAll();
            this.FlowmeterCoefficientViewModelBindingSource.DataSource = _AssetsFlowmeterCoefficient;
            _FlowmeterVmo = new Vmo.AssetsFlowmeterMainVmo();
            _FlowmeterVmo.SeriesID = SeriesID;
             this.selectFlagsPopupCtrl1.SetBindingData<AssetsFlags>();
            if (series == null)
            {
                return;
            }
            _series = series;
            _vmo = new Vmo.AssetsFlowmeterMainVmo();
            _vmo.SeriesID = series.ID;
            var flags = await BLLFactory<Yw.BLL.SysFlag>.Instance.GetBySysType(HStation.Assets.DataType.FlowmeterMain);
            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.txtMinorLoss.EditValue = _last.MinorLoss;
                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.AssetsFlowmeterMainVmo, 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.txtMinorLoss.Text.Trim()))
            {
                this.dxErrorProvider1.SetError(this.TextEditMinorLoss, "必填项");
                this.dxErrorProvider1.SetError(this.txtMinorLoss, "必填项");
                return false;
            }
            var tagname = this.txtTagName.Text.Trim();
            if (!string.IsNullOrEmpty(tagname))
            {
                if (await BLLFactory<HStation.BLL.AssetsFlowmeterMain>.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 (_AssetsFlowmeterCoefficient == 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();
            if (_vmo == null)
            {
                return;
            }
            if (!await Valid())
            {
                return;
            }
            _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();
            var id = await BLLFactory<HStation.BLL.AssetsFlowmeterMain>.Instance.Insert(_vmo);
            if (id < 1)
            {
                TipFormHelper.ShowError("添加失败!");
                return;
            }
            var vmo = await BLLFactory<HStation.BLL.AssetsFlowmeterMain>.Instance.GetByID(id);
            _last = vmo;
            this.ReloadDataEvent?.Invoke(vmo);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        //口径选择变化事件
        private void TextEditCaliber_SelectedIndexChanged(object sender, EventArgs e)
        //获取所有系数列表
        private async Task<List<AssetsFlowmeterFactorVmo>> GetAllFactorList()
        {
            if (_AssetsFlowmeterCoefficient == null)
                return;
            if (double.TryParse(TextEditCaliber.Text, out double caliber))
            if (_allFactorList == null)
            {
                foreach (var item in _AssetsFlowmeterCoefficient)
                _allFactorList = await BLLFactory<HStation.BLL.AssetsFlowmeterFactor>.Instance.GetAll();
                if (_allFactorList == null)
                {
                    if (item.Caliber.HasValue)
                    _allFactorList = new List<AssetsFlowmeterFactorVmo>();
                }
            }
            return _allFactorList;
        }
        private List<AssetsFlowmeterFactorVmo> _allFactorList = null;
        //匹配系数
        private async void MatchingFactor()
        {
            if (this.txtMinorLoss.EditValue == null)
            {
                var allFactorList = await GetAllFactorList();
                if (allFactorList != null && allFactorList.Count > 0)
                {
                    var name = this.txtName.Text.Trim();
                    var factorList = allFactorList.ToList();
                    if (!string.IsNullOrEmpty(name))
                    {
                        if (Math.Abs(Convert.ToDouble(item.Caliber) - caliber) < 10)
                        {
                            this.TextEditCoefficient.Text = item.MinorLoss.ToString();
                            return;
                        }
                        factorList = factorList.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.Contains(name)).ToList();
                    }
                    var factor = factorList.FirstOrDefault();
                    if (factor != null)
                    {
                        this.txtMinorLoss.EditValue = factor.MinorLoss;
                    }
                }
            }
        }
        //找到最相近的材料
        private AssetsFlowmeterCoefficientVmo GetCoefficientByMaterial(string name)
        private void txtMaterial_EditValueChanged(object sender, EventArgs e)
        {
            AssetsFlowmeterCoefficientVmo select = null;
            int maxMatchedChars = 0;
            foreach (var item in _AssetsFlowmeterCoefficient)
            {
                int matchedChars = GetIntersect(item.Material, name);
                if (matchedChars > maxMatchedChars)
                {
                    maxMatchedChars = matchedChars;
                    select = item;
                    return select;
                }
            }
            return select;
            MatchingFactor();
        }
        private int GetIntersect(string str1, string str2)
        private void txtCaliber_EditValueChanged(object sender, EventArgs e)
        {
            if (str1 == null || str2 == null) return 0;
            return string.Join("", str1.Intersect(str2)).Count();
            MatchingFactor();
        }
        //完成
        private async void BtnOk_ClickAsync(object sender, EventArgs e)
        {
            if (!(Valid()))
                return;
            _FlowmeterVmo.Description = DescriptionTextEdit.Text.Trim();
            _FlowmeterVmo.Name = TextEditName.Text.Trim();
            _FlowmeterVmo.KeyWord = this.TextEditKeyWord.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            if (double.TryParse(TextEditMinorLoss.Text, out double minorLoss))
            {
                _FlowmeterVmo.MinorLoss = minorLoss;
            }
            _FlowmeterVmo.Flags = this.selectFlagsPopupCtrl1.SelectedFlags;
            if (await this.ReloadDataEvent.Invoke(_FlowmeterVmo))
            {
                TipFormHelper.ShowSucceed("添加成功!");
            }
            else
            {
                TipFormHelper.ShowError("添加失败!");
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            var vm = this.treeList1.GetCurrentViewModel(_AssetsFlowmeterCoefficient);
            if (vm == null)
                return;
            this.TextEditCoefficient.Text = vm.MinorLoss.ToString();
        }
    }
}