FormEditRule.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using DevExpress.XtraLayout;
  12. using ProjectBase.Data.Logs;
  13. using SIMDP.BLL;
  14. using SIMDP.Model;
  15. using SIMDP.Util;
  16. using ProjectBase.Data.BaseDAL;
  17. namespace SIMDP
  18. {
  19. public partial class FormEditRule : DevExpress.XtraEditors.XtraForm
  20. {
  21. private DataTable dtRule = new DataTable(); //下拉框--ID和名称绑定的数据源
  22. private BindingSource bsType = new BindingSource(); //下拉框--类型绑定的数据源
  23. private BindingSource bsQuery = new BindingSource(); //下拉框--查询条件绑定的数据源
  24. private List<TextEdit> nameList = new List<TextEdit>(); //动态生成的textEdit,用来存放变量名称
  25. private List<TextEdit> textList = new List<TextEdit>(); //动态生成的textEdit,用来存放变量序号
  26. private List<LookUpEdit> typeList = new List<LookUpEdit>(); //动态生成的lookupEdit,用来存放变量类型
  27. private List<LookUpEdit> queryList = new List<LookUpEdit>(); //动态生成的lookupEdit,用来存放变量是否作为查询条件
  28. private int count = 1;
  29. /// <summary>
  30. /// 需要修改的规则对象
  31. /// </summary>
  32. public MoRule rule { get; set; }
  33. /// <summary>
  34. /// 定义委托
  35. /// </summary>
  36. public delegate void save();
  37. /// <summary>
  38. /// 定义事件,保存数据成功后刷新FormPLC中的数据
  39. /// </summary>
  40. public event save saveData;
  41. /// <summary>
  42. /// 构造函数
  43. /// </summary>
  44. public FormEditRule()
  45. {
  46. InitializeComponent();
  47. }
  48. private void FormEditRule_Load(object sender, EventArgs e)
  49. {
  50. InitControls();
  51. }
  52. /// <summary>
  53. /// 初始化控件
  54. /// </summary>
  55. private void InitControls()
  56. {
  57. //下拉框--类型
  58. bsType.DataSource = SysEnvironment.dirType;
  59. this.lookUp_TypeVar1.Properties.DataSource = bsType;
  60. this.lookUp_TypeVar1.Properties.ValueMember = "Key";
  61. this.lookUp_TypeVar1.Properties.DisplayMember = "Value";
  62. this.lookUp_TypeVar1.Properties.NullText = "请您选择";
  63. //this.lookUp_TypeVar1.Enabled = false;
  64. //下拉框--查询条件
  65. bsQuery.DataSource = SysEnvironment.dirRuleValueQuery;
  66. this.lookUp_QueryVar1.Properties.DataSource = bsQuery;
  67. this.lookUp_QueryVar1.Properties.ValueMember = "Key";
  68. this.lookUp_QueryVar1.Properties.DisplayMember = "Value";
  69. this.lookUp_QueryVar1.Properties.NullText = "请您选择";
  70. if (rule != null)
  71. {
  72. this.txt_RuleName.Text = rule.RuleName;
  73. DataTable dt = BLLFactory<BlRule>.Instance.AnalyzeRuleValue(rule);
  74. if (dt == null || dt.Rows.Count <= 0)
  75. {
  76. return;
  77. }
  78. count = dt.Rows.Count;
  79. for (int i = 1; i < count; i++)
  80. {
  81. DrawControls(i);
  82. }
  83. this.textEditName.Text = dt.Rows[0]["Name"].ToString();
  84. this.lookUp_TypeVar1.EditValue = Convert.ToInt32(dt.Rows[0]["Type"]);
  85. this.txt_OrderVar1.Text = dt.Rows[0]["OrderNum"].ToString();
  86. this.lookUp_QueryVar1.EditValue = dt.Rows[0]["Query"].ToString();
  87. for (int i = 0; i < textList.Count; i++)
  88. {
  89. nameList[i].Text = dt.Rows[i+1]["Name"].ToString();
  90. typeList[i].EditValue = Convert.ToInt32(dt.Rows[i+1]["Type"]);
  91. textList[i].Text = dt.Rows[i+1]["OrderNum"].ToString();
  92. queryList[i].EditValue = dt.Rows[i+1]["Query"].ToString();
  93. }
  94. }
  95. }
  96. private void btn_Cancel_Click(object sender, EventArgs e)
  97. {
  98. this.Close();
  99. }
  100. private void btn_Plus_Click(object sender, EventArgs e)
  101. {
  102. if (count > 21)
  103. {
  104. DevExpress.XtraEditors.XtraMessageBox.Show("已增加到20条不能继续增加。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  105. return;
  106. }
  107. DrawControls(count);
  108. count++;
  109. }
  110. /// <summary>
  111. /// 动态创建控件
  112. /// </summary>
  113. /// <param name="i"></param>
  114. private void DrawControls(int i)
  115. {
  116. try
  117. {
  118. int y = 26 + 24*i;
  119. this.layoutControl1.BeginUpdate();
  120. DevExpress.XtraEditors.TextEdit textName = new TextEdit();
  121. textName.Location = new System.Drawing.Point(84, 38);
  122. textName.Name = "txtRulesName" + i.ToString();
  123. textName.Size = new System.Drawing.Size(186, 20);
  124. textName.StyleController = this.layoutControl1;
  125. this.layoutControl1.Controls.Add(textName);
  126. nameList.Add(textName);
  127. DevExpress.XtraLayout.LayoutControlItem layout1 = new DevExpress.XtraLayout.LayoutControlItem();
  128. layout1.Control = textName;
  129. layout1.Location = new System.Drawing.Point(0, y);
  130. //layout1.MaxSize = new System.Drawing.Size(150, 24);
  131. //layout1.MinSize = new System.Drawing.Size(80, 24);
  132. layout1.Name = "layout" + (i + 3).ToString();
  133. layout1.Size = new System.Drawing.Size(262, 24);
  134. layout1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  135. layout1.Text = "变量名称(*):";
  136. layout1.TextAlignMode = TextAlignModeItem.AutoSize;
  137. layout1.TextSize = new System.Drawing.Size(69, 14);
  138. DevExpress.XtraEditors.LookUpEdit lookUp2 = new LookUpEdit();
  139. lookUp2.Location = new System.Drawing.Point(337, 38);
  140. lookUp2.Name = "lookUp_Type" + i.ToString();
  141. lookUp2.Size = new System.Drawing.Size(83, 20);
  142. lookUp2.StyleController = this.layoutControl1;
  143. lookUp2.Properties.DataSource = bsType;
  144. lookUp2.Properties.ValueMember = "Key";
  145. lookUp2.Properties.DisplayMember = "Value";
  146. lookUp2.Properties.NullText = "请您选择";
  147. //lookUp2.Enabled = false;
  148. this.layoutControl1.Controls.Add(lookUp2);
  149. typeList.Add(lookUp2);
  150. DevExpress.XtraLayout.LayoutControlItem layout2 = new DevExpress.XtraLayout.LayoutControlItem();
  151. layout2.Control = lookUp2;
  152. layout2.Location = new System.Drawing.Point(262, y);
  153. layout2.MaxSize = new System.Drawing.Size(150, 24);
  154. layout2.MinSize = new System.Drawing.Size(118, 24);
  155. layout2.Name = "layout" + (i + 2).ToString();
  156. layout2.Size = new System.Drawing.Size(150, 24);
  157. layout2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  158. layout2.Text = "变量类型:";
  159. layout2.TextAlignMode = DevExpress.XtraLayout.TextAlignModeItem.AutoSize;
  160. layout2.TextSize = new System.Drawing.Size(60, 14);
  161. layout2.TextToControlDistance = 5;
  162. DevExpress.XtraEditors.TextEdit text = new TextEdit();
  163. text.Location = new System.Drawing.Point(63, 38);
  164. text.Name = "txt_OrderNum" + i.ToString();
  165. text.Size = new System.Drawing.Size(95, 20);
  166. text.StyleController = this.layoutControl1;
  167. this.layoutControl1.Controls.Add(text);
  168. textList.Add(text);
  169. DevExpress.XtraLayout.LayoutControlItem layout3 = new DevExpress.XtraLayout.LayoutControlItem();
  170. layout3.Control = text;
  171. layout3.Location = new System.Drawing.Point(450, y);
  172. layout3.MaxSize = new System.Drawing.Size(150, 24);
  173. layout3.MinSize = new System.Drawing.Size(80, 24);
  174. layout3.Name = "layout" + (i + 3).ToString();
  175. layout3.Size = new System.Drawing.Size(150, 24);
  176. layout3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  177. layout3.Text = "变量序号:";
  178. layout3.TextAlignMode = TextAlignModeItem.AutoSize;
  179. layout3.TextSize = new System.Drawing.Size(60, 14);
  180. DevExpress.XtraEditors.LookUpEdit lookUp4 = new LookUpEdit();
  181. lookUp4.Location = new System.Drawing.Point(699, 38);
  182. lookUp4.Name = "lookUp_Query" + i.ToString();
  183. lookUp4.Size = new System.Drawing.Size(79, 20);
  184. lookUp4.StyleController = this.layoutControl1;
  185. lookUp4.Properties.DataSource = bsQuery;
  186. lookUp4.Properties.ValueMember = "Key";
  187. lookUp4.Properties.DisplayMember = "Value";
  188. lookUp4.Properties.NullText = "请您选择";
  189. this.layoutControl1.Controls.Add(lookUp4);
  190. queryList.Add(lookUp4);
  191. DevExpress.XtraLayout.LayoutControlItem layout4 = new DevExpress.XtraLayout.LayoutControlItem();
  192. layout4.Control = lookUp4;
  193. layout4.Location = new System.Drawing.Point(600, y);
  194. layout4.MaxSize = new System.Drawing.Size(170, 24);
  195. layout4.MinSize = new System.Drawing.Size(80, 24);
  196. layout4.Name = "layout" + (i + 4).ToString();
  197. layout4.Size = new System.Drawing.Size(170, 24);
  198. layout4.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
  199. layout4.Text = "作为查询条件:";
  200. layout4.TextSize = new System.Drawing.Size(84, 14);
  201. this.layoutControlGroup1.Items.AddRange(new DevExpress.XtraLayout.BaseLayoutItem[] { layout1,layout2,layout3,layout4});
  202. this.layoutControl1.EndUpdate();
  203. }
  204. catch (Exception ex)
  205. {
  206. LogHelper.log.Error(string.Format("动态创建控件出现错误:{0}", ex));
  207. }
  208. }
  209. private void btn_Minus_Click(object sender, EventArgs e)
  210. {
  211. if (count <= 1)
  212. {
  213. DevExpress.XtraEditors.XtraMessageBox.Show("已减少到1条不能继续减少。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  214. return;
  215. }
  216. RemoveControls();
  217. count--;
  218. }
  219. /// <summary>
  220. /// 动态移除控件
  221. /// </summary>
  222. private void RemoveControls()
  223. {
  224. try
  225. {
  226. this.layoutControl1.BeginUpdate();
  227. for (int i = 0; i < 4; i++)
  228. {
  229. int numItem = this.layoutControlGroup1.Items.Count;
  230. int numControls = this.layoutControl1.Controls.Count;
  231. this.layoutControlGroup1.Items.RemoveAt(numItem - 1);
  232. this.layoutControl1.Controls.RemoveAt(numControls - 1);
  233. }
  234. textList.RemoveAt(textList.Count -1);
  235. nameList.RemoveAt(nameList.Count - 1);
  236. typeList.RemoveAt(typeList.Count - 1);
  237. queryList.RemoveAt(queryList.Count - 1);
  238. //IdList.RemoveAt(IdList.Count -1);
  239. this.layoutControl1.EndUpdate();
  240. }
  241. catch (Exception ex)
  242. {
  243. LogHelper.log.Error(string.Format("动态移除控件出现错误:{0}", ex));
  244. }
  245. }
  246. private void btn_Add_Click(object sender, EventArgs e)
  247. {
  248. try
  249. {
  250. List<string> nameText = new List<string>();
  251. nameText.Add(this.textEditName.Text);
  252. List<string> orderText = new List<string>();
  253. orderText.Add(txt_OrderVar1.Text);
  254. if (string.IsNullOrEmpty(this.txt_RuleName.Text)|| string.IsNullOrEmpty(this.textEditName.Text) || lookUp_TypeVar1.Text == "请您选择" || lookUp_QueryVar1.Text == "请您选择" || string.IsNullOrEmpty(txt_OrderVar1.Text))
  255. {
  256. DevExpress.XtraEditors.XtraMessageBox.Show("请将信息填写完整。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  257. return;
  258. }
  259. for (int i = 0; i < textList.Count; i++)
  260. {
  261. if (string.IsNullOrEmpty(nameList[i].Text)||string.IsNullOrEmpty(textList[i].Text) || typeList[i].Text == "请您选择" || queryList[i].Text == "请您选择" )
  262. {
  263. DevExpress.XtraEditors.XtraMessageBox.Show("请将信息填写完整。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  264. return;
  265. }
  266. orderText.Add(textList[i].Text);
  267. nameText.Add(nameList[i].Text);
  268. }
  269. //判断是否存在相同的变量ID和序号
  270. //HashSet<string> idSet = new HashSet<string>();
  271. HashSet<string> orderSet = new HashSet<string>();
  272. foreach (string str in orderText)
  273. {
  274. bool bOk = orderSet.Add(str);
  275. if (!bOk)
  276. {
  277. DevExpress.XtraEditors.XtraMessageBox.Show("存在相同的序号,请修改。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  278. return;
  279. }
  280. }
  281. DataTable dt = new DataTable();
  282. //dt.Columns.Add("ID", typeof(string));
  283. dt.Columns.Add("Name", typeof(string));
  284. dt.Columns.Add("Type", typeof(string));
  285. dt.Columns.Add("OrderNum", typeof(string));
  286. dt.Columns.Add("Query", typeof(string));
  287. dt.Rows.Add(textEditName.Text, lookUp_TypeVar1.EditValue.ToString(), txt_OrderVar1.Text, lookUp_QueryVar1.Text);
  288. for (int i = 0; i < textList.Count; i++)
  289. {
  290. string name = nameList[i].Text;
  291. string type = typeList[i].EditValue.ToString();
  292. string orderNum = textList[i].Text;
  293. string query = queryList[i].Text;
  294. dt.Rows.Add(name, type, orderNum, query);
  295. }
  296. //将dataTable中的数据按照规则组合成数据表rules中的rule_value字段值
  297. string ruleValue = BLLFactory<BlRule>.Instance.FormRuleValue(dt);
  298. bool flag = false;
  299. if (rule == null)
  300. {
  301. MoRule mo = new MoRule { RuleName = txt_RuleName.Text, RuleValue = ruleValue };
  302. //插入数据库
  303. flag = BLLFactory<BlRule>.Instance.Insert(mo);
  304. }
  305. else
  306. {
  307. rule.RuleName = this.txt_RuleName.Text;
  308. rule.RuleValue = ruleValue;
  309. //修改数据库
  310. flag = BLLFactory<BlRule>.Instance.Update(rule,rule.RuleId);
  311. }
  312. if (flag)
  313. {
  314. DevExpress.XtraEditors.XtraMessageBox.Show("保存成功。", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  315. saveData();
  316. this.Close();
  317. }
  318. else
  319. {
  320. DevExpress.XtraEditors.XtraMessageBox.Show("保存失败。", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  321. return;
  322. }
  323. }
  324. catch (Exception ex)
  325. {
  326. DevExpress.XtraEditors.XtraMessageBox.Show("保存失败。", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  327. LogHelper.log.Error(string.Format("插入或修改数据库rules出现错误:{0}", ex));
  328. }
  329. }
  330. private void lookUp_EditValueChanged(object sender, EventArgs e)
  331. {
  332. LookUpEdit lookUpId = (LookUpEdit)sender; //点击的变量ID控件
  333. string name = lookUpId.Name.Trim();
  334. string i = name.Substring(name.Length-1,1);
  335. //变量名称的控件
  336. Control control = this.Controls.Find("lookUp_Name"+i, true)[0];
  337. if (control == null)
  338. {
  339. DevExpress.XtraEditors.XtraMessageBox.Show("选择失败,控件发生错误。", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  340. return;
  341. }
  342. ((LookUpEdit)control).EditValue = lookUpId.EditValue;
  343. //变量类型的控件
  344. control = this.Controls.Find("lookUp_Type" + i, true)[0];
  345. if (control == null)
  346. {
  347. DevExpress.XtraEditors.XtraMessageBox.Show("选择失败,控件发生错误。", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  348. return;
  349. }
  350. DataColumn[] clos = new DataColumn[1]; //设置varName主键
  351. clos[0] = dtRule.Columns["DATA_POINT_ID"];
  352. dtRule.PrimaryKey = clos;
  353. DataRow row = dtRule.Rows.Find(lookUpId.EditValue);
  354. int type = Convert.ToInt32(row["DATA_POINT_TYPE"]);
  355. ((LookUpEdit)control).EditValue = type;
  356. }
  357. }
  358. }