Make_CustomLabelPrint.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. using LabelManager2;
  2. using Seagull.BarTender.Print;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Threading;
  15. using System.Windows.Forms;
  16. using UAS_MES_NEW.DataOperate;
  17. using UAS_MES_NEW.Entity;
  18. using UAS_MES_NEW.PublicForm;
  19. using UAS_MES_NEW.PublicMethod;
  20. namespace UAS_MES_NEW.Make
  21. {
  22. public partial class Make_CustomLabelPrint : Form
  23. {
  24. DataHelper dh;
  25. LogStringBuilder sql = new LogStringBuilder();
  26. AutoSizeFormClass asc = new AutoSizeFormClass();
  27. Thread InitPrint;
  28. //存放产品信息
  29. DataTable dt;
  30. //存放标签信息
  31. DataTable listA;
  32. //存放标签参数列表信息
  33. DataTable paramsInfo;
  34. //记录当前的步骤数
  35. int currentIndex = 0;
  36. FileInfo info;
  37. Engine engine;
  38. //ftpOperater ftp;
  39. LabelFormatDocument format;
  40. StringBuilder sb = new StringBuilder();
  41. bool canPrint = false;
  42. string[] inputValues;//存放用户输入的参数值
  43. public Make_CustomLabelPrint()
  44. {
  45. InitializeComponent();
  46. }
  47. private void Make_CustomLablePrint_Load(object sender, EventArgs e)
  48. {
  49. asc.controllInitializeSize(this);
  50. OperateResult.AppendText(">>请输入产品代码\n", Color.Black);
  51. InitPrint = new Thread(InPrint);
  52. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  53. BaseUtil.SetFormCenter(stw);
  54. stw.ShowDialog();
  55. pr_code.Focus();
  56. dh = SystemInf.dh;
  57. }
  58. private void InPrint()
  59. {
  60. try
  61. {
  62. engine = new Engine(true);
  63. }
  64. catch (Exception ex)
  65. {
  66. OperateResult.AppendText(ex.Message + ex.StackTrace, Color.Red);
  67. }
  68. }
  69. private void pr_code_KeyDown(object sender, KeyEventArgs e)
  70. {
  71. //按下enter键
  72. if (e.KeyCode == Keys.Enter)
  73. {
  74. //判断产品是否存在
  75. dt = (DataTable)dh.ExecuteSql("select pr_code,pr_detail,pr_spec from product where pr_code='" + pr_code.Text + "' and pr_statuscode ='AUDITED'", "select");
  76. //查询没有值,返回提示用户“产品:XXX,不存在或者为审核”,清空产品代码输入框中的值
  77. if (dt.Rows.Count == 0)
  78. {
  79. OperateResult.AppendText("<<产品:" + pr_code.Text + ",不存在或者未审核\n", Color.Red, pr_code);
  80. return;
  81. }
  82. //查询产品是否有自定义的标签模板
  83. sql.Clear();
  84. sql.Append("select la_code,la_id,substr(la_url,instr(la_url,'\\',-1)+1)la_name,la_url,la_isdefault ");
  85. sql.Append("from label where la_prodcode='" + pr_code.Text + "' and la_templatetype='自定义' and la_statuscode='AUDITED' order by la_isdefault");
  86. listA = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  87. if (listA.Rows.Count == 0)
  88. {
  89. OperateResult.AppendText("<<产品:" + pr_code.Text + ",没有维护自定义模板\n", Color.Red, pr_code);
  90. return;
  91. }
  92. string la_id = listA.Rows[0]["la_id"].ToString();
  93. string la_code = listA.Rows[0]["la_code"].ToString();
  94. Console.WriteLine(listA.Rows[0]["la_isdefault"].ToString());
  95. {
  96. //说明没有默认的模板,那就取pl_id最大的
  97. 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");
  98. //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");
  99. }
  100. //标签显示
  101. PrintLabel.DataSource = listA;
  102. PrintLabel.DisplayMember = "la_url";
  103. PrintLabel.ValueMember = "la_id";
  104. //有值,填充界面
  105. BaseUtil.SetFormValue(this.Controls, dt);
  106. //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");
  107. }
  108. }
  109. private void inputText_KeyDown(object sender, KeyEventArgs e)
  110. {
  111. //按下enter键
  112. if (e.KeyCode == Keys.Enter)
  113. {
  114. //产品编号为空
  115. if (pr_code.Text.Trim() == "")
  116. {
  117. OperateResult.AppendText("<<请先输入产品编号\n", Color.Red);
  118. return;
  119. }
  120. if (inputText.Text.Trim() == "")
  121. {
  122. OperateResult.AppendText("<<输入不能为空\n", Color.Red);
  123. return;
  124. }
  125. OperateResult.AppendText(">>" + inputText.Text + "\n", Color.Black);
  126. if (paramsInfo == null || paramsInfo.Rows.Count == 0)
  127. {
  128. OperateResult.AppendText("<<无可用标签\n", Color.Red, inputText);
  129. return;
  130. }
  131. else
  132. {
  133. //下标为1的时候实例化存值的数组
  134. if (currentIndex == 1)
  135. {
  136. string fp_name = "";
  137. inputValues = new string[paramsInfo.Rows.Count];
  138. sb = new StringBuilder();
  139. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'").ToString();
  140. string LA_SOFTTYPE = dh.getFieldDataByCondition("label", "LA_SOFTTYPE", "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'").ToString();
  141. FileInfo PrintFile = new FileInfo(@"C:\打印标签\" + PrintLabel.Text);
  142. if (!PrintFile.Exists)
  143. {
  144. if (LA_SOFTTYPE != "")
  145. {
  146. string[] fpid = LA_SOFTTYPE.Split(';');
  147. for (int i = 0; i < fpid.Length; i++)
  148. {
  149. if (fpid[i] != "")
  150. {
  151. DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
  152. if (label.Rows.Count > 0)
  153. {
  154. string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
  155. fp_name = label.Rows[0]["fp_name"].ToString();
  156. WebClient wc = new WebClient();
  157. wc.DownloadFile("http://192.168.110.18:8099/" + fp_path, @"C:\打印标签\" + fp_name);
  158. }
  159. }
  160. }
  161. engine.Stop();
  162. engine = new Engine(true);
  163. BaseUtil.WriteLbl();
  164. }
  165. }
  166. string filechangetime = PrintFile.LastWriteTime.ToString();
  167. //if (filechangetime != filelastwritetime)
  168. //{
  169. // engine = new Engine(true);
  170. // filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  171. // string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  172. // dh.UpdateByCondition("label", update, "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'");
  173. //}
  174. format = engine.Documents.Open(@"C:\打印标签\" + listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString());
  175. if (format == null)
  176. {
  177. MessageBox.Show("标签文件打开失败");
  178. return;
  179. }
  180. format = engine.Documents.Open(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString());
  181. }
  182. //将值赋到format对应的变量
  183. for (int i = 0; i < format.SubStrings.Count; i++)
  184. {
  185. //找到对应的变量名
  186. if (format.SubStrings[i].Name == paramsInfo.Rows[currentIndex - 1]["lp_name"].ToString())
  187. {
  188. //如果当时采集行的 lp_valuetype=’INPUT’,并且lp_sql 为空,则直接将采集的值赋给标签打印
  189. if (paramsInfo.Rows[currentIndex - 1]["lp_valuetype"].ToString() == "INPUT" && paramsInfo.Rows[currentIndex - 1]["lp_sql"].ToString() == "")
  190. {
  191. format.SubStrings[i].Value = inputText.Text;
  192. }
  193. else
  194. {
  195. //取sql拿值赋给变量
  196. string sql = paramsInfo.Rows[currentIndex - 1]["lp_sql"].ToString();
  197. Regex ConnoteA = new Regex("{\\w+}");
  198. foreach (Match mch in ConnoteA.Matches(sql))
  199. {
  200. string x = mch.Value.Trim();
  201. sql = sql.Replace(x, "'" + inputText.Text + "'");
  202. }
  203. format.SubStrings[i].Value = dh.GetLabelParam(sql).ToString();
  204. sb.AppendLine("打印参数【" + format.SubStrings[i].Name + "】赋值," + "取值SQL:" + sql + ",取到值" + format.SubStrings[i].Value);
  205. }
  206. inputValues[currentIndex - 1] = format.SubStrings[i].Value;
  207. }
  208. }
  209. if (currentIndex == paramsInfo.Rows.Count)
  210. {
  211. OperateResult.AppendText("<<输入完成,正在打印\n", Color.Black, inputText);
  212. canPrint = true;
  213. //打印
  214. LogManager.DoLog(sb.ToString());
  215. //保存本次赋值进行打印
  216. format.PrintSetup.PrinterName = Printer.Text;
  217. format.PrintSetup.IdenticalCopiesOfLabel = int.Parse(PrintNum.Text);
  218. format.Print();
  219. for (int i = 0; i < format.SubStrings.Count; i++)
  220. {
  221. if (format.SubStrings[i].Name != "Serial1")
  222. {
  223. Console.WriteLine(format.SubStrings[i].Name);
  224. format.SubStrings[i].Value = null;
  225. }
  226. }
  227. //讲文件最后写入时间改成数据库中拿到的时间
  228. //记录打印日志
  229. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "自定义标打印", "成功打印", "", "");
  230. //打印成功
  231. OperateResult.AppendText("<<打印成功\n", Color.Green);
  232. //重置currenIndex为1
  233. currentIndex = 1;
  234. OperateResult.AppendText(">>请输入自定义模板的" + paramsInfo.Rows[0]["lp_name"].ToString() + "所需参数\n", Color.Black);
  235. }
  236. else
  237. {
  238. canPrint = false;
  239. currentIndex++;
  240. OperateResult.AppendText("<<请输入自定义模板的" + paramsInfo.Rows[currentIndex - 1]["lp_name"].ToString() + "所需参数\n", Color.Green, inputText);
  241. }
  242. }
  243. }
  244. }
  245. private void PrintLabel_TextChanged(object sender, EventArgs e)
  246. {
  247. if (PrintLabel.Text.StartsWith("System"))
  248. {
  249. return;
  250. }
  251. 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");
  252. if (paramsInfo.Rows.Count > 0)
  253. {
  254. //根据查询出来的参数,一次提示用户, “<<SN001 “,
  255. //“>> 请输入自定义模板的(XXXX参数名称)lp_name”
  256. OperateResult.AppendText("<<" + listA.Rows[PrintLabel.SelectedIndex]["la_code"].ToString() + "\n", Color.Black);
  257. OperateResult.AppendText(">>请输入自定义模板" + paramsInfo.Rows[0]["lp_name"].ToString() + "所需参数\n", Color.Green);
  258. //打开模板路径
  259. currentIndex = 1;
  260. //聚焦输入框
  261. inputText.Focus();
  262. }
  263. else
  264. {
  265. OperateResult.AppendText("<<标签编号:" + listA.Rows[0]["la_code"].ToString() + "无标签参数\n", Color.Red);
  266. }
  267. }
  268. private void Make_CustomLabelPrint_SizeChanged(object sender, EventArgs e)
  269. {
  270. asc.controlAutoSize(this);
  271. }
  272. private void Make_CustomLabelPrint_FormClosing(object sender, FormClosingEventArgs e)
  273. {
  274. }
  275. private void pr_code_Leave(object sender, EventArgs e)
  276. {
  277. //pr_code_KeyDown(sender, new KeyEventArgs(Keys.Enter));
  278. }
  279. private void printButton_Click(object sender, EventArgs e)
  280. {
  281. if (canPrint)
  282. {
  283. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'").ToString();
  284. FileInfo PrintFile = new FileInfo(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString());
  285. if (PrintFile == null)
  286. {
  287. MessageBox.Show("打印文件不存在");
  288. return;
  289. }
  290. string filechangetime = PrintFile.LastWriteTime.ToString();
  291. //if (filechangetime != filelastwritetime)
  292. //{
  293. // engine = new Engine(true);
  294. // BaseUtil.WriteLbl();
  295. // filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  296. // string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  297. // dh.UpdateByCondition("label", update, "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'");
  298. //}
  299. format = engine.Documents.Open(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString());
  300. sb = new StringBuilder();
  301. //将值赋到format对应的变量
  302. for (int i = 0; i < format.SubStrings.Count; i++)
  303. {
  304. for (int j = 0; j < paramsInfo.Rows.Count; j++)
  305. {
  306. //找到对应的变量名
  307. if (format.SubStrings[i].Name == paramsInfo.Rows[j]["lp_name"].ToString())
  308. {
  309. //直接赋值
  310. format.SubStrings[i].Value = inputValues[j];
  311. }
  312. }
  313. }
  314. //打印
  315. LogManager.DoLog(sb.ToString());
  316. //保存本次赋值进行打印
  317. format.PrintSetup.PrinterName = Printer.Text;
  318. format.PrintSetup.IdenticalCopiesOfLabel = int.Parse(PrintNum.Text);
  319. format.Print();
  320. for (int i = 0; i < format.SubStrings.Count; i++)
  321. {
  322. if (format.SubStrings[i].Name != "Serial1")
  323. {
  324. Console.WriteLine(format.SubStrings[i].Name);
  325. format.SubStrings[i].Value = null;
  326. }
  327. }
  328. //讲文件最后写入时间改成数据库中拿到的时间
  329. //记录打印日志
  330. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "自定义标打印", "成功打印", "", "");
  331. //打印成功
  332. OperateResult.AppendText("<<打印成功\n", Color.Green);
  333. }
  334. else
  335. {
  336. OperateResult.AppendText("<<标签参数未全部输入完成\n", Color.Red);
  337. }
  338. }
  339. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  340. {
  341. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  342. {
  343. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  344. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  345. }
  346. }
  347. }
  348. }