Print.cs 19 KB

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