Print.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 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)
  210. {
  211. ErrorMessage = "";
  212. DataTable dt = new DataTable();
  213. if (PrintNum == 0)
  214. {
  215. PrintNum = 1;
  216. }
  217. //if (IfRePrint == "-1")
  218. //{
  219. // string printNumber = dh.getFieldDataByCondition("CONFIGS", "DATA", "code='printNumber' and caller='MESSetting'").ToString();
  220. // if (dh.getRowCount("labelprintlog", "lpl_type='" + LabelType + "' and lpl_value='" + SnCode + "'") > int.Parse(printNumber))
  221. // {
  222. // ErrorMessage = "标签补打超出允许打印次数【" + printNumber + "】";
  223. // return false;
  224. // }
  225. //}
  226. if (IfRePrint != "-1" && LabelType != "机身标")
  227. {
  228. bool allowPallte = true;
  229. if ((SystemInf.dh.getFieldDataByCondition("master", "MA_FUNCTION", "ma_user='" + SystemInf.CurrentDB + "'").ToString() == "万年MES系统(正式)"))
  230. {
  231. allowPallte = false;
  232. }
  233. if (LabelType == "卡通箱标" || LabelType == "大箱标" || (LabelType == "栈板标" && allowPallte) || LabelType == "彩盒标")
  234. {
  235. 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");
  236. }
  237. else
  238. {
  239. 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");
  240. }
  241. ////如果已经打印过了,则不允许再打印
  242. if (dt.Rows.Count > 0)
  243. {
  244. ErrorMessage = SnCode + LabelType + "已打印";
  245. return false;
  246. }
  247. }
  248. else
  249. {
  250. //if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "彩盒标")
  251. //{
  252. // dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "'", "select");
  253. // if (dt.Rows.Count == 0)
  254. // {
  255. // ErrorMessage = SnCode + LabelType + "未打印,不允许补打";
  256. // return false;
  257. // }
  258. //}
  259. }
  260. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  261. string LA_SOFTTYPE = dh.getFieldDataByCondition("label", "LA_SOFTTYPE", "la_id = '" + LaID + "'").ToString();
  262. //如果有附件上传的话
  263. string fp_name = "";
  264. //下载附件
  265. FileInfo PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
  266. if (!PrintFile.Exists)
  267. {
  268. if (LA_SOFTTYPE != "")
  269. {
  270. string[] fpid = LA_SOFTTYPE.Split(';');
  271. for (int i = 0; i < fpid.Length; i++)
  272. {
  273. if (fpid[i] != "")
  274. {
  275. DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
  276. if (label.Rows.Count > 0)
  277. {
  278. string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
  279. fp_name = label.Rows[0]["fp_name"].ToString();
  280. WebClient wc = new WebClient();
  281. wc.DownloadFile("https://mes.lihantech.cn/" + fp_path, @"C:\打印标签\" + fp_name);
  282. }
  283. }
  284. }
  285. lbl.Stop();
  286. lbl = new Engine(true);
  287. BaseUtil.WriteLbl();
  288. }
  289. }
  290. else
  291. {
  292. string filechangetime = PrintFile.LastWriteTime.ToString();
  293. System.DateTime dateTime1 = System.DateTime.Parse(filechangetime);
  294. System.DateTime dateTime2 = System.DateTime.Parse(filelastwritetime);
  295. if (dateTime1 < dateTime2)
  296. {
  297. if (LA_SOFTTYPE != "")
  298. {
  299. string[] fpid = LA_SOFTTYPE.Split(';');
  300. for (int i = 0; i < fpid.Length; i++)
  301. {
  302. if (fpid[i] != "")
  303. {
  304. DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
  305. if (label.Rows.Count > 0)
  306. {
  307. string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
  308. fp_name = label.Rows[0]["fp_name"].ToString();
  309. WebClient wc = new WebClient();
  310. wc.DownloadFile("https://mes.lihantech.cn/" + fp_path, @"C:\打印标签\" + fp_name);
  311. FileInfo file = new FileInfo(@"C:\打印标签\" + fp_name);
  312. file.CreationTime = Convert.ToDateTime(filelastwritetime);
  313. }
  314. }
  315. }
  316. }
  317. lbl.Stop();
  318. lbl = new Engine(true);
  319. BaseUtil.WriteLbl();
  320. }
  321. }
  322. PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
  323. if (!PrintFile.Exists)
  324. {
  325. MessageBox.Show("打印文件不存在");
  326. return false;
  327. }
  328. //查询模板对应的取值SQL和参数名称
  329. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  330. StringBuilder sb = new StringBuilder();
  331. if (!PrintFile.Exists)
  332. {
  333. MessageBox.Show("打印文件不存在");
  334. return false;
  335. }
  336. format = lbl.Documents.Open(@"C:\打印标签\" + LabelName);
  337. if (format == null)
  338. {
  339. MessageBox.Show("标签文件打开失败");
  340. return false;
  341. }
  342. string ParamValue = format.SubStrings.GetAll("#", "$");
  343. string[] paramname = ParamValue.Split('$');
  344. for (int i = 0; i < paramname.Length; i++)
  345. {
  346. paramname[i] = paramname[i].Split('#')[0];
  347. }
  348. //执行全部的SQL
  349. for (int i = 0; i < dt.Rows.Count; i++)
  350. {
  351. string sql = dt.Rows[i]["lp_sql"].ToString();
  352. try
  353. {
  354. Regex ConnoteA = new Regex("{\\w+}");
  355. foreach (Match mch in ConnoteA.Matches(sql))
  356. {
  357. string x = mch.Value.Trim();
  358. sql = sql.Replace(x, "'" + SnCode + "'");
  359. }
  360. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  361. if (Param.Rows.Count == 0)
  362. continue;
  363. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  364. if (Param.Rows.Count > 0)
  365. {
  366. int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count;
  367. for (int j = 0; j < LoopTime; j++)
  368. {
  369. //if (paramname.Contains(dt.Rows[i]["lp_name"].ToString().ToUpper()))
  370. //{
  371. // if (format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper()] != null)
  372. // {
  373. // format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper()].Value = Param.Rows[0][0].ToString();
  374. // }
  375. //}
  376. //if (paramname.Contains(dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)))
  377. //{
  378. // if (format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)] != null)
  379. // {
  380. // format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)].Value = Param.Rows[j][0].ToString(); ;
  381. // }
  382. //}
  383. //try
  384. //{
  385. // if (format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper()] != null)
  386. // {
  387. // format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper()].Value = Param.Rows[0][0].ToString();
  388. // }
  389. // if (format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)] != null)
  390. // {
  391. // format.SubStrings[dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)].Value = Param.Rows[j][0].ToString(); ;
  392. // }
  393. //}
  394. //catch (Exception e)
  395. //{
  396. // Console.WriteLine( e.Message);
  397. //}
  398. for (int k = 0; k < format.SubStrings.Count; k++)
  399. {
  400. if (j == 0 & format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  401. {
  402. Console.WriteLine(format.SubStrings[k].Name.ToUpper());
  403. format.SubStrings[k].Value = Param.Rows[0][0].ToString();
  404. break;
  405. }
  406. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  407. if (format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  408. {
  409. Console.WriteLine(format.SubStrings[k].Name.ToUpper());
  410. format.SubStrings[k].Value = Param.Rows[j][0].ToString();
  411. break;
  412. }
  413. }
  414. }
  415. }
  416. }
  417. catch (System.Exception)
  418. {
  419. MessageBox.Show("SQL维护不正确");
  420. }
  421. }
  422. LogManager.DoLog(sb.ToString());
  423. //保存本次赋值进行打印a
  424. format.PrintSetup.PrinterName = PrinterName;
  425. format.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  426. format.Print();
  427. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  428. try
  429. {
  430. for (int k = 0; k < format.SubStrings.Count; k++)
  431. {
  432. if (format.SubStrings[k].Value != null)
  433. {
  434. format.SubStrings[k].Value = "";
  435. }
  436. }
  437. }
  438. catch (Exception ex)
  439. {
  440. LogManager.DoLog(ex.Message + ex.StackTrace);
  441. }
  442. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode, PrintFile.Name);
  443. return true;
  444. }
  445. public static bool BarTender(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)
  446. {
  447. ErrorMessage = "";
  448. DataTable dt = new DataTable();
  449. if (PrintNum == 0)
  450. {
  451. PrintNum = 1;
  452. }
  453. //if (IfRePrint == "-1")
  454. //{
  455. // string printNumber = dh.getFieldDataByCondition("CONFIGS", "DATA", "code='printNumber' and caller='MESSetting'").ToString();
  456. // if (dh.getRowCount("labelprintlog", "lpl_type='" + LabelType + "' and lpl_value='" + SnCode + "'") > int.Parse(printNumber))
  457. // {
  458. // ErrorMessage = "标签补打超出允许打印次数【" + printNumber + "】";
  459. // return false;
  460. // }
  461. //}
  462. //if (IfRePrint != "-1" && LabelType != "机身标")
  463. //{
  464. // bool allowPallte = true;
  465. // if ((SystemInf.dh.getFieldDataByCondition("master", "MA_FUNCTION", "ma_user='" + SystemInf.CurrentDB + "'").ToString() == "万年MES系统(正式)"))
  466. // {
  467. // allowPallte = false;
  468. // }
  469. // if (LabelType == "卡通箱标" || LabelType == "大箱标" || (LabelType == "栈板标" && allowPallte) || LabelType == "彩盒标")
  470. // {
  471. // 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");
  472. // }
  473. // else
  474. // {
  475. // 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");
  476. // }
  477. // ////如果已经打印过了,则不允许再打印
  478. // if (dt.Rows.Count > 0)
  479. // {
  480. // ErrorMessage = SnCode + LabelType + "已打印";
  481. // return false;
  482. // }
  483. //}
  484. //else
  485. //{
  486. // //if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "彩盒标")
  487. // //{
  488. // // dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "'", "select");
  489. // // if (dt.Rows.Count == 0)
  490. // // {
  491. // // ErrorMessage = SnCode + LabelType + "未打印,不允许补打";
  492. // // return false;
  493. // // }
  494. // //}
  495. //}
  496. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  497. string LA_SOFTTYPE = dh.getFieldDataByCondition("label", "LA_SOFTTYPE", "la_id = '" + LaID + "'").ToString();
  498. //如果有附件上传的话
  499. string fp_name = "";
  500. //下载附件
  501. FileInfo PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
  502. if (!PrintFile.Exists)
  503. {
  504. if (LA_SOFTTYPE != "")
  505. {
  506. string[] fpid = LA_SOFTTYPE.Split(';');
  507. for (int i = 0; i < fpid.Length; i++)
  508. {
  509. if (fpid[i] != "")
  510. {
  511. DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
  512. if (label.Rows.Count > 0)
  513. {
  514. string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
  515. fp_name = label.Rows[0]["fp_name"].ToString();
  516. WebClient wc = new WebClient();
  517. wc.DownloadFile("https://mes.lihantech.cn/" + fp_path, @"C:\打印标签\" + fp_name);
  518. }
  519. }
  520. }
  521. }
  522. }
  523. else
  524. {
  525. string filechangetime = PrintFile.LastWriteTime.ToString();
  526. System.DateTime dateTime1 = System.DateTime.Parse(filechangetime);
  527. System.DateTime dateTime2 = System.DateTime.Parse(filelastwritetime);
  528. if (dateTime1 < dateTime2)
  529. {
  530. if (LA_SOFTTYPE != "")
  531. {
  532. string[] fpid = LA_SOFTTYPE.Split(';');
  533. for (int i = 0; i < fpid.Length; i++)
  534. {
  535. if (fpid[i] != "")
  536. {
  537. DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
  538. if (label.Rows.Count > 0)
  539. {
  540. string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
  541. fp_name = label.Rows[0]["fp_name"].ToString();
  542. WebClient wc = new WebClient();
  543. wc.DownloadFile("https://mes.lihantech.cn/" + fp_path, @"C:\打印标签\" + fp_name);
  544. FileInfo file = new FileInfo(@"C:\打印标签\" + fp_name);
  545. file.CreationTime = Convert.ToDateTime(filelastwritetime);
  546. }
  547. }
  548. }
  549. }
  550. BaseUtil.WriteLbl();
  551. }
  552. }
  553. PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
  554. if (!PrintFile.Exists)
  555. {
  556. MessageBox.Show("打印文件不存在");
  557. return false;
  558. }
  559. //查询模板对应的取值SQL和参数名称
  560. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  561. StringBuilder sb = new StringBuilder();
  562. if (!PrintFile.Exists)
  563. {
  564. MessageBox.Show("打印文件不存在");
  565. return false;
  566. }
  567. doc2 = lbl.Formats.Open(@"C:\打印标签\" + LabelName);
  568. if (doc2 == null)
  569. {
  570. MessageBox.Show("标签文件打开失败");
  571. return false;
  572. }
  573. //执行全部的SQL
  574. for (int i = 0; i < dt.Rows.Count; i++)
  575. {
  576. string sql = dt.Rows[i]["lp_sql"].ToString();
  577. try
  578. {
  579. Regex ConnoteA = new Regex("{\\w+}");
  580. foreach (Match mch in ConnoteA.Matches(sql))
  581. {
  582. string x = mch.Value.Trim();
  583. sql = sql.Replace(x, "'" + SnCode + "'");
  584. }
  585. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  586. if (Param.Rows.Count == 0)
  587. continue;
  588. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  589. if (Param.Rows.Count > 0)
  590. {
  591. int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count;
  592. for (int j = 0; j < LoopTime; j++)
  593. {
  594. for (int k = 0; k < doc2.NamedSubStrings.Count; k++)
  595. {
  596. if (j == 0 & doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  597. {
  598. doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[0][0].ToString());
  599. }
  600. if (doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  601. {
  602. doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[j][0].ToString());
  603. }
  604. }
  605. }
  606. }
  607. }
  608. catch (System.Exception)
  609. {
  610. MessageBox.Show("SQL维护不正确");
  611. }
  612. }
  613. doc2.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  614. // 序列标签数
  615. doc2.PrintSetup.NumberSerializedLabels = 1;
  616. doc2.PrintSetup.Printer = PrinterName;
  617. doc2.PrintOut(false, false);
  618. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  619. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode, PrintFile.Name);
  620. return true;
  621. }
  622. }
  623. }