using DevExpress.CodeParser;
using DevExpress.XtraEditors;
using System;
using System.Windows.Forms;
namespace IStation.WinFrmUI
{
///
/// 设置一天小时段
///
public partial class ArrayFormatParasCtrl : UserControl, IFormatParas
{
public ArrayFormatParasCtrl()
{
InitializeComponent();
}
private Model.SignalType.ArrayFormatParas _model = null;
private List _unitTypes = null;
private List _unitValues = null;
private bool _isNON = false;//0
private bool _isCustomer = false;//999
//初始化数据
public async void Set(string paras)
{
_unitTypes = await new BLL.UnitType().GetAll();
_unitValues = await new BLL.UnitValue().GetAll();
if (_unitTypes != null && _unitTypes.Count > 0)
{
foreach (var unitType in _unitTypes)
{
this.UnitTypeImageComboBoxEdit.Properties.Items.Add(unitType.Name, unitType.Code, -1);
}
}
_model = Model.SignalType.ArrayFormatParas.ToModel(paras);
if (_model == null)
{
_model = new Model.SignalType.ArrayFormatParas();
this.UnitTypeImageComboBoxEdit.SelectedIndex = 0;
}
else
{
this.UnitTypeImageComboBoxEdit.EditValue = _model.UnitType;
}
this.DecimalPlacesTextEdit.EditValue = _model.DecimalPlaces;
}
//单位类型变换
private void UnitTypeImageComboBoxEdit_EditValueChanged(object sender, EventArgs e)
{
if (_unitTypes == null || _unitTypes.Count < 1)
return;
var code = (int)this.UnitTypeImageComboBoxEdit.EditValue;
SetUnitValueComboBoxEdit(code);
}
private void SetUnitValueComboBoxEdit(int unitTypeCode)
{
_isNON = false;
this.UnitValueComboBoxEdit.Text = string.Empty;
this.UnitValueComboBoxEdit.Properties.Items.Clear();
this.UnitValueComboBoxEdit.Enabled = true;
this.UnitValueComboBoxEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
if (unitTypeCode == 0)
{
_isNON = true;
this.UnitValueComboBoxEdit.Enabled = false;
}
else if (unitTypeCode == 999)
{
_isCustomer = true;
this.UnitValueComboBoxEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
if (unitTypeCode == _model.UnitType)
{
this.UnitValueComboBoxEdit.Text = _model.UnitValue;
}
}
else
{
var unitType = _unitTypes.Find(x => x.Code == unitTypeCode);
if (unitType == null)
return;
var unitValues = _unitValues.FindAll(x => x.TypeID == unitType.ID);
if (unitValues == null || unitValues.Count < 1)
return;
foreach (var item in unitValues)
{
var keyIntValue = new KeyIntValue(item.Name, item.Code);
this.UnitValueComboBoxEdit.Properties.Items.Add(keyIntValue);
}
this.UnitValueComboBoxEdit.SelectedIndex = 0;
if (unitTypeCode == _model.UnitType)
{
if (int.TryParse(_model.UnitValue, out int value))
{
var index = unitValues.FindIndex(x => x.Code == value);
this.UnitValueComboBoxEdit.SelectedIndex = index;
}
}
}
}
//验证
public bool Valid()
{
if (this.UnitTypeImageComboBoxEdit.EditValue == null)
{
XtraMessageBox.Show("请选择单位类型!");
return false;
}
if (!_isNON)
{
if (string.IsNullOrEmpty(this.UnitValueComboBoxEdit.Text))
{
XtraMessageBox.Show("请选择/录入单位值!");
return false;
}
}
return true;
}
//获取时间
public string Get()
{
if (!Valid())
return default;
_model.UnitType = (int)this.UnitTypeImageComboBoxEdit.EditValue;
if (_isNON)
{
_model.UnitValue = string.Empty;
}
else if (_isCustomer)
{
_model.UnitValue = this.UnitValueComboBoxEdit.Text.TrimEnd();
}
else
{
var keyIntValue = (this.UnitValueComboBoxEdit.SelectedItem as KeyIntValue);
_model.UnitValue = keyIntValue?.Value.ToString() ?? string.Empty;
}
var decimalPlaces = this.DecimalPlacesTextEdit.Text.Trim();
if (!string.IsNullOrEmpty(decimalPlaces))
_model.DecimalPlaces = int.Parse(decimalPlaces);
else
_model.DecimalPlaces = null;
return _model.ToJson();
}
}
}