using LabelManager2; using Seagull.BarTender.Print; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Windows.Forms; using UAS_MES_NEW.DataOperate; using UAS_MES_NEW.Entity; using UAS_MES_NEW.PublicForm; using UAS_MES_NEW.PublicMethod; namespace UAS_MES_NEW.Make { public partial class Make_CustomLabelPrint : Form { DataHelper dh; LogStringBuilder sql = new LogStringBuilder(); AutoSizeFormClass asc = new AutoSizeFormClass(); Thread InitPrint; //存放产品信息 DataTable dt; //存放标签信息 DataTable listA; //存放标签参数列表信息 DataTable paramsInfo; //记录当前的步骤数 int currentIndex = 0; FileInfo info; Engine engine; //ftpOperater ftp; LabelFormatDocument format; StringBuilder sb = new StringBuilder(); bool canPrint = false; string[] inputValues;//存放用户输入的参数值 public Make_CustomLabelPrint() { InitializeComponent(); } private void Make_CustomLablePrint_Load(object sender, EventArgs e) { asc.controllInitializeSize(this); OperateResult.AppendText(">>请输入产品代码\n", Color.Black); InitPrint = new Thread(InPrint); SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序"); BaseUtil.SetFormCenter(stw); stw.ShowDialog(); pr_code.Focus(); dh = SystemInf.dh; } private void InPrint() { try { engine = new Engine(true); } catch (Exception ex) { OperateResult.AppendText(ex.Message+ex.StackTrace, Color.Red); } } private void pr_code_KeyDown(object sender, KeyEventArgs e) { //按下enter键 if (e.KeyCode == Keys.Enter) { //判断产品是否存在 dt = (DataTable)dh.ExecuteSql("select pr_code,pr_detail,pr_spec from product where pr_code='" + pr_code.Text + "' and pr_statuscode ='AUDITED'", "select"); //查询没有值,返回提示用户“产品:XXX,不存在或者为审核”,清空产品代码输入框中的值 if (dt.Rows.Count == 0) { OperateResult.AppendText("<<产品:" + pr_code.Text + ",不存在或者未审核\n", Color.Red, pr_code); return; } //查询产品是否有自定义的标签模板 sql.Clear(); sql.Append("select la_code,la_id,substr(la_url,instr(la_url,'\\',-1)+1)la_name,la_url,la_isdefault "); sql.Append("from label where la_prodcode='" + pr_code.Text + "' and la_templatetype='自定义' and la_statuscode='AUDITED' order by la_isdefault"); listA = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); if (listA.Rows.Count == 0) { OperateResult.AppendText("<<产品:" + pr_code.Text + ",没有维护自定义模板\n", Color.Red, pr_code); return; } if (listA.Rows[0]["la_isdefault"].ToString() != "-1") { //说明没有默认的模板,那就取pl_id最大的 listA = (DataTable)dh.ExecuteSql("select la_code,la_id,substr(la_url,instr(la_url,'\\',-1)+1)la_name,la_url,la_isdefault from label where la_prodcode='" + pr_code.Text + "' and la_templatetype='自定义' and la_statuscode='AUDITED' order by la_isdefault", "select"); } //标签显示 PrintLabel.DataSource = listA; PrintLabel.DisplayMember = "la_url"; PrintLabel.ValueMember = "la_id"; //有值,填充界面 BaseUtil.SetFormValue(this.Controls, dt); //paramsInfo = (DataTable)dh.ExecuteSql("select lp_detno, lp_name, lp_valuetype,lp_sql from label left join labelparameter on lp_laid=la_id where la_code='"+ listA.Rows[0]["pl_labelcode"].ToString() + "' order by lp_detno asc", "select"); } } private void inputText_KeyDown(object sender, KeyEventArgs e) { //按下enter键 if (e.KeyCode == Keys.Enter) { //产品编号为空 if (pr_code.Text.Trim() == "") { OperateResult.AppendText("<<请先输入产品编号\n", Color.Red); return; } if (inputText.Text.Trim() == "") { OperateResult.AppendText("<<输入不能为空\n", Color.Red); return; } OperateResult.AppendText(">>" + inputText.Text + "\n", Color.Black); if (paramsInfo == null || paramsInfo.Rows.Count == 0) { OperateResult.AppendText("<<无可用标签\n", Color.Red, inputText); return; } else { //下标为1的时候实例化存值的数组 if (currentIndex == 1) { inputValues = new string[paramsInfo.Rows.Count]; sb = new StringBuilder(); string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'").ToString(); FileInfo PrintFile = new FileInfo(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString()); if (PrintFile == null) { MessageBox.Show("打印文件不存在"); return; } string filechangetime = PrintFile.LastWriteTime.ToString(); if (filechangetime != filelastwritetime) { engine = new Engine(true); filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"); string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')"; dh.UpdateByCondition("label", update, "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'"); } format = engine.Documents.Open(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString()); } //将值赋到format对应的变量 for (int i = 0; i < format.SubStrings.Count; i++) { //找到对应的变量名 if (format.SubStrings[i].Name == paramsInfo.Rows[currentIndex - 1]["lp_name"].ToString()) { //如果当时采集行的 lp_valuetype=’INPUT’,并且lp_sql 为空,则直接将采集的值赋给标签打印 if (paramsInfo.Rows[currentIndex - 1]["lp_valuetype"].ToString() == "INPUT" && paramsInfo.Rows[currentIndex - 1]["lp_sql"].ToString() == "") { format.SubStrings[i].Value = inputText.Text; } else { //取sql拿值赋给变量 string sql = paramsInfo.Rows[currentIndex - 1]["lp_sql"].ToString(); Regex ConnoteA = new Regex("{\\w+}"); foreach (Match mch in ConnoteA.Matches(sql)) { string x = mch.Value.Trim(); sql = sql.Replace(x, "'" + inputText.Text + "'"); } format.SubStrings[i].Value = dh.GetLabelParam(sql).ToString(); sb.AppendLine("打印参数【" + format.SubStrings[i].Name + "】赋值," + "取值SQL:" + sql + ",取到值" + format.SubStrings[i].Value); } inputValues[currentIndex - 1] = format.SubStrings[i].Value; } } if (currentIndex == paramsInfo.Rows.Count) { OperateResult.AppendText("<<输入完成,正在打印\n", Color.Black, inputText); canPrint = true; //打印 LogManager.DoLog(sb.ToString()); //保存本次赋值进行打印 format.PrintSetup.PrinterName = Printer.Text; format.PrintSetup.IdenticalCopiesOfLabel = int.Parse(PrintNum.Text); format.Print(); for (int i = 0; i < format.SubStrings.Count; i++) { format.SubStrings[i].Value = null; } //讲文件最后写入时间改成数据库中拿到的时间 //记录打印日志 LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "自定义标打印", "成功打印", "", ""); //打印成功 OperateResult.AppendText("<<打印成功\n", Color.Green); //重置currenIndex为1 currentIndex = 1; OperateResult.AppendText(">>请输入自定义模板的" + paramsInfo.Rows[0]["lp_name"].ToString() + "所需参数\n", Color.Black); } else { canPrint = false; currentIndex++; OperateResult.AppendText("<<请输入自定义模板的" + paramsInfo.Rows[currentIndex - 1]["lp_name"].ToString() + "所需参数\n", Color.Green, inputText); } } } } private void PrintLabel_TextChanged(object sender, EventArgs e) { if (PrintLabel.Text.StartsWith("System")) { return; } paramsInfo = (DataTable)dh.ExecuteSql("select lp_detno, lp_name, lp_valuetype,lp_sql from label left join labelparameter on lp_laid=la_id where la_code='" + listA.Rows[PrintLabel.SelectedIndex]["la_code"].ToString() + "'and LA_TEMPLATETYPE = '自定义' order by to_number(lp_detno) asc", "select"); if (paramsInfo.Rows.Count > 0) { //根据查询出来的参数,一次提示用户, “<> 请输入自定义模板的(XXXX参数名称)lp_name” OperateResult.AppendText("<<" + listA.Rows[PrintLabel.SelectedIndex]["la_code"].ToString() + "\n", Color.Black); OperateResult.AppendText(">>请输入自定义模板" + paramsInfo.Rows[0]["lp_name"].ToString() + "所需参数\n", Color.Green); //打开模板路径 currentIndex = 1; //聚焦输入框 inputText.Focus(); } else { OperateResult.AppendText("<<标签编号:" + listA.Rows[0]["la_code"].ToString() + "无标签参数\n", Color.Red); } } private void Make_CustomLabelPrint_SizeChanged(object sender, EventArgs e) { asc.controlAutoSize(this); } private void Make_CustomLabelPrint_FormClosing(object sender, FormClosingEventArgs e) { } private void pr_code_Leave(object sender, EventArgs e) { //pr_code_KeyDown(sender, new KeyEventArgs(Keys.Enter)); } private void printButton_Click(object sender, EventArgs e) { if (canPrint) { string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'").ToString(); FileInfo PrintFile = new FileInfo(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString()); if (PrintFile == null) { MessageBox.Show("打印文件不存在"); return; } string filechangetime = PrintFile.LastWriteTime.ToString(); if (filechangetime != filelastwritetime) { engine = new Engine(true); BaseUtil.WriteLbl(); filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"); string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')"; dh.UpdateByCondition("label", update, "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'"); } format = engine.Documents.Open(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString()); sb = new StringBuilder(); //将值赋到format对应的变量 for (int i = 0; i < format.SubStrings.Count; i++) { for (int j = 0; j < paramsInfo.Rows.Count; j++) { //找到对应的变量名 if (format.SubStrings[i].Name == paramsInfo.Rows[j]["lp_name"].ToString()) { //直接赋值 format.SubStrings[i].Value = inputValues[j]; } } } //打印 LogManager.DoLog(sb.ToString()); //保存本次赋值进行打印 format.PrintSetup.PrinterName = Printer.Text; format.PrintSetup.IdenticalCopiesOfLabel = int.Parse(PrintNum.Text); format.Print(); for (int i = 0; i < format.SubStrings.Count; i++) { format.SubStrings[i].Value = null; } //讲文件最后写入时间改成数据库中拿到的时间 //记录打印日志 LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "自定义标打印", "成功打印", "", ""); //打印成功 OperateResult.AppendText("<<打印成功\n", Color.Green); } else { OperateResult.AppendText("<<标签参数未全部输入完成\n", Color.Red); } } private void PrintLabel_SelectedValueChanged(object sender, EventArgs e) { if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView") { string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString(); PrintNum.Text = (PrintNums == "" ? "1" : PrintNums); } } } }