using System; using System.Data; using System.Text; using System.Windows.Forms; using UAS_MES_NEW.CustomControl.TextBoxWithIcon; using UAS_MES_NEW.DataOperate; using UAS_MES_NEW.Entity; using UAS_MES_NEW.PublicMethod; using System.Drawing; using System.Text.RegularExpressions; using System.Collections.Generic; using System.Threading; using UAS_MES_NEW.PublicForm; namespace UAS_MES_NEW.Make { public partial class Make_SMTBind : Form { string ErrorMessage = ""; List TSN = new List(); Dictionary CheckBarcode = new Dictionary(); DataHelper dh; LogStringBuilder sql = new LogStringBuilder(); DataTable DBFind; //保存ListB中的数据 DataTable dt1; DataTable dt = new DataTable(); DataTable dbfind; ExcelHandler eh; Thread thread; AutoSizeFormClass asc = new AutoSizeFormClass(); List CollectData = new List(); List CollectDataSonCode = new List(); List SPID = new List(); public Make_SMTBind() { InitializeComponent(); } private void Make_FeedingCollection_Load(object sender, EventArgs e) { asc.controllInitializeSize(this); LockMakeCode.GetMakeCodeCtl(ma_code); ma_code.SetLockCheckBox(LockMakeCode); //工单号放大镜配置 eh = new ExcelHandler(); ma_code.TableName = "make left join product on ma_prodcode=pr_code"; ma_code.SelectField = "ma_code # 工单号,ma_prodcode # 产品编号,ma_qty # 工单数量,ma_craftcode # 途程编号,pr_spec # 产品名称,ma_bomversion # Bom版本,ma_softversion # 软件版本,ma_ecncode # ECN号,pr_pcbacount # 拼版数"; ma_code.FormName = Name; ma_code.DBTitle = "工单查询"; ma_code.SetValueField = new string[] { "ma_code", "ma_prodcode", "ma_qty", "pr_spec", "ma_craftcode", "ma_bomversion", "ma_softversion", "ma_ecncode", "pr_pcbacount" }; ma_code.Condition = "ma_statuscode='STARTED'"; ma_code.DbChange += Ma_code_DBChange; code.GotFocus+= text_getfocus; code.Focus(); dh = SystemInf.dh; StepCount.StepCode = User.CurrentStepCode; StepCount.Source = User.UserSourceCode; StepCount.LineCode = User.UserLineCode; StepCount.Dh = dh; StepCount.Start(); } private void text_getfocus(object sender, EventArgs e) { code.ImeMode = ImeMode.Close; } private void Ma_code_DBChange(object sender, EventArgs e) { DBFind = ma_code.ReturnData; BaseUtil.SetFormValue(this.Controls, DBFind); } private void Make_FeedingCollection_SizeChanged(object sender, EventArgs e) { asc.controlAutoSize(this); } private void Clean_Click(object sender, EventArgs e) { OperateResult.Clear(); } private void Confirm_Click(object sender, EventArgs e) { KeyEventArgs e2 = new KeyEventArgs(Keys.Enter); code_KeyDown(sender, e2); } private void code_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { if (LogicHandler.CheckStepAttribute(Tag.ToString(), User.UserSourceCode, out ErrorMessage)) { if (Loading.Checked) { if (code.Text.Replace(" ", "") == ""||pr_pcbacount.Value == 0 || pr_pcbacount.Value == 1) { OperateResult.AppendText(">>采集数据与拼版数不可为空\n", Color.Red); return; } else { if (sn_code.Text == "") { if (ma_code.Text != "" && !LogicHandler.CheckSnRule(ma_code.Text, ma_prodcode.Text, code.Text, out ErrorMessage)) { OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, code); return; } int maqty = int.Parse(dh.getFieldDataByCondition("make", "ma_qty", "ma_code = '" + ma_code.Text + "'").ToString()); int nowqty = int.Parse(dh.getFieldDataByCondition("smtbind", "count(1) cn ", "sb_makecode = '" + ma_code.Text + "'").ToString()); if (nowqty+ pr_pcbacount.Value > maqty) { OperateResult.AppendText(">>已绑定数"+ nowqty + "+拼版数"+ pr_pcbacount.Value + "大于工单数"+ maqty + "不允许操作\n", Color.Red); return; } sql.Clear(); sql.Append("select rownum,sb_maincode,sb_makecode,sb_barcode,sb_prodcode,sb_indate from SMTBIND where sb_maincode=(select sb_maincode from SMTBIND "); sql.Append("where sb_barcode='" + code.Text + "')"); dt1 = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); if (dt1.Rows.Count > 0) { string sb_makecode = dt1.Rows[0]["sb_makecode"].ToString(); if (ma_code.Text != "" && ma_code.Text != sb_makecode) { OperateResult.AppendText(">>条码已绑定至工单" + sb_makecode + "\n", Color.Red, code); return; } ma_code.Text = dt1.Rows[0]["sb_makecode"].ToString(); LockMakeCode.Checked = true; ma_code_UserControlTextChanged(sender, e); BaseUtil.FillDgvWithDataTable(LabelDataGridView, dt1); if (dt1.Rows.Count < pr_pcbacount.Value) { sn_code.Text = dt1.Rows[0]["sb_maincode"].ToString(); } code.Text = ""; } else { if (ma_code.Text == "") { OperateResult.AppendText(">>请先选择工单号\n", Color.Red, code); return; } if (!LogicHandler.CheckSnRule(ma_code.Text, ma_prodcode.Text, code.Text, out ErrorMessage)) { OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, code); return; } sn_code.Text = code.Text; sql.Clear(); sql.Append("insert into smtbind(sb_id,sb_maincode,sb_barcode,sb_makecode,sb_prodcode,sb_inman,sb_indate,sb_status)"); sql.Append("values(smtbind_seq.nextval,'" + sn_code.Text + "','" + code.Text + "','" + ma_code.Text + "','" + ma_prodcode.Text + "',"); sql.Append("'" + User.UserCode + "',sysdate,0)"); dh.ExecuteSql(sql.GetString(), "insert"); RefreshData(); sql.Clear(); sql.Append("select rownum,sb_maincode,sb_barcode,sb_prodcode,sb_indate from SMTBIND where sb_maincode=(select sb_maincode from SMTBIND "); sql.Append("where sb_barcode='" + code.Text + "')"); dt1 = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); BaseUtil.FillDgvWithDataTable(LabelDataGridView, dt1); code.Text = ""; } } else { if (ma_code.Text == "") { OperateResult.AppendText(">>请先选择工单号\n", Color.Red, code); return; } if (!LogicHandler.CheckSnRule(ma_code.Text, ma_prodcode.Text, code.Text, out ErrorMessage)) { OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, code); return; } if (dh.CheckExist("smtbind", "sb_barcode='" + code.Text + "'")) { OperateResult.AppendText(">>条码" + code.Text + "已绑定\n", Color.Red, code); return; } sql.Clear(); sql.Append("insert into smtbind(sb_id,sb_maincode,sb_barcode,sb_makecode,sb_prodcode,sb_inman,sb_indate,sb_status)"); sql.Append("values(smtbind_seq.nextval,'" + sn_code.Text + "','" + code.Text + "','" + ma_code.Text + "','" + ma_prodcode.Text + "',"); sql.Append("'" + User.UserCode + "',sysdate,0)"); dh.ExecuteSql(sql.GetString(), "insert"); RefreshData(); sql.Clear(); sql.Append("select rownum,sb_maincode,sb_barcode,sb_prodcode,sb_indate from SMTBIND where sb_maincode=(select sb_maincode from SMTBIND "); sql.Append("where sb_barcode='" + code.Text + "')"); dt1 = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); BaseUtil.FillDgvWithDataTable(LabelDataGridView, dt1); if (LabelDataGridView.Rows.Count == pr_pcbacount.Value) { sn_code.Text = ""; } code.Text = ""; } } } //勾选了下料的时候 else if (UnLoading.Checked) { sql.Clear(); sql.Append("select rownum,sb_maincode,sb_makecode,sb_barcode,sb_prodcode,sb_indate from SMTBIND where sb_maincode=(select sb_maincode from SMTBIND "); sql.Append("where sb_barcode='" + code.Text + "')"); dt1 = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); if (dt1.Rows.Count > 0) { string sb_makecode = dt1.Rows[0]["sb_makecode"].ToString(); if (ma_code.Text != "" && ma_code.Text != sb_makecode) { OperateResult.AppendText(">>条码已绑定至工单" + sb_makecode + "\n", Color.Red, code); return; } ma_code.Text = dt1.Rows[0]["sb_makecode"].ToString(); LockMakeCode.Checked = true; ma_code_UserControlTextChanged(sender, e); BaseUtil.FillDgvWithDataTable(LabelDataGridView, dt1); string sb_maincode = dt1.Rows[0]["sb_maincode"].ToString(); //如果采集的是主条码则拆分所有的条码 if (sb_maincode == code.Text) { string closetab_confirm = MessageBox.Show(this.ParentForm, "确认拆解所有条码", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString(); if (closetab_confirm == "Yes") { dh.ExecuteSql("delete from smtbind where sb_maincode='" + code.Text + "'", "delete"); } } else { dh.ExecuteSql("delete from smtbind where sb_barcode='" + code.Text + "'", "delete"); } sql.Clear(); sql.Append("select rownum,sb_maincode,sb_barcode,sb_prodcode,sb_indate from SMTBIND where sb_maincode='" + sb_maincode + "'"); dt1 = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); BaseUtil.FillDgvWithDataTable(LabelDataGridView, dt1); sn_code.Text = ""; code.Text = ""; RefreshData(); } } } else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, code); } } private void ClearSn_code_Click(object sender, EventArgs e) { sn_code.Clear(); SPID.Clear(); TSN.Clear(); CheckBarcode.Clear(); CollectData.Clear(); CollectDataSonCode.Clear(); if (dt1 != null) BaseUtil.CleanDataTableData(dt1); BaseUtil.CleanDGVData(LabelDataGridView); OperateResult.AppendText(">>请采集序列号\n", Color.Black); } private void ma_code_UserControlTextChanged(object sender, EventArgs e) { if (ma_code.Text.Length > 5) { string ErrMessage = ""; if (LogicHandler.CheckMakeStatus(ma_code.Text, out ErrMessage)) { mcd_inqty.Text = ""; mcd_remainqty.Text = ""; sql.Clear(); sql.Append("select ma_qty,ma_prodcode,pr_detail,pr_pcbacount "); sql.Append("from make left join makecraftdetail on mcd_macode=ma_code left join product on ma_prodcode=pr_code where "); sql.Append("ma_code='" + ma_code.Text + "' and mcd_stepcode='" + User.CurrentStepCode + "'"); DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); if (dt.Rows.Count > 0) BaseUtil.SetFormValue(Controls, dt); RefreshData(); sql.Clear(); sql.Append("select rownum,sb_maincode,sb_barcode,sb_prodcode,sb_makecode,sb_indate from SMTBIND where sb_maincode=(select sb_maincode from SMTBIND "); sql.Append("where sb_barcode='" + code.Text + "')"); dt1 = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); BaseUtil.FillDgvWithDataTable(LabelDataGridView, dt1); } } } /// /// 刷新采集数据 /// private void RefreshData() { sql.Clear(); sql.Append("select count(1) mcd_inqty,max(ma_qty)-count(1) mcd_remainqty from smtbind left join make on ma_code= sb_makecode where sb_makecode='" + ma_code.Text + "' "); DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); if (dt.Rows.Count > 0) BaseUtil.SetFormValue(Controls, dt); } string mincode; int sncount; int startsn; string firstsn; string lastsn; private void Crbut_Click(object sender, EventArgs e) { if(ma_code.Text==""|| Prefix.Text == "" || Num.Text == "" || Rn.Text == "" || pr_pcbacount.Value == 0 || Rnle.Value == 0) { OperateResult.AppendText(">>有关信息未录入或拼版数,流水长度未维护\n", Color.Red); return; } try { sncount = int.Parse(Num.Text); int maqty = int.Parse(dh.getFieldDataByCondition("make", "ma_qty", "ma_code = '" + ma_code.Text + "'").ToString()); int nowqty = int.Parse(dh.getFieldDataByCondition("smtbind", "count(1) cn ", "sb_makecode = '" + ma_code.Text + "'").ToString()); if (nowqty + sncount > maqty) { OperateResult.AppendText(">>已绑定数" + nowqty + "+待绑定数" + sncount + "大于工单数" + maqty + "不允许操作\n", Color.Red); return; } if (DSCIN.Checked) { startsn = int.Parse(Rn.Text); } else { startsn = int.Parse(Hex2Ten(Rn.Text)); } if (sncount % pr_pcbacount.Value != 0) { OperateResult.AppendText(">>数量不是拼版数倍数\n", Color.Red); return; } if (DSCIN.Checked) { if ((startsn + (sncount - 1)*(sqit.Value)).ToString().Length > Rnle.Value) { OperateResult.AppendText(">>流水号长度超过设置长度\n", Color.Red); return; } } else { if (ConvertNumToHex((startsn + (sncount - 1) * (sqit.Value )).ToString()).Length > Rnle.Value) { OperateResult.AppendText(">>流水号长度超过设置长度\n", Color.Red); return; } } if (DSCIN.Checked) { firstsn = Prefix.Text + lpad(int.Parse(Rnle.Value.ToString()), startsn.ToString()); lastsn = Prefix.Text + lpad(int.Parse(Rnle.Value.ToString()), (startsn + (sncount - 1) * (sqit.Value)).ToString()); } else { firstsn = Prefix.Text + lpad(int.Parse(Rnle.Value.ToString()), ConvertNumToHex(startsn.ToString())); lastsn = Prefix.Text + lpad(int.Parse(Rnle.Value.ToString()), ConvertNumToHex((startsn + (sncount - 1) * (sqit.Value )).ToString())); } if (dh.CheckExist("SMTBIND", "sb_barcode between '"+ firstsn + "' and '"+ lastsn + "' ")) { OperateResult.AppendText(">>SN导入重复\n", Color.Red); return; } thread = new Thread(NewPrint); SetLoadingWindow stw = new SetLoadingWindow(thread, "请稍候..."); BaseUtil.SetFormCenter(stw); stw.ShowDialog(); } catch (Exception ex) { OperateResult.AppendText(">>"+ex.Message+"\n", Color.Red); } } private void NewPrint() { List maincodelist = new List(); List nowcodelist = new List(); if (DSCIN.Checked) { for (int i = 0; i < sncount; i++) { string nowsn = Prefix.Text + lpad(int.Parse(Rnle.Value.ToString()), (startsn + i*(sqit.Value)).ToString()); if (i % pr_pcbacount.Value == 0) mincode = nowsn; maincodelist.Add(mincode); nowcodelist.Add(nowsn); } } else { for (int i = 0; i < sncount; i++) { string nowsn = Prefix.Text + lpad(int.Parse(Rnle.Value.ToString()), ConvertNumToHex((startsn + i * (sqit.Value )).ToString())); if (i % pr_pcbacount.Value == 0) mincode = nowsn; maincodelist.Add(mincode); nowcodelist.Add(nowsn); } } sql.Clear(); sql.Append("insert into smtbind(sb_id,sb_maincode,sb_barcode,sb_makecode,sb_prodcode,sb_inman,sb_indate,sb_status)"); sql.Append("values(smtbind_seq.nextval,:maincode,:nowcode,'" + ma_code.Text + "','" + ma_prodcode.Text + "',"); sql.Append("'" + User.UserCode + "',sysdate,0)"); dh.BatchInsert(sql.GetString(), new string[] { "maincode", "nowcode" }, maincodelist.ToArray(), nowcodelist.ToArray()); MessageBox.Show("导入成功!"); OperateResult.AppendText(">>生成完毕\n", Color.Black); OperateResult.AppendText(">>起始号:" + firstsn + "\n", Color.Black); OperateResult.AppendText(">>截止号:" + lastsn + "\n", Color.Black); LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "SMT拼版绑定", firstsn+"到"+lastsn+"绑定成功,数量"+ sncount+",间隔"+ sqit.Value, "",""); } private static string lpad(int length, string number) { while (number.Length < length) { number = "0" + number; } number = number.Substring(number.Length - length, length); return number; } private void Debut_Click(object sender, EventArgs e) { //if (!(ma_code.Text == "")) //{ // string logout_confirm = MessageBox.Show(this.ParentForm, "是否删除此工单拼版绑定记录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString(); // if (logout_confirm == "Yes") // { // dh.ExecuteSql("delete SMTBIND where sb_makecode ='"+ma_code.Text+"'", "delete"); // OperateResult.AppendText(">>工单"+ma_code.Text+ "拼版绑定记录清除成功\n", Color.Green); // } //} //else { // OperateResult.AppendText(">>工单号不能为空\n", Color.Red); //} try { sncount = int.Parse(Num.Text); if (DSCIN.Checked) { startsn = int.Parse(Rn.Text); } else { startsn = int.Parse(Hex2Ten(Rn.Text)); } if (sncount % pr_pcbacount.Value != 0) { OperateResult.AppendText(">>数量不是拼版数倍数\n", Color.Red); return; } if (DSCIN.Checked) { if ((startsn + (sncount - 1) * (sqit.Value)).ToString().Length > Rnle.Value) { OperateResult.AppendText(">>流水号长度超过设置长度\n", Color.Red); return; } } else { if (ConvertNumToHex((startsn + (sncount - 1) * (sqit.Value)).ToString()).Length > Rnle.Value) { OperateResult.AppendText(">>流水号长度超过设置长度\n", Color.Red); return; } } if (DSCIN.Checked) { firstsn = Prefix.Text + lpad(int.Parse(Rnle.Value.ToString()), startsn.ToString()); lastsn = Prefix.Text + lpad(int.Parse(Rnle.Value.ToString()), (startsn + (sncount - 1) * (sqit.Value)).ToString()); } else { firstsn = Prefix.Text + lpad(int.Parse(Rnle.Value.ToString()), ConvertNumToHex(startsn.ToString())); lastsn = Prefix.Text + lpad(int.Parse(Rnle.Value.ToString()), ConvertNumToHex((startsn + (sncount - 1) * (sqit.Value)).ToString())); } string logout_confirm = MessageBox.Show(this.ParentForm, "是否删除SN: "+ firstsn + " 到 "+ lastsn + " 拼版绑定记录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString(); if (logout_confirm == "Yes") { dh.ExecuteSql("delete SMTBIND where sb_barcode between '" + firstsn + "' and '" + lastsn + "'", "delete"); OperateResult.AppendText(">>SN: " + firstsn + " 到 " + lastsn + " 拼版绑定记录清除成功\n", Color.Green); } } catch (Exception ex) { OperateResult.AppendText(">>" + ex.Message + "\n", Color.Red); } } /// /// 从十进制转换到十六进制 /// /// /// public static string ConvertNumToHex(string ten) { ulong Numb = Convert.ToUInt64(ten); ulong divValue, resValue; string hex = ""; do { divValue = (ulong)Math.Floor((decimal)(Numb / 16)); resValue = Numb % 16; hex = GetNumb(resValue) + hex; Numb = divValue; } while (Numb >= 16); if (Numb != 0) hex = GetNumb(Numb) + hex; return hex; } public static string GetNumb(ulong Numb) { switch (Numb) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: return Numb.ToString(); case 10: return "A"; case 11: return "B"; case 12: return "C"; case 13: return "D"; case 14: return "E"; case 15: return "F"; default: return ""; } } public static string Hex2Ten(string hex) { int ten = 0; for (int i = 0, j = hex.Length - 1; i < hex.Length; i++) { ten += HexChar2Value(hex.Substring(i, 1)) * ((int)Math.Pow(16, j)); j--; } return ten.ToString(); } public static int HexChar2Value(string hexChar) { switch (hexChar) { case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": return Convert.ToInt32(hexChar); case "a": case "A": return 10; case "b": case "B": return 11; case "c": case "C": return 12; case "d": case "D": return 13; case "e": case "E": return 14; case "f": case "F": return 15; default: return 0; } } private void normalButton2_Click(object sender, EventArgs e) { DialogResult result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; } } private void normalButton3_Click(object sender, EventArgs e) { if (ma_code.Text == "" || textBox1.Text == "" || ma_prodcode.Text == "") { MessageBox.Show("工单号,产品编号与文件地址不可为空"); return; } dt = eh.ImportExcel(textBox1.Text, "test"); int number = 0; //for (int i = 0; i < dt.Rows.Count; i++) //{ // MessageBox.Show(dt.Rows[i]["RefDes"].ToString()+":"+ dt.Rows[i]["Layer"].ToString()); //} //BaseUtil.FillDgvWithDataTable(LabelDataGridView, dt); List maincodelist = new List(); List nowcodelist = new List(); for (int i = 0; i < dt.Rows.Count; i++) { if (dt.Rows[i]["工单"].ToString().Trim()==ma_code.Text&&dt.Rows[i]["拼板号"].ToString().Trim().Length >2 && dt.Rows[i]["读码内容"].ToString().Trim().Length > 2 && dt.Rows[i]["拼板号"].ToString().Trim().Substring(0, 2) == dt.Rows[i]["读码内容"].ToString().Trim().Substring(0, 2)) { maincodelist.Add(dt.Rows[i]["拼板号"].ToString().Trim()); nowcodelist.Add(dt.Rows[i]["读码内容"].ToString().Trim()); //number++; } } sql.Clear(); sql.Append("insert into smtbind_temp(MAINCODE,BARCODE)"); sql.Append("values(:maincode,:nowcode)"); dh.BatchInsert(sql.GetString(), new string[] { "maincode", "nowcode" }, maincodelist.ToArray(), nowcodelist.ToArray()); number = int.Parse(dh.getFieldDataByCondition("smtbind_temp", "count(1) cn ", "BARCODE not in (select sb_barcode from smtbind )").ToString()); int maqty = int.Parse(dh.getFieldDataByCondition("make", "ma_qty", "ma_code = '" + ma_code.Text + "'").ToString()); int nowqty = int.Parse(dh.getFieldDataByCondition("smtbind", "count(1) cn ", "sb_makecode = '" + ma_code.Text + "'").ToString()); if (nowqty + number > maqty) { OperateResult.AppendText(">>已绑定数" + nowqty + "+待绑定数" + number + "大于工单数" + maqty + "不允许操作\n", Color.Red); dh.ExecuteSql("delete SMTBIND_TEMP", "delete"); return; } sql.Clear(); sql.Append("merge into smtbind using SMTBIND_TEMP on (sb_barcode = barcode) when not MATCHED then insert (sb_id,sb_maincode,sb_barcode,sb_makecode,sb_prodcode,sb_inman,sb_indate,sb_status)"); sql.Append(" values (SMTBIND_seq.nextval ,maincode,barcode,'" + ma_code.Text + "','"+ma_prodcode.Text+"','"+User.UserCode+"',sysdate,0)"); dh.ExecuteSql(sql.GetString(), "insert"); dh.ExecuteSql("delete SMTBIND_TEMP", "delete"); MessageBox.Show("导入成功"+ number + "条!"); OperateResult.AppendText(">>导入成功" + number + "条!\n", Color.Green); } } }