Print.cs 22 KB

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