Print.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 = new DataHelper();
  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. //else
  88. //{
  89. // for (int j = 0; j < doc.Variables.FormVariables.Count; j++)
  90. // {
  91. // if (doc.Variables.FormVariables.Item(j + 1).Name == dt.Rows[i]["lp_name"].ToString())
  92. // {
  93. // doc.Variables.FormVariables.Item(j + 1).Value = Param.Rows[0][0].ToString();
  94. // break;
  95. // }
  96. // }
  97. //}
  98. }
  99. catch (System.Exception ex)
  100. {
  101. MessageBox.Show("SQL维护不正确");
  102. }
  103. }
  104. LogManager.DoLog(sb.ToString());
  105. //保存本次赋值进行打印
  106. //doc.Save();
  107. doc.Printer.SwitchTo(PrinterName);
  108. doc.PrintDocument(PrintNum);
  109. //doc.Close();
  110. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  111. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  112. {
  113. doc.Variables.FormVariables.Item(k + 1).Value = null;
  114. }
  115. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  116. return true;
  117. }
  118. 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)
  119. {
  120. ErrorMessage = "";
  121. DataTable dt = new DataTable();
  122. if (IfRePrint != "-1")
  123. {
  124. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  125. {
  126. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  127. }
  128. else
  129. {
  130. 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");
  131. }
  132. ////如果已经打印过了,则不允许再打印
  133. if (dt.Rows.Count > 0)
  134. {
  135. ErrorMessage = SnCode + LabelType + "已打印";
  136. return false;
  137. }
  138. }
  139. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  140. FileInfo PrintFile = new FileInfo(LabelName);
  141. //打开模板路径
  142. //查询模板对应的取值SQL和参数名称
  143. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  144. StringBuilder sb = new StringBuilder();
  145. if (PrintFile == null)
  146. {
  147. MessageBox.Show("打印文件不存在");
  148. return false;
  149. }
  150. string filechangetime = PrintFile.LastWriteTime.ToString();
  151. if (filechangetime != filelastwritetime)
  152. {
  153. lbl.Quit();
  154. lbl = new ApplicationClass();
  155. BaseUtil.WriteLbl();
  156. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  157. string update = "la_lastwritetime = to_date((regexp_substr('"+ filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  158. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  159. }
  160. doc = lbl.Documents.Open(LabelName, true);
  161. //执行全部的SQL
  162. for (int i = 0; i < dt.Rows.Count; i++)
  163. {
  164. string sql = dt.Rows[i]["lp_sql"].ToString();
  165. try
  166. {
  167. Regex ConnoteA = new Regex("{\\w+}");
  168. foreach (Match mch in ConnoteA.Matches(sql))
  169. {
  170. string x = mch.Value.Trim();
  171. sql = sql.Replace(x, "'" + SnCode + "'");
  172. }
  173. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  174. if (Param.Rows.Count == 0)
  175. continue;
  176. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  177. if (Param.Rows.Count > 0)
  178. {
  179. int LoopTime = Param.Rows.Count > 100 ? 100 : Param.Rows.Count;
  180. for (int j = 0; j < LoopTime; j++)
  181. {
  182. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  183. {
  184. if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  185. {
  186. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
  187. }
  188. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  189. if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  190. {
  191. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
  192. }
  193. }
  194. }
  195. }
  196. ////否则只查询名称相等的参数赋值
  197. //else
  198. //{
  199. // for (int j = 0; j < doc.Variables.FormVariables.Count; j++)
  200. // {
  201. // if (doc.Variables.FormVariables.Item(j + 1).Name == dt.Rows[i]["lp_name"].ToString())
  202. // {
  203. // doc.Variables.FormVariables.Item(j + 1).Value = Param.Rows[0][0].ToString();
  204. // break;
  205. // }
  206. // }
  207. //}
  208. }
  209. catch (System.Exception ex)
  210. {
  211. MessageBox.Show("SQL维护不正确");
  212. }
  213. }
  214. LogManager.DoLog(sb.ToString());
  215. //保存本次赋值进行打印
  216. //doc.Save();
  217. doc.Printer.SwitchTo(PrinterName);
  218. doc.PrintDocument(PrintNum);
  219. //doc.Close();
  220. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  221. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  222. {
  223. doc.Variables.FormVariables.Item(k + 1).Value = null;
  224. }
  225. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  226. return true;
  227. }
  228. }
  229. }