using DevExpress.Printing.Core.PdfExport.Metafile; 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.Net; 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_StencilPrint : 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_StencilPrint() { 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; } string la_id = listA.Rows[0]["la_id"].ToString(); string la_code = listA.Rows[0]["la_code"].ToString(); Console.WriteLine(listA.Rows[0]["la_isdefault"].ToString()); { //说明没有默认的模板,那就取pl_id最大的 listA = (DataTable)dh.ExecuteSql("select fp_name la_url,'" + la_id + "' la_id,'" + la_code + "'la_code from FILEPATH where fp_id in (select * from table(select parsestring(LA_SOFTTYPE,';') from label where la_id='" + la_id + "' ) where COLUMN_VALUE is not null)", "select"); //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键 string oErrorMessage = ""; 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 { if (Print.BarTenderS(Tag.ToString(), ref engine, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, inputText.Text, int.Parse(PrintNum.Text),"", pr_code.Text, "自定义", "-1", out oErrorMessage)) { OperateResult.AppendText(oErrorMessage + "\n", Color.Red); } } } } 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++) { if (format.SubStrings[i].Name != "Serial1") { Console.WriteLine(format.SubStrings[i].Name); 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); } } } }