Print.cs 22 KB

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