采集策略.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. public 采集策略()
  16. {
  17. InitializeComponent();
  18. si_type1.CheckedChanged += CheckChanged;
  19. si_type3.CheckedChanged += CheckChanged;
  20. si_type4.CheckedChanged += CheckChanged;
  21. si_type2.CheckedChanged += CheckChanged;
  22. si_type5.CheckedChanged += CheckChanged;
  23. }
  24. public 采集策略(string SG_CODE)
  25. {
  26. InitializeComponent();
  27. sg_code.Text = SG_CODE;
  28. si_type1.CheckedChanged += CheckChanged;
  29. si_type3.CheckedChanged += CheckChanged;
  30. si_type4.CheckedChanged += CheckChanged;
  31. si_type2.CheckedChanged += CheckChanged;
  32. si_type5.CheckedChanged += CheckChanged;
  33. }
  34. private void 采集策略_Load(object sender, EventArgs e)
  35. {
  36. asc.controllInitializeSize(this);
  37. dh = SystemInf.dh;
  38. pb_name.FormName = Name;
  39. pb_name.SetValueField = new string[] { "pb_name" };
  40. pb_name.TableName = "productbrand";
  41. pb_name.SelectField = "pb_name # 品牌名称,pb_remark # 备注";
  42. si_kind1.Text = "全部";
  43. si_kind2.Text = "全部";
  44. si_kind3.Text = "全部";
  45. si_kind4.Text = "全部";
  46. si_kind5.Text = "全部";
  47. if (sg_code.Text != "")
  48. {
  49. KeyEventArgs e2 = new KeyEventArgs(Keys.Enter);
  50. sg_code_KeyDown(sender, e2);
  51. }
  52. Width = Width + 1;
  53. }
  54. private void 采集策略_SizeChanged(object sender, EventArgs e)
  55. {
  56. asc.controlAutoSize(this);
  57. }
  58. private void Save_Click(object sender, EventArgs e)
  59. {
  60. if (sg_code.Text == "")
  61. {
  62. BaseUtil.ShowError("策略编号不能为空");
  63. }
  64. //如果是Enter获取的数据之后则这个隐藏字段的值不为空
  65. if (sg_id.Text != "")
  66. {
  67. sql.Clear();
  68. sql.Append("update scangroup set sg_name='" + sg_name.Text + "',sg_code='" + sg_code.Text + "',sg_brand='" + pb_name.Text + "',");
  69. sql.Append("sg_separator=:sg_separator where sg_id=" + sg_id.Text);
  70. dh.ExecuteSql(sql.ToString(), "update", new string[] { sg_separator.Text });
  71. for (int i = 1; i < 6; i++)
  72. {
  73. //如果从表的ID不为空则表示此条记录已存在需要更新
  74. if (Controls["si_id" + i].Text != "")
  75. {
  76. int Enable = 0;
  77. if ((Controls["si_type" + i] as CheckBox).Checked)
  78. {
  79. Enable = 1;
  80. }
  81. sql.Clear();
  82. sql.Append("update scanitem set si_innerdetno='" + Controls["si_innerdetno" + i].Text + "',si_item='" + Controls["si_item" + i].Text + "',si_detno='" + Controls["si_detno" + i].Text + "',");
  83. sql.Append("si_index='" + Controls["si_index" + i].Text + "',si_indexstring='" + Controls["si_indexstring" + i].Text + "',");
  84. sql.Append("si_kind='" + Controls["si_kind" + i].Text + "', si_length='" + Controls["si_length" + i].Text + "',si_expressionitem='" + Controls["si_expressionitem" + i].Text + "',");
  85. sql.Append("si_expression=:si_expression,si_enable=" + Enable + " where si_id=" + Controls["si_id" + i].Text);
  86. dh.ExecuteSql(sql.ToString(), "update", new string[] { Controls["si_expression" + i].Text });
  87. }
  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 < 6; 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) 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 + "')");
  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 "品牌":
  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 < 6; 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. }