采集策略.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.Data;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using UAS_LabelMachine.Entity;
  6. using UAS_LabelMachine.PublicMethod;
  7. namespace UAS_LabelMachine
  8. {
  9. public partial class 采集策略 : Form
  10. {
  11. AutoSizeFormClass asc = new AutoSizeFormClass();
  12. DataHelper dh;
  13. StringBuilder sql = new StringBuilder();
  14. DataTable dt;
  15. int ItemNum = 5;
  16. public 采集策略()
  17. {
  18. InitializeComponent();
  19. si_type1.CheckedChanged += CheckChanged;
  20. si_type3.CheckedChanged += CheckChanged;
  21. si_type4.CheckedChanged += CheckChanged;
  22. si_type2.CheckedChanged += CheckChanged;
  23. si_type5.CheckedChanged += CheckChanged;
  24. }
  25. public 采集策略(string SG_CODE)
  26. {
  27. InitializeComponent();
  28. sg_code.Text = SG_CODE;
  29. si_type1.CheckedChanged += CheckChanged;
  30. si_type3.CheckedChanged += CheckChanged;
  31. si_type4.CheckedChanged += CheckChanged;
  32. si_type2.CheckedChanged += CheckChanged;
  33. si_type5.CheckedChanged += CheckChanged;
  34. }
  35. private void 采集策略_Load(object sender, EventArgs e)
  36. {
  37. asc.controllInitializeSize(this);
  38. dh = SystemInf.dh;
  39. pb_name.FormName = Name;
  40. pb_name.SetValueField = new string[] { "pb_name" };
  41. pb_name.TableName = "productbrand";
  42. pb_name.SelectField = "pb_name # 品牌名称,pb_remark # 备注";
  43. si_kind1.Text = "全部";
  44. si_kind2.Text = "全部";
  45. si_kind3.Text = "全部";
  46. si_kind4.Text = "全部";
  47. si_kind5.Text = "全部";
  48. if (sg_code.Text != "")
  49. {
  50. KeyEventArgs e2 = new KeyEventArgs(Keys.Enter);
  51. sg_code_KeyDown(sender, e2);
  52. }
  53. Width = Width + 1;
  54. }
  55. private void 采集策略_SizeChanged(object sender, EventArgs e)
  56. {
  57. asc.controlAutoSize(this);
  58. }
  59. private void Save_Click(object sender, EventArgs e)
  60. {
  61. if (sg_code.Text == "")
  62. {
  63. BaseUtil.ShowError("策略编号不能为空");
  64. }
  65. //如果是Enter获取的数据之后则这个隐藏字段的值不为空
  66. if (sg_id.Text != "")
  67. {
  68. sql.Clear();
  69. sql.Append("update scangroup set sg_name='" + sg_name.Text + "',sg_code='" + sg_code.Text + "',sg_brand='" + pb_name.Text + "',");
  70. sql.Append("sg_separator=:sg_separator where sg_id=" + sg_id.Text);
  71. dh.ExecuteSql(sql.ToString(), "update", new string[] { sg_separator.Text });
  72. dh.ExecuteSql("delete from scanitem where si_sgid=" + sg_id.Text, "delete");
  73. for (int i = 1; i <= ItemNum; i++)
  74. {
  75. //保存四条记录,通过si_enable字段来判断是否启用
  76. int Enable = 0;
  77. if ((Controls["si_type" + i] as CheckBox).Checked)
  78. {
  79. Enable = 1;
  80. }
  81. string si_id = dh.GetSEQ("scanitem_seq");
  82. sql.Clear();
  83. sql.Append("insert into scanitem(si_id,si_sgid,si_detno,si_item,si_kind,si_indexstring,si_index,si_expressionitem,si_expression,si_enable,si_innerdetno,si_length) values(" + si_id + "," + sg_id.Text);
  84. sql.Append(",'" + Controls["si_detno" + i].Text + "','" + Controls["si_item" + i].Text + "','" + Controls["si_kind" + i].Text + "',");
  85. sql.Append("'" + Controls["si_indexstring" + i].Text + "','" + Controls["si_index" + i].Text + "','" + Controls["si_expressionitem" + i].Text + "',:si_expression," + Enable + ",'" + Controls["si_innerdetno" + i].Text + "','" + Controls["si_length" + i].Text + "')");
  86. dh.ExecuteSql(sql.ToString(), "insert", new string[] { Controls["si_expression" + i].Text });
  87. Controls["si_id" + i].Text = si_id;
  88. }
  89. MessageBox.Show("保存成功");
  90. }
  91. else
  92. {
  93. //判断策略编号是否存在
  94. if (!dh.CheckExist("scangroup", "sg_code='" + sg_code.Text + "'"))
  95. {
  96. //如果是新增的从序列获取主键的ID
  97. string sg_id1 = dh.GetSEQ("scangroup_seq");
  98. sql.Clear();
  99. sql.Append("insert into scangroup (sg_id,sg_code,sg_name,sg_brand,sg_separator) values(" + sg_id1 + ",'" + sg_code.Text + "','" + sg_name.Text + "',");
  100. sql.Append("'" + pb_name.Text + "',:sg_separator)");
  101. dh.ExecuteSql(sql.ToString(), "insert", new string[] { sg_separator.Text });
  102. //遍历需要采集的字段
  103. for (int i = 1; i <= ItemNum; i++)
  104. {
  105. //保存四条记录,通过si_enable字段来判断是否启用
  106. int Enable = 0;
  107. if ((Controls["si_type" + i] as CheckBox).Checked)
  108. {
  109. Enable = 1;
  110. }
  111. string si_id = dh.GetSEQ("scanitem_seq");
  112. sql.Clear();
  113. sql.Append("insert into scanitem(si_id,si_sgid,si_detno,si_item,si_kind,si_indexstring,si_index,si_expressionitem,si_expression,si_enable,si_innerdetno,si_length) values(" + si_id + "," + sg_id1);
  114. sql.Append(",'" + Controls["si_detno" + i].Text + "','" + Controls["si_item" + i].Text + "','" + Controls["si_kind" + i].Text + "',");
  115. sql.Append("'" + Controls["si_indexstring" + i].Text + "','" + Controls["si_index" + i].Text + "','" + Controls["si_expressionitem" + i].Text + "',:si_expression," + Enable + ",'" + Controls["si_innerdetno" + i].Text + "','" + Controls["si_length" + i].Text + "')");
  116. dh.ExecuteSql(sql.ToString(), "insert", new string[] { Controls["si_expression" + i].Text });
  117. Controls["si_id" + i].Text = si_id;
  118. }
  119. MessageBox.Show("保存成功");
  120. //保存成功后将值更新回界面,再次点击保存就是更新操作
  121. sg_id.Text = sg_id1;
  122. }
  123. else
  124. {
  125. MessageBox.Show("策略编号" + sg_code.Text + "已存在!");
  126. }
  127. }
  128. }
  129. private void Delete_Click(object sender, EventArgs e)
  130. {
  131. if (sg_id.Text != "")
  132. {
  133. string close = MessageBox.Show(this.ParentForm, "是否删除此策略", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  134. if (close.ToString() == "Yes")
  135. {
  136. dh.ExecuteSql("delete from scangroup where sg_id=" + sg_id.Text, "delete");
  137. dh.ExecuteSql("delete from scanitem where si_sgid=" + sg_id.Text, "delete");
  138. MessageBox.Show("删除成功");
  139. BaseUtil.CleanForm(this);
  140. }
  141. }
  142. }
  143. private void CheckChanged(object sender, EventArgs e)
  144. {
  145. CheckBox cb = sender as CheckBox;
  146. //用来表示当前的勾选框的所属的TextBox
  147. int i = 0;
  148. switch (cb.Text)
  149. {
  150. case "DateCode":
  151. i = 1;
  152. break;
  153. case "LotNo":
  154. i = 2;
  155. break;
  156. case "QTY":
  157. i = 3;
  158. break;
  159. case "MPN":
  160. i = 4;
  161. break;
  162. case "CPN":
  163. i = 5;
  164. break;
  165. default:
  166. break;
  167. }
  168. //通过勾选状态来控制TextBox是否启用
  169. if (cb.Checked)
  170. {
  171. Controls["si_detno" + i].Enabled = true;
  172. Controls["si_item" + i].Text = cb.Text;
  173. Controls["si_kind" + i].Enabled = true;
  174. Controls["si_expression" + i].Enabled = true;
  175. Controls["si_expressionitem" + i].Enabled = true;
  176. Controls["si_index" + i].Enabled = true;
  177. Controls["si_indexstring" + i].Enabled = true;
  178. Controls["si_length" + i].Enabled = true;
  179. Controls["si_innerdetno" + i].Enabled = true;
  180. }
  181. else
  182. {
  183. Controls["si_detno" + i].Enabled = false;
  184. Controls["si_item" + i].Enabled = false;
  185. Controls["si_kind" + i].Enabled = false;
  186. Controls["si_expression" + i].Enabled = false;
  187. Controls["si_expressionitem" + i].Enabled = false;
  188. Controls["si_index" + i].Enabled = false;
  189. Controls["si_indexstring" + i].Enabled = false;
  190. Controls["si_length" + i].Enabled = false;
  191. Controls["si_innerdetno" + i].Enabled = false;
  192. }
  193. }
  194. private void sg_code_KeyDown(object sender, KeyEventArgs e)
  195. {
  196. if (e.KeyCode == Keys.Enter)
  197. {
  198. dt = (DataTable)dh.ExecuteSql("select sg_id,sg_code,sg_name,sg_brand as pb_name,sg_separator from scangroup where sg_code='" + sg_code.Text + "'", "select");
  199. if (dt.Rows.Count > 0)
  200. {
  201. BaseUtil.SetFormValue(this.Controls, dt);
  202. dt = (DataTable)dh.ExecuteSql("select * from scanitem where si_sgid=" + sg_id.Text, "select");
  203. //通过匹配采集的字段名称给界面赋值
  204. for (int i = 0; i < dt.Rows.Count; i++)
  205. {
  206. for (int j = 1; j <= ItemNum; j++)
  207. {
  208. //如果Item和CheckBox的文字相等
  209. if (dt.Rows[i]["si_item"].ToString() == Controls["si_type" + j].Text)
  210. {
  211. //如果状态不为0则表示启用
  212. if (dt.Rows[i]["si_enable"].ToString() != "0")
  213. (Controls["si_type" + j] as CheckBox).CheckState = CheckState.Checked;
  214. else
  215. (Controls["si_type" + j] as CheckBox).CheckState = CheckState.Unchecked;
  216. Controls["si_id" + j].Text = dt.Rows[i]["si_id"].ToString();
  217. Controls["si_detno" + j].Text = dt.Rows[i]["si_detno"].ToString();
  218. Controls["si_kind" + j].Text = dt.Rows[i]["si_kind"].ToString();
  219. Controls["si_indexstring" + j].Text = dt.Rows[i]["si_indexstring"].ToString();
  220. Controls["si_index" + j].Text = dt.Rows[i]["si_index"].ToString();
  221. Controls["si_item" + j].Text = dt.Rows[i]["si_item"].ToString();
  222. Controls["si_kind" + j].Text = dt.Rows[i]["si_kind"].ToString();
  223. Controls["si_length" + j].Text = dt.Rows[i]["si_length"].ToString();
  224. Controls["si_innerdetno" + j].Text = dt.Rows[i]["si_innerdetno"].ToString();
  225. Controls["si_expression" + j].Text = dt.Rows[i]["si_expression"].ToString();
  226. Controls["si_expressionitem" + j].Text = dt.Rows[i]["si_expressionitem"].ToString();
  227. }
  228. }
  229. }
  230. }
  231. else
  232. MessageBox.Show("不存在编号为" + sg_code.Text + "的采集策略");
  233. }
  234. }
  235. /// <summary>
  236. /// 清除界面的信息
  237. /// </summary>
  238. /// <param name="sender"></param>
  239. /// <param name="e"></param>
  240. private void Add_Click(object sender, EventArgs e)
  241. {
  242. BaseUtil.CleanForm(this);
  243. for (int i = 1; i < 6; i++)
  244. (Controls["si_type" + i] as CheckBox).Checked = false;
  245. }
  246. }
  247. }