Print.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. namespace UAS_MES.PublicMethod
  10. {
  11. class Print
  12. {
  13. static DataHelper dh = new DataHelper();
  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. //else
  87. //{
  88. // for (int j = 0; j < doc.Variables.FormVariables.Count; j++)
  89. // {
  90. // if (doc.Variables.FormVariables.Item(j + 1).Name == dt.Rows[i]["lp_name"].ToString())
  91. // {
  92. // doc.Variables.FormVariables.Item(j + 1).Value = Param.Rows[0][0].ToString();
  93. // break;
  94. // }
  95. // }
  96. //}
  97. }
  98. catch (System.Exception ex)
  99. {
  100. MessageBox.Show("SQL维护不正确");
  101. }
  102. }
  103. LogManager.DoLog(sb.ToString());
  104. //保存本次赋值进行打印
  105. //doc.Save();
  106. doc.Printer.SwitchTo(PrinterName);
  107. doc.PrintDocument(PrintNum);
  108. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  109. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  110. return true;
  111. }
  112. }
  113. }