using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
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_NewMatainInf : CustomControl.BaseForm.BaseForm
{
//所有用到了headBar的部分都需要这段代码
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
public static extern int GetWindowLong(HandleRef hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
///
/// 序列号
///
string sncode = "";
///
/// 不良组代码
///
string bgcode = "";
///
/// 不良代码
///
string bccode = "";
///
/// MakeSerial表ID
///
string msid = "";
///
/// MakeBad表主键
///
string mbid = "";
DataHelper dh;
DataTable dt;
LogStringBuilder sql = new LogStringBuilder();
public Make_NewMatainInf(string iBgName, string iBgCode, string iBcName, string iBccode, string iSnCode, string iMsID, string iMbID)
{
InitializeComponent();
bg_name.Text = iBgName;
bc_name.Text = iBcName;
bgcode = iBgCode;
bccode = iBccode;
sncode = iSnCode;
msid = iMsID;
mbid = iMbID;
}
private void Make_NewMatainInf_Load(object sender, EventArgs e)
{
dh = SystemInf.dh;
string pr_code = dh.getFieldDataByCondition("makeserial", "ms_prodcode", "ms_sncode='" + sncode + "'").ToString();
string pk_code = dh.getFieldDataByCondition("product left join productkind on pk_name=pr_kind", "pk_code", "pr_code='" + pr_code + "'").ToString();
//加载不良原因组数据
sql.Clear();
sql.Append("select nrg_code,nrg_name from PRODUCTBADREASONGROUP left join ");
sql.Append("QUA_NGReasonGroup on nrg_code=pbr_brgcode where pbr_kindcode='" + pk_code + "'");
dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
//未查询到则查询全部数据
if (dt.Rows.Count == 0)
dt = (DataTable)dh.ExecuteSql("select nrg_code,nrg_name from QUA_NGReasonGroup where nrg_statuscode ='AUDITED'", "select");
AddDataToListView(nrg_name_lsv, dt);
//加载不良原因
string nrg_code = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i == dt.Rows.Count - 1)
nrg_code += "'" + dt.Rows[i]["nrg_code"] + "'";
else
nrg_code += "'" + dt.Rows[i]["nrg_code"] + "',";
}
//加载责任别
dt = (DataTable)dh.ExecuteSql("select nd_code,nd_name from ngduty", "select");
AddDataToListView(mbr_dutycode_lsv, dt);
//加载解决方案
sql.Clear();
sql.Append("select so_code,so_name from PRODUCTSOLUTION left join solution on ");
sql.Append("so_code=ps_socode where ps_kindcode='" + pk_code + "'");
dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
if (dt.Rows.Count == 0)
dt = (DataTable)dh.ExecuteSql("select so_code,so_name from solution order by so_id", "select");
AddDataToListView(mbr_solutioncode_lsv, dt);
for (int i = 0; i < nrg_name_lsv.Items.Count; i++)
{
if (nrg_name_lsv.Items[i].SubItems[0].Text == bgcode)
{
ListView_ItemSelectionChanged(nrg_name_lsv, new ListViewItemSelectionChangedEventArgs(nrg_name_lsv.Items[i], i, true));
nrg_name_lsv.Items[i].Selected = true;
}
}
}
///
/// 往ListView添加数据
///
///
///
private void AddDataToListView(ListView lsv, DataTable dt)
{
lsv.Items.Clear();
lsv.BeginUpdate();
for (int i = 0; i < dt.Rows.Count; i++)
{
ListViewItem lvi = new ListViewItem(dt.Rows[i][0].ToString());
//第一列是勾选列,设置列头文本为空
for (int j = 1; j < dt.Columns.Count; j++)
lvi.SubItems.Add(dt.Rows[i][j].ToString());
lsv.Items.Add(lvi);
}
lsv.EndUpdate();
}
private void Cancel_Click(object sender, EventArgs e)
{
Close();
}
private int GetListViewCheckCount(ListView lsv)
{
int CheckedNum = 0;
//已存在在ListView中的Item是不能添加到其他ListView中的,需要调用其克隆的方法
for (int i = lsv.Items.Count - 1; i >= 0; i--)
{
if (lsv.Items[i].Selected)
CheckedNum++;
}
return CheckedNum;
}
private string GetListViewSelectedItemText(ListView lsv, int ItemIndex)
{
for (int i = lsv.Items.Count - 1; i >= 0; i--)
{
if (lsv.Items[i].Selected)
return lsv.Items[i].SubItems[ItemIndex].Text;
}
return null;
}
private void Save_Click(object sender, EventArgs e)
{
if (bg_name.Text == "" || bc_name.Text == "")
{
MessageBox.Show("不良代码组和不良代码不能为空");
return;
}
string[] mbc_component = new string[GetListViewCheckCount(mbc_component_lsv)];
string[] nrg_name = new string[GetListViewCheckCount(nrg_name_lsv)];
string[] nr_name = new string[GetListViewCheckCount(nr_name_lsv)];
string[] mbr_dutycode = new string[GetListViewCheckCount(mbr_dutycode_lsv)];
string ErrorMessage = "";
if (nrg_name.Length == 0)
ErrorMessage += " 不良原因组 ";
if (nr_name.Length == 0)
ErrorMessage += " 不良原因 ";
if (mbr_dutycode.Length == 0)
ErrorMessage += " 责任别 ";
if (ErrorMessage == "")
{
for (int i = 0; i < mbc_component_lsv.Items.Count; i++)
{
if (mbc_component_lsv.Items[i].Checked)
{
mbc_component_lsv.Items[i].Selected = true;
}
else
{
mbc_component_lsv.Items[i].Selected = false;
}
}
string mbccomponent = GetListViewSelectedItemText(mbc_component_lsv, 0);
string nrgcode = GetListViewSelectedItemText(nrg_name_lsv, 0);
string nrgname = GetListViewSelectedItemText(nrg_name_lsv, 1);
string nrcode = GetListViewSelectedItemText(nr_name_lsv, 0);
string nrname = GetListViewSelectedItemText(nr_name_lsv, 1);
string mbrdutycode = GetListViewSelectedItemText(mbr_dutycode_lsv, 0);
string mbrdutyname = GetListViewSelectedItemText(mbr_dutycode_lsv, 1);
string mbrsolutioncode = GetListViewSelectedItemText(mbr_solutioncode_lsv, 0);
string mbrsolutionname = GetListViewSelectedItemText(mbr_solutioncode_lsv, 1);
dt = (DataTable)dh.ExecuteSql("select ms_makecode from makeserial where ms_id='" + msid + "' and ms_sncode='" + sncode + "' and (ms_status=3 or ms_reworkstatus=3) ", "select");
if (dt.Rows.Count > 0)
{
string macode = dt.Rows[0]["ms_makecode"].ToString();
string mbr_id = dh.GetSEQ("makebadreason_seq");
//存在不良组件进行提示
if (dh.CheckExist("makebadreason left join makebad on mbr_mbid=mb_id and mbr_sncode=mb_sncode", "mbr_badcode='" + bccode + "' and mbr_sncode='" + sncode + "' and mbr_brcode='" + nrcode + "' and mb_status=0"))
{
ErrorMessage = "不良代码【" + bc_name.Text + "】已存在不良原因【" + nrname + "】\n";
}
if (ErrorMessage != "")
{
MessageBox.Show(ErrorMessage);
return;
}
else
{
//保存不良组件
sql.Clear();
sql.Append("insert into makebadrscom (mbc_id,mbc_mbrid,mbc_component,mbc_badcode,");
sql.Append("mbc_brcode,mbc_sncode,mbc_makecode,mbc_indate,mbc_inman ) values ");
sql.Append("(makebadrscom_seq.nextval,'" + mbr_id + "' ,'" + mbccomponent + "',");
sql.Append("'" + bccode + "','" + nrcode + "','" + sncode + "','" + macode + "',sysdate,'" + User.UserCode + "')");
dh.ExecuteSql(sql.GetString(), "insert");
Close();
}
if (mbccomponent != "" || mbccomponent != null)
{
//保存不良原因
sql.Clear();
sql.Append("insert into makebadreason (mbr_mbid,mbr_id,mbr_brcode,mbr_solutioncode,");
sql.Append("mbr_dutycode,mbr_brgcode,mbr_badcode,mbr_sncode,mbr_makecode,mbr_indate,");
sql.Append("mbr_inman,mbr_brname,mbr_solutionname,mbr_badname,mbr_brgname,mbr_dutyname) select '" + mbid + "','" + mbr_id + "','" + nrcode + "',");
sql.Append("'" + mbrsolutioncode + "','" + mbrdutycode + "','" + nrgcode + "','" + bccode + "',");
sql.Append("'" + sncode + "','" + macode + "',sysdate,'" + User.UserCode + "','" + nrname + "','" + mbrsolutionname + "','" + bc_name.Text + "','" + nrgname + "','" + mbrdutyname + "' from dual");
dh.ExecuteSql(sql.GetString(), "insert");
}
}
else MessageBox.Show("序列号错误,不存在或者不处于维修状态");
}
else MessageBox.Show(ErrorMessage + "未勾选");
}
private void ListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
e.Item.BackColor = Color.LightBlue;
foreach (ListViewItem item in (sender as ListView).Items)
{
if (item != e.Item)
item.BackColor = Color.White;
}
}
private void nrg_name_lsv_SelectedIndexChanged(object sender, EventArgs e)
{
string nrg_code = "";
if (nrg_name_lsv.SelectedItems.Count > 0) //或者使用SelectedItem!=null判断
{
nrg_code = nrg_name_lsv.SelectedItems[0].SubItems[0].Text;
}
sql.Clear();
sql.Append("select nr_code,nr_name,nr_group from QUA_NGREASONGROUPDET left join QUA_NGREASONGROUP ");
sql.Append("on nrgd_nrgid=nrg_id left join QUA_NGReason on nrgd_nrcode=nr_code where ");
sql.Append("nrg_code in('" + (nrg_code == "" ? "''" : nrg_code) + "')");
dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
AddDataToListView(nr_name_lsv, dt);
for (int i = 0; i < nr_name_lsv.Items.Count; i++)
{
if (nr_name_lsv.Items[i].SubItems[0].Text == bccode)
{
ListView_ItemSelectionChanged(nr_name_lsv, new ListViewItemSelectionChangedEventArgs(nr_name_lsv.Items[i], i, true));
nr_name_lsv.Items[i].Selected = true;
}
}
}
private void headBar1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
private void mbc_component_lsv_ItemChecked(object sender, ItemCheckedEventArgs e)
{
if (e.Item.Checked)
{
foreach (ListViewItem item in this.mbc_component_lsv.CheckedItems)
{
if (item != e.Item)
{
item.Checked = false;
}
}
}
}
}
}