Make_CustomLabelPrint.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using LabelManager2;
  2. using Seagull.BarTender.Print;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Threading;
  14. using System.Windows.Forms;
  15. using UAS_MES_NEW.DataOperate;
  16. using UAS_MES_NEW.Entity;
  17. using UAS_MES_NEW.PublicForm;
  18. using UAS_MES_NEW.PublicMethod;
  19. namespace UAS_MES_NEW.Make
  20. {
  21. public partial class Make_CustomLabelPrint : Form
  22. {
  23. DataHelper dh;
  24. LogStringBuilder sql = new LogStringBuilder();
  25. AutoSizeFormClass asc = new AutoSizeFormClass();
  26. Thread InitPrint;
  27. //存放产品信息
  28. DataTable dt;
  29. //存放标签信息
  30. DataTable listA;
  31. //存放标签参数列表信息
  32. DataTable paramsInfo;
  33. //记录当前的步骤数
  34. int currentIndex = 0;
  35. FileInfo info;
  36. Engine engine;
  37. //ftpOperater ftp;
  38. LabelFormatDocument format;
  39. StringBuilder sb = new StringBuilder();
  40. bool canPrint = false;
  41. string[] inputValues;//存放用户输入的参数值
  42. public Make_CustomLabelPrint()
  43. {
  44. InitializeComponent();
  45. }
  46. private void Make_CustomLablePrint_Load(object sender, EventArgs e)
  47. {
  48. asc.controllInitializeSize(this);
  49. OperateResult.AppendText(">>请输入产品代码\n", Color.Black);
  50. InitPrint = new Thread(InPrint);
  51. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  52. BaseUtil.SetFormCenter(stw);
  53. stw.ShowDialog();
  54. pr_code.Focus();
  55. dh = SystemInf.dh;
  56. }
  57. private void InPrint()
  58. {
  59. try
  60. {
  61. engine = new Engine(true);
  62. }
  63. catch (Exception ex)
  64. {
  65. OperateResult.AppendText(ex.Message+ex.StackTrace, Color.Red);
  66. }
  67. }
  68. private void pr_code_KeyDown(object sender, KeyEventArgs e)
  69. {
  70. //按下enter键
  71. if (e.KeyCode == Keys.Enter)
  72. {
  73. //判断产品是否存在
  74. dt = (DataTable)dh.ExecuteSql("select pr_code,pr_detail,pr_spec from product where pr_code='" + pr_code.Text + "' and pr_statuscode ='AUDITED'", "select");
  75. //查询没有值,返回提示用户“产品:XXX,不存在或者为审核”,清空产品代码输入框中的值
  76. if (dt.Rows.Count == 0)
  77. {
  78. OperateResult.AppendText("<<产品:" + pr_code.Text + ",不存在或者未审核\n", Color.Red, pr_code);
  79. return;
  80. }
  81. //查询产品是否有自定义的标签模板
  82. sql.Clear();
  83. sql.Append("select la_code,la_id,substr(la_url,instr(la_url,'\\',-1)+1)la_name,la_url,la_isdefault ");
  84. sql.Append("from label where la_prodcode='" + pr_code.Text + "' and la_templatetype='自定义' and la_statuscode='AUDITED' order by la_isdefault");
  85. listA = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  86. if (listA.Rows.Count == 0)
  87. {
  88. OperateResult.AppendText("<<产品:" + pr_code.Text + ",没有维护自定义模板\n", Color.Red, pr_code);
  89. return;
  90. }
  91. if (listA.Rows[0]["la_isdefault"].ToString() != "-1")
  92. {
  93. //说明没有默认的模板,那就取pl_id最大的
  94. listA = (DataTable)dh.ExecuteSql("select la_code,la_id,substr(la_url,instr(la_url,'\\',-1)+1)la_name,la_url,la_isdefault from label where la_prodcode='" + pr_code.Text + "' and la_templatetype='自定义' and la_statuscode='AUDITED' order by la_isdefault", "select");
  95. }
  96. //标签显示
  97. PrintLabel.DataSource = listA;
  98. PrintLabel.DisplayMember = "la_url";
  99. PrintLabel.ValueMember = "la_id";
  100. //有值,填充界面
  101. BaseUtil.SetFormValue(this.Controls, dt);
  102. //paramsInfo = (DataTable)dh.ExecuteSql("select lp_detno, lp_name, lp_valuetype,lp_sql from label left join labelparameter on lp_laid=la_id where la_code='"+ listA.Rows[0]["pl_labelcode"].ToString() + "' order by lp_detno asc", "select");
  103. }
  104. }
  105. private void inputText_KeyDown(object sender, KeyEventArgs e)
  106. {
  107. //按下enter键
  108. if (e.KeyCode == Keys.Enter)
  109. {
  110. //产品编号为空
  111. if (pr_code.Text.Trim() == "")
  112. {
  113. OperateResult.AppendText("<<请先输入产品编号\n", Color.Red);
  114. return;
  115. }
  116. if (inputText.Text.Trim() == "")
  117. {
  118. OperateResult.AppendText("<<输入不能为空\n", Color.Red);
  119. return;
  120. }
  121. OperateResult.AppendText(">>" + inputText.Text + "\n", Color.Black);
  122. if (paramsInfo == null || paramsInfo.Rows.Count == 0)
  123. {
  124. OperateResult.AppendText("<<无可用标签\n", Color.Red, inputText);
  125. return;
  126. }
  127. else
  128. {
  129. //下标为1的时候实例化存值的数组
  130. if (currentIndex == 1)
  131. {
  132. inputValues = new string[paramsInfo.Rows.Count];
  133. sb = new StringBuilder();
  134. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'").ToString();
  135. FileInfo PrintFile = new FileInfo(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString());
  136. if (PrintFile == null)
  137. {
  138. MessageBox.Show("打印文件不存在");
  139. return;
  140. }
  141. string filechangetime = PrintFile.LastWriteTime.ToString();
  142. if (filechangetime != filelastwritetime)
  143. {
  144. engine = new Engine(true);
  145. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  146. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  147. dh.UpdateByCondition("label", update, "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'");
  148. }
  149. format = engine.Documents.Open(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString());
  150. }
  151. //将值赋到format对应的变量
  152. for (int i = 0; i < format.SubStrings.Count; i++)
  153. {
  154. //找到对应的变量名
  155. if (format.SubStrings[i].Name == paramsInfo.Rows[currentIndex - 1]["lp_name"].ToString())
  156. {
  157. //如果当时采集行的 lp_valuetype=’INPUT’,并且lp_sql 为空,则直接将采集的值赋给标签打印
  158. if (paramsInfo.Rows[currentIndex - 1]["lp_valuetype"].ToString() == "INPUT" && paramsInfo.Rows[currentIndex - 1]["lp_sql"].ToString() == "")
  159. {
  160. format.SubStrings[i].Value = inputText.Text;
  161. }
  162. else
  163. {
  164. //取sql拿值赋给变量
  165. string sql = paramsInfo.Rows[currentIndex - 1]["lp_sql"].ToString();
  166. Regex ConnoteA = new Regex("{\\w+}");
  167. foreach (Match mch in ConnoteA.Matches(sql))
  168. {
  169. string x = mch.Value.Trim();
  170. sql = sql.Replace(x, "'" + inputText.Text + "'");
  171. }
  172. format.SubStrings[i].Value = dh.GetLabelParam(sql).ToString();
  173. sb.AppendLine("打印参数【" + format.SubStrings[i].Name + "】赋值," + "取值SQL:" + sql + ",取到值" + format.SubStrings[i].Value);
  174. }
  175. inputValues[currentIndex - 1] = format.SubStrings[i].Value;
  176. }
  177. }
  178. if (currentIndex == paramsInfo.Rows.Count)
  179. {
  180. OperateResult.AppendText("<<输入完成,正在打印\n", Color.Black, inputText);
  181. canPrint = true;
  182. //打印
  183. LogManager.DoLog(sb.ToString());
  184. //保存本次赋值进行打印
  185. format.PrintSetup.PrinterName = Printer.Text;
  186. format.PrintSetup.IdenticalCopiesOfLabel = int.Parse(PrintNum.Text);
  187. format.Print();
  188. for (int i = 0; i < format.SubStrings.Count; i++)
  189. {
  190. format.SubStrings[i].Value = null;
  191. }
  192. //讲文件最后写入时间改成数据库中拿到的时间
  193. //记录打印日志
  194. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "自定义标打印", "成功打印", "", "");
  195. //打印成功
  196. OperateResult.AppendText("<<打印成功\n", Color.Green);
  197. //重置currenIndex为1
  198. currentIndex = 1;
  199. OperateResult.AppendText(">>请输入自定义模板的" + paramsInfo.Rows[0]["lp_name"].ToString() + "所需参数\n", Color.Black);
  200. }
  201. else
  202. {
  203. canPrint = false;
  204. currentIndex++;
  205. OperateResult.AppendText("<<请输入自定义模板的" + paramsInfo.Rows[currentIndex - 1]["lp_name"].ToString() + "所需参数\n", Color.Green, inputText);
  206. }
  207. }
  208. }
  209. }
  210. private void PrintLabel_TextChanged(object sender, EventArgs e)
  211. {
  212. if (PrintLabel.Text.StartsWith("System"))
  213. {
  214. return;
  215. }
  216. paramsInfo = (DataTable)dh.ExecuteSql("select lp_detno, lp_name, lp_valuetype,lp_sql from label left join labelparameter on lp_laid=la_id where la_code='" + listA.Rows[PrintLabel.SelectedIndex]["la_code"].ToString() + "'and LA_TEMPLATETYPE = '自定义' order by to_number(lp_detno) asc", "select");
  217. if (paramsInfo.Rows.Count > 0)
  218. {
  219. //根据查询出来的参数,一次提示用户, “<<SN001 “,
  220. //“>> 请输入自定义模板的(XXXX参数名称)lp_name”
  221. OperateResult.AppendText("<<" + listA.Rows[PrintLabel.SelectedIndex]["la_code"].ToString() + "\n", Color.Black);
  222. OperateResult.AppendText(">>请输入自定义模板" + paramsInfo.Rows[0]["lp_name"].ToString() + "所需参数\n", Color.Green);
  223. //打开模板路径
  224. currentIndex = 1;
  225. //聚焦输入框
  226. inputText.Focus();
  227. }
  228. else
  229. {
  230. OperateResult.AppendText("<<标签编号:" + listA.Rows[0]["la_code"].ToString() + "无标签参数\n", Color.Red);
  231. }
  232. }
  233. private void Make_CustomLabelPrint_SizeChanged(object sender, EventArgs e)
  234. {
  235. asc.controlAutoSize(this);
  236. }
  237. private void Make_CustomLabelPrint_FormClosing(object sender, FormClosingEventArgs e)
  238. {
  239. }
  240. private void pr_code_Leave(object sender, EventArgs e)
  241. {
  242. //pr_code_KeyDown(sender, new KeyEventArgs(Keys.Enter));
  243. }
  244. private void printButton_Click(object sender, EventArgs e)
  245. {
  246. if (canPrint)
  247. {
  248. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'").ToString();
  249. FileInfo PrintFile = new FileInfo(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString());
  250. if (PrintFile == null)
  251. {
  252. MessageBox.Show("打印文件不存在");
  253. return;
  254. }
  255. string filechangetime = PrintFile.LastWriteTime.ToString();
  256. if (filechangetime != filelastwritetime)
  257. {
  258. engine = new Engine(true);
  259. BaseUtil.WriteLbl();
  260. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  261. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  262. dh.UpdateByCondition("label", update, "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'");
  263. }
  264. format = engine.Documents.Open(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString());
  265. sb = new StringBuilder();
  266. //将值赋到format对应的变量
  267. for (int i = 0; i < format.SubStrings.Count; i++)
  268. {
  269. for (int j = 0; j < paramsInfo.Rows.Count; j++)
  270. {
  271. //找到对应的变量名
  272. if (format.SubStrings[i].Name == paramsInfo.Rows[j]["lp_name"].ToString())
  273. {
  274. //直接赋值
  275. format.SubStrings[i].Value = inputValues[j];
  276. }
  277. }
  278. }
  279. //打印
  280. LogManager.DoLog(sb.ToString());
  281. //保存本次赋值进行打印
  282. format.PrintSetup.PrinterName = Printer.Text;
  283. format.PrintSetup.IdenticalCopiesOfLabel = int.Parse(PrintNum.Text);
  284. format.Print();
  285. for (int i = 0; i < format.SubStrings.Count; i++)
  286. {
  287. format.SubStrings[i].Value = null;
  288. }
  289. //讲文件最后写入时间改成数据库中拿到的时间
  290. //记录打印日志
  291. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "自定义标打印", "成功打印", "", "");
  292. //打印成功
  293. OperateResult.AppendText("<<打印成功\n", Color.Green);
  294. }
  295. else
  296. {
  297. OperateResult.AppendText("<<标签参数未全部输入完成\n", Color.Red);
  298. }
  299. }
  300. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  301. {
  302. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  303. {
  304. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  305. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  306. }
  307. }
  308. }
  309. }