using HStation.Service.Assets;
using HStation.Vmo;
using System.Data;
using Yw;
namespace HStation.WinFrmUI
{
public partial class AddAssetsTankMainDlg : DevExpress.XtraEditors.XtraForm
{
public AddAssetsTankMainDlg()
{
InitializeComponent();
this.IconOptions.Icon = Yw.WinFrmUI.GlobalParas.AppIcon;
this.layoutControl1.SetupLayoutControl();
this.generalOkAndCancelCtrl1.OkEvent += GeneralOkAndCancelCtrl1_OkEvent;
}
private static AssetsTankMainVmo _last = null;
///
/// 返回数据事件
///
public event Action ReloadDataEvent;
private HStation.Vmo.AssetsTankSeriesVmo _series = null;
private HStation.Vmo.AssetsTankMainVmo _vmo = null;
///
/// 绑定数据
///
public async void SetBindingData(HStation.Vmo.AssetsTankSeriesVmo series)
{
if (series == null)
{
return;
}
_series = series;
_vmo = new Vmo.AssetsTankMainVmo();
_vmo.SeriesID = series.ID;
var flags = await BLLFactory.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;
}
}
//验证
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.txtMaxLevel.Text.Trim()))
{
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.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.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))
{
_vmo.DN = DN;
}
if (double.TryParse(this.txtMinLevel.Text, out double MinLevel))
{
_vmo.MinLevel = MinLevel;
}
if (double.TryParse(this.txtMaxLevel.Text, out double MaxLevel))
{
_vmo.MaxLevel = MaxLevel;
}
if (double.TryParse(this.txtMinVol.Text, out double MinVol))
{
_vmo.MinVol = MinVol;
}
_vmo.OverFlow = this.textEditVoerFlow.Checked;
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();
}
}
}