using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using UAS_MES.CustomControl.DataGrid_View; using UAS_MES.DataOperate; using UAS_MES.Entity; using UAS_MES.PublicMethod; namespace UAS_MES.OQC { public partial class OQC_SamplingDataCollection : Form { AutoSizeFormClass asc = new AutoSizeFormClass(); DataHelper dh; DataTable dt; DataTable BadCode; List dgvr = new List(); LogStringBuilder sql = new LogStringBuilder(); string[] LevelDefect = new string[] { "A#A", "B#B", "C#C", "D#D" }; DataTable TempForCheckType = new DataTable(); DataTable TempForBadGroup = new DataTable(); string ErrorMessage = ""; public OQC_SamplingDataCollection() { InitializeComponent(); } private void 抽样数据采集_Load(object sender, EventArgs e) { asc.controllInitializeSize(this); BaseUtil.DataGridViewNotSort(CheckTypeDGV); BaseUtil.DataGridViewNotSort(WaitChooseDGV); dh = new DataHelper(); } private void 抽样数据采集_SizeChanged(object sender, EventArgs e) { asc.controlAutoSize(this); } private void GetBatch_Click(object sender, EventArgs e) { if (LogicHandler.CheckStepAttribute(Tag.ToString(), User.UserSourceCode, out ErrorMessage)) { DataTable[] dt = LogicHandler.GetOQCBatch(ms_sncode.Text, obd_outboxcode.Text, ob_checkno.Text, "OQCDataCollection", out ErrorMessage); if (ErrorMessage == "") { BaseUtil.SetFormValue(Controls, dt[0]); if (ob_aqlcode.Text == "") { } dgvr.Clear(); } else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red); } else { OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red); } } private void Clean_Click(object sender, EventArgs e) { OperateResult.Clear(); } private void sncode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { if (CheckSnCode()) { dt = (DataTable)dh.ExecuteSql("select or_id,or_reasoncode bg_code, oi_description bg_name from OQCNGReason where or_checkno='" + ob_checkno.Text + "' and or_sncode='" + sncode.Text + "'", "select"); BaseUtil.FillDgvWithDataTable(ChoosedDGV, dt); GetBatchTypeGridData(); } } } private bool CheckSnCode() { //输入的序列号不能为空 if (sncode.Text != "") { //判断当前的检验状态 if (ob_status.Text == "待检验" || ob_status.Text == "检验中") { dt = (DataTable)dh.ExecuteSql("select obd_sncode,ob_source,ob_makecode from OQCBatchDetail left join OQCBatch on ob_id=obd_obid where obd_sncode='" + sncode.Text + "'", "select"); if (dt.Rows.Count > 0) { if (dt.Rows[0]["ob_source"].ToString() != "工序") { return true; } else { string ErrorMessage = ""; string oMakeCode = ""; string oMsID = ""; if (LogicHandler.CheckStepSNAndMacode(dt.Rows[0]["ob_makecode"].ToString(), User.UserSourceCode, sncode.Text, User.UserCode, out oMakeCode, out oMsID, out ErrorMessage)) { return true; } else { OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red); return false; } } } else { OperateResult.AppendText(">>序列号" + sncode.Text + "不属于当前抽检批次\n", Color.Red); return false; } } else { OperateResult.AppendText(">>送检批次必须是待检验或者检验中\n", Color.Red); return false; } } else { OperateResult.AppendText(">>序列号不能为空\n", Color.Red); return false; } } private void GetBatchTypeGridData() { dt = (DataTable)dh.ExecuteSql("select oi_checkkind,max(oi_sampleqty) oi_count,nvl(max(oi_checkqty),0) oi_checkedcount from OQCBatch left join OQCItems on ob_checkno=oi_checkno where oi_checkno='" + ob_checkno.Text + "' group by oi_checkkind ", "select"); BaseUtil.FillExpandDgvWithDataTable(CheckTypeDGV, dt, true,true); if (CheckTypeDGV.Rows.Count == 0) { OperateResult.AppendText(">>请先维护抽样计划\n", Color.Red); return; } string checkkind = ""; for (int i = 0; i < CheckTypeDGV.Rows.Count; i++) { checkkind +=("'"+ CheckTypeDGV.Rows[i].Cells["oi_itemcode"].Value.ToString() + "',") ; } sql.Clear(); sql.Append("select 1 choose,oi_checkkind,ois_sncode,nvl(ois_id,0) ois_id ,oi_itemcode ,nvl(ois_ifng,0) oi_ng,nvl(ois_defectlevel,'-1')"); sql.Append("oi_leveldefect,ois_remark,case nvl(ois_id,0) when 0 then '未检验' else '已检验' end ois_status from OQCItems "); sql.Append("left join OQCItemSamples on ois_sncode='" + sncode.Text + "' and ois_checkno=oi_checkno and "); sql.Append("ois_itemcode=oi_itemcode and ois_projectcode = oi_projectcode where oi_checkno ='" + ob_checkno.Text + "' "); sql.Append("and oi_checkkind in (" + checkkind.Substring(0,checkkind.Length-1) + ")"); TempForCheckType = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); sql.Clear(); sql.Append("select bg_code,bg_name from product left join productkind "); sql.Append("on pr_kind=pk_name left join PRODUCTBADGROUP on pk_code= pb_kindcode "); sql.Append("left join badgroup on bg_code=pb_badgroup where pr_code='" + ob_prodcode.Text + "'"); BadCode = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); BaseUtil.FillExpandDgvWithDataTable(WaitChooseDGV, BadCode, true,false); } private void Confirm_Click(object sender, EventArgs e) { //只能操作检验中或者待检验的批次 if (ob_status.Text == "待检验" || ob_status.Text == "检验中") { if (!CheckSnCode()) return; //判断检验项是否勾选,如勾选取出明细数据 for (int i = TempForCheckType.Rows.Count - 1; i >= 0; i--) { if (TempForCheckType.Rows[i][0].ToString() == "False") { TempForCheckType.Rows.RemoveAt(i); } } if (TempForCheckType != null && TempForCheckType.Rows.Count > 0) { //用于存放更新的数据 List ois_ifng_update = new List(); List ois_defectlevel_update = new List(); List ois_remark_update = new List(); List ois_id_update = new List(); //用于存放插入的数据 List ois_ifng_insert = new List(); List ois_defectlevel_insert = new List(); List ois_remark_insert = new List(); List oi_itemcode_insert = new List(); for (int i = 0; i < TempForCheckType.Rows.Count; i++) { //将布尔值转换为0,-1 if (TempForCheckType.Rows[i]["ois_id"].ToString() != "" && TempForCheckType.Rows[i]["ois_id"].ToString() != "0") { ois_ifng_update.Add(TempForCheckType.Rows[i]["oi_ng"].ToString()); ois_defectlevel_update.Add(TempForCheckType.Rows[i]["oi_leveldefect"].ToString()); ois_remark_update.Add(TempForCheckType.Rows[i]["ois_remark"].ToString()); ois_id_update.Add(TempForCheckType.Rows[i]["ois_id"].ToString()); } else { oi_itemcode_insert.Add(TempForCheckType.Rows[i]["oi_itemcode"].ToString()); ois_ifng_insert.Add(TempForCheckType.Rows[i]["oi_ng"].ToString()); ois_defectlevel_insert.Add(TempForCheckType.Rows[i]["oi_leveldefect"].ToString()); ois_remark_insert.Add(TempForCheckType.Rows[i]["ois_remark"].ToString()); } } //判断是否含不通过的内容 bool AllPass = true; for (int i = 0; i < ois_ifng_insert.Count; i++) { if (ois_ifng_insert[i] != "0") AllPass = false; } for (int i = 0; i < ois_ifng_update.Count; i++) { if (ois_ifng_update[i] != "0") AllPass = false; } if (ChoosedDGV.RowCount > 0 || AllPass) { //执行批量更新的SQL if (ois_id_update.Count > 0) { dh.BatchInsert("update OQCItemSamples set ois_ifng=:ois_ifng,ois_defectlevel=:ois_defectlevel,ois_remark=:ois_remark,ois_indate=sysdate where ois_id=:ois_id", new string[] { "ois_ifng", "ois_defectlevel", "ois_remark", "ois_id" }, ois_ifng_update.ToArray(), ois_defectlevel_update.ToArray(), ois_remark_update.ToArray(), ois_id_update.ToArray()); } //如果主键为空,并且存在需要插入的数据则执行插入操作 if (oi_itemcode_insert.Count > 0) { string[] id = new string[oi_itemcode_insert.Count]; for (int i = 0; i < id.Length; i++) { id[i] = dh.GetSEQ("OQCItemSamples_SEQ"); } sql.Clear(); sql.Append("insert when (not exists (select ois_id from OQCItemSamples where ois_checkno='" + ob_checkno.Text + "' and ois_sncode='" + sncode.Text + "' and ois_itemcode =:ois_itemcode)) then "); sql.Append("into OQCItemSamples (ois_id,ois_checkno,ois_makecode, ois_sncode,ois_projectcode,ois_itemcode,"); sql.Append("ois_ifng,ois_defectlevel,ois_remark,ois_indate)select :id,'" + ob_checkno.Text + "',"); sql.Append("'" + ob_makecode.Text + "','" + sncode.Text + "','" + ob_projectcode.Text + "',:ois_itemcode,:ois_ifng,:ois_defectlevel,:ois_remark,sysdate from dual"); dh.BatchInsert(sql.GetString(), new string[] { "ois_itemcode", "id", "ois_itemcode", "ois_ifng", "ois_defectlevel", "ois_remark" }, oi_itemcode_insert.ToArray(), id, oi_itemcode_insert.ToArray(), ois_ifng_insert.ToArray(), ois_defectlevel_insert.ToArray(), ois_remark_insert.ToArray()); } //采集不良信息 List bc_code = new List(); List bc_name = new List(); for (int i = 0; i < ChoosedDGV.RowCount; i++) { bc_code.Add(ChoosedDGV.Rows[i].Cells[1].Value.ToString()); bc_name.Add(ChoosedDGV.Rows[i].Cells[2].Value.ToString()); } if (bc_name.Count > 0) { dh.ExecuteSql("delete from OQCNGReason where or_sncode='" + sncode.Text + "' and or_checkno='" + ob_checkno.Text + "'", "delete"); sql.Clear(); sql.Append("insert into OQCNGReason(or_id, or_checkno, or_makecode, or_sncode, or_reasoncode, oi_description, oi_remark)"); sql.Append("values (OQCNGReason_seq.nextval,'" + ob_checkno.Text + "','" + ob_makecode.Text + "','" + sncode.Text + "',:a,:b,'" + ob_remark.Text + "')"); dh.BatchInsert(sql.GetString(), new string[] { "a", "b" }, bc_code.ToArray(), bc_name.ToArray()); } //更新项目编号中的抽检数、不合格数,根据采样项目记录 sql.Clear(); sql.Append("UPDATE OQCItems SET (oi_ngqty,oi_checkqty)=(select nvl(sum(case when "); sql.Append("nvl(ois_ifng,0)=0 then 0 else 1 end),0) ,count(1) from OQCItemSamples where ois_checkno=oi_checkno "); sql.Append("and ois_itemcode=oi_itemcode)where oi_checkno ='" + ob_checkno.Text + "' and oi_projectcode ='" + ob_projectcode.Text + "'"); dh.ExecuteSql(sql.GetString(), "update"); //更新检验状态,如果是待检验的更新为检验中 dh.ExecuteSql("update OQCBatch set ob_status='CHECKING' where ob_checkno='" + ob_checkno.Text + "' and ob_status='UNCHECK'", "update"); //更新批次中的合格数不合格数:用抽检批检验项目表获取最大的抽检数和不合格数 dh.ExecuteSql("update OQCBATCH set (ob_ngqty,ob_okqty)=(select nvl(max(oi_ngqty),0),max(oi_checkqty)-nvl(max(oi_ngqty),0) from OQCItems where oi_checkno ='" + ob_checkno.Text + "') where ob_checkno ='" + ob_checkno.Text + "'", "update"); OperateResult.AppendText(">>序列号 " + sncode.Text + "操作成功\n", Color.Green); GetBatch.PerformClick(); ChoosedDGV.DataSource = null; BaseUtil.CleanDGVData(CheckTypeDGV); BaseUtil.CleanDGVData(WaitChooseDGV); sncode.Text = ""; TempForCheckType.Clear(); TempForBadGroup.Clear(); sncode.Focus(); } else OperateResult.AppendText(">>含有未通过项请勾选不良明细\n", Color.Red); } else OperateResult.AppendText(">>请勾选送检明细\n", Color.Red); } else OperateResult.AppendText(">>必须是待检验或者检测中的送检批才能进行此操作\n", Color.Red); } private void BatchPass_Click(object sender, EventArgs e) { if (CheckBefore()) { if (int.Parse(ob_ngqty.Text == "" ? "0" : ob_ngqty.Text) <= int.Parse(ob_maxngacceptqty.Text == "" ? ob_maxngacceptqty.Text : ob_maxngacceptqty.Text)) { dh.ExecuteSql("update OQCBatch set ob_status='OK',ob_result='OK' where ob_checkno='" + ob_checkno.Text + "'", "select"); string ErrorMessage; LogicHandler.UpdateOQCMessage(ms_sncode.Text, ob_checkno.Text,ob_makecode.Text, "OQC批判过", User.UserSourceCode, User.UserName, "批次通过", out ErrorMessage); //记录操作日志 LogicHandler.InsertMakeProcess(ms_sncode.Text, ob_makecode.Text, User.UserSourceCode, "批结果判定", "批次通过", User.UserName); GetBatch.PerformClick(); OperateResult.AppendText(">>"+ob_checkno.Text+"通过批成功\n", Color.Green); } else OperateResult.AppendText(">>当前批次不合格数为" + ob_ngqty.Text + ",大于最大不合格允通过数" + ob_maxngacceptqty.Text + "\n", Color.Red); } } private bool CheckBefore() { if (ob_maxngacceptqty.Text == "" || ob_maxngacceptqty.Text == "0") { if (ob_aqlcode.Text == "") OperateResult.AppendText(">>最大不合格允许通过数为空的时候必须填写维护AQL标准\n", Color.Red); else { dt = (DataTable)dh.ExecuteSql("select nvl(ad_minqty,0)ad_minqty,nvl(ad_maxqty,0)ad_maxqty,nvl(ad_maxngacceptqty,0)ad_maxngacceptqty from QUA_Aql left join qua_aqldetail on ad_alid=al_id where al_code='" + ob_aqlcode.Text + "'", "select"); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { int batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text); int minqty = int.Parse(dt.Rows[i]["ad_minqty"].ToString()); int maxqty = int.Parse(dt.Rows[i]["ad_maxqty"].ToString()); if (batchqty > minqty && batchqty <= maxqty) { ob_maxngacceptqty.Text = dt.Rows[i]["ad_maxngacceptqty"].ToString(); return true; } } } } } else { sql.Clear(); sql.Append("select wm_concat(oi_itemcode) codes from OQCitems where "); sql.Append("nvl(oi_checkqty,0)0 and rownum<30 and oi_checkno='" + ob_checkno.Text + "'"); dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); if (dt.Rows.Count > 0) { if (dt.Rows[0]["codes"].ToString() != "") { OperateResult.AppendText(">>项目编号抽检数未达到样本数,项目编号[" + dt.Rows[0]["codes"].ToString() + "]\n", Color.Red); return false; } else return true; } } return false; } private void WaitReject_Click(object sender, EventArgs e) { BaseUtil.CleanDGVData(ChoosedDGV); } private void ChooseedReject_Click(object sender, EventArgs e) { if (TempForBadGroup != null) { for (int i = TempForBadGroup.Rows.Count - 1; i >= 0; i--) { if (TempForBadGroup.Rows[i][0].ToString() != "True") TempForBadGroup.Rows.RemoveAt(i); } BaseUtil.FillDgvWithDataTable(ChoosedDGV, TempForBadGroup.Copy()); } else OperateResult.AppendText(">>请先勾选不良明细\n", Color.Red); } private void CheckTypeDGV_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { if (!dgvr.Contains(CheckTypeDGV.Rows[e.RowIndex])) { string checkkind = CheckTypeDGV.Rows[e.RowIndex].Cells["oi_itemcode"].Value.ToString(); DataTable dt = BaseUtil.filterDataTable(TempForCheckType,"oi_checkkind='"+checkkind+"'"); DataGridViewRow headerRow = new DataGridViewRow(); DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell(); textcell.Value = ""; headerRow.Cells.Add(textcell); textcell = new DataGridViewTextBoxCell(); textcell.Value = "检验项目"; headerRow.Cells.Add(textcell); textcell = new DataGridViewTextBoxCell(); textcell.Value = "不通过"; headerRow.Cells.Add(textcell); textcell = new DataGridViewTextBoxCell(); textcell.Value = "缺陷等级"; headerRow.Cells.Add(textcell); textcell = new DataGridViewTextBoxCell(); textcell.Value = "备注"; headerRow.Cells.Add(textcell); textcell = new DataGridViewTextBoxCell(); textcell.Value = "检验状态"; headerRow.Cells.Add(textcell); headerRow.ReadOnly = true; CollapseDataGridViewRow cl = new CollapseDataGridViewRow(); try { cl = (CollapseDataGridViewRow)(CheckTypeDGV.Rows[e.RowIndex]); } catch { } cl.Rows.Add(headerRow); for (int i = 0; i < dt.Rows.Count; i++) { DataGridViewRow dataRow = new DataGridViewRow(); //标记展开的子行 dataRow.Tag = "SonRow"; DataGridViewCheckBoxCell checkcell = new DataGridViewCheckBoxCell(); checkcell.Tag = "SonRow"; checkcell.Value=true; dataRow.Cells.Add(checkcell); textcell = new DataGridViewTextBoxCell(); textcell.Value = dt.Rows[i]["oi_itemcode"].ToString(); dataRow.Cells.Add(textcell); textcell.ReadOnly = true; DataGridViewCheckBoxCell checkcel2 = new DataGridViewCheckBoxCell(); checkcel2.Value = dt.Rows[i]["oi_ng"].ToString() != "0" ? true : false; dataRow.Cells.Add(checkcel2); textcell.ReadOnly = false; DataGridViewComboBoxCell combocell = new DataGridViewComboBoxCell(); BaseUtil.SetDGVCellComboxData(combocell, "display", "value", LevelDefect); combocell.Value = dt.Rows[i]["oi_leveldefect"].ToString(); dataRow.Cells.Add(combocell); textcell.ReadOnly = false; textcell = new DataGridViewTextBoxCell(); textcell.Value = dt.Rows[i]["ois_remark"].ToString(); dataRow.Cells.Add(textcell); textcell.ReadOnly = false; textcell = new DataGridViewTextBoxCell(); textcell.Value = dt.Rows[i]["ois_status"].ToString(); dataRow.Cells.Add(textcell); textcell.ReadOnly = true; textcell = new DataGridViewTextBoxCell(); textcell.Value = dt.Rows[i]["ois_id"].ToString(); dataRow.Cells.Add(textcell); cl.Rows.Add(dataRow); } dgvr.Add(CheckTypeDGV.Rows[e.RowIndex]); } } private void CheckTypeDGV_DataError(object sender, DataGridViewDataErrorEventArgs e) { } private void WaitChooseDGV_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { if (!dgvr.Contains(WaitChooseDGV.Rows[e.RowIndex])) { string bg_code = WaitChooseDGV.Rows[e.RowIndex].Cells["bg_code"].Value.ToString(); DataTable dt = (DataTable)dh.ExecuteSql("select bc_code,bc_name from BADCODE where bc_groupcode='" + bg_code + "'", "select"); DataGridViewRow headerRow = new DataGridViewRow(); DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell(); textcell.Value = ""; headerRow.Cells.Add(textcell); textcell = new DataGridViewTextBoxCell(); textcell.Value = "不良代码"; headerRow.Cells.Add(textcell); textcell = new DataGridViewTextBoxCell(); textcell.Value = "不良原因"; headerRow.Cells.Add(textcell); headerRow.ReadOnly = true; CollapseDataGridViewRow cl = new CollapseDataGridViewRow(); try { cl = (CollapseDataGridViewRow)(WaitChooseDGV.Rows[e.RowIndex]); } catch (Exception) { } cl.Rows.Add(headerRow); for (int i = 0; i < dt.Rows.Count; i++) { DataGridViewRow dataRow = new DataGridViewRow(); //标记展开的子行 dataRow.Tag = "SonRow"; DataGridViewCheckBoxCell checkcell = new DataGridViewCheckBoxCell(); checkcell.Tag = "SonRow"; dataRow.Cells.Add(checkcell); textcell = new DataGridViewTextBoxCell(); textcell.Value = dt.Rows[i]["bc_code"].ToString(); dataRow.Cells.Add(textcell); textcell = new DataGridViewTextBoxCell(); textcell.Value = dt.Rows[i]["bc_name"].ToString(); dataRow.Cells.Add(textcell); cl.Rows.Add(dataRow); } dgvr.Add(WaitChooseDGV.Rows[e.RowIndex]); } } //如果当前行的Cell有修改过就将CheckBox勾选上 private void CheckTypeDGV_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex > 0 && e.ColumnIndex > 0) CheckTypeDGV.Rows[e.RowIndex].Cells[0].Value = true; try { if (CheckTypeDGV.Rows[e.RowIndex].Cells[0].Tag != null) { if (CheckTypeDGV.Rows[e.RowIndex].Cells[0].Tag.ToString() == "SonRow") BaseUtil.GetExpandDGVCheckedRow(CheckTypeDGV, TempForCheckType, e.RowIndex, 1); } } catch (Exception) { } } private void WaitChooseDGV_DataError(object sender, DataGridViewDataErrorEventArgs e) { } private void WaitChooseDGV_CellContentClick(object sender, DataGridViewCellEventArgs e) { BaseUtil.ExpandDGVCheck(WaitChooseDGV, e); } private void CheckTypeDGV_CellContentClick(object sender, DataGridViewCellEventArgs e) { BaseUtil.ExpandDGVCheck(CheckTypeDGV, e); } private void bccode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { dt = (DataTable)dh.ExecuteSql("select bc_code bg_code,bc_name bg_name from badgroup left join badcode on bg_code=bc_groupcode where bc_code='" + bccode.Text + "'", "select"); if (dt.Rows.Count > 0) { try { dt.Merge(ChoosedDGV.DataSource as DataTable); BaseUtil.FillDgvWithDataTable(ChoosedDGV, dt); } catch (Exception) { } } else OperateResult.AppendText(">>不良代码不存在\n", Color.Red); } } private void ob_checkno_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { GetBatch.PerformClick(); } } private void ms_sncode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) GetBatch.PerformClick(); } private void obd_outboxcode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) GetBatch.PerformClick(); } private void WaitChooseDGV_CellValueChanged(object sender, DataGridViewCellEventArgs e) { try { if (e.ColumnIndex == 0) { if (WaitChooseDGV.Rows[e.RowIndex].Cells[0].Tag != null) { if (WaitChooseDGV.Rows[e.RowIndex].Cells[0].Tag.ToString() == "SonRow") BaseUtil.GetExpandDGVCheckedRow(WaitChooseDGV, TempForBadGroup, e.RowIndex, 1); } } } catch (Exception) { } } } }