Print.cs 23 KB

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