Print.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using UAS_MES.DataOperate;
  2. using LabelManager2;
  3. using System.Data;
  4. using System.Text;
  5. using System.IO;
  6. using UAS_MES.Entity;
  7. using System.Text.RegularExpressions;
  8. using System.Windows.Forms;
  9. using System;
  10. namespace UAS_MES.PublicMethod
  11. {
  12. class Print
  13. {
  14. static DataHelper dh = SystemInf.dh;
  15. //CodeSoft打印的驱动和文件
  16. static Document doc;
  17. ////CodeSoft的打印机
  18. //string CodeSpft_Printer;
  19. static FileInfo info;
  20. public Print() { }
  21. 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)
  22. {
  23. ErrorMessage = "";
  24. DataTable dt = new DataTable();
  25. if (IfRePrint != "-1")
  26. {
  27. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  28. {
  29. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  30. }
  31. else
  32. {
  33. 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");
  34. }
  35. ////如果已经打印过了,则不允许再打印
  36. if (dt.Rows.Count > 0)
  37. {
  38. ErrorMessage = SnCode + LabelType + "已打印";
  39. return false;
  40. }
  41. }
  42. //打开模板路径
  43. //查询模板对应的取值SQL和参数名称
  44. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  45. StringBuilder sb = new StringBuilder();
  46. if (doc == null)
  47. {
  48. MessageBox.Show("打印文件不存在");
  49. return false;
  50. }
  51. //执行全部的SQL
  52. for (int i = 0; i < dt.Rows.Count; i++)
  53. {
  54. string sql = dt.Rows[i]["lp_sql"].ToString();
  55. try
  56. {
  57. Regex ConnoteA = new Regex("{\\w+}");
  58. foreach (Match mch in ConnoteA.Matches(sql))
  59. {
  60. string x = mch.Value.Trim();
  61. sql = sql.Replace(x, "'" + SnCode + "'");
  62. }
  63. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  64. if (Param.Rows.Count == 0)
  65. continue;
  66. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  67. if (Param.Rows.Count > 0)
  68. {
  69. int LoopTime = Param.Rows.Count > 100 ? 100 : Param.Rows.Count;
  70. for (int j = 0; j < LoopTime; j++)
  71. {
  72. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  73. {
  74. if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  75. {
  76. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
  77. }
  78. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  79. if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  80. {
  81. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
  82. }
  83. }
  84. }
  85. }
  86. }
  87. catch (System.Exception ex)
  88. {
  89. MessageBox.Show("SQL维护不正确");
  90. }
  91. }
  92. LogManager.DoLog(sb.ToString());
  93. //保存本次赋值进行打印
  94. doc.Printer.SwitchTo(PrinterName);
  95. doc.PrintDocument(PrintNum);
  96. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  97. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  98. {
  99. doc.Variables.FormVariables.Item(k + 1).Value = null;
  100. }
  101. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  102. return true;
  103. }
  104. 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)
  105. {
  106. ErrorMessage = "";
  107. DataTable dt = new DataTable();
  108. if (IfRePrint != "-1")
  109. {
  110. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  111. {
  112. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  113. }
  114. else
  115. {
  116. 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");
  117. }
  118. ////如果已经打印过了,则不允许再打印
  119. if (dt.Rows.Count > 0)
  120. {
  121. ErrorMessage = SnCode + LabelType + "已打印";
  122. return false;
  123. }
  124. }
  125. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  126. FileInfo PrintFile = new FileInfo(LabelName);
  127. //打开模板路径
  128. //查询模板对应的取值SQL和参数名称
  129. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  130. StringBuilder sb = new StringBuilder();
  131. if (!PrintFile.Exists)
  132. {
  133. MessageBox.Show("打印文件不存在");
  134. return false;
  135. }
  136. string filechangetime = PrintFile.LastWriteTime.ToString();
  137. if (filechangetime != filelastwritetime)
  138. {
  139. lbl.Quit();
  140. lbl = new ApplicationClass();
  141. BaseUtil.WriteLbl();
  142. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  143. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  144. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  145. }
  146. doc = lbl.Documents.Open(LabelName, true);
  147. if (doc == null)
  148. {
  149. MessageBox.Show("标签文件打开失败");
  150. return false;
  151. }
  152. //执行全部的SQL
  153. for (int i = 0; i < dt.Rows.Count; i++)
  154. {
  155. string sql = dt.Rows[i]["lp_sql"].ToString();
  156. try
  157. {
  158. Regex ConnoteA = new Regex("{\\w+}");
  159. foreach (Match mch in ConnoteA.Matches(sql))
  160. {
  161. string x = mch.Value.Trim();
  162. sql = sql.Replace(x, "'" + SnCode + "'");
  163. }
  164. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  165. if (Param.Rows.Count == 0)
  166. continue;
  167. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  168. if (Param.Rows.Count > 0)
  169. {
  170. int LoopTime = Param.Rows.Count > 100 ? 100 : Param.Rows.Count;
  171. for (int j = 0; j < LoopTime; j++)
  172. {
  173. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  174. {
  175. if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  176. {
  177. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
  178. }
  179. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  180. if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  181. {
  182. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
  183. }
  184. }
  185. }
  186. }
  187. }
  188. catch (System.Exception ex)
  189. {
  190. MessageBox.Show("SQL维护不正确");
  191. }
  192. }
  193. LogManager.DoLog(sb.ToString());
  194. //保存本次赋值进行打印
  195. doc.Printer.SwitchTo(PrinterName);
  196. doc.PrintDocument(PrintNum);
  197. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  198. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  199. {
  200. doc.Variables.FormVariables.Item(k + 1).Value = null;
  201. }
  202. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  203. return true;
  204. }
  205. }
  206. }