Print.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using UAS_MES_NEW.DataOperate;
  2. using LabelManager2;
  3. using System.Data;
  4. using System.Text;
  5. using System.IO;
  6. using UAS_MES_NEW.Entity;
  7. using System.Text.RegularExpressions;
  8. using System.Windows.Forms;
  9. using Seagull.BarTender.Print;
  10. using System;
  11. namespace UAS_MES_NEW.PublicMethod
  12. {
  13. class Print
  14. {
  15. static DataHelper dh = SystemInf.dh;
  16. //Bartender打印的驱动和文件
  17. static Document doc;
  18. ////Bartender的打印机
  19. //string CodeSpft_Printer;
  20. static LabelFormatDocument format;
  21. static BarTender.Format doc2;
  22. public Print() { }
  23. public static bool SinglePrint(string iCaller, BarTender.Application lbl, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
  24. {
  25. ErrorMessage = "";
  26. DataTable dt = new DataTable();
  27. if (IfRePrint != "-1")
  28. {
  29. if (LabelType == "卡通箱标" || LabelType == "彩盒标" || LabelType == "栈板标" || LabelType == "机身标")
  30. {
  31. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  32. }
  33. else
  34. {
  35. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  36. }
  37. //////如果已经打印过了,则不允许再打印
  38. //if (dt.Rows.Count > 0)
  39. //{
  40. // ErrorMessage = SnCode + LabelType + "已打印";
  41. // return false;
  42. //}
  43. }
  44. // string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  45. // FileInfo PrintFile = new FileInfo(LabelName);
  46. //打开模板路径
  47. //查询模板对应的取值SQL和参数名称
  48. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  49. //StringBuilder sb = new StringBuilder();
  50. //if (!PrintFile.Exists)
  51. //{
  52. // MessageBox.Show("打印文件不存在");
  53. // return false;
  54. //}
  55. //string filechangetime = PrintFile.LastWriteTime.ToString();
  56. //if (filechangetime != filelastwritetime)
  57. //{
  58. // lbl.Dispose();
  59. // lbl = new Engine();
  60. // // BaseUtil.WriteLbl();
  61. // filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  62. // string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  63. // dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  64. //}
  65. doc2 = lbl.Formats.Open(LabelName);
  66. if (doc2 == null)
  67. {
  68. MessageBox.Show("标签文件打开失败");
  69. return false;
  70. }
  71. //执行全部的SQL
  72. for (int i = 0; i < dt.Rows.Count; i++)
  73. {
  74. string sql = dt.Rows[i]["lp_sql"].ToString();
  75. try
  76. {
  77. Regex ConnoteA = new Regex("{\\w+}");
  78. foreach (Match mch in ConnoteA.Matches(sql))
  79. {
  80. string x = mch.Value.Trim();
  81. sql = sql.Replace(x, "'" + SnCode + "'");
  82. }
  83. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  84. if (Param.Rows.Count == 0)
  85. continue;
  86. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  87. if (Param.Rows.Count > 0)
  88. {
  89. int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count;
  90. for (int j = 0; j < LoopTime; j++)
  91. {
  92. for (int k = 0; k < doc2.NamedSubStrings.Count; k++)
  93. {
  94. if (j == 0 & doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  95. {
  96. doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[0][0].ToString());
  97. }
  98. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  99. //if (j != 0 & doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j))
  100. //{
  101. // doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[j][0].ToString());
  102. //}
  103. if (doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  104. {
  105. doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[j][0].ToString());
  106. }
  107. }
  108. }
  109. }
  110. }
  111. catch (System.Exception ex)
  112. {
  113. MessageBox.Show("SQL维护不正确" + ex.Message);
  114. }
  115. }
  116. // LogManager.DoLog(sb.ToString());
  117. //保存本次赋值进行打印
  118. // doc2.Printer.SwitchTo(PrinterName);
  119. // 同样标签的份数
  120. doc2.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  121. // 序列标签数
  122. doc2.PrintSetup.NumberSerializedLabels = 1;
  123. doc2.PrintSetup.Printer = PrinterName;
  124. doc2.PrintOut(false, false);
  125. doc2.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
  126. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  127. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  128. //打印完毕
  129. lbl.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);
  130. return true;
  131. }
  132. public static bool Bartender(string iCaller, ref BarTender.Application doc, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
  133. {
  134. ErrorMessage = "";
  135. DataTable dt = new DataTable();
  136. if (IfRePrint != "-1")
  137. {
  138. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  139. {
  140. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  141. }
  142. else
  143. {
  144. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  145. }
  146. ////如果已经打印过了,则不允许再打印
  147. if (dt.Rows.Count > 0)
  148. {
  149. ErrorMessage = SnCode + LabelType + "已打印";
  150. return false;
  151. }
  152. }
  153. //打开模板路径
  154. //查询模板对应的取值SQL和参数名称
  155. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  156. StringBuilder sb = new StringBuilder();
  157. try
  158. {
  159. doc2 = doc.Formats.Open(LabelName);
  160. }
  161. catch (System.Exception)
  162. {
  163. doc2 = doc.Formats.Open(LabelName);
  164. }
  165. if (doc == null)
  166. {
  167. MessageBox.Show("打印文件不存在");
  168. return false;
  169. }
  170. //执行全部的SQL
  171. for (int i = 0; i < dt.Rows.Count; i++)
  172. {
  173. string sql = dt.Rows[i]["lp_sql"].ToString();
  174. try
  175. {
  176. Regex ConnoteA = new Regex("{\\w+}");
  177. foreach (Match mch in ConnoteA.Matches(sql))
  178. {
  179. string x = mch.Value.Trim();
  180. sql = sql.Replace(x, "'" + SnCode + "'");
  181. }
  182. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  183. if (Param.Rows.Count == 0)
  184. continue;
  185. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  186. if (Param.Rows.Count > 0)
  187. {
  188. int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count;
  189. for (int j = 0; j < LoopTime; j++)
  190. {
  191. for (int k = 0; k < doc2.NamedSubStrings.Count; k++)
  192. {
  193. if (j == 0 & doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  194. {
  195. doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[0][0].ToString());
  196. }
  197. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  198. //if (j != 0 & doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j))
  199. //{
  200. // doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[j][0].ToString());
  201. //}
  202. if (doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  203. {
  204. doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[j][0].ToString());
  205. }
  206. }
  207. }
  208. }
  209. }
  210. catch (Exception)
  211. {
  212. MessageBox.Show("SQL维护不正确");
  213. }
  214. }
  215. LogManager.DoLog(sb.ToString());
  216. //保存本次赋值进行打印a
  217. doc2.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  218. // 序列标签数
  219. doc2.PrintSetup.NumberSerializedLabels = 1;
  220. doc2.PrintSetup.Printer = PrinterName;
  221. doc2.PrintOut(false, false);
  222. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  223. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  224. return true;
  225. }
  226. public static bool Bartender(string iCaller, ref ApplicationClass lbl, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
  227. {
  228. ErrorMessage = "";
  229. DataTable dt = new DataTable();
  230. if (IfRePrint != "-1")
  231. {
  232. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  233. {
  234. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + LabelName + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  235. }
  236. else
  237. {
  238. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + LabelName + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  239. }
  240. ////如果已经打印过了,则不允许再打印
  241. if (dt.Rows.Count > 0)
  242. {
  243. ErrorMessage = SnCode + LabelType + "已打印";
  244. return false;
  245. }
  246. }
  247. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  248. FileInfo PrintFile = new FileInfo(LabelName);
  249. //打开模板路径
  250. //查询模板对应的取值SQL和参数名称
  251. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "' order by to_number(lp_detno)", "select");
  252. StringBuilder sb = new StringBuilder();
  253. if (!PrintFile.Exists)
  254. {
  255. MessageBox.Show("打印文件不存在");
  256. return false;
  257. }
  258. string filechangetime = PrintFile.LastWriteTime.ToString();
  259. if (filechangetime != filelastwritetime)
  260. {
  261. lbl.Quit();
  262. lbl = new ApplicationClass();
  263. BaseUtil.WriteLbl();
  264. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  265. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  266. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  267. }
  268. doc = lbl.Documents.Open(LabelName, true);
  269. if (doc == null)
  270. {
  271. MessageBox.Show("标签文件打开失败");
  272. return false;
  273. }
  274. //执行全部的SQL
  275. for (int i = 0; i < dt.Rows.Count; i++)
  276. {
  277. string sql = dt.Rows[i]["lp_sql"].ToString();
  278. try
  279. {
  280. Regex ConnoteA = new Regex("{\\w+}");
  281. foreach (Match mch in ConnoteA.Matches(sql))
  282. {
  283. string x = mch.Value.Trim();
  284. sql = sql.Replace(x, "'" + SnCode + "'");
  285. }
  286. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  287. if (Param.Rows.Count == 0)
  288. continue;
  289. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  290. if (Param.Rows.Count > 0)
  291. {
  292. int LoopTime = Param.Rows.Count > 100 ? 100 : Param.Rows.Count;
  293. for (int j = 0; j < LoopTime; j++)
  294. {
  295. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  296. {
  297. if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  298. {
  299. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
  300. }
  301. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  302. if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  303. {
  304. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
  305. }
  306. }
  307. }
  308. }
  309. }
  310. catch (System.Exception)
  311. {
  312. MessageBox.Show("SQL维护不正确");
  313. }
  314. }
  315. LogManager.DoLog(sb.ToString());
  316. //保存本次赋值进行打印
  317. doc.Printer.SwitchTo(PrinterName);
  318. doc.PrintDocument(PrintNum);
  319. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  320. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  321. {
  322. doc.Variables.FormVariables.Item(k + 1).Value = null;
  323. }
  324. LogicHandler.doLabelPrintLog(SnCode, LabelType + LabelName, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  325. return true;
  326. }
  327. public static bool BarTender1(string iCaller, ref Engine lbl, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
  328. {
  329. ErrorMessage = "";
  330. DataTable dt = new DataTable();
  331. //if (IfRePrint != "-1")
  332. //{
  333. // if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  334. // {
  335. // dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  336. // }
  337. // else
  338. // {
  339. // dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  340. // }
  341. // ////如果已经打印过了,则不允许再打印
  342. // if (dt.Rows.Count > 0)
  343. // {
  344. // ErrorMessage = SnCode + LabelType + "已打印";
  345. // return false;
  346. // }
  347. //}
  348. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  349. FileInfo PrintFile = new FileInfo(LabelName);
  350. //打开模板路径
  351. //查询模板对应的取值SQL和参数名称
  352. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  353. StringBuilder sb = new StringBuilder();
  354. if (!PrintFile.Exists)
  355. {
  356. MessageBox.Show("打印文件不存在");
  357. return false;
  358. }
  359. string filechangetime = PrintFile.LastWriteTime.ToString();
  360. if (filechangetime != filelastwritetime)
  361. {
  362. lbl.Stop();
  363. lbl = new Engine(true);
  364. BaseUtil.WriteLbl();
  365. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  366. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  367. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  368. }
  369. format = lbl.Documents.Open(LabelName);
  370. if (format == null)
  371. {
  372. MessageBox.Show("标签文件打开失败");
  373. return false;
  374. }
  375. //执行全部的SQL
  376. for (int i = 0; i < dt.Rows.Count; i++)
  377. {
  378. string sql = dt.Rows[i]["lp_sql"].ToString();
  379. try
  380. {
  381. Regex ConnoteA = new Regex("{\\w+}");
  382. foreach (Match mch in ConnoteA.Matches(sql))
  383. {
  384. string x = mch.Value.Trim();
  385. sql = sql.Replace(x, "'" + SnCode + "'");
  386. }
  387. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  388. if (Param.Rows.Count == 0)
  389. continue;
  390. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  391. if (Param.Rows.Count > 0)
  392. {
  393. int LoopTime = Param.Rows.Count > 100 ? 100 : Param.Rows.Count;
  394. for (int j = 0; j < LoopTime; j++)
  395. {
  396. for (int k = 0; k < format.SubStrings.Count; k++)
  397. {
  398. if (j == 0 & format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  399. {
  400. format.SubStrings[k].Value = Param.Rows[0][0].ToString();
  401. }
  402. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  403. if (format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  404. {
  405. format.SubStrings[k].Value = Param.Rows[j][0].ToString();
  406. }
  407. }
  408. }
  409. }
  410. }
  411. catch (System.Exception)
  412. {
  413. MessageBox.Show("SQL维护不正确");
  414. }
  415. }
  416. LogManager.DoLog(sb.ToString());
  417. //保存本次赋值进行打印
  418. format.PrintSetup.PrinterName = PrinterName;
  419. format.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  420. format.Print();
  421. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  422. for (int k = 0; k < format.SubStrings.Count; k++)
  423. {
  424. format.SubStrings[k].Value = null;
  425. }
  426. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  427. return true;
  428. }
  429. }
  430. }