123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- 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 DevExpress.XtraLayout;
- using ProjectBase.Data.Logs;
- using SIMDP.BLL;
- using SIMDP.Model;
- using ProjectBase.Util;
- using ProjectBase.Data.BaseDAL;
- namespace SIMDP.View
- {
- public partial class FormEditRule : DevExpress.XtraEditors.XtraForm
- {
- private DataTable dtRule = new DataTable(); //下拉框--ID和名称绑定的数据源
- private BindingSource bsType = new BindingSource(); //下拉框--类型绑定的数据源
- private BindingSource bsQuery = new BindingSource(); //下拉框--查询条件绑定的数据源
- private List<TextEdit> nameList = new List<TextEdit>(); //动态生成的textEdit,用来存放变量名称
- private List<TextEdit> textList = new List<TextEdit>(); //动态生成的textEdit,用来存放变量序号
- private List<LookUpEdit> typeList = new List<LookUpEdit>(); //动态生成的lookupEdit,用来存放变量类型
- private List<LookUpEdit> queryList = new List<LookUpEdit>(); //动态生成的lookupEdit,用来存放变量是否作为查询条件
- private int count = 1;
- /// <summary>
- /// 需要修改的规则对象
- /// </summary>
- public MoRule rule { get; set; }
-
- /// <summary>
- /// 定义委托
- /// </summary>
- public delegate void save();
- /// <summary>
- /// 定义事件,保存数据成功后刷新FormPLC中的数据
- /// </summary>
- public event save saveData;
- /// <summary>
- /// 构造函数
- /// </summary>
- public FormEditRule()
- {
- InitializeComponent();
- }
- private void FormEditRule_Load(object sender, EventArgs e)
- {
- InitControls();
- }
- /// <summary>
- /// 初始化控件
- /// </summary>
- private void InitControls()
- {
- //下拉框--类型
- bsType.DataSource = SysEnvironment.dirType;
- this.lookUp_TypeVar1.Properties.DataSource = bsType;
- this.lookUp_TypeVar1.Properties.ValueMember = "Key";
- this.lookUp_TypeVar1.Properties.DisplayMember = "Value";
- this.lookUp_TypeVar1.Properties.NullText = "请您选择";
- //this.lookUp_TypeVar1.Enabled = false;
- //下拉框--查询条件
- bsQuery.DataSource = SysEnvironment.dirRuleValueQuery;
- this.lookUp_QueryVar1.Properties.DataSource = bsQuery;
- this.lookUp_QueryVar1.Properties.ValueMember = "Key";
- this.lookUp_QueryVar1.Properties.DisplayMember = "Value";
- this.lookUp_QueryVar1.Properties.NullText = "请您选择";
- if (rule != null)
- {
- this.txt_RuleName.Text = rule.RuleName;
- DataTable dt = BLLFactory<BlRule>.Instance.AnalyzeRuleValue(rule);
- if (dt == null || dt.Rows.Count <= 0)
- {
- return;
- }
- count = dt.Rows.Count;
- for (int i = 1; i < count; i++)
- {
- DrawControls(i);
- }
- this.textEditName.Text = dt.Rows[0]["Name"].ToString();
- this.lookUp_TypeVar1.EditValue = Convert.ToInt32(dt.Rows[0]["Type"]);
- this.txt_OrderVar1.Text = dt.Rows[0]["OrderNum"].ToString();
- this.lookUp_QueryVar1.EditValue = dt.Rows[0]["Query"].ToString();
- for (int i = 0; i < textList.Count; i++)
- {
- nameList[i].Text = dt.Rows[i+1]["Name"].ToString();
- typeList[i].EditValue = Convert.ToInt32(dt.Rows[i+1]["Type"]);
- textList[i].Text = dt.Rows[i+1]["OrderNum"].ToString();
- queryList[i].EditValue = dt.Rows[i+1]["Query"].ToString();
- }
- }
- }
- private void btn_Cancel_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void btn_Plus_Click(object sender, EventArgs e)
- {
- if (count > 21)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("已增加到20条不能继续增加。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- DrawControls(count);
- count++;
- }
- /// <summary>
- /// 动态创建控件
- /// </summary>
- /// <param name="i"></param>
- private void DrawControls(int i)
- {
- try
- {
- int y = 26 + 24*i;
- this.layoutControl1.BeginUpdate();
- DevExpress.XtraEditors.TextEdit textName = new TextEdit();
- textName.Location = new System.Drawing.Point(84, 38);
- textName.Name = "txtRulesName" + i.ToString();
- textName.Size = new System.Drawing.Size(186, 20);
- textName.StyleController = this.layoutControl1;
- this.layoutControl1.Controls.Add(textName);
- nameList.Add(textName);
- DevExpress.XtraLayout.LayoutControlItem layout1 = new DevExpress.XtraLayout.LayoutControlItem();
- layout1.Control = textName;
- layout1.Location = new System.Drawing.Point(0, y);
- //layout1.MaxSize = new System.Drawing.Size(150, 24);
- //layout1.MinSize = new System.Drawing.Size(80, 24);
- layout1.Name = "layout" + (i + 3).ToString();
- layout1.Size = new System.Drawing.Size(262, 24);
- layout1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
- layout1.Text = "变量名称(*):";
- layout1.TextAlignMode = TextAlignModeItem.AutoSize;
- layout1.TextSize = new System.Drawing.Size(69, 14);
- DevExpress.XtraEditors.LookUpEdit lookUp2 = new LookUpEdit();
- lookUp2.Location = new System.Drawing.Point(337, 38);
- lookUp2.Name = "lookUp_Type" + i.ToString();
- lookUp2.Size = new System.Drawing.Size(83, 20);
- lookUp2.StyleController = this.layoutControl1;
- lookUp2.Properties.DataSource = bsType;
- lookUp2.Properties.ValueMember = "Key";
- lookUp2.Properties.DisplayMember = "Value";
- lookUp2.Properties.NullText = "请您选择";
- //lookUp2.Enabled = false;
- this.layoutControl1.Controls.Add(lookUp2);
- typeList.Add(lookUp2);
- DevExpress.XtraLayout.LayoutControlItem layout2 = new DevExpress.XtraLayout.LayoutControlItem();
- layout2.Control = lookUp2;
- layout2.Location = new System.Drawing.Point(262, y);
- layout2.MaxSize = new System.Drawing.Size(150, 24);
- layout2.MinSize = new System.Drawing.Size(118, 24);
- layout2.Name = "layout" + (i + 2).ToString();
- layout2.Size = new System.Drawing.Size(150, 24);
- layout2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
- layout2.Text = "变量类型:";
- layout2.TextAlignMode = DevExpress.XtraLayout.TextAlignModeItem.AutoSize;
- layout2.TextSize = new System.Drawing.Size(60, 14);
- layout2.TextToControlDistance = 5;
- DevExpress.XtraEditors.TextEdit text = new TextEdit();
- text.Location = new System.Drawing.Point(63, 38);
- text.Name = "txt_OrderNum" + i.ToString();
- text.Size = new System.Drawing.Size(95, 20);
- text.StyleController = this.layoutControl1;
- this.layoutControl1.Controls.Add(text);
- textList.Add(text);
- DevExpress.XtraLayout.LayoutControlItem layout3 = new DevExpress.XtraLayout.LayoutControlItem();
- layout3.Control = text;
- layout3.Location = new System.Drawing.Point(450, y);
- layout3.MaxSize = new System.Drawing.Size(150, 24);
- layout3.MinSize = new System.Drawing.Size(80, 24);
- layout3.Name = "layout" + (i + 3).ToString();
- layout3.Size = new System.Drawing.Size(150, 24);
- layout3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
- layout3.Text = "变量序号:";
- layout3.TextAlignMode = TextAlignModeItem.AutoSize;
- layout3.TextSize = new System.Drawing.Size(60, 14);
- DevExpress.XtraEditors.LookUpEdit lookUp4 = new LookUpEdit();
- lookUp4.Location = new System.Drawing.Point(699, 38);
- lookUp4.Name = "lookUp_Query" + i.ToString();
- lookUp4.Size = new System.Drawing.Size(79, 20);
- lookUp4.StyleController = this.layoutControl1;
- lookUp4.Properties.DataSource = bsQuery;
- lookUp4.Properties.ValueMember = "Key";
- lookUp4.Properties.DisplayMember = "Value";
- lookUp4.Properties.NullText = "请您选择";
- this.layoutControl1.Controls.Add(lookUp4);
- queryList.Add(lookUp4);
- DevExpress.XtraLayout.LayoutControlItem layout4 = new DevExpress.XtraLayout.LayoutControlItem();
- layout4.Control = lookUp4;
- layout4.Location = new System.Drawing.Point(600, y);
- layout4.MaxSize = new System.Drawing.Size(170, 24);
- layout4.MinSize = new System.Drawing.Size(80, 24);
- layout4.Name = "layout" + (i + 4).ToString();
- layout4.Size = new System.Drawing.Size(170, 24);
- layout4.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
- layout4.Text = "作为查询条件:";
- layout4.TextSize = new System.Drawing.Size(84, 14);
- this.layoutControlGroup1.Items.AddRange(new DevExpress.XtraLayout.BaseLayoutItem[] { layout1,layout2,layout3,layout4});
- this.layoutControl1.EndUpdate();
- }
- catch (Exception ex)
- {
- LogHelper.log.Error(string.Format("动态创建控件出现错误:{0}", ex));
- }
- }
- private void btn_Minus_Click(object sender, EventArgs e)
- {
- if (count <= 1)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("已减少到1条不能继续减少。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- RemoveControls();
- count--;
- }
- /// <summary>
- /// 动态移除控件
- /// </summary>
- private void RemoveControls()
- {
- try
- {
- this.layoutControl1.BeginUpdate();
- for (int i = 0; i < 4; i++)
- {
- int numItem = this.layoutControlGroup1.Items.Count;
- int numControls = this.layoutControl1.Controls.Count;
- this.layoutControlGroup1.Items.RemoveAt(numItem - 1);
- this.layoutControl1.Controls.RemoveAt(numControls - 1);
- }
- textList.RemoveAt(textList.Count -1);
- nameList.RemoveAt(nameList.Count - 1);
- typeList.RemoveAt(typeList.Count - 1);
- queryList.RemoveAt(queryList.Count - 1);
- //IdList.RemoveAt(IdList.Count -1);
- this.layoutControl1.EndUpdate();
- }
- catch (Exception ex)
- {
- LogHelper.log.Error(string.Format("动态移除控件出现错误:{0}", ex));
- }
- }
- private void btn_Add_Click(object sender, EventArgs e)
- {
- try
- {
- List<string> nameText = new List<string>();
- nameText.Add(this.textEditName.Text);
- List<string> orderText = new List<string>();
- orderText.Add(txt_OrderVar1.Text);
- if (string.IsNullOrEmpty(this.txt_RuleName.Text)|| string.IsNullOrEmpty(this.textEditName.Text) || lookUp_TypeVar1.Text == "请您选择" || lookUp_QueryVar1.Text == "请您选择" || string.IsNullOrEmpty(txt_OrderVar1.Text))
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("请将信息填写完整。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- for (int i = 0; i < textList.Count; i++)
- {
- if (string.IsNullOrEmpty(nameList[i].Text)||string.IsNullOrEmpty(textList[i].Text) || typeList[i].Text == "请您选择" || queryList[i].Text == "请您选择" )
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("请将信息填写完整。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- orderText.Add(textList[i].Text);
- nameText.Add(nameList[i].Text);
- }
- //判断是否存在相同的变量ID和序号
- //HashSet<string> idSet = new HashSet<string>();
- HashSet<string> orderSet = new HashSet<string>();
- foreach (string str in orderText)
- {
- bool bOk = orderSet.Add(str);
- if (!bOk)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("存在相同的序号,请修改。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- }
- DataTable dt = new DataTable();
- //dt.Columns.Add("ID", typeof(string));
- dt.Columns.Add("Name", typeof(string));
- dt.Columns.Add("Type", typeof(string));
- dt.Columns.Add("OrderNum", typeof(string));
- dt.Columns.Add("Query", typeof(string));
- dt.Rows.Add(textEditName.Text, lookUp_TypeVar1.EditValue.ToString(), txt_OrderVar1.Text, lookUp_QueryVar1.Text);
- for (int i = 0; i < textList.Count; i++)
- {
- string name = nameList[i].Text;
- string type = typeList[i].EditValue.ToString();
- string orderNum = textList[i].Text;
- string query = queryList[i].Text;
- dt.Rows.Add(name, type, orderNum, query);
- }
- //将dataTable中的数据按照规则组合成数据表rules中的rule_value字段值
- string ruleValue = BLLFactory<BlRule>.Instance.FormRuleValue(dt);
- bool flag = false;
- if (rule == null)
- {
- MoRule mo = new MoRule { RuleName = txt_RuleName.Text, RuleValue = ruleValue };
- //插入数据库
- flag = BLLFactory<BlRule>.Instance.Insert(mo);
- }
- else
- {
- rule.RuleName = this.txt_RuleName.Text;
- rule.RuleValue = ruleValue;
- //修改数据库
- flag = BLLFactory<BlRule>.Instance.Update(rule,rule.RuleId);
- }
- if (flag)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("保存成功。", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
- saveData();
- this.Close();
- }
- else
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("保存失败。", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- }
- catch (Exception ex)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("保存失败。", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
- LogHelper.log.Error(string.Format("插入或修改数据库rules出现错误:{0}", ex));
- }
- }
- private void lookUp_EditValueChanged(object sender, EventArgs e)
- {
- LookUpEdit lookUpId = (LookUpEdit)sender; //点击的变量ID控件
- string name = lookUpId.Name.Trim();
- string i = name.Substring(name.Length-1,1);
- //变量名称的控件
- Control control = this.Controls.Find("lookUp_Name"+i, true)[0];
- if (control == null)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("选择失败,控件发生错误。", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- ((LookUpEdit)control).EditValue = lookUpId.EditValue;
- //变量类型的控件
- control = this.Controls.Find("lookUp_Type" + i, true)[0];
- if (control == null)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("选择失败,控件发生错误。", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- DataColumn[] clos = new DataColumn[1]; //设置varName主键
- clos[0] = dtRule.Columns["DATA_POINT_ID"];
- dtRule.PrimaryKey = clos;
- DataRow row = dtRule.Rows.Find(lookUpId.EditValue);
- int type = Convert.ToInt32(row["DATA_POINT_TYPE"]);
- ((LookUpEdit)control).EditValue = type;
- }
- }
- }
|