using DevExpress.XtraEditors;
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 AddAssetsThreelinkFactorDlg : DevExpress.XtraEditors.XtraForm
{
public AddAssetsThreelinkFactorDlg()
{
InitializeComponent();
this.IconOptions.Icon = Yw.WinFrmUI.GlobalParas.AppIcon;
this.layoutControl1.SetupLayoutControl();
this.generalOkAndCancelCtrl1.OkEvent += GeneralOkAndCancelCtrl1_OkEvent;
}
private static AssetsThreelinkFactorVmo _last = null;
///
/// 返回数据事件
///
public event Action ReloadDataEvent;
private HStation.Vmo.AssetsThreelinkFactorVmo _vmo = null;
///
/// 绑定数据
///
public void SetBindingData()
{
_vmo = new Vmo.AssetsThreelinkFactorVmo();
if (_last != null)
{
this.txtName.EditValue = _last.Name;
this.textEditBranchThroughCoefficient.EditValue = _last.BranchThroughMinorLoss;
this.textEditRunThroughCoefficient.EditValue = _last.RunThroughMinorLoss;
this.txtDescription.EditValue = _last.Description;
}
}
//验证
private bool Valid()
{
this.dxErrorProvider1.ClearErrors();
if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
{
this.dxErrorProvider1.SetError(this.txtName, "必填项");
return false;
}
if (string.IsNullOrEmpty(this.textEditRunThroughCoefficient.Text.Trim()))
{
this.dxErrorProvider1.SetError(this.textEditRunThroughCoefficient, "必填项");
return false;
}
if (string.IsNullOrEmpty(this.textEditBranchThroughCoefficient.Text.Trim()))
{
this.dxErrorProvider1.SetError(this.textEditBranchThroughCoefficient, "必填项");
return false;
}
return true;
}
//确定
private async void GeneralOkAndCancelCtrl1_OkEvent()
{
if (_vmo == null)
{
return;
}
if (!Valid())
{
return;
}
_vmo.Name = this.txtName.Text.Trim();
_vmo.RunThroughMinorLoss = double.Parse(this.textEditRunThroughCoefficient.EditValue?.ToString());
_vmo.BranchThroughMinorLoss = double.Parse(this.textEditBranchThroughCoefficient.EditValue?.ToString());
_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();
}
}
}