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_DE : 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 string[] bccode;
public string[] bgcode;
public Make_NewBadCode_DE(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;
bccode = new string[RejectCount];
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;
}
}
}
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)
{
}
}
}
}