Print.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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" && LabelType != "栈板标" && LabelType != "机身标")
  216. {
  217. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标" || LabelType == "彩盒标")
  218. {
  219. 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");
  220. }
  221. else
  222. {
  223. 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");
  224. }
  225. ////如果已经打印过了,则不允许再打印
  226. if (dt.Rows.Count > 0)
  227. {
  228. ErrorMessage = SnCode + LabelType + "已打印";
  229. return false;
  230. }
  231. }
  232. else
  233. {
  234. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "彩盒标")
  235. {
  236. 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");
  237. if (dt.Rows.Count == 0)
  238. {
  239. ErrorMessage = SnCode + LabelType + "未打印,不允许补打";
  240. return false;
  241. }
  242. }
  243. }
  244. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  245. string LA_SOFTTYPE = dh.getFieldDataByCondition("label", "LA_SOFTTYPE", "la_id = '" + LaID + "'").ToString();
  246. //如果有附件上传的话
  247. string fp_name = "";
  248. //下载附件
  249. FileInfo PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
  250. if (!PrintFile.Exists)
  251. {
  252. if (LA_SOFTTYPE != "")
  253. {
  254. string[] fpid = LA_SOFTTYPE.Split(';');
  255. for (int i = 0; i < fpid.Length; i++)
  256. {
  257. if (fpid[i] != "")
  258. {
  259. DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
  260. if (label.Rows.Count > 0)
  261. {
  262. string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
  263. fp_name = label.Rows[0]["fp_name"].ToString();
  264. WebClient wc = new WebClient();
  265. wc.DownloadFile("http://218.64.89.153:8099/" + fp_path, @"C:\打印标签\" + fp_name);
  266. }
  267. }
  268. }
  269. lbl.Stop();
  270. lbl = new Engine(true);
  271. BaseUtil.WriteLbl();
  272. }
  273. }
  274. else
  275. {
  276. string filechangetime = PrintFile.LastWriteTime.ToString();
  277. System.DateTime dateTime1 = System.DateTime.Parse(filechangetime);
  278. System.DateTime dateTime2 = System.DateTime.Parse(filelastwritetime);
  279. if (dateTime1 < dateTime2)
  280. {
  281. if (LA_SOFTTYPE != "")
  282. {
  283. string[] fpid = LA_SOFTTYPE.Split(';');
  284. for (int i = 0; i < fpid.Length; i++)
  285. {
  286. if (fpid[i] != "")
  287. {
  288. DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
  289. if (label.Rows.Count > 0)
  290. {
  291. string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
  292. fp_name = label.Rows[0]["fp_name"].ToString();
  293. WebClient wc = new WebClient();
  294. wc.DownloadFile("http://218.64.89.153:8099/" + fp_path, @"C:\打印标签\" + fp_name);
  295. FileInfo file = new FileInfo(@"C:\打印标签\" + fp_name);
  296. file.CreationTime = Convert.ToDateTime(filelastwritetime);
  297. }
  298. }
  299. }
  300. }
  301. lbl.Stop();
  302. lbl = new Engine(true);
  303. BaseUtil.WriteLbl();
  304. }
  305. }
  306. PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
  307. if (!PrintFile.Exists)
  308. {
  309. MessageBox.Show("打印文件不存在");
  310. return false;
  311. }
  312. //查询模板对应的取值SQL和参数名称
  313. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  314. StringBuilder sb = new StringBuilder();
  315. if (!PrintFile.Exists)
  316. {
  317. MessageBox.Show("打印文件不存在");
  318. return false;
  319. }
  320. format = lbl.Documents.Open(@"C:\打印标签\" + LabelName);
  321. if (format == null)
  322. {
  323. MessageBox.Show("标签文件打开失败");
  324. return false;
  325. }
  326. //执行全部的SQL
  327. for (int i = 0; i < dt.Rows.Count; i++)
  328. {
  329. string sql = dt.Rows[i]["lp_sql"].ToString();
  330. try
  331. {
  332. Regex ConnoteA = new Regex("{\\w+}");
  333. foreach (Match mch in ConnoteA.Matches(sql))
  334. {
  335. string x = mch.Value.Trim();
  336. sql = sql.Replace(x, "'" + SnCode + "'");
  337. }
  338. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  339. if (Param.Rows.Count == 0)
  340. continue;
  341. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  342. if (Param.Rows.Count > 0)
  343. {
  344. int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count;
  345. for (int j = 0; j < LoopTime; j++)
  346. {
  347. for (int k = 0; k < format.SubStrings.Count; k++)
  348. {
  349. if (j == 0 & format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  350. {
  351. format.SubStrings[k].Value = Param.Rows[0][0].ToString();
  352. }
  353. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  354. if (format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  355. {
  356. format.SubStrings[k].Value = Param.Rows[j][0].ToString();
  357. }
  358. }
  359. }
  360. }
  361. }
  362. catch (System.Exception)
  363. {
  364. MessageBox.Show("SQL维护不正确");
  365. }
  366. }
  367. LogManager.DoLog(sb.ToString());
  368. //保存本次赋值进行打印a
  369. format.PrintSetup.PrinterName = PrinterName;
  370. format.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  371. format.Print();
  372. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  373. try
  374. {
  375. for (int k = 0; k < format.SubStrings.Count; k++)
  376. {
  377. format.SubStrings[k].Value = null;
  378. }
  379. }
  380. catch (Exception ex)
  381. {
  382. LogManager.DoLog(ex.Message + ex.StackTrace);
  383. }
  384. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode, PrintFile.Name);
  385. return true;
  386. }
  387. }
  388. }