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_NewBadCode : 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 macode = "";
DataHelper dh;
DataTable dt;
LogStringBuilder sql = new LogStringBuilder();
public Make_NewBadCode(string iSnCode, string iMakeCode)
{
InitializeComponent();
sncode = iSnCode;
macode = iMakeCode;
}
private void Make_NewBadCode_Load(object sender, EventArgs e)
{
dh = SystemInf.dh;
ms_makecode.Text = macode;
ms_sncode.Text = sncode;
string pr_code = dh.getFieldDataByCondition("makeserial", "ms_prodcode", "ms_sncode='" + sncode + "' and ms_makecode='" + macode + "'").ToString();
sql.Clear();
sql.Append("select bg_code,bg_name from product left join productkind on pr_kind =pk_name ");
sql.Append("left join productbadgroup on pb_kindcode= pk_code left join badgroup on ");
sql.Append("bg_code=pb_badgroup where pr_code='" + pr_code + "' and nvl(pb_kindcode,'')<>'' and bg_statuscode='AUDITED' order by case when bg_code='" + User.DefaultBadGroup + "' then 0 else 1 end");
dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
//如果未查询到数据则获取全部的不良组别
if (dt.Rows.Count == 0)
{
sql.Clear();
sql.Append("select bg_code,bg_name from badgroup where bg_statuscode='AUDITED' order by case when bg_code='" + User.DefaultBadGroup + "' then 0 else 1 end");
dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
badgroup.DataSource = dt;
badgroup.DisplayMember = "bg_name";
badgroup.ValueMember = "bg_code";
LoadBadCodeListView();
}
else
{
badgroup.DataSource = dt;
badgroup.DisplayMember = "bg_name";
badgroup.ValueMember = "bg_code";
LoadBadCodeListView();
}
}
private void ChooseedReject_Click(object sender, EventArgs e)
{
int CheckedNum = 0;
//是否已经添加到不良
bool AddToReject = false;
for (int i = WaitRejectList.Items.Count - 1; i >= 0; i--)
{
if (WaitRejectList.Items[i].Checked)
{
AddToReject = false;
for (int j = 0; j < ChoosedRejectList.Items.Count; j++)
{
if ((ChoosedRejectList.Items[j].SubItems[3].Text == WaitRejectList.Items[i].SubItems[3].Text))
{
AddToReject = true;
break;
}
}
if (!AddToReject)
{
WaitRejectList.Items[i].Checked = false;
ChoosedRejectList.Items.Add((ListViewItem)WaitRejectList.Items[i].Clone());
WaitRejectList.Items[i].Remove();
CheckedNum++;
}
}
}
if (CheckedNum == 0)
{
for (int i = WaitRejectList.Items.Count - 1; i >= 0; i--)
{
AddToReject = false;
for (int j = 0; j < ChoosedRejectList.Items.Count; j++)
{
if ((ChoosedRejectList.Items[j].SubItems[3].Text == WaitRejectList.Items[i].SubItems[3].Text))
{
AddToReject = true;
break;
}
}
if (!AddToReject)
{
ChoosedRejectList.Items.Add((ListViewItem)WaitRejectList.Items[i].Clone());
WaitRejectList.Items[i].Remove();
CheckedNum++;
}
}
}
}
private void WaitReject_Click(object sender, EventArgs e)
{
int CheckedNum = 0;
//已存在在ListView中的Item是不能添加到其他ListView中的,需要调用其克隆的方法
for (int i = ChoosedRejectList.Items.Count - 1; i >= 0; i--)
{
if (ChoosedRejectList.Items[i].Checked)
{
ChoosedRejectList.Items[i].Checked = false;
WaitRejectList.Items.Add((ListViewItem)ChoosedRejectList.Items[i].Clone());
ChoosedRejectList.Items[i].Remove();
CheckedNum++;
}
}
if (CheckedNum == 0)
{
for (int i = ChoosedRejectList.Items.Count - 1; i >= 0; i--)
{
WaitRejectList.Items.Add((ListViewItem)ChoosedRejectList.Items[i].Clone());
ChoosedRejectList.Items[i].Remove();
}
}
}
private void Cancel_Click(object sender, EventArgs e)
{
Close();
}
private void Save_Click(object sender, EventArgs e)
{
if (ms_sncode.Text == "" || ms_makecode.Text == "")
{
MessageBox.Show("序列号和工单号不允许为空");
return;
}
int RejectCount = ChoosedRejectList.Items.Count;
string[] bccode = new string[RejectCount];
string[] bgcode = new string[RejectCount];
string[] id = new string[RejectCount];
string ErrorMessage = "";
if (ChoosedRejectList.Items.Count == 0)
{
MessageBox.Show("不良品必须选择不良原因和不良代码!");
return;
}
else
{
//用户未填写不良代码一切按照正常逻辑,填写了从index=1开始执行
for (int i = 0; i < RejectCount; i++)
{
bgcode[i] = ChoosedRejectList.Items[i].SubItems[1].Text;
//获取所有的不良代码
bccode[i] = ChoosedRejectList.Items[i].SubItems[3].Text;
//获取所有的不良备注
id[i] = dh.GetSEQ("MakeBad_seq");
}
}
sql.Clear();
sql.Append("insert when (not exists(select 1 from makebad where mb_sncode='" + sncode + "' and mb_makecode='" + macode + "' and ");
sql.Append("mb_status=0 and mb_badcode=:bc_code)) then into makebad(mb_id,mb_makecode,mb_mscode,mb_sncode,mb_inman,");
sql.Append("mb_indate,mb_stepcode,mb_sourcecode,mb_badcode,mb_bgcode,mb_badtable,mb_status)");
sql.Append("select DISTINCT :id,ma_code,ms_code,ms_sncode,'" + User.UserCode + "',sysdate,'" + User.CurrentStepCode + "',ms_sourcecode,:bc_code,:bg_code,'',");
sql.Append("'0' from make left join makeSerial on ms_makecode=ma_code left join stepProduct on ");
sql.Append("sp_mothercode=ma_prodcode and sp_stepcode=ms_nextstepcode and ms_craftcode = sp_craftcode where ms_sncode='" + sncode + "' and ms_makecode = '" + macode + "'");
dh.BatchInsert(sql.GetString(), new string[] { "bc_code", "id", "bc_code", "bg_code" }, bccode, id, bccode, bgcode);
MessageBox.Show("保存成功");
Close();
}
private void badgroup_SelectedIndexChanged(object sender, EventArgs e)
{
LoadBadCodeListView();
}
///
/// 加载不良代码的ListView的信息
///
private void LoadBadCodeListView()
{
string bg_id = badgroup.SelectedValue.ToString();
sql.Clear();
sql.Append("select bg_code,bg_name,bc_code,bc_name,bc_note from badgroupdetail left join badgroup on bgd_bgid=bg_id ");
sql.Append("left join badcode on bgd_badcode=bc_code where bg_code='" + ((bg_id != "" && bg_id != "System.Data.DataRowView") ? bg_id.ToString() : "") + "' and bg_code is not null ");
dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
BadInfo.DataSource = dt;
//往listview中添加数据
WaitRejectList.Items.Clear();
WaitRejectList.BeginUpdate();
for (int i = 0; i < dt.Rows.Count; i++)
{
ListViewItem lvi = new ListViewItem();
//第一列是勾选列,设置列头文本为空
lvi.Text = "";
for (int j = 0; j < dt.Columns.Count; j++)
lvi.SubItems.Add(dt.Rows[i][j].ToString());
WaitRejectList.Items.Add(lvi);
}
WaitRejectList.EndUpdate();
}
private void headBar1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
private void Filter_UserControlTextChanged(object sender, EventArgs e)
{
try
{
DataRow[] dr = (BadInfo.DataSource as DataTable).Select(" bc_name like '%" + Filter.Text + "%'");
DataTable dt = (BadInfo.DataSource as DataTable).Clone();
for (int i = 0; i < dr.Length; i++)
{
dt.Rows.Add(dr[i].ItemArray);
}
WaitRejectList.Items.Clear();
WaitRejectList.BeginUpdate();
for (int i = 0; i < dt.Rows.Count; i++)
{
ListViewItem lvi = new ListViewItem();
//第一列是勾选列,设置列头文本为空
lvi.Text = "";
for (int j = 0; j < dt.Columns.Count; j++)
lvi.SubItems.Add(dt.Rows[i][j].ToString());
WaitRejectList.Items.Add(lvi);
}
WaitRejectList.EndUpdate();
}
catch (Exception)
{
}
}
}
}