using System; using System.Data; using System.Text; using System.Windows.Forms; using UAS_LabelMachine.Entity; using UAS_LabelMachine.PublicMethod; namespace UAS_LabelMachine { public partial class 采集策略 : Form { AutoSizeFormClass asc = new AutoSizeFormClass(); DataHelper dh; StringBuilder sql = new StringBuilder(); DataTable dt; int ItemNum = 5; public 采集策略() { InitializeComponent(); si_type1.CheckedChanged += CheckChanged; si_type3.CheckedChanged += CheckChanged; si_type4.CheckedChanged += CheckChanged; si_type2.CheckedChanged += CheckChanged; si_type5.CheckedChanged += CheckChanged; } public 采集策略(string SG_CODE) { InitializeComponent(); sg_code.Text = SG_CODE; si_type1.CheckedChanged += CheckChanged; si_type3.CheckedChanged += CheckChanged; si_type4.CheckedChanged += CheckChanged; si_type2.CheckedChanged += CheckChanged; si_type5.CheckedChanged += CheckChanged; } private void 采集策略_Load(object sender, EventArgs e) { asc.controllInitializeSize(this); dh = SystemInf.dh; pb_name.FormName = Name; pb_name.SetValueField = new string[] { "pb_name" }; pb_name.TableName = "productbrand"; pb_name.SelectField = "pb_name # 品牌名称,pb_remark # 备注"; si_kind1.Text = "全部"; si_kind2.Text = "全部"; si_kind3.Text = "全部"; si_kind4.Text = "全部"; si_kind5.Text = "全部"; if (sg_code.Text != "") { KeyEventArgs e2 = new KeyEventArgs(Keys.Enter); sg_code_KeyDown(sender, e2); } Width = Width + 1; } private void 采集策略_SizeChanged(object sender, EventArgs e) { asc.controlAutoSize(this); } private void Save_Click(object sender, EventArgs e) { if (sg_code.Text == "") { BaseUtil.ShowError("策略编号不能为空"); } //如果是Enter获取的数据之后则这个隐藏字段的值不为空 if (sg_id.Text != "") { sql.Clear(); sql.Append("update scangroup set sg_name='" + sg_name.Text + "',sg_code='" + sg_code.Text + "',sg_brand='" + pb_name.Text + "',"); sql.Append("sg_separator=:sg_separator where sg_id=" + sg_id.Text); dh.ExecuteSql(sql.ToString(), "update", new string[] { sg_separator.Text }); dh.ExecuteSql("delete from scanitem where si_sgid=" + sg_id.Text, "delete"); for (int i = 1; i <= ItemNum; i++) { //保存四条记录,通过si_enable字段来判断是否启用 int Enable = 0; if ((Controls["si_type" + i] as CheckBox).Checked) { Enable = 1; } string si_id = dh.GetSEQ("scanitem_seq"); sql.Clear(); 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); sql.Append(",'" + Controls["si_detno" + i].Text + "','" + Controls["si_item" + i].Text + "','" + Controls["si_kind" + i].Text + "',"); 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 + "')"); dh.ExecuteSql(sql.ToString(), "insert", new string[] { Controls["si_expression" + i].Text }); Controls["si_id" + i].Text = si_id; } MessageBox.Show("保存成功"); } else { //判断策略编号是否存在 if (!dh.CheckExist("scangroup", "sg_code='" + sg_code.Text + "'")) { //如果是新增的从序列获取主键的ID string sg_id1 = dh.GetSEQ("scangroup_seq"); sql.Clear(); sql.Append("insert into scangroup (sg_id,sg_code,sg_name,sg_brand,sg_separator) values(" + sg_id1 + ",'" + sg_code.Text + "','" + sg_name.Text + "',"); sql.Append("'" + pb_name.Text + "',:sg_separator)"); dh.ExecuteSql(sql.ToString(), "insert", new string[] { sg_separator.Text }); //遍历需要采集的字段 for (int i = 1; i <= ItemNum; i++) { //保存四条记录,通过si_enable字段来判断是否启用 int Enable = 0; if ((Controls["si_type" + i] as CheckBox).Checked) { Enable = 1; } string si_id = dh.GetSEQ("scanitem_seq"); sql.Clear(); 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); sql.Append(",'" + Controls["si_detno" + i].Text + "','" + Controls["si_item" + i].Text + "','" + Controls["si_kind" + i].Text + "',"); 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 + "')"); dh.ExecuteSql(sql.ToString(), "insert", new string[] { Controls["si_expression" + i].Text }); Controls["si_id" + i].Text = si_id; } MessageBox.Show("保存成功"); //保存成功后将值更新回界面,再次点击保存就是更新操作 sg_id.Text = sg_id1; } else { MessageBox.Show("策略编号" + sg_code.Text + "已存在!"); } } } private void Delete_Click(object sender, EventArgs e) { if (sg_id.Text != "") { string close = MessageBox.Show(this.ParentForm, "是否删除此策略", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString(); if (close.ToString() == "Yes") { dh.ExecuteSql("delete from scangroup where sg_id=" + sg_id.Text, "delete"); dh.ExecuteSql("delete from scanitem where si_sgid=" + sg_id.Text, "delete"); MessageBox.Show("删除成功"); BaseUtil.CleanForm(this); } } } private void CheckChanged(object sender, EventArgs e) { CheckBox cb = sender as CheckBox; //用来表示当前的勾选框的所属的TextBox int i = 0; switch (cb.Text) { case "DateCode": i = 1; break; case "LotNo": i = 2; break; case "QTY": i = 3; break; case "MPN": i = 4; break; case "CPN": i = 5; break; default: break; } //通过勾选状态来控制TextBox是否启用 if (cb.Checked) { Controls["si_detno" + i].Enabled = true; Controls["si_item" + i].Text = cb.Text; Controls["si_kind" + i].Enabled = true; Controls["si_expression" + i].Enabled = true; Controls["si_expressionitem" + i].Enabled = true; Controls["si_index" + i].Enabled = true; Controls["si_indexstring" + i].Enabled = true; Controls["si_length" + i].Enabled = true; Controls["si_innerdetno" + i].Enabled = true; } else { Controls["si_detno" + i].Enabled = false; Controls["si_item" + i].Enabled = false; Controls["si_kind" + i].Enabled = false; Controls["si_expression" + i].Enabled = false; Controls["si_expressionitem" + i].Enabled = false; Controls["si_index" + i].Enabled = false; Controls["si_indexstring" + i].Enabled = false; Controls["si_length" + i].Enabled = false; Controls["si_innerdetno" + i].Enabled = false; } } private void sg_code_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { 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"); if (dt.Rows.Count > 0) { BaseUtil.SetFormValue(this.Controls, dt); dt = (DataTable)dh.ExecuteSql("select * from scanitem where si_sgid=" + sg_id.Text, "select"); //通过匹配采集的字段名称给界面赋值 for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 1; j <= ItemNum; j++) { //如果Item和CheckBox的文字相等 if (dt.Rows[i]["si_item"].ToString() == Controls["si_type" + j].Text) { //如果状态不为0则表示启用 if (dt.Rows[i]["si_enable"].ToString() != "0") (Controls["si_type" + j] as CheckBox).CheckState = CheckState.Checked; else (Controls["si_type" + j] as CheckBox).CheckState = CheckState.Unchecked; Controls["si_id" + j].Text = dt.Rows[i]["si_id"].ToString(); Controls["si_detno" + j].Text = dt.Rows[i]["si_detno"].ToString(); Controls["si_kind" + j].Text = dt.Rows[i]["si_kind"].ToString(); Controls["si_indexstring" + j].Text = dt.Rows[i]["si_indexstring"].ToString(); Controls["si_index" + j].Text = dt.Rows[i]["si_index"].ToString(); Controls["si_item" + j].Text = dt.Rows[i]["si_item"].ToString(); Controls["si_kind" + j].Text = dt.Rows[i]["si_kind"].ToString(); Controls["si_length" + j].Text = dt.Rows[i]["si_length"].ToString(); Controls["si_innerdetno" + j].Text = dt.Rows[i]["si_innerdetno"].ToString(); Controls["si_expression" + j].Text = dt.Rows[i]["si_expression"].ToString(); Controls["si_expressionitem" + j].Text = dt.Rows[i]["si_expressionitem"].ToString(); } } } } else MessageBox.Show("不存在编号为" + sg_code.Text + "的采集策略"); } } /// /// 清除界面的信息 /// /// /// private void Add_Click(object sender, EventArgs e) { BaseUtil.CleanForm(this); for (int i = 1; i < 6; i++) (Controls["si_type" + i] as CheckBox).Checked = false; } } }