Print.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. namespace UAS_MES_NEW.PublicMethod
  11. {
  12. class Print
  13. {
  14. static DataHelper dh = SystemInf.dh;
  15. //CodeSoft打印的驱动和文件
  16. static Document doc;
  17. static LabelFormatDocument doc2;
  18. ////CodeSoft的打印机
  19. //string CodeSpft_Printer;
  20. static FileInfo info;
  21. public Print() { }
  22. 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)
  23. {
  24. ErrorMessage = "";
  25. DataTable dt = new DataTable();
  26. if (IfRePrint != "-1")
  27. {
  28. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  29. {
  30. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  31. }
  32. else
  33. {
  34. 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");
  35. }
  36. ////如果已经打印过了,则不允许再打印
  37. if (dt.Rows.Count > 0)
  38. {
  39. ErrorMessage = SnCode + LabelType + "已打印";
  40. return false;
  41. }
  42. }
  43. //打开模板路径
  44. //查询模板对应的取值SQL和参数名称
  45. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  46. StringBuilder sb = new StringBuilder();
  47. if (doc == null)
  48. {
  49. MessageBox.Show("打印文件不存在");
  50. return false;
  51. }
  52. //执行全部的SQL
  53. for (int i = 0; i < dt.Rows.Count; i++)
  54. {
  55. string sql = dt.Rows[i]["lp_sql"].ToString();
  56. try
  57. {
  58. Regex ConnoteA = new Regex("{\\w+}");
  59. foreach (Match mch in ConnoteA.Matches(sql))
  60. {
  61. string x = mch.Value.Trim();
  62. sql = sql.Replace(x, "'" + SnCode + "'");
  63. }
  64. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  65. if (Param.Rows.Count == 0)
  66. continue;
  67. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  68. if (Param.Rows.Count > 0)
  69. {
  70. int LoopTime = Param.Rows.Count > 100 ? 100 : Param.Rows.Count;
  71. for (int j = 0; j < LoopTime; j++)
  72. {
  73. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  74. {
  75. if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  76. {
  77. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
  78. }
  79. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  80. if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  81. {
  82. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
  83. }
  84. }
  85. }
  86. }
  87. }
  88. catch (System.Exception)
  89. {
  90. MessageBox.Show("SQL维护不正确");
  91. }
  92. }
  93. LogManager.DoLog(sb.ToString());
  94. //保存本次赋值进行打印
  95. doc.Printer.SwitchTo(PrinterName);
  96. doc.PrintDocument(PrintNum);
  97. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  98. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  99. {
  100. doc.Variables.FormVariables.Item(k + 1).Value = null;
  101. }
  102. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  103. return true;
  104. }
  105. 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)
  106. {
  107. ErrorMessage = "";
  108. DataTable dt = new DataTable();
  109. if (IfRePrint != "-1")
  110. {
  111. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  112. {
  113. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  114. }
  115. else
  116. {
  117. 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");
  118. }
  119. ////如果已经打印过了,则不允许再打印
  120. //if (dt.Rows.Count > 0)
  121. //{
  122. // ErrorMessage = SnCode + LabelType + "已打印";
  123. // return false;
  124. //}
  125. }
  126. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  127. FileInfo PrintFile = new FileInfo(LabelName);
  128. //打开模板路径
  129. //查询模板对应的取值SQL和参数名称
  130. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  131. StringBuilder sb = new StringBuilder();
  132. if (!PrintFile.Exists)
  133. {
  134. MessageBox.Show("打印文件不存在");
  135. return false;
  136. }
  137. string filechangetime = PrintFile.LastWriteTime.ToString();
  138. if (filechangetime != filelastwritetime)
  139. {
  140. lbl.Quit();
  141. lbl = new ApplicationClass();
  142. BaseUtil.WriteLbl();
  143. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  144. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  145. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  146. }
  147. doc = lbl.Documents.Open(LabelName, true);
  148. if (doc == null)
  149. {
  150. MessageBox.Show("标签文件打开失败");
  151. return false;
  152. }
  153. //执行全部的SQL
  154. for (int i = 0; i < dt.Rows.Count; i++)
  155. {
  156. string sql = dt.Rows[i]["lp_sql"].ToString();
  157. try
  158. {
  159. Regex ConnoteA = new Regex("{\\w+}");
  160. foreach (Match mch in ConnoteA.Matches(sql))
  161. {
  162. string x = mch.Value.Trim();
  163. sql = sql.Replace(x, "'" + SnCode + "'");
  164. }
  165. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  166. if (Param.Rows.Count == 0)
  167. continue;
  168. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  169. if (Param.Rows.Count > 0)
  170. {
  171. int LoopTime = Param.Rows.Count > 100 ? 100 : Param.Rows.Count;
  172. for (int j = 0; j < LoopTime; j++)
  173. {
  174. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  175. {
  176. if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  177. {
  178. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
  179. }
  180. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  181. if (j != 0 && doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j))
  182. {
  183. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
  184. }
  185. }
  186. }
  187. }
  188. }
  189. catch (System.Exception)
  190. {
  191. MessageBox.Show("SQL维护不正确");
  192. }
  193. }
  194. LogManager.DoLog(sb.ToString());
  195. //保存本次赋值进行打印
  196. doc.Printer.SwitchTo(PrinterName);
  197. doc.PrintDocument(PrintNum);
  198. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  199. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  200. {
  201. doc.Variables.FormVariables.Item(k + 1).Value = null;
  202. }
  203. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  204. return true;
  205. }
  206. public static bool SinglePrint(string iCaller, Seagull.BarTender.Print.Engine lbl, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
  207. {
  208. ErrorMessage = "";
  209. DataTable dt = new DataTable();
  210. if (IfRePrint != "-1")
  211. {
  212. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  213. {
  214. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  215. }
  216. else
  217. {
  218. 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");
  219. }
  220. //////如果已经打印过了,则不允许再打印
  221. //if (dt.Rows.Count > 0)
  222. //{
  223. // ErrorMessage = SnCode + LabelType + "已打印";
  224. // return false;
  225. //}
  226. }
  227. // string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  228. // FileInfo PrintFile = new FileInfo(LabelName);
  229. //打开模板路径
  230. //查询模板对应的取值SQL和参数名称
  231. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  232. //StringBuilder sb = new StringBuilder();
  233. //if (!PrintFile.Exists)
  234. //{
  235. // MessageBox.Show("打印文件不存在");
  236. // return false;
  237. //}
  238. //string filechangetime = PrintFile.LastWriteTime.ToString();
  239. //if (filechangetime != filelastwritetime)
  240. //{
  241. // lbl.Dispose();
  242. // lbl = new Engine();
  243. // // BaseUtil.WriteLbl();
  244. // filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  245. // string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  246. // dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  247. //}
  248. doc2 = lbl.Documents.Open(LabelName);
  249. if (doc2 == null)
  250. {
  251. MessageBox.Show("标签文件打开失败");
  252. return false;
  253. }
  254. //执行全部的SQL
  255. for (int i = 0; i < dt.Rows.Count; i++)
  256. {
  257. string sql = dt.Rows[i]["lp_sql"].ToString();
  258. try
  259. {
  260. Regex ConnoteA = new Regex("{\\w+}");
  261. foreach (Match mch in ConnoteA.Matches(sql))
  262. {
  263. string x = mch.Value.Trim();
  264. sql = sql.Replace(x, "'" + SnCode + "'");
  265. }
  266. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  267. if (Param.Rows.Count == 0)
  268. continue;
  269. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  270. if (Param.Rows.Count > 0)
  271. {
  272. int LoopTime = Param.Rows.Count > 100 ? 100 : Param.Rows.Count;
  273. for (int j = 0; j < LoopTime; j++)
  274. {
  275. for (int k = 0; k < doc2.SubStrings.Count; k++)
  276. {
  277. if (j == 0 & doc2.SubStrings[k + 1].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  278. {
  279. doc2.SubStrings[k + 1].Value = (Param.Rows[0][0].ToString());
  280. }
  281. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  282. if (j != 0 & doc2.SubStrings[k + 1].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j))
  283. {
  284. doc2.SubStrings[k + 1].Value = (Param.Rows[j][0].ToString());
  285. }
  286. }
  287. }
  288. }
  289. }
  290. catch (System.Exception ex)
  291. {
  292. MessageBox.Show("SQL维护不正确" + ex.Message);
  293. }
  294. }
  295. // LogManager.DoLog(sb.ToString());
  296. //保存本次赋值进行打印
  297. // doc2.Printer.SwitchTo(PrinterName);
  298. // 同样标签的份数
  299. doc2.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  300. // 序列标签数
  301. doc2.PrintSetup.NumberOfSerializedLabels = 1;
  302. doc2.PrintSetup.PrinterName = PrinterName;
  303. doc2.Print();
  304. doc2.Close(Seagull.BarTender.Print.SaveOptions.DoNotSaveChanges);
  305. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  306. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  307. //打印完毕
  308. return true;
  309. }
  310. }
  311. }