using DevComponents.DotNetBar.Controls; using NPOI.SS.Formula.Eval; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using UAS_MES_NEW.DataOperate; using UAS_MES_NEW.Entity; using UAS_MES_NEW.PublicMethod; namespace UAS_MES_NEW.Make { public partial class Make_HandFeedQualityConfirm : Form { public Make_HandFeedQualityConfirm() { InitializeComponent(); } StringBuilder SQL = new StringBuilder(); DataTable dt, data; DataHelper dh; bool qaSwitching; private void Make_HandFeedQualityConfirm_Load(object sender, EventArgs e) { dh = SystemInf.dh; dgv.AutoGenerateColumns = false; data = new DataTable(); data.Columns.Add("isScan", typeof(bool)); data.Columns.Add("rn", typeof(int)); data.Columns.Add("ms_sncode", typeof(string)); data.Columns.Add("line_name", typeof(string)); data.Columns.Add("side", typeof(string)); data.Columns.Add("position", typeof(string)); data.Columns.Add("reel_position", typeof(string)); data.Columns.Add("part_no", typeof(string)); data.Columns.Add("reel_qty", typeof(string)); data.Columns.Add("reelremark", typeof(string)); data.Columns.Add("reelnum", typeof(string)); data.Columns.Add("qa_emp", typeof(string)); data.Columns.Add("qa_date", typeof(string)); data.Columns.Add("qa_type", typeof(string)); data.Columns.Add("qa_result", typeof(string)); data.Columns.Add("update_time", typeof(string)); data.Columns.Add("handreel_id", typeof(string)); dgv.DataSource = data; submit.Enabled = false; LockMakeCode.GetMakeCodeCtl(ma_code); ma_code.SetLockCheckBox(LockMakeCode); ma_code.TableName = "make left join product on ma_prodcode=pr_code"; ma_code.SelectField = "ma_code # 工单号,ma_prodcode # 产品编号,ma_qty # 工单数量,pr_spec # 产品规格,ma_softversion # 软件版本,ma_salecode # 销售单号,pr_sendchecktype # 产品送检方式,pr_pcbacount # 拼版数"; ma_code.FormName = Name; ma_code.SetValueField = new string[] { "ma_code", "ma_prodcode", "ma_qty", "pr_spec", "ma_softversion", "ma_salecode", "nvl(pr_sendchecktype,'LineCode')pr_sendchecktype", "pr_pcbacount" }; ma_code.Condition = "ma_statuscode='STARTED'"; ma_code.Focus(); } private void ma_code_DbChange(object sender, EventArgs e) { BaseUtil.SetFormValue(this.Controls, ma_code.ReturnData); LoadHandReel(); } private void LoadHandReel() { string makeCode = ma_code.Text.Trim(); if (string.IsNullOrEmpty(makeCode)) { MessageBox.Show("请输入工单", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); ma_code.Focus(); return; } SQL.Clear(); /*SQL.Append($@"SELECT m.ms_sncode, h.line_name, h.side, h.position, h.reel_position, h.part_no, h.reel_qty, h.reelremark, h.reelnum, h.qa_emp, to_char(h.qa_date,'yyyy-mm-dd hh24:mi:ss') qa_date, h.qa_type, to_char(h.update_time,'yyyy-mm-dd hh24:mi:ss') update_time, h.handreel_id FROM makeserial m, smt_handreel_log h WHERE h.serial_number = m.ms_sncode AND m.ms_makecode = '{makeCode}' ORDER BY m.ms_sncode, h.handreel_id");*/ SQL.Append($@"SELECT m.ms_sncode, h.line_name, h.side, h.position, h.reel_position, h.part_no, h.reel_qty, h.reelremark, h.reelnum, h.qa_emp, to_char(h.qa_date, 'yyyy-mm-dd hh24:mi:ss') qa_date, h.qa_type, to_char(h.update_time, 'yyyy-mm-dd hh24:mi:ss') update_time, h.handreel_id FROM makeserial m, smt_handreel_log h WHERE h.serial_number = m.ms_sncode AND m.ms_makecode = '{makeCode}' AND h.update_time IN(SELECT MAX(t2.update_time) update_time FROM makeserial t1, smt_handreel_log t2 WHERE t2.serial_number = t1.ms_sncode AND t1.ms_makecode = '{makeCode}' GROUP BY t2.serial_number) ORDER BY m.ms_sncode, h.handreel_id"); dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select"); data.Rows.Clear(); if (dt.Rows.Count == 0) { submit.Enabled = false; MessageBox.Show("该工单没有手补料序列号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); ma_code.Focus(); return; } for (int i = 0; i < dt.Rows.Count; i++) { DataRow src = dt.Rows[i]; string qaType = NormalizeQaType(src["qa_type"]); data.Rows.Add(false, i + 1, src["ms_sncode"].ToString(), src["line_name"].ToString(), src["side"].ToString(), src["position"].ToString(), src["reel_position"].ToString(), src["part_no"].ToString(), src["reel_qty"].ToString(), src["reelremark"].ToString(), src["reelnum"].ToString(), src["qa_emp"].ToString(), src["qa_date"].ToString(), qaType, QaTypeText(qaType), src["update_time"].ToString(), src["handreel_id"].ToString()); } ApplyLoadedStyle(); submit.Enabled = true; snVal.Text = ""; snVal.Focus(); } private void snVal_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode != Keys.Enter) return; e.Handled = true; e.SuppressKeyPress = true; string scanSn = snVal.Text.Replace(";", ";").Trim(); snVal.Text = scanSn; if (string.IsNullOrEmpty(scanSn)) { MessageBox.Show("请扫描序列号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); snVal.Focus(); return; } if (data.Rows.Count == 0) { MessageBox.Show("请先输入工单", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); ma_code.Focus(); return; } string qaType = SelectedQaType(); if (qaType == "") { MessageBox.Show("请先选择合格或不合格", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } bool found = false; bool already = true; int firstRow = -1; for (int i = 0; i < data.Rows.Count; i++) { if (!string.Equals(data.Rows[i]["ms_sncode"].ToString(), scanSn, StringComparison.OrdinalIgnoreCase)) continue; found = true; if (firstRow < 0) firstRow = i; bool scanned = data.Rows[i]["isScan"] != DBNull.Value && Convert.ToBoolean(data.Rows[i]["isScan"]); if (!scanned || data.Rows[i]["qa_type"].ToString() != qaType) already = false; MarkScanRow(i, qaType); } if (!found) { UpdateSN("C"); MessageBox.Show("该序列号不在当前工单手补料记录中", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { UpdateSN("L", scanSn); if (already) MessageBox.Show("序列号已标记", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); else if (firstRow >= 0) dgv.FirstDisplayedScrollingRowIndex = firstRow; } snVal.Focus(); snVal.SelectAll(); } private void submit_Click(object sender, EventArgs e) { if (data.Rows.Count == 0) { MessageBox.Show("请先输入工单", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); ma_code.Focus(); return; } List scanRows = new List(); List missing = new List(); foreach (DataRow row in data.Rows) { bool scanned = row["isScan"] != DBNull.Value && Convert.ToBoolean(row["isScan"]); if (!scanned) continue; string sn = row["ms_sncode"].ToString(); string position = row["position"].ToString(); string qaType = NormalizeQaType(row["qa_type"]); if (qaType == "") { missing.Add("序列号" + sn + "当前站位" + position); continue; } scanRows.Add(row); } if (missing.Count > 0) { MessageBox.Show(string.Join("、", missing) + "未选择合格或不合格", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (scanRows.Count == 0) { MessageBox.Show("请先扫描序列号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); snVal.Focus(); return; } string emp = User.UserName ?? ""; if (emp.Length > 20) emp = emp.Substring(0, 20); string confirmTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); try { foreach (DataRow row in scanRows) { string idSql = row["handreel_id"].ToString(); string qaType = NormalizeQaType(row["qa_type"]); SQL.Clear(); SQL.Append($@"UPDATE smt_handreel_log SET qa_emp = '{emp}', qa_date = sysdate, qa_type = '{qaType}' WHERE handreel_id = '{idSql}'"); dh.ExecuteSql(SQL.ToString(), "update"); } } catch (Exception ex) { MessageBox.Show("品质确认失败:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } List snList = new List(); foreach (DataRow row in scanRows) { row["qa_emp"] = emp; row["qa_date"] = confirmTime; string sn = row["ms_sncode"].ToString(); if (!snList.Contains(sn)) snList.Add(sn); } MessageBox.Show($"序列号:{string.Join("、", snList)},数量{snList.Count},品质确认完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); snVal.Focus(); snVal.SelectAll(); snVal.Text = ""; } private void qaPass_CheckedChanged(object sender, EventArgs e) { if (qaSwitching || !qaPass.Checked) return; qaSwitching = true; qaFail.Checked = false; qaSwitching = false; } private void qaFail_CheckedChanged(object sender, EventArgs e) { if (qaSwitching || !qaFail.Checked) return; qaSwitching = true; qaPass.Checked = false; qaSwitching = false; } private string SelectedQaType() { if (qaPass.Checked) return "1"; if (qaFail.Checked) return "0"; return ""; } private string NormalizeQaType(object value) { string text = value == null || value == DBNull.Value ? "" : value.ToString().Trim(); decimal number; if (decimal.TryParse(text, out number)) { if (number == 1) return "1"; if (number == 0 && text != "") return "0"; } return ""; } private string QaTypeText(string qaType) { if (qaType == "1") return "合格"; if (qaType == "0") return "不合格"; return ""; } private void MarkScanRow(int rowIndex, string qaType) { data.Rows[rowIndex]["isScan"] = true; data.Rows[rowIndex]["qa_type"] = qaType; data.Rows[rowIndex]["qa_result"] = QaTypeText(qaType); PaintRow(rowIndex, qaType); } private void ApplyLoadedStyle() { int count = Math.Min(data.Rows.Count, dgv.Rows.Count); for (int i = 0; i < count; i++) PaintRow(i, NormalizeQaType(data.Rows[i]["qa_type"])); } private void PaintRow(int rowIndex, string qaType) { if (qaType == "1") { dgv.Rows[rowIndex].DefaultCellStyle.BackColor = Color.Green; dgv.Rows[rowIndex].DefaultCellStyle.ForeColor = Color.Black; } else if (qaType == "0") { dgv.Rows[rowIndex].DefaultCellStyle.BackColor = Color.DarkRed; dgv.Rows[rowIndex].DefaultCellStyle.ForeColor = Color.White; } else { dgv.Rows[rowIndex].DefaultCellStyle.BackColor = dgv.DefaultCellStyle.BackColor; dgv.Rows[rowIndex].DefaultCellStyle.ForeColor = dgv.DefaultCellStyle.ForeColor; } } private void UpdateSN(string type, string sn = "") { if (type == "C") { serialNumber.Text = ""; workOrder.Text = ""; productCode.Text = ""; productName.Text = ""; } else if (type == "L") { SQL.Clear(); SQL.Append($@"SELECT ms_sncode,ma_code,pr_code,pr_spec FROM makeserial,make,product WHERE ms_sncode = '{sn}' AND ms_makecode = ma_code AND ms_prodcode = pr_code"); dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select"); if (dt.Rows.Count > 0) { serialNumber.Text = dt.Rows[0]["ms_sncode"].ToString(); workOrder.Text = dt.Rows[0]["ma_code"].ToString(); productCode.Text = dt.Rows[0]["pr_code"].ToString(); productName.Text = dt.Rows[0]["pr_spec"].ToString(); } else { UpdateSN("C"); } } } } }