Print.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 BarTender.Format doc2;
  23. static LabelFormatDocument format;
  24. public Print() { }
  25. 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)
  26. {
  27. ErrorMessage = "";
  28. DataTable dt = new DataTable();
  29. if (IfRePrint != "-1")
  30. {
  31. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  32. {
  33. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  34. }
  35. else
  36. {
  37. 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");
  38. }
  39. ////如果已经打印过了,则不允许再打印
  40. if (dt.Rows.Count > 0)
  41. {
  42. ErrorMessage = SnCode + LabelType + "已打印";
  43. return false;
  44. }
  45. }
  46. //打开模板路径
  47. //查询模板对应的取值SQL和参数名称
  48. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  49. StringBuilder sb = new StringBuilder();
  50. if (doc == null)
  51. {
  52. MessageBox.Show("打印文件不存在");
  53. return false;
  54. }
  55. //执行全部的SQL
  56. for (int i = 0; i < dt.Rows.Count; i++)
  57. {
  58. string sql = dt.Rows[i]["lp_sql"].ToString();
  59. try
  60. {
  61. Regex ConnoteA = new Regex("{\\w+}");
  62. foreach (Match mch in ConnoteA.Matches(sql))
  63. {
  64. string x = mch.Value.Trim();
  65. sql = sql.Replace(x, "'" + SnCode + "'");
  66. }
  67. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  68. if (Param.Rows.Count == 0)
  69. continue;
  70. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  71. if (Param.Rows.Count > 0)
  72. {
  73. int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count;
  74. for (int j = 0; j < LoopTime; j++)
  75. {
  76. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  77. {
  78. if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  79. {
  80. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
  81. }
  82. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  83. if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  84. {
  85. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
  86. }
  87. }
  88. }
  89. }
  90. }
  91. catch (System.Exception)
  92. {
  93. MessageBox.Show("SQL维护不正确");
  94. }
  95. }
  96. LogManager.DoLog(sb.ToString());
  97. //保存本次赋值进行打印
  98. doc.Printer.SwitchTo(PrinterName);
  99. doc.PrintDocument(PrintNum);
  100. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  101. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  102. {
  103. doc.Variables.FormVariables.Item(k + 1).Value = null;
  104. }
  105. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  106. return true;
  107. }
  108. 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)
  109. {
  110. ErrorMessage = "";
  111. DataTable dt = new DataTable();
  112. if (IfRePrint != "-1")
  113. {
  114. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  115. {
  116. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + LabelName + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  117. }
  118. else
  119. {
  120. 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");
  121. }
  122. ////如果已经打印过了,则不允许再打印
  123. if (dt.Rows.Count > 0)
  124. {
  125. ErrorMessage = SnCode + LabelType + "已打印";
  126. return false;
  127. }
  128. }
  129. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  130. FileInfo PrintFile = new FileInfo(LabelName);
  131. //打开模板路径
  132. //查询模板对应的取值SQL和参数名称
  133. 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");
  134. StringBuilder sb = new StringBuilder();
  135. if (!PrintFile.Exists)
  136. {
  137. MessageBox.Show("打印文件不存在");
  138. return false;
  139. }
  140. string filechangetime = PrintFile.LastWriteTime.ToString();
  141. if (filechangetime != filelastwritetime)
  142. {
  143. lbl.Quit();
  144. lbl = new ApplicationClass();
  145. BaseUtil.WriteLbl();
  146. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  147. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  148. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  149. }
  150. doc = lbl.Documents.Open(LabelName, true);
  151. if (doc == null)
  152. {
  153. MessageBox.Show("标签文件打开失败");
  154. return false;
  155. }
  156. //执行全部的SQL
  157. for (int i = 0; i < dt.Rows.Count; i++)
  158. {
  159. string sql = dt.Rows[i]["lp_sql"].ToString();
  160. try
  161. {
  162. Regex ConnoteA = new Regex("{\\w+}");
  163. foreach (Match mch in ConnoteA.Matches(sql))
  164. {
  165. string x = mch.Value.Trim();
  166. sql = sql.Replace(x, "'" + SnCode + "'");
  167. }
  168. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  169. if (Param.Rows.Count == 0)
  170. continue;
  171. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  172. if (Param.Rows.Count > 0)
  173. {
  174. int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count;
  175. for (int j = 0; j < LoopTime; j++)
  176. {
  177. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  178. {
  179. if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  180. {
  181. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
  182. }
  183. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  184. if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  185. {
  186. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
  187. }
  188. }
  189. }
  190. }
  191. }
  192. catch (System.Exception)
  193. {
  194. MessageBox.Show("SQL维护不正确");
  195. }
  196. }
  197. LogManager.DoLog(sb.ToString());
  198. //保存本次赋值进行打印
  199. doc.Printer.SwitchTo(PrinterName);
  200. doc.PrintDocument(PrintNum);
  201. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  202. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  203. {
  204. doc.Variables.FormVariables.Item(k + 1).Value = null;
  205. }
  206. LogicHandler.doLabelPrintLog(SnCode, LabelType + LabelName, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  207. return true;
  208. }
  209. 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)
  210. {
  211. ErrorMessage = "";
  212. DataTable dt = new DataTable();
  213. if (IfRePrint != "-1")
  214. {
  215. if (LabelType == "卡通箱标" || LabelType == "彩盒标" || LabelType == "栈板标" || LabelType == "机身标")
  216. {
  217. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  218. }
  219. else
  220. {
  221. 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");
  222. }
  223. //////如果已经打印过了,则不允许再打印
  224. //if (dt.Rows.Count > 0)
  225. //{
  226. // ErrorMessage = SnCode + LabelType + "已打印";
  227. // return false;
  228. //}
  229. }
  230. // string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  231. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  232. string LA_SOFTTYPE = dh.getFieldDataByCondition("label", "LA_SOFTTYPE", "la_id = '" + LaID + "'").ToString();
  233. //如果有附件上传的话
  234. string fp_name = "";
  235. //下载附件
  236. FileInfo PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
  237. if (!PrintFile.Exists)
  238. {
  239. if (LA_SOFTTYPE != "")
  240. {
  241. string[] fpid = LA_SOFTTYPE.Split(';');
  242. for (int i = 0; i < fpid.Length; i++)
  243. {
  244. if (fpid[i] != "")
  245. {
  246. DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
  247. if (label.Rows.Count > 0)
  248. {
  249. string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
  250. fp_name = label.Rows[0]["fp_name"].ToString();
  251. WebClient wc = new WebClient();
  252. wc.DownloadFile("http://192.168.110.18:8099/" + fp_path, @"C:\打印标签\" + fp_name);
  253. }
  254. }
  255. }
  256. lbl = new BarTender.Application();
  257. BaseUtil.WriteLbl();
  258. }
  259. }
  260. else
  261. {
  262. string filechangetime = PrintFile.LastWriteTime.ToString();
  263. System.DateTime dateTime1 = System.DateTime.Parse(filechangetime);
  264. System.DateTime dateTime2 = System.DateTime.Parse(filelastwritetime);
  265. if (dateTime1 < dateTime2)
  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://192.168.110.18:8099/" + fp_path, @"C:\打印标签\" + fp_name);
  281. FileInfo file = new FileInfo(@"C:\打印标签\" + fp_name);
  282. file.CreationTime = Convert.ToDateTime(filelastwritetime);
  283. }
  284. }
  285. }
  286. }
  287. lbl = new BarTender.Application();
  288. BaseUtil.WriteLbl();
  289. }
  290. }
  291. PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
  292. //打开模板路径
  293. //查询模板对应的取值SQL和参数名称
  294. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  295. //StringBuilder sb = new StringBuilder();
  296. //if (!PrintFile.Exists)
  297. //{
  298. // MessageBox.Show("打印文件不存在");
  299. // return false;
  300. //}
  301. //string filechangetime = PrintFile.LastWriteTime.ToString();
  302. //if (filechangetime != filelastwritetime)
  303. //{
  304. // lbl.Dispose();
  305. // lbl = new Engine();
  306. // // BaseUtil.WriteLbl();
  307. // filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  308. // string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  309. // dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  310. //}
  311. doc2 = lbl.Formats.Open(@"C:\打印标签\" + LabelName);
  312. if (doc2 == null)
  313. {
  314. MessageBox.Show("标签文件打开失败");
  315. return false;
  316. }
  317. //执行全部的SQL
  318. for (int i = 0; i < dt.Rows.Count; i++)
  319. {
  320. string sql = dt.Rows[i]["lp_sql"].ToString();
  321. try
  322. {
  323. Regex ConnoteA = new Regex("{\\w+}");
  324. foreach (Match mch in ConnoteA.Matches(sql))
  325. {
  326. string x = mch.Value.Trim();
  327. sql = sql.Replace(x, "'" + SnCode + "'");
  328. }
  329. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  330. if (Param.Rows.Count == 0)
  331. continue;
  332. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  333. if (Param.Rows.Count > 0)
  334. {
  335. int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count;
  336. for (int j = 0; j < LoopTime; j++)
  337. {
  338. for (int k = 0; k < doc2.NamedSubStrings.Count; k++)
  339. {
  340. if (j == 0 & doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  341. {
  342. doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[0][0].ToString());
  343. }
  344. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  345. //if (j != 0 & doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j))
  346. //{
  347. // doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[j][0].ToString());
  348. //}
  349. if (doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  350. {
  351. doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[j][0].ToString());
  352. }
  353. }
  354. }
  355. }
  356. }
  357. catch (System.Exception ex)
  358. {
  359. MessageBox.Show("SQL维护不正确" + ex.Message);
  360. }
  361. }
  362. // LogManager.DoLog(sb.ToString());
  363. //保存本次赋值进行打印
  364. // doc2.Printer.SwitchTo(PrinterName);
  365. // 同样标签的份数
  366. doc2.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  367. // 序列标签数
  368. doc2.PrintSetup.NumberSerializedLabels = 1;
  369. doc2.PrintSetup.Printer = PrinterName;
  370. doc2.PrintOut(false, false);
  371. doc2.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
  372. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  373. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  374. //打印完毕
  375. lbl.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);
  376. return true;
  377. }
  378. public static bool BarTenderS(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)
  379. {
  380. ErrorMessage = "";
  381. DataTable dt = new DataTable();
  382. if (PrintNum == 0)
  383. {
  384. PrintNum = 1;
  385. }
  386. if (IfRePrint == "-1")
  387. {
  388. string printNumber = dh.getFieldDataByCondition("CONFIGS", "DATA", "code='printNumber' and caller='MESSetting'").ToString();
  389. if (dh.getRowCount("labelprintlog", "lpl_type='" + LabelType + "' and lpl_value='" + SnCode + "'") > int.Parse(printNumber))
  390. {
  391. ErrorMessage = "标签补打超出允许打印次数【" + printNumber + "】";
  392. return false;
  393. }
  394. }
  395. if (IfRePrint != "-1" && LabelType != "机身标")
  396. {
  397. bool allowPallte = true;
  398. if ((SystemInf.dh.getFieldDataByCondition("master", "MA_FUNCTION", "ma_user='" + SystemInf.CurrentDB + "'").ToString() == "万年MES系统(正式)"))
  399. {
  400. allowPallte = false;
  401. }
  402. if (LabelType == "卡通箱标" || LabelType == "大箱标" || (LabelType == "栈板标" && allowPallte) || LabelType == "彩盒标")
  403. {
  404. 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");
  405. }
  406. else
  407. {
  408. 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");
  409. }
  410. ////如果已经打印过了,则不允许再打印
  411. if (dt.Rows.Count > 0)
  412. {
  413. ErrorMessage = SnCode + LabelType + "已打印";
  414. return false;
  415. }
  416. }
  417. else
  418. {
  419. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "彩盒标")
  420. {
  421. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "'", "select");
  422. if (dt.Rows.Count == 0)
  423. {
  424. ErrorMessage = SnCode + LabelType + "未打印,不允许补打";
  425. return false;
  426. }
  427. }
  428. }
  429. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  430. string LA_SOFTTYPE = dh.getFieldDataByCondition("label", "LA_SOFTTYPE", "la_id = '" + LaID + "'").ToString();
  431. //如果有附件上传的话
  432. string fp_name = "";
  433. //下载附件
  434. FileInfo PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
  435. if (!PrintFile.Exists)
  436. {
  437. if (LA_SOFTTYPE != "")
  438. {
  439. string[] fpid = LA_SOFTTYPE.Split(';');
  440. for (int i = 0; i < fpid.Length; i++)
  441. {
  442. if (fpid[i] != "")
  443. {
  444. DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
  445. if (label.Rows.Count > 0)
  446. {
  447. string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
  448. fp_name = label.Rows[0]["fp_name"].ToString();
  449. WebClient wc = new WebClient();
  450. wc.DownloadFile("http://192.168.110.18:8099/" + fp_path, @"C:\打印标签\" + fp_name);
  451. }
  452. }
  453. }
  454. lbl.Stop();
  455. lbl = new Engine(true);
  456. BaseUtil.WriteLbl();
  457. }
  458. }
  459. else
  460. {
  461. string filechangetime = PrintFile.LastWriteTime.ToString();
  462. System.DateTime dateTime1 = System.DateTime.Parse(filechangetime);
  463. System.DateTime dateTime2 = System.DateTime.Parse(filelastwritetime);
  464. if (dateTime1 < dateTime2)
  465. {
  466. if (LA_SOFTTYPE != "")
  467. {
  468. string[] fpid = LA_SOFTTYPE.Split(';');
  469. for (int i = 0; i < fpid.Length; i++)
  470. {
  471. if (fpid[i] != "")
  472. {
  473. DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
  474. if (label.Rows.Count > 0)
  475. {
  476. string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
  477. fp_name = label.Rows[0]["fp_name"].ToString();
  478. WebClient wc = new WebClient();
  479. wc.DownloadFile("http://192.168.110.18:8099/" + fp_path, @"C:\打印标签\" + fp_name);
  480. FileInfo file = new FileInfo(@"C:\打印标签\" + fp_name);
  481. file.CreationTime = Convert.ToDateTime(filelastwritetime);
  482. }
  483. }
  484. }
  485. }
  486. lbl.Stop();
  487. lbl = new Engine(true);
  488. BaseUtil.WriteLbl();
  489. }
  490. }
  491. PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
  492. if (!PrintFile.Exists)
  493. {
  494. MessageBox.Show("打印文件不存在");
  495. return false;
  496. }
  497. //查询模板对应的取值SQL和参数名称
  498. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  499. StringBuilder sb = new StringBuilder();
  500. if (!PrintFile.Exists)
  501. {
  502. MessageBox.Show("打印文件不存在");
  503. return false;
  504. }
  505. format = lbl.Documents.Open(@"C:\打印标签\" + LabelName);
  506. if (format == null)
  507. {
  508. MessageBox.Show("标签文件打开失败");
  509. return false;
  510. }
  511. string ParamValue = format.SubStrings.GetAll("#", "$");
  512. string[] paramname = ParamValue.Split('$');
  513. for (int i = 0; i < paramname.Length; i++)
  514. {
  515. paramname[i] = paramname[i].Split('#')[0];
  516. }
  517. //执行全部的SQL
  518. for (int i = 0; i < dt.Rows.Count; i++)
  519. {
  520. string sql = dt.Rows[i]["lp_sql"].ToString();
  521. try
  522. {
  523. Regex ConnoteA = new Regex("{\\w+}");
  524. foreach (Match mch in ConnoteA.Matches(sql))
  525. {
  526. string x = mch.Value.Trim();
  527. sql = sql.Replace(x, "'" + SnCode + "'");
  528. }
  529. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  530. if (Param.Rows.Count == 0)
  531. continue;
  532. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  533. if (Param.Rows.Count > 0)
  534. {
  535. int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count;
  536. for (int j = 0; j < LoopTime; j++)
  537. {
  538. //if (paramname.Contains(dt.Rows[i]["lp_name"].ToString().ToUpper()))
  539. //{
  540. // if (format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper()] != null)
  541. // {
  542. // format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper()].Value = Param.Rows[0][0].ToString();
  543. // }
  544. //}
  545. //if (paramname.Contains(dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)))
  546. //{
  547. // if (format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)] != null)
  548. // {
  549. // format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)].Value = Param.Rows[j][0].ToString(); ;
  550. // }
  551. //}
  552. //try
  553. //{
  554. // if (format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper()] != null)
  555. // {
  556. // format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper()].Value = Param.Rows[0][0].ToString();
  557. // }
  558. // if (format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)] != null)
  559. // {
  560. // format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)].Value = Param.Rows[j][0].ToString(); ;
  561. // }
  562. //}
  563. //catch (Exception e)
  564. //{
  565. // Console.WriteLine( e.Message);
  566. //}
  567. for (int k = 0; k < format.SubStrings.Count; k++)
  568. {
  569. if (j == 0 & format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  570. {
  571. format.SubStrings[k].Value = Param.Rows[0][0].ToString();
  572. break;
  573. }
  574. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  575. if (format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  576. {
  577. format.SubStrings[k].Value = Param.Rows[j][0].ToString();
  578. break;
  579. }
  580. }
  581. }
  582. }
  583. }
  584. catch (System.Exception)
  585. {
  586. MessageBox.Show("SQL维护不正确");
  587. }
  588. }
  589. LogManager.DoLog(sb.ToString());
  590. //保存本次赋值进行打印a
  591. format.PrintSetup.PrinterName = PrinterName;
  592. format.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  593. format.Print();
  594. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  595. try
  596. {
  597. for (int k = 0; k < format.SubStrings.Count; k++)
  598. {
  599. format.SubStrings[k].Value = null;
  600. }
  601. }
  602. catch (Exception ex)
  603. {
  604. LogManager.DoLog(ex.Message + ex.StackTrace);
  605. }
  606. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode, PrintFile.Name);
  607. return true;
  608. }
  609. }
  610. }