Print.cs 11 KB

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