using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using gregn6Lib; using MESClassLibrary.BLL.Log; using MESClassLibrary.BLL.Mistake247; using MESClassLibrary.DAL; using MESClassLibrary.Model; namespace PurchingMistakeNew { public partial class Form7 : Form { public Form7() { InitializeComponent(); } private string order = ""; private void Form7_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; textBox1.Enabled = true; textBox2.Enabled = false; panel1.Visible = false; textBox1.Text = ""; textBox2.Text = ""; initData(); SqlStart(); } public void initData() { string[] part = { "A2477901300", "A2477901400", "A2477900300", "A2477900400" }; string[] sum = new string[4]; DataTable dt = null; RecordBLL bll = new RecordBLL(); string batch = DateTime.Now.ToString("yyyy-MM-dd").Substring(2, 2) + DateTime.Now.ToString("yyyy-MM-dd").Substring(5, 2) + DateTime.Now.ToString("yyyy-MM-dd").Substring(8, 2); //string batch = "191111"; try { for (int i = 0; i < 4; i++) { dt = bll.GetCount(part[i], batch); if (dt != null && dt.Rows.Count > 0) { sum[i] = dt.Rows[0]["sum"].ToString(); } else { sum[i] = ""; } } uc1.PartInfo = "A2477901300 X247后部左侧扰流板"; uc2.PartInfo = "A2477901400 X247后部右侧扰流板"; uc3.PartInfo = "A2477900300 H247后部左侧扰流板"; uc4.PartInfo = "A2477900400 H247后部右侧扰流板"; uc1.Sum = sum[0]; uc2.Sum = sum[1]; uc3.Sum = sum[2]; uc4.Sum = sum[3]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); } } private void textBox4_KeyDown(object sender, KeyEventArgs e) { try { if (e.KeyCode == Keys.Enter) { if (textBox4.Text.Trim().Length != 12) { MessageBox.Show("订单号有误,请重新扫描!", "提示", MessageBoxButtons.OK); textBox4.Focus(); return; } #region 判断订单号是否存在 if (initList()) return; #endregion } } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); } } private bool initList() { Plan247BLL bll = new Plan247BLL(); int a = 0; this.listView1.Items.Clear(); DataTable dt = bll.SearhByOrderNo(textBox4.Text.Trim()); if (dt != null && dt.Rows.Count > 0) { #region 判断订单是否生产完成 for (int i = 0; i < dt.Rows.Count; i++) { if (Convert.ToInt32(dt.Rows[i]["IsFinish"].ToString()) == 1) { a += 1; if (a == dt.Rows.Count) { MessageBox.Show("订单已完成,请扫描其他订单!", "提示", MessageBoxButtons.OK); textBox4.Focus(); return true; } } else { ListViewItem lvi = new ListViewItem(dt.Rows[i]["OrderNo"].ToString()); lvi.SubItems.Add(dt.Rows[i]["PartNo"].ToString()); //后面添加的Item都为SubItems ,即为子项 lvi.SubItems.Add(dt.Rows[i]["PartName2"].ToString()); lvi.SubItems.Add(dt.Rows[i]["OrderCount"].ToString()); lvi.SubItems.Add(dt.Rows[i]["CompleteCount"].ToString()); listView1.Items.Add(lvi); } } #endregion } else { MessageBox.Show("订单号不存在,请重新扫描!", "提示", MessageBoxButtons.OK); textBox4.Focus(); return true; } return false; } private void textBox1_KeyDown(object sender, KeyEventArgs e) { try { MistakeBLL bll = new MistakeBLL(); AssemblyBLL abll = new AssemblyBLL(); RecordBLL rbll = new RecordBLL(); BoxModel bmd = new BoxModel(); string partno = "", PartName = "", BoxNo = "", newBoxNo = "", banBatchNo = "", newBatchNo = ""; int packCount = 0; if (e.KeyCode == Keys.Enter) { if (textBox1.Text.Trim() != "") { #region 查询当前配置码是否有未进行视觉检测的产品 DataTable dt = abll.SearchPrintByDeviceNo(textBox1.Text.Trim()); if (dt != null && dt.Rows.Count > 0) { MessageBox.Show("无法扫描配置码,存在未进行视觉检测的产品!"); textBox1.SelectAll(); textBox1.Focus(); return; } #endregion textBox2.Enabled = true; textBox2.Focus(); } else { MessageBox.Show("请扫描设备条码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); textBox1.Focus(); return; } } } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); } } private void textBox2_KeyDown(object sender, KeyEventArgs e) { try { string partNo = "", deviceNo = "", partsub = "" ; int BoxCount = 0, type = 0, orderCount = 0, ActualCount = 0; RecordBLL rbll = new RecordBLL(); MistakeBLL bll = new MistakeBLL(); AssemblyBLL abll = new AssemblyBLL(); Plan247BLL pbll = new Plan247BLL(); AssemblyModel md = new AssemblyModel(); RecordModel rmd = new RecordModel(); string batchNo = "", newbatchNo = ""; if (e.KeyCode == Keys.Enter) { #region 是否扫描单号 if (textBox4.Text.Trim() == "") { MessageBox.Show("请扫描计划单!", "提示", MessageBoxButtons.OK); textBox4.Focus(); return; } #endregion if (textBox2.Text.Trim().Length < 10) { MessageBox.Show("半成品条码有误,请重新扫描!", "提示", MessageBoxButtons.OK); textBox2.Focus(); return; } if (VerifyHelper.ScanSjBarCodeFormat(textBox2.Text.Trim()) == false) { MessageBox.Show($"扫描条码[{textBox2.Text.Trim()}]格式错误!", "提示", MessageBoxButtons.OK); textBox2.Text = ""; textBox2.Focus(); return; } #region 判断计划单里是否有此产品的生产计划并且是未完成 DataTable hdt = pbll.IsHavePlan(textBox2.Text.Trim().Substring(0, 10), textBox4.Text.Trim()); if (hdt != null && hdt.Rows.Count > 0) { order = hdt.Rows[0]["OrderNo"].ToString(); orderCount = Convert.ToInt32(hdt.Rows[0]["OrderCount"].ToString()); } else { MessageBox.Show("该零件已生产完成!", "提示", MessageBoxButtons.OK); textBox2.Focus(); return; } #endregion deviceNo = textBox1.Text.Trim(); partNo = textBox2.Text.Substring(0, 10); #region 判断半成品和设备码是否匹配 DataTable dt = bll.IsMath(deviceNo, partNo); if (dt != null && dt.Rows.Count > 0) { #region 根据半成品查找总成零件号 DataTable dt2 = bll.SearchByNo(partNo, deviceNo); if (dt2 != null && dt2.Rows.Count > 0) { partsub = dt2.Rows[0]["PartNo2"].ToString(); #region 判断总成零件当前批次是否存在 newbatchNo = DateTime.Now.ToString("yyyy-MM-dd").Substring(2, 2) + DateTime.Now.ToString("yyyy-MM-dd").Substring(5, 2) + DateTime.Now.ToString("yyyy-MM-dd").Substring(8, 2); try { DataTable partdt = abll.searchPartNo(dt2.Rows[0]["PartNo2"] + "." + newbatchNo); if (partdt != null && partdt.Rows.Count > 0) { md.PartNo = partdt.Rows[0]["PartNo"].ToString().Substring(0, partdt.Rows[0]["PartNo"].ToString().Length - 6) + (Convert.ToInt32(partdt.Rows[0]["PartNo"].ToString().Substring(partdt.Rows[0]["PartNo"].ToString().Length - 6, 6)) + 1) .ToString() .PadLeft(6, '0'); } else { md.PartNo = dt2.Rows[0]["PartNo2"] + "." + newbatchNo + "." + "000001"; } } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(),MethodBase.GetCurrentMethod()); MessageBox.Show("连接数据库失败!"); return; } #endregion md.PartName = dt2.Rows[0]["PartName2"].ToString(); #region 保存总成条码 md.ID = Guid.NewGuid().ToString(); md.BatchNo = DateTime.Now.ToString("yyyy-MM-dd").Substring(2, 2) + DateTime.Now.ToString("yyyy-MM-dd").Substring(5, 2) + DateTime.Now.ToString("yyyy-MM-dd").Substring(8, 2); #region 根据半成品零件号判断总成类型、总成条码模板、打印机 if (partNo == "1230002627") { md.PartType = "X247 后部左侧扰流板"; type = 6; } if (partNo == "1230002600") { md.PartType = "X247 后部右侧扰流板"; type = 7; } if (partNo == "1230005784") { md.PartType = "H247 后部左侧扰流板"; type = 9; } if (partNo == "1230005785") { md.PartType = "H247 后部右侧扰流板"; type = 10; } #endregion md.Color = "通用"; md.ColorNo = ""; md.TapeBatchNo = md.BatchNo; md.Validity = "3个月"; md.ValidityDate = "从" + DateTime.Now.ToString("yyyy-MM-dd") + "到" + DateTime.Now.AddMonths(3).ToString("yyyy-MM-dd"); md.Flag = 0; md.DeviceNo = deviceNo; abll.AddInfo(md); #endregion #region 打印总成条码 //report.Register(""); //report.ConnectionString = ConfigurationManager.ConnectionStrings["report"].ToString(); //report.Print(false); //abll.UpdateInfo(md); #endregion #region 更新订单完成数 //pbll.UpdateCount(partsub, order); #endregion #region 保存记录 rmd.ID = Guid.NewGuid().ToString(); rmd.PartNo1 = textBox2.Text.Trim(); rmd.PartNo2 = md.PartNo; rmd.Type = type; rmd.DeviceNo = textBox1.Text.Trim(); rbll.AddInfo(rmd); #endregion } #endregion } else { MessageBox.Show("匹配失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } #endregion textBox1.Text = ""; textBox2.Text = ""; textBox2.Enabled = false; textBox1.Focus(); //bindData(); //initData(); } } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK); } } private void button1_Click(object sender, EventArgs e) { panel1.Visible = true; } private void button2_Click(object sender, EventArgs e) { try { AssemblyBLL bll = new AssemblyBLL(); AssemblyModel md = new AssemblyModel(); string part = "", PrinterName = "", filename = ""; gregn6Lib.GridppReport report = new gregn6Lib.GridppReport(); if (textBox3.Text.Trim() == "") { MessageBox.Show("总成条码不能为空!", "提示", MessageBoxButtons.OK); textBox3.Focus(); return; } DataTable dt = bll.searchPartNo(textBox3.Text.Trim()); if (dt != null && dt.Rows.Count > 0) { #region 补打 md.ID = Guid.NewGuid().ToString(); md.PartNo = textBox3.Text.Trim(); md.PartName = dt.Rows[0]["PartName"].ToString(); md.BatchNo = dt.Rows[0]["BatchNo"].ToString(); md.Color = dt.Rows[0]["Color"].ToString(); md.ColorNo = dt.Rows[0]["ColorNo"].ToString(); md.PartType = dt.Rows[0]["PartType"].ToString(); md.TapeBatchNo = dt.Rows[0]["TapeBatchNo"].ToString(); md.Validity = dt.Rows[0]["Validity"].ToString(); md.ValidityDate = dt.Rows[0]["ValidityDate"].ToString(); md.Des = dt.Rows[0]["Des"].ToString(); md.Flag = 1; bll.AddInfo(md); part = textBox3.Text.Trim().Substring(0, textBox3.Text.Trim().IndexOf(".")); if (part == "A2477901300") { md.PartType = "X247 后部左侧扰流板"; //report.LoadFromFile(@"D:\X247 后部左侧扰流板.grf"); //report.Printer.PrinterName = ConfigurationManager.AppSettings["Printer1"].ToString(); PrinterName = ConfigurationManager.AppSettings["Printer1"].ToString(); } if (part == "A2477901400") { md.PartType = "X247 后部右侧扰流板"; //MessageBox.Show("1"); //report.LoadFromFile(@"D:\X247 后部右侧扰流板.grf"); //MessageBox.Show("2"); //report.Printer.PrinterName = ConfigurationManager.AppSettings["Printer2"].ToString(); //MessageBox.Show("3"); PrinterName = ConfigurationManager.AppSettings["Printer2"].ToString(); } if (part == "A2477900300") { md.PartType = "H247 后部左侧扰流板"; //report.LoadFromFile(@"D:\H247 后部左侧扰流板.grf"); //report.Printer.PrinterName = ConfigurationManager.AppSettings["Printer1"].ToString(); PrinterName = ConfigurationManager.AppSettings["Printer1"].ToString(); } if (part == "A2477900400") { md.PartType = "H247 后部右侧扰流板"; //report.LoadFromFile(@"D:\H247 后部右侧扰流板.grf"); //report.Printer.PrinterName = ConfigurationManager.AppSettings["Printer2"].ToString(); PrinterName = ConfigurationManager.AppSettings["Printer2"].ToString(); } #endregion filename = Application.StartupPath + @"\11.grf"; //report.Register(""); //report.ConnectionString = ConfigurationManager.ConnectionStrings["report"].ToString(); //report.Print(false); DataTable headTable = new DataTable(); headTable.Columns.Add("PartNo"); headTable.Columns.Add("PartName"); headTable.Columns.Add("BatchNo"); DataRow row = headTable.NewRow(); row["PartNo"] = textBox3.Text.Trim(); row["PartName"] = md.PartName; row["BatchNo"] = md.BatchNo; headTable.Rows.Add(row); //MyReport6 rp = new MyReport6(filename, headTable, null); //rp.Report.Printer.PrinterName = PrinterName; //rp.Report.Print(false); //var report = new ReportHelper(filename, headTable, null, // (int)GRPaperOrientation.grpoDefault, 1, PrinterName); //report.Print(false); ReportHelper rp = new ReportHelper(filename, headTable, null, (int)GRPaperOrientation.grpoDefault, 1, PrinterName); rp.Report.Print(false); bll.UpdateInfo(md); } else { MessageBox.Show("总成条码不存在!", "提示", MessageBoxButtons.OK); textBox3.Focus(); return; } } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); } } private void button3_Click(object sender, EventArgs e) { panel1.Visible = false; } private void SqlStart() { try { SqlDependency.Start(@"Persist Security Info=true;Initial Catalog=BBMPT;Data Source=10.60.101.9;User ID=sa;Password=a1+"); SqlDependencyWatch(); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(),MethodBase.GetCurrentMethod()); } } private void SqlDependencyWatch() { string sSQL = "SELECT [ToVisual] FROM [dbo].[tb_Assembly_247] where deviceNo in ('A6','A7','A9','A10')"; using (SqlConnection connection = new SqlConnection(@"Persist Security Info=true;Initial Catalog=BBMPT;Data Source=10.60.101.9;User ID=sa;Password=a1+")) { using (SqlCommand command = new SqlCommand(sSQL, connection)) { command.CommandType = CommandType.Text; connection.Open(); SqlDependency dependency = new SqlDependency(command); dependency.OnChange += new OnChangeEventHandler(SQLTableOnChange); SqlDataReader sdr = command.ExecuteReader(); } } } private void SQLTableOnChange(object sender, SqlNotificationEventArgs e) { SqlDependencyWatch(); //AfterScanBarcode(); this.Invoke(new Action(() => { AfterScanBarcode(); })); } private void AfterScanBarcode() { try { string partNo = "", deviceNo = "", id = "",no="",name=""; int type = 0; RecordBLL rbll = new RecordBLL(); MistakeBLL bll = new MistakeBLL(); AssemblyBLL abll = new AssemblyBLL(); Plan247BLL pbll = new Plan247BLL(); AssemblyModel md = new AssemblyModel(); RecordModel rmd = new RecordModel(); string batchNo = "", newbatchNo = "", PrinterName = "", filename = ""; #region 监听 string tmp_deviceNo = ""; #region 判断是否有未打印的 DataTable dt2 = abll.IsHavePrint("A6", "A7", "A9", "A10"); if (dt2 != null && dt2.Rows.Count > 0) { } else { return; } #endregion DataTable dt = abll.GetDeviceNo("A6", "A7", "A9", "A10"); if (dt != null && dt.Rows.Count > 0) { id = dt.Rows[0]["ID"].ToString(); partNo = dt.Rows[0]["PartNo"].ToString().Substring(0, 11); no = dt.Rows[0]["PartNo"].ToString(); name = dt.Rows[0]["PartName"].ToString(); batchNo = dt.Rows[0]["BatchNo"].ToString(); } else { return; } #endregion #region 根据半成品零件号判断总成类型、总成条码模板、打印机 if (partNo == "A2477901300") { md.PartType = "X247 后部左侧扰流板"; //report.LoadFromFile(@"D:\X247 后部左侧扰流板.grf"); //report.Printer.PrinterName = ConfigurationManager.AppSettings["Printer1"].ToString(); PrinterName = ConfigurationManager.AppSettings["Printer1"].ToString(); } if (partNo == "A2477901400") { md.PartType = "X247 后部右侧扰流板"; //MessageBox.Show("1"); //report.LoadFromFile(@"D:\X247 后部右侧扰流板.grf"); //MessageBox.Show("2"); //report.Printer.PrinterName = ConfigurationManager.AppSettings["Printer2"].ToString(); //MessageBox.Show("3"); PrinterName = ConfigurationManager.AppSettings["Printer2"].ToString(); } if (partNo == "A2477900300") { md.PartType = "H247 后部左侧扰流板"; //report.LoadFromFile(@"D:\H247 后部左侧扰流板.grf"); //report.Printer.PrinterName = ConfigurationManager.AppSettings["Printer1"].ToString(); PrinterName = ConfigurationManager.AppSettings["Printer1"].ToString(); } if (partNo == "A2477900400") { md.PartType = "H247 后部右侧扰流板"; //report.LoadFromFile(@"D:\H247 后部右侧扰流板.grf"); //report.Printer.PrinterName = ConfigurationManager.AppSettings["Printer2"].ToString(); PrinterName = ConfigurationManager.AppSettings["Printer2"].ToString(); } #endregion #region 打印总成条码 filename = Application.StartupPath + @"\11.grf"; //report.Register(""); //report.ConnectionString = ConfigurationManager.ConnectionStrings["report"].ToString(); //report.Print(false); DataTable headTable = new DataTable(); headTable.Columns.Add("PartNo"); headTable.Columns.Add("PartName"); headTable.Columns.Add("BatchNo"); DataRow row = headTable.NewRow(); row["PartNo"] = no; row["PartName"] = name; row["BatchNo"] = batchNo; headTable.Rows.Add(row); //MyReport6 rp = new MyReport6(filename, headTable, null); //rp.Report.Printer.PrinterName = PrinterName; //rp.Report.Print(false); //var report = new ReportHelper(filename, headTable, null, // (int)GRPaperOrientation.grpoDefault, 1, PrinterName); //report.Print(false); ReportHelper rp = new ReportHelper(filename, headTable, null, (int)GRPaperOrientation.grpoDefault, 1, PrinterName); rp.Report.Print(false); //report.Register(""); //report.ConnectionString = ConfigurationManager.ConnectionStrings["report"].ToString(); abll.UpdatePrintByID(id); #endregion #region 更新订单完成数 pbll.UpdateCount(partNo, textBox4.Text.Trim()); #endregion #region 更新打印时间 rbll.UpdateCreateTime1(dt.Rows[0]["PartNo"].ToString()); #endregion initData(); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(),MethodBase.GetCurrentMethod()); } } } }