Print.cs 12 KB

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