Print.cs 11 KB

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