Print.cs 23 KB

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