Make_StencilPrint.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using DevExpress.Printing.Core.PdfExport.Metafile;
  2. using LabelManager2;
  3. using Seagull.BarTender.Print;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net;
  13. using System.Text;
  14. using System.Text.RegularExpressions;
  15. using System.Threading;
  16. using System.Windows.Forms;
  17. using UAS_MES_NEW.DataOperate;
  18. using UAS_MES_NEW.Entity;
  19. using UAS_MES_NEW.PublicForm;
  20. using UAS_MES_NEW.PublicMethod;
  21. namespace UAS_MES_NEW.Make
  22. {
  23. public partial class Make_StencilPrint : Form
  24. {
  25. DataHelper dh;
  26. LogStringBuilder sql = new LogStringBuilder();
  27. AutoSizeFormClass asc = new AutoSizeFormClass();
  28. Thread InitPrint;
  29. //存放产品信息
  30. DataTable dt;
  31. //存放标签信息
  32. DataTable listA;
  33. //存放标签参数列表信息
  34. DataTable paramsInfo;
  35. //记录当前的步骤数
  36. int currentIndex = 0;
  37. FileInfo info;
  38. Engine engine;
  39. //ftpOperater ftp;
  40. LabelFormatDocument format;
  41. StringBuilder sb = new StringBuilder();
  42. bool canPrint = false;
  43. string[] inputValues;//存放用户输入的参数值
  44. public Make_StencilPrint()
  45. {
  46. InitializeComponent();
  47. }
  48. private void Make_CustomLablePrint_Load(object sender, EventArgs e)
  49. {
  50. asc.controllInitializeSize(this);
  51. OperateResult.AppendText(">>请输入产品代码\n", Color.Black);
  52. InitPrint = new Thread(InPrint);
  53. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  54. BaseUtil.SetFormCenter(stw);
  55. stw.ShowDialog();
  56. pr_code.Focus();
  57. dh = SystemInf.dh;
  58. }
  59. private void InPrint()
  60. {
  61. try
  62. {
  63. engine = new Engine(true);
  64. }
  65. catch (Exception ex)
  66. {
  67. OperateResult.AppendText(ex.Message + ex.StackTrace, Color.Red);
  68. }
  69. }
  70. private void pr_code_KeyDown(object sender, KeyEventArgs e)
  71. {
  72. //按下enter键
  73. if (e.KeyCode == Keys.Enter)
  74. {
  75. //判断产品是否存在
  76. dt = (DataTable)dh.ExecuteSql("select pr_code,pr_detail,pr_spec from product where pr_code='" + pr_code.Text + "' and pr_statuscode ='AUDITED'", "select");
  77. //查询没有值,返回提示用户“产品:XXX,不存在或者为审核”,清空产品代码输入框中的值
  78. if (dt.Rows.Count == 0)
  79. {
  80. OperateResult.AppendText("<<产品:" + pr_code.Text + ",不存在或者未审核\n", Color.Red, pr_code);
  81. return;
  82. }
  83. //查询产品是否有自定义的标签模板
  84. sql.Clear();
  85. sql.Append("select la_code,la_id,substr(la_url,instr(la_url,'\\',-1)+1)la_name,la_url,la_isdefault ");
  86. sql.Append("from label where la_prodcode='" + pr_code.Text + "' and la_templatetype='自定义' and la_statuscode='AUDITED' order by la_isdefault");
  87. listA = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  88. if (listA.Rows.Count == 0)
  89. {
  90. OperateResult.AppendText("<<产品:" + pr_code.Text + ",没有维护自定义模板\n", Color.Red, pr_code);
  91. return;
  92. }
  93. string la_id = listA.Rows[0]["la_id"].ToString();
  94. string la_code = listA.Rows[0]["la_code"].ToString();
  95. Console.WriteLine(listA.Rows[0]["la_isdefault"].ToString());
  96. {
  97. //说明没有默认的模板,那就取pl_id最大的
  98. 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");
  99. //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");
  100. }
  101. //标签显示
  102. PrintLabel.DataSource = listA;
  103. PrintLabel.DisplayMember = "la_url";
  104. PrintLabel.ValueMember = "la_id";
  105. //有值,填充界面
  106. BaseUtil.SetFormValue(this.Controls, dt);
  107. //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");
  108. }
  109. }
  110. private void inputText_KeyDown(object sender, KeyEventArgs e)
  111. {
  112. //按下enter键
  113. string oErrorMessage = "";
  114. if (e.KeyCode == Keys.Enter)
  115. {
  116. //产品编号为空
  117. if (pr_code.Text.Trim() == "")
  118. {
  119. OperateResult.AppendText("<<请先输入产品编号\n", Color.Red);
  120. return;
  121. }
  122. if (inputText.Text.Trim() == "")
  123. {
  124. OperateResult.AppendText("<<输入不能为空\n", Color.Red);
  125. return;
  126. }
  127. OperateResult.AppendText(">>" + inputText.Text + "\n", Color.Black);
  128. if (paramsInfo == null || paramsInfo.Rows.Count == 0)
  129. {
  130. OperateResult.AppendText("<<无可用标签\n", Color.Red, inputText);
  131. return;
  132. }
  133. else
  134. {
  135. 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))
  136. {
  137. OperateResult.AppendText(oErrorMessage + "\n", Color.Red);
  138. }
  139. }
  140. }
  141. }
  142. private void PrintLabel_TextChanged(object sender, EventArgs e)
  143. {
  144. if (PrintLabel.Text.StartsWith("System"))
  145. {
  146. return;
  147. }
  148. 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");
  149. if (paramsInfo.Rows.Count > 0)
  150. {
  151. //根据查询出来的参数,一次提示用户, “<<SN001 “,
  152. //“>> 请输入自定义模板的(XXXX参数名称)lp_name”
  153. OperateResult.AppendText("<<" + listA.Rows[PrintLabel.SelectedIndex]["la_code"].ToString() + "\n", Color.Black);
  154. OperateResult.AppendText(">>请输入自定义模板" + paramsInfo.Rows[0]["lp_name"].ToString() + "所需参数\n", Color.Green);
  155. //打开模板路径
  156. currentIndex = 1;
  157. //聚焦输入框
  158. inputText.Focus();
  159. }
  160. else
  161. {
  162. OperateResult.AppendText("<<标签编号:" + listA.Rows[0]["la_code"].ToString() + "无标签参数\n", Color.Red);
  163. }
  164. }
  165. private void Make_CustomLabelPrint_SizeChanged(object sender, EventArgs e)
  166. {
  167. asc.controlAutoSize(this);
  168. }
  169. private void Make_CustomLabelPrint_FormClosing(object sender, FormClosingEventArgs e)
  170. {
  171. }
  172. private void pr_code_Leave(object sender, EventArgs e)
  173. {
  174. //pr_code_KeyDown(sender, new KeyEventArgs(Keys.Enter));
  175. }
  176. private void printButton_Click(object sender, EventArgs e)
  177. {
  178. if (canPrint)
  179. {
  180. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'").ToString();
  181. FileInfo PrintFile = new FileInfo(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString());
  182. if (PrintFile == null)
  183. {
  184. MessageBox.Show("打印文件不存在");
  185. return;
  186. }
  187. string filechangetime = PrintFile.LastWriteTime.ToString();
  188. //if (filechangetime != filelastwritetime)
  189. //{
  190. // engine = new Engine(true);
  191. // BaseUtil.WriteLbl();
  192. // filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  193. // string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  194. // dh.UpdateByCondition("label", update, "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'");
  195. //}
  196. format = engine.Documents.Open(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString());
  197. sb = new StringBuilder();
  198. //将值赋到format对应的变量
  199. for (int i = 0; i < format.SubStrings.Count; i++)
  200. {
  201. for (int j = 0; j < paramsInfo.Rows.Count; j++)
  202. {
  203. //找到对应的变量名
  204. if (format.SubStrings[i].Name == paramsInfo.Rows[j]["lp_name"].ToString())
  205. {
  206. //直接赋值
  207. format.SubStrings[i].Value = inputValues[j];
  208. }
  209. }
  210. }
  211. //打印
  212. LogManager.DoLog(sb.ToString());
  213. //保存本次赋值进行打印
  214. format.PrintSetup.PrinterName = Printer.Text;
  215. format.PrintSetup.IdenticalCopiesOfLabel = int.Parse(PrintNum.Text);
  216. format.Print();
  217. for (int i = 0; i < format.SubStrings.Count; i++)
  218. {
  219. if (format.SubStrings[i].Name != "Serial1")
  220. {
  221. Console.WriteLine(format.SubStrings[i].Name);
  222. format.SubStrings[i].Value = null;
  223. }
  224. }
  225. //讲文件最后写入时间改成数据库中拿到的时间
  226. //记录打印日志
  227. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "自定义标打印", "成功打印", "", "");
  228. //打印成功
  229. OperateResult.AppendText("<<打印成功\n", Color.Green);
  230. }
  231. else
  232. {
  233. OperateResult.AppendText("<<标签参数未全部输入完成\n", Color.Red);
  234. }
  235. }
  236. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  237. {
  238. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  239. {
  240. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  241. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  242. }
  243. }
  244. }
  245. }