Print.cs 10 KB

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