|
|
|
|
using BarTenderPrint;
|
|
|
|
|
using Mesnac.Codd.Session;
|
|
|
|
|
using Mesnac.Compressor.Data;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace FrmPrint
|
|
|
|
|
{
|
|
|
|
|
public partial class Form1 : Form
|
|
|
|
|
{
|
|
|
|
|
protected DbHandler db = new DbHandler();
|
|
|
|
|
DbHelper dbHelper = null;
|
|
|
|
|
SerialEquip Com;
|
|
|
|
|
string Port = System.Configuration.ConfigurationManager.AppSettings["Com"];
|
|
|
|
|
public delegate void Transit(string data);
|
|
|
|
|
public Form1()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
Console.WriteLine("开始初始化");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string constr = System.Configuration.ConfigurationManager.AppSettings["SqlString"];
|
|
|
|
|
DbSession dbsession = new DbSession(SqlClientFactory.Instance, constr);
|
|
|
|
|
dbHelper = new DbHelper(dbsession);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("数据库连接异常,请检查网络");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
BindClass();
|
|
|
|
|
bindMachine();
|
|
|
|
|
InitPort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InitPort()
|
|
|
|
|
{
|
|
|
|
|
//Com = new SerialEquip(Port);
|
|
|
|
|
//Com.OpenCom(null);
|
|
|
|
|
//Com.SerialData += Receive;
|
|
|
|
|
}
|
|
|
|
|
public void Receive(string Data)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Reresh(Data);
|
|
|
|
|
PrintPrepare(Data);
|
|
|
|
|
}
|
|
|
|
|
catch(Exception e)
|
|
|
|
|
{
|
|
|
|
|
//ICSharpCode.Core.LoggingService.Debug(e.ToString())
|
|
|
|
|
Console.WriteLine(e.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PrintPrepare(string Data)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(Data) || Data.Length < 4)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("条码长度不够");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Print pt= new FrmPrint.Print();
|
|
|
|
|
pt.barcode = Data;
|
|
|
|
|
////查询当前机种信息
|
|
|
|
|
pt.Machine= Convert.ToInt32(cb_machine.SelectedValue);
|
|
|
|
|
//pt.Machine = 1;//没办法加载,强制置1
|
|
|
|
|
pt.Shift= getShift(cb_shift.SelectedValue.ToString());
|
|
|
|
|
|
|
|
|
|
ThreadPool.QueueUserWorkItem(new WaitCallback(Print), pt);
|
|
|
|
|
//Print(Data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 更新barcode
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="barcode"></param>
|
|
|
|
|
private void Reresh(string barcode)
|
|
|
|
|
{
|
|
|
|
|
if (this.InvokeRequired)
|
|
|
|
|
{
|
|
|
|
|
this.Invoke(new Transit(Reresh), barcode);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.txt_barcode.Text = barcode.ToString();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 机种绑定
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void bindMachine()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
dbHelper.ClearParameter();
|
|
|
|
|
dbHelper.CommandType = CommandType.Text;
|
|
|
|
|
dbHelper.CommandText = "SELECT ProductID,ProductName FROM dbo.T_BD_ProductInfo";
|
|
|
|
|
DataTable dt= dbHelper.ToDataTable();
|
|
|
|
|
|
|
|
|
|
cb_machine.DisplayMember = "ProductName";
|
|
|
|
|
cb_machine.ValueMember = "ProductID";
|
|
|
|
|
cb_machine.DataSource = dt;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("机种绑定失败");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Print(object print)
|
|
|
|
|
{
|
|
|
|
|
lock (string.Empty)
|
|
|
|
|
{
|
|
|
|
|
var pt = print as Print;
|
|
|
|
|
if(pt==null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
PrintManager pm = new PrintManager();
|
|
|
|
|
pm.Print(pt.barcode,pt.Machine,pt.Shift);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void BindClass()
|
|
|
|
|
{
|
|
|
|
|
DataTable dt = new DataTable();
|
|
|
|
|
dt.Columns.Add("key", typeof(string));
|
|
|
|
|
dt.Columns.Add("value", typeof(string));
|
|
|
|
|
DataRow dr = dt.NewRow();
|
|
|
|
|
dr[0] = "01";
|
|
|
|
|
dr[1] = "早班";
|
|
|
|
|
dt.Rows.Add(dr);
|
|
|
|
|
|
|
|
|
|
DataRow dr2 = dt.NewRow();
|
|
|
|
|
dr2[0] = "02";
|
|
|
|
|
dr2[1] = "中班";
|
|
|
|
|
dt.Rows.Add(dr2);
|
|
|
|
|
|
|
|
|
|
DataRow dr3 = dt.NewRow();
|
|
|
|
|
dr3[0] = "03";
|
|
|
|
|
dr3[1] = "晚班";
|
|
|
|
|
dt.Rows.Add(dr3);
|
|
|
|
|
|
|
|
|
|
this.cb_shift.DataSource = dt;
|
|
|
|
|
this.cb_shift.DisplayMember = "value";
|
|
|
|
|
this.cb_shift.ValueMember = "key";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
private string getShift(string shift)
|
|
|
|
|
{
|
|
|
|
|
string shiftA = "A";
|
|
|
|
|
switch (shift)
|
|
|
|
|
{
|
|
|
|
|
case "01":
|
|
|
|
|
shiftA = "A";
|
|
|
|
|
break;
|
|
|
|
|
case "02":
|
|
|
|
|
shiftA = "B";
|
|
|
|
|
break;
|
|
|
|
|
case "03":
|
|
|
|
|
shiftA = "C";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return shiftA;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string getDateString(string semiBarcode)
|
|
|
|
|
{
|
|
|
|
|
string datestring = "20";
|
|
|
|
|
if (string.IsNullOrEmpty(semiBarcode))
|
|
|
|
|
{
|
|
|
|
|
datestring = DateTime.Now.ToString();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string year = semiBarcode.Substring(1, 2);
|
|
|
|
|
string month = semiBarcode.Substring(3, 2);
|
|
|
|
|
string date = semiBarcode.Substring(5, 2);
|
|
|
|
|
|
|
|
|
|
datestring += year + "-" + month + "-" + date;
|
|
|
|
|
}
|
|
|
|
|
return datestring;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DataTable GetCurrentMachine(string MachineID)
|
|
|
|
|
{
|
|
|
|
|
if (dbHelper == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
dbHelper.ClearParameter();
|
|
|
|
|
dbHelper.CommandType = CommandType.Text;
|
|
|
|
|
dbHelper.CommandText = "";
|
|
|
|
|
string sql = " SELECT * FROM dbo.T_BD_ProductInfo WHERE ProductID='" + MachineID + "'";
|
|
|
|
|
dbHelper.CommandText = sql;
|
|
|
|
|
Console.WriteLine("sql语句:"+sql);
|
|
|
|
|
return dbHelper.ToDataTable();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("数据查询错误:"+ex.ToString());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string LastWorkProductionInfo(string barcode, string TableName)
|
|
|
|
|
{
|
|
|
|
|
if (dbHelper == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
dbHelper.ClearParameter();
|
|
|
|
|
dbHelper.CommandType = CommandType.Text;
|
|
|
|
|
dbHelper.CommandText = "";
|
|
|
|
|
sb.Append(" SELECT TOP(1) State FROM " + TableName + " WHERE ScanBarcode='").Append(barcode).Append("' order by InsertTime desc ");
|
|
|
|
|
dbHelper.CommandText = sb.ToString();
|
|
|
|
|
DataTable dt = dbHelper.ToDataTable();
|
|
|
|
|
|
|
|
|
|
return dbHelper.ToScalar().ToString();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("数据查询错误:" + ex.ToString());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btn_close_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
this.Com.Comm.Close();
|
|
|
|
|
this.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btn_print_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
PrintPrepare(txt_barcode.Text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Form1_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Print
|
|
|
|
|
{
|
|
|
|
|
public int Machine;
|
|
|
|
|
public string Shift = "";
|
|
|
|
|
public string barcode = "";
|
|
|
|
|
}
|
|
|
|
|
}
|