Print.cs 20 KB

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