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
{
public partial class AddAssetsFourlinkMainDlg : DevExpress.XtraEditors.XtraForm
{
public AddAssetsFourlinkMainDlg()
{
InitializeComponent();
this.IconOptions.Icon = Yw.WinFrmUI.GlobalParas.AppIcon;
this.layoutControl1.SetupLayoutControl();
this.generalOkAndCancelCtrl1.OkEvent += GeneralOkAndCancelCtrl1_OkEvent;
}
private static AssetsFourlinkMainVmo _last = null;
///
/// 返回数据事件
///
public event Action ReloadDataEvent;
private HStation.Vmo.AssetsFourlinkSeriesVmo _series = null;
private HStation.Vmo.AssetsFourlinkMainVmo _vmo = null;
///
/// 绑定数据
///
public async void SetBindingData(HStation.Vmo.AssetsFourlinkSeriesVmo series)
{
if (series == null)
{
return;
}
_series = series;
_vmo = new Vmo.AssetsFourlinkMainVmo();
_vmo.SeriesID = series.ID;
var flags = await BLLFactory.Instance.GetBySysType(HStation.Assets.DataType.FourlinkMain);
this.setFlagsEditCtrl1.SetBindingData(flags?.Select(x => x.Name).ToList(), null);
if (_last != null)
{
this.txtName.EditValue = _last.Name;
this.txtMaterial.EditValue = _last.Material;
this.txtKeyWord.EditValue = KeyWordHelper.ToString(_last.KeyWords);
this.txtCaliber.EditValue = _last.Caliber;
this.txtCoefficient.EditValue = _last.MinorLoss;
this.setFlagsEditCtrl1.SetBindingData(flags?.Select(x => x.Name).ToList(), _last.Flags);
this.txtTagName.EditValue = _last.TagName;
this.txtDescription.EditValue = _last.Description;
}
}
//验证
private async Task Valid()
{
this.dxErrorProvider1.ClearErrors();
if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
{
this.dxErrorProvider1.SetError(this.txtName, "必填项");
return false;
}
if (string.IsNullOrEmpty(this.txtCoefficient.Text.Trim()))
{
this.dxErrorProvider1.SetError(this.txtCoefficient, "必填项");
return false;
}
var tagname = this.txtTagName.Text.Trim();
if (!string.IsNullOrEmpty(tagname))
{
if (await BLLFactory.Instance.IsExistTagName(tagname))
{
this.dxErrorProvider1.SetError(this.txtTagName, "重复");
return false;
}
}
return true;
}
//确定
private async void GeneralOkAndCancelCtrl1_OkEvent()
{
if (_series == null)
{
return;
}
if (_vmo == null)
{
return;
}
if (!await Valid())
{
return;
}
_vmo.Name = this.txtName.Text.Trim();
_vmo.Material = this.txtMaterial.Text.Trim();
_vmo.Caliber = this.txtCaliber.EditValue == null ? null : double.Parse(this.txtCaliber.EditValue?.ToString());
_vmo.MinorLoss = double.Parse(this.txtCoefficient.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.Instance.Insert(_vmo);
if (id < 1)
{
TipFormHelper.ShowError("添加失败!");
return;
}
var vmo = await BLLFactory.Instance.GetByID(id);
_last = vmo;
this.ReloadDataEvent?.Invoke(vmo);
this.DialogResult = DialogResult.OK;
this.Close();
}
//获取所有系数列表
private async Task> GetAllFactorList()
{
if (_allFactorList == null)
{
_allFactorList = await BLLFactory.Instance.GetAll();
if (_allFactorList == null)
{
_allFactorList = new List();
}
}
return _allFactorList;
}
private List _allFactorList = null;
//匹配系数
private async void MatchingFactor()
{
if (this.txtCoefficient.EditValue == null)
{
var allFactorList = await GetAllFactorList();
if (allFactorList != null && allFactorList.Count > 0)
{
var material = this.txtMaterial.Text.Trim();
double? caliber = this.txtCaliber.EditValue == null ? null : double.Parse(this.txtCaliber.EditValue.ToString());
var factorList = allFactorList.ToList();
if (!string.IsNullOrEmpty(material))
{
factorList = factorList.Where(x => !string.IsNullOrEmpty(x.Material) && x.Material.Contains(material)).ToList();
}
if (caliber.HasValue)
{
factorList = factorList.Where(x => x.Caliber.HasValue && x.Caliber.Value == caliber.Value).ToList();
}
var factor = factorList.FirstOrDefault();
if (factor != null)
{
this.txtCoefficient.EditValue = factor.MinorLoss;
}
}
}
}
private void txtMaterial_EditValueChanged(object sender, EventArgs e)
{
MatchingFactor();
}
private void txtCaliber_EditValueChanged(object sender, EventArgs e)
{
MatchingFactor();
}
}
}