using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SIMDP.Model;
using SIMDP.Util;
using ProjectBase.Data.BaseDAL;
using SIMDP.BLL;
using ProjectBase.Data.Logs;
using ProjectBase.Util;
namespace SIMDP
{
public partial class FormEditUserParameter : DevExpress.XtraEditors.XtraForm
{
private MoUserParameter moUserPara = null;
public FormEditUserParameter(MoUserParameter userPara)
{
InitializeComponent();
InitControls();
moUserPara = userPara;
}
private void FormEditUserParameter_Load(object sender, EventArgs e)
{
if (moUserPara != null)
{
this.textEditParaName.EditValue = moUserPara.UserParaName;
this.textEditParaValue.EditValue = moUserPara.UserParaValue;
//this.dateEditPara.EditValue = moUserPara.UserParaTime;
//this.lookUpEditParaType.EditValue = Convert.ToInt32(moUserPara.UserParaType);
this.checkEditValid.Checked = moUserPara.UserParaValid;
this.richTextBoxParaDescription.Text = moUserPara.UserParaDescription;
this.textEditParaUnit.EditValue = moUserPara.UserParaUnit;
this.textEditParaName.Enabled = false;
}
else
{
this.checkEditValid.Checked = true;
}
}
///
/// 初始化控件
///
private void InitControls()
{
//下拉框--参数类型
BindingSource bsParaType = new BindingSource();
bsParaType.DataSource = SysEnvironment.dirSysParameterType;
this.lookUpEditParaType.Properties.DataSource = bsParaType;
this.lookUpEditParaType.Properties.ValueMember = "Key";
this.lookUpEditParaType.Properties.DisplayMember = "Value";
this.lookUpEditParaType.Properties.NullText = null;
//下拉框--参数范围参考条件
BindingSource bsCondition = new BindingSource();
bsCondition.DataSource = SysEnvironment.dirSysParameterData;
this.lookUpEditCondition.Properties.DataSource = bsCondition;
this.lookUpEditCondition.Properties.ValueMember = "Key";
this.lookUpEditCondition.Properties.DisplayMember = "Value";
this.lookUpEditCondition.Properties.NullText = null;
}
///
/// 保存
///
///
///
private void SimpleButtonSave_Click(object sender, EventArgs e)
{
if (!Validation())
{
return;
}
//if (!ParameterTypeValidation())
// return;
//if (!ParameterDataValidation())
// return;
try
{
MoUserParameter mo = new MoUserParameter();
mo.UserParaName = this.textEditParaName.EditValue.ToString();
mo.UserParaValue = this.textEditParaValue.EditValue.ToString();
//mo.UserParaTime = Convert.ToDateTime(this.dateEditPara.EditValue);
mo.UserParaUnit = this.textEditParaUnit.EditValue.ToString();
//mo.UserParaType = this.lookUpEditParaType.EditValue.ToString();
mo.UserParaValid = this.checkEditValid.Checked;
//mo.UserParaScope = this.textEditStart.EditValue != null ? this.textEditStart.EditValue.ToString() : "";
//if (this.textEditStart.EditValue != null && this.textEditEnd.EditValue != null)
// mo.UserParaScope += "~";
//mo.UserParaScope += this.textEditEnd.EditValue != null ? this.textEditEnd.EditValue.ToString() : "";
mo.UserParaDescription = this.richTextBoxParaDescription.Text;
//BlSystemParameter bl = new BlSystemParameter();
bool bOK = false;
if (moUserPara == null)
bOK = BLLFactory.Instance.Insert(mo);
else
{
mo.UserParaId = moUserPara.UserParaId;
bOK = BLLFactory.Instance.Update(mo, mo.UserParaId);
}
if (!bOK)
{
DevExpress.XtraEditors.XtraMessageBox.Show("用户参数编辑失败!");
}
else
{
DevExpress.XtraEditors.XtraMessageBox.Show("用户参数编辑成功!");
this.Close();
}
}
catch (Exception ex)
{
DevExpress.XtraEditors.XtraMessageBox.Show("保存用户参数相关信息失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
LogHelper.log.Error(string.Format("保存用户参数相关信息出现错误:{0}", ex));
}
}
///
/// 验证输入的值的类型
///
///
private bool ParameterTypeValidation()
{
int paraType = Convert.ToInt32(this.lookUpEditParaType.EditValue);
bool bOK = false;
switch (paraType)
{
case 0:
bOK = true;
break;
case 1:
bOK = TypeValidation.IsNumerical(this.textEditParaValue.EditValue.ToString());
break;
case 2:
bOK = TypeValidation.IsInteger(this.textEditParaValue.EditValue.ToString());
break;
case 3:
bOK = TypeValidation.IsTime(this.textEditParaValue.EditValue.ToString());
break;
case 4:
bOK = TypeValidation.IsDate(this.textEditParaValue.EditValue.ToString());
break;
default:
bOK = true;
break;
}
if (!bOK)
{
DevExpress.XtraEditors.XtraMessageBox.Show("输入的值的类型不符合取值条件!");
return false;
}
return true;
}
///
/// 验证输入的值
///
///
private bool ParameterDataValidation()
{
int paraType = Convert.ToInt32(this.lookUpEditParaType.EditValue);
int paraCondition = Convert.ToInt32(this.lookUpEditCondition.EditValue);
bool bOK = false;
switch (paraCondition)
{
case 1: //介于
{
if (paraType == 1)//数值比较
{
if ( Convert.ToDecimal(this.textEditParaValue.Text) >= Convert.ToDecimal(this.textEditStart.Text) &&
Convert.ToDecimal(this.textEditParaValue.Text) <= Convert.ToDecimal(this.textEditEnd.Text))
{
bOK = true;
}
}
else if (paraType == 2)//整型比较
{
if (Convert.ToInt32(this.textEditParaValue.Text) >= Convert.ToInt32(this.textEditStart.Text) &&
Convert.ToInt32(this.textEditParaValue.Text) <= Convert.ToInt32(this.textEditEnd.Text))
{
bOK = true;
}
}
else //时间和日期比较
{
if ((0 <= this.textEditParaValue.EditValue.ToString().CompareTo(this.textEditStart.EditValue.ToString())) &&
(0 >= this.textEditParaValue.EditValue.ToString().CompareTo(this.textEditEnd.EditValue.ToString())))
{
bOK = true;
}
}
break;
}
case 2: //未介于
{
if (paraType == 1)//数值比较
{
if (Convert.ToDecimal(this.textEditParaValue.Text) < Convert.ToDecimal(this.textEditStart.Text) ||
Convert.ToDecimal(this.textEditParaValue.Text) > Convert.ToDecimal(this.textEditEnd.Text))
{
bOK = true;
}
}
else if (paraType == 2)//整型比较
{
if (Convert.ToInt32(this.textEditParaValue.Text) < Convert.ToInt32(this.textEditStart.Text) ||
Convert.ToInt32(this.textEditParaValue.Text) > Convert.ToInt32(this.textEditEnd.Text))
{
bOK = true;
}
}
else //时间和日期比较
{
if (0 > this.textEditParaValue.EditValue.ToString().CompareTo(this.textEditStart.EditValue.ToString()) ||
0 < this.textEditParaValue.EditValue.ToString().CompareTo(this.textEditEnd.EditValue.ToString()))
{
bOK = true;
}
}
break;
}
case 3: //等于
{
if (this.textEditParaValue.EditValue.Equals(this.textEditStart.EditValue))
{
bOK = true;
}
break;
}
case 4: //不等于
{
if (!this.textEditParaValue.EditValue.Equals(this.textEditStart.EditValue))
{
bOK = true;
}
break;
}
case 5: //大于
{
if (paraType == 1)//数值比较
{
if (Convert.ToDecimal(this.textEditParaValue.Text) > Convert.ToDecimal(this.textEditStart.Text) )
{
bOK = true;
}
}
else if (paraType == 2)//整型比较
{
if (Convert.ToInt32(this.textEditParaValue.Text) > Convert.ToInt32(this.textEditStart.Text) )
{
bOK = true;
}
}
else //时间和日期比较
{
if (0 < this.textEditParaValue.EditValue.ToString().CompareTo(this.textEditStart.EditValue.ToString()))
{
bOK = true;
}
}
break;
}
case 6: //大于等于
{
if (paraType == 1)//数值比较
{
if (Convert.ToDecimal(this.textEditParaValue.Text) >= Convert.ToDecimal(this.textEditStart.Text))
{
bOK = true;
}
}
else if (paraType == 2)//整型比较
{
if (Convert.ToInt32(this.textEditParaValue.Text) >= Convert.ToInt32(this.textEditStart.Text))
{
bOK = true;
}
}
else //时间和日期比较
{
if (0 <= this.textEditParaValue.EditValue.ToString().CompareTo(this.textEditStart.EditValue.ToString()))
{
bOK = true;
}
}
break;
}
case 7: //小于
{
if (paraType == 1)//数值比较
{
if (Convert.ToDecimal(this.textEditParaValue.Text) < Convert.ToDecimal(this.textEditEnd.Text))
{
bOK = true;
}
}
else if (paraType == 2)//整型比较
{
if (Convert.ToInt32(this.textEditParaValue.Text) < Convert.ToInt32(this.textEditEnd.Text))
{
bOK = true;
}
}
else //时间和日期比较
{
if (0 > this.textEditParaValue.EditValue.ToString().CompareTo(this.textEditEnd.EditValue.ToString()))
{
bOK = true;
}
}
break;
}
case 8: //小于等于
{
if (paraType == 1)//数值比较
{
if (Convert.ToDecimal(this.textEditParaValue.Text) <= Convert.ToDecimal(this.textEditEnd.Text))
{
bOK = true;
}
}
else if (paraType == 2)//整型比较
{
if (Convert.ToInt32(this.textEditParaValue.Text) <= Convert.ToInt32(this.textEditEnd.Text))
{
bOK = true;
}
}
else //时间和日期比较
{
if (0 >= this.textEditParaValue.EditValue.ToString().CompareTo(this.textEditEnd.EditValue.ToString()))
{
bOK = true;
}
}
break;
}
default:
bOK = true;
break;
}
if (!bOK)
{
DevExpress.XtraEditors.XtraMessageBox.Show("输入的值不符合取值范围!");
return false;
}
return true;
}
///
/// 参数值的类型改变
///
///
///
private void lookUpEditParaType_EditValueChanged(object sender, EventArgs e)
{
if (this.lookUpEditParaType.EditValue != null && !string.IsNullOrEmpty(this.lookUpEditParaType.EditValue.ToString()))
{
this.lookUpEditCondition.EditValue = null;
this.lookUpEditCondition.ItemIndex = -1;
int paraType = Convert.ToInt32(this.lookUpEditParaType.EditValue);
switch (paraType)
{
case 0:
this.lookUpEditCondition.Enabled = false;
this.layoutControlItemMin.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
this.layoutControlItemMax.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
this.textEditParaValue.Properties.NullValuePromptShowForEmptyValue = true;
this.textEditParaValue.Properties.NullValuePrompt = "请输入文本";
break;
case 1:
this.lookUpEditCondition.Enabled = true;
this.layoutControlItemMin.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
this.layoutControlItemMin.Text = "最小值(*):";
this.layoutControlItemMax.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
this.layoutControlItemMax.Text = "最大值(*):";
this.textEditStart.Properties.NullValuePromptShowForEmptyValue = true;
this.textEditStart.Properties.NullValuePrompt = "请输入小于3位小数的数值";
this.textEditEnd.Properties.NullValuePromptShowForEmptyValue = true;
this.textEditEnd.Properties.NullValuePrompt = "请输入小于3位小数的数值";
this.textEditParaValue.Properties.NullValuePromptShowForEmptyValue = true;
this.textEditParaValue.Properties.NullValuePrompt = "请输入小于3位小数的数值";
break;
case 2:
this.lookUpEditCondition.Enabled = true;
this.layoutControlItemMin.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
this.layoutControlItemMin.Text = "最小值(*):";
this.layoutControlItemMax.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
this.layoutControlItemMax.Text = "最大值(*):";
this.textEditStart.Properties.NullValuePromptShowForEmptyValue = true;
this.textEditStart.Properties.NullValuePrompt = "请输入整数";
this.textEditEnd.Properties.NullValuePromptShowForEmptyValue = true;
this.textEditEnd.Properties.NullValuePrompt = "请输入整数";
this.textEditParaValue.Properties.NullValuePromptShowForEmptyValue = true;
this.textEditParaValue.Properties.NullValuePrompt = "请输入整数";
break;
case 3:
this.lookUpEditCondition.Enabled = true;
this.layoutControlItemMin.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
this.layoutControlItemMin.Text = "开始时间(*):";
this.layoutControlItemMax.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
this.layoutControlItemMax.Text = "结束时间(*):";
this.textEditStart.Properties.NullValuePromptShowForEmptyValue = true;
this.textEditStart.Properties.NullValuePrompt = "00:00:00";
this.textEditEnd.Properties.NullValuePromptShowForEmptyValue = true;
this.textEditEnd.Properties.NullValuePrompt = "00:00:00";
this.textEditParaValue.Properties.NullValuePromptShowForEmptyValue = true;
this.textEditParaValue.Properties.NullValuePrompt = "00:00:00";
break;
case 4:
this.lookUpEditCondition.Enabled = true;
this.layoutControlItemMin.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
this.layoutControlItemMin.Text = "开始日期(*):";
this.layoutControlItemMax.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
this.layoutControlItemMax.Text = "结束日期(*):";
this.textEditStart.Properties.NullValuePromptShowForEmptyValue = true;
this.textEditStart.Properties.NullValuePrompt = "0000/00/00";
this.textEditEnd.Properties.NullValuePromptShowForEmptyValue = true;
this.textEditEnd.Properties.NullValuePrompt = "0000/00/00";
this.textEditParaValue.Properties.NullValuePromptShowForEmptyValue = true;
this.textEditParaValue.Properties.NullValuePrompt = "0000/00/00";
break;
default:
break;
}
}
}
///
/// 取值条件改变
///
///
///
private void lookUpEditCondition_EditValueChanged(object sender, EventArgs e)
{
if (this.lookUpEditCondition.EditValue != null && !string.IsNullOrEmpty(this.lookUpEditCondition.EditValue.ToString()))
{
int paraType = Convert.ToInt32(this.lookUpEditCondition.EditValue);
switch (paraType)
{
case 1:
this.layoutControlItemMin.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
this.layoutControlItemMax.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
break;
case 2:
this.layoutControlItemMin.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
this.layoutControlItemMax.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
break;
case 3:
case 4:
case 5:
case 6:
this.layoutControlItemMin.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
this.layoutControlItemMax.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
break;
case 7:
case 8:
this.layoutControlItemMin.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
this.layoutControlItemMax.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
break;
default:
break;
}
}
}
///
/// 验证必填项是否为空
///
///
private bool Validation()
{
if (string.IsNullOrEmpty(this.textEditParaName.Text) )
{
DevExpress.XtraEditors.XtraMessageBox.Show("请先输入用户参数名称!");
return false;
}
//if ( this.dateEditPara.EditValue == null || string.IsNullOrEmpty(this.dateEditPara.Text))
//{
// DevExpress.XtraEditors.XtraMessageBox.Show("请先选择生效日期!");
// return false;
//}
//if (this.lookUpEditParaType.EditValue == null || string.IsNullOrEmpty(this.lookUpEditParaType.EditValue.ToString()))
//{
// DevExpress.XtraEditors.XtraMessageBox.Show("请先选择用户参数值类型!");
// return false;
//}
//int paraType = Convert.ToInt32(this.lookUpEditParaType.EditValue);
//if (paraType != 0 && this.lookUpEditCondition.EditValue == null)
//{
// DevExpress.XtraEditors.XtraMessageBox.Show("请先选择用户参数值范围取值条件!");
// return false;
//}
//if (this.layoutControlItemMin.Visible)
//{
// int index = this.layoutControlItemMin.Text.IndexOf("*");
// string str = this.layoutControlItemMin.Text.Substring(0,index -1 );
// if (string.IsNullOrEmpty(this.textEditStart.Text))
// {
// DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("请先输入{0}!", str));
// return false;
// }
//}
//if (this.layoutControlItemMax.Visible)
//{
// int index = this.layoutControlItemMax.Text.IndexOf("*");
// string str = this.layoutControlItemMax.Text.Substring(0, index - 1);
// if (string.IsNullOrEmpty(this.textEditEnd.Text))
// {
// DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("请先输入{0}!", str));
// return false;
// }
//}
if (string.IsNullOrEmpty(this.textEditParaValue.Text))
{
DevExpress.XtraEditors.XtraMessageBox.Show("请先输入参数值!");
return false;
}
if (string.IsNullOrEmpty(this.textEditParaUnit.Text))
{
DevExpress.XtraEditors.XtraMessageBox.Show("请先输入参数单位!");
return false;
}
return true;
}
}
}