Print.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. //CodeSoft打印的驱动和文件
  16. static Document doc;
  17. ////CodeSoft的打印机
  18. //string CodeSpft_Printer;
  19. static LabelFormatDocument format;
  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. catch (System.Exception)
  88. {
  89. MessageBox.Show("SQL维护不正确");
  90. }
  91. }
  92. LogManager.DoLog(sb.ToString());
  93. //保存本次赋值进行打印
  94. doc.Printer.SwitchTo(PrinterName);
  95. doc.PrintDocument(PrintNum);
  96. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  97. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  98. {
  99. doc.Variables.FormVariables.Item(k + 1).Value = null;
  100. }
  101. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  102. return true;
  103. }
  104. 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)
  105. {
  106. ErrorMessage = "";
  107. DataTable dt = new DataTable();
  108. if (IfRePrint != "-1")
  109. {
  110. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  111. {
  112. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + LabelName + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  113. }
  114. else
  115. {
  116. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + LabelName + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  117. }
  118. ////如果已经打印过了,则不允许再打印
  119. if (dt.Rows.Count > 0)
  120. {
  121. ErrorMessage = SnCode + LabelType + "已打印";
  122. return false;
  123. }
  124. }
  125. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  126. FileInfo PrintFile = new FileInfo(LabelName);
  127. //打开模板路径
  128. //查询模板对应的取值SQL和参数名称
  129. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "' order by to_number(lp_detno)", "select");
  130. StringBuilder sb = new StringBuilder();
  131. if (!PrintFile.Exists)
  132. {
  133. MessageBox.Show("打印文件不存在");
  134. return false;
  135. }
  136. string filechangetime = PrintFile.LastWriteTime.ToString();
  137. if (filechangetime != filelastwritetime)
  138. {
  139. lbl.Quit();
  140. lbl = new ApplicationClass();
  141. BaseUtil.WriteLbl();
  142. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  143. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  144. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  145. }
  146. doc = lbl.Documents.Open(LabelName, true);
  147. if (doc == null)
  148. {
  149. MessageBox.Show("标签文件打开失败");
  150. return false;
  151. }
  152. //执行全部的SQL
  153. for (int i = 0; i < dt.Rows.Count; i++)
  154. {
  155. string sql = dt.Rows[i]["lp_sql"].ToString();
  156. try
  157. {
  158. Regex ConnoteA = new Regex("{\\w+}");
  159. foreach (Match mch in ConnoteA.Matches(sql))
  160. {
  161. string x = mch.Value.Trim();
  162. sql = sql.Replace(x, "'" + SnCode + "'");
  163. }
  164. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  165. if (Param.Rows.Count == 0)
  166. continue;
  167. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  168. if (Param.Rows.Count > 0)
  169. {
  170. int LoopTime = Param.Rows.Count > 100 ? 100 : Param.Rows.Count;
  171. for (int j = 0; j < LoopTime; j++)
  172. {
  173. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  174. {
  175. if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  176. {
  177. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
  178. }
  179. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  180. if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  181. {
  182. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
  183. }
  184. }
  185. }
  186. }
  187. }
  188. catch (System.Exception)
  189. {
  190. MessageBox.Show("SQL维护不正确");
  191. }
  192. }
  193. LogManager.DoLog(sb.ToString());
  194. //保存本次赋值进行打印
  195. doc.Printer.SwitchTo(PrinterName);
  196. doc.PrintDocument(PrintNum);
  197. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  198. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  199. {
  200. doc.Variables.FormVariables.Item(k + 1).Value = null;
  201. }
  202. LogicHandler.doLabelPrintLog(SnCode, LabelType + LabelName, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  203. return true;
  204. }
  205. 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)
  206. {
  207. ErrorMessage = "";
  208. DataTable dt = new DataTable();
  209. //if (IfRePrint != "-1")
  210. //{
  211. // if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  212. // {
  213. // dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  214. // }
  215. // else
  216. // {
  217. // dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpZZZZZl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  218. // }
  219. // ////如果已经打印过了,则不允许再打印
  220. // if (dt.Rows.Count > 0)
  221. // {
  222. // ErrorMessage = SnCode + LabelType + "已打印";
  223. // return false;
  224. // }
  225. //}
  226. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  227. FileInfo PrintFile = new FileInfo(LabelName);
  228. //打开模板路径
  229. //查询模板对应的取值SQL和参数名称
  230. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  231. StringBuilder sb = new StringBuilder();
  232. if (!PrintFile.Exists)
  233. {
  234. MessageBox.Show("打印文件不存在");
  235. return false;
  236. }
  237. string filechangetime = PrintFile.LastWriteTime.ToString();
  238. if (filechangetime != filelastwritetime)
  239. {
  240. lbl.Stop();
  241. lbl = new Engine(true);
  242. BaseUtil.WriteLbl();
  243. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  244. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  245. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  246. }
  247. format = lbl.Documents.Open(LabelName);
  248. if (format == null)
  249. {
  250. MessageBox.Show("标签文件打开失败");
  251. return false;
  252. }
  253. //执行全部的SQL
  254. for (int i = 0; i < dt.Rows.Count; i++)
  255. {
  256. string sql = dt.Rows[i]["lp_sql"].ToString();
  257. try
  258. {
  259. Regex ConnoteA = new Regex("{\\w+}");
  260. foreach (Match mch in ConnoteA.Matches(sql))
  261. {
  262. string x = mch.Value.Trim();
  263. sql = sql.Replace(x, "'" + SnCode + "'");
  264. }
  265. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  266. if (Param.Rows.Count == 0)
  267. continue;
  268. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  269. if (Param.Rows.Count > 0)
  270. {
  271. int LoopTime = Param.Rows.Count > 100 ? 100 : Param.Rows.Count;
  272. for (int j = 0; j < LoopTime; j++)
  273. {
  274. for (int k = 0; k < format.SubStrings.Count; k++)
  275. {
  276. if (j == 0 & format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  277. {
  278. format.SubStrings[k].Value = Param.Rows[0][0].ToString();
  279. }
  280. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  281. if (format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  282. {
  283. format.SubStrings[k].Value = Param.Rows[j][0].ToString();
  284. }
  285. }
  286. }
  287. }
  288. }
  289. catch (System.Exception)
  290. {
  291. MessageBox.Show("SQL维护不正确");
  292. }
  293. }
  294. LogManager.DoLog(sb.ToString());
  295. //保存本次赋值进行打印
  296. format.PrintSetup.PrinterName = PrinterName;
  297. format.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  298. format.Print();
  299. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  300. for (int k = 0; k < format.SubStrings.Count; k++)
  301. {
  302. format.SubStrings[k].Value = null;
  303. }
  304. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  305. return true;
  306. }
  307. }
  308. }