Make_CustomLabelPrint.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using LabelManager2;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Windows.Forms;
  13. using UAS_MES.DataOperate;
  14. using UAS_MES.PublicForm;
  15. using UAS_MES.PublicMethod;
  16. namespace UAS_MES.Make
  17. {
  18. public partial class Make_CustomLabelPrint : Form
  19. {
  20. DataHelper dh;
  21. LogStringBuilder sql = new LogStringBuilder();
  22. AutoSizeFormClass asc = new AutoSizeFormClass();
  23. ApplicationClass lbl;
  24. Thread InitPrint;
  25. //存放产品信息
  26. DataTable dt;
  27. //存放标签信息
  28. DataTable listA;
  29. //存放标签参数列表信息
  30. DataTable paramsInfo;
  31. //记录当前的步骤数
  32. int currentIndex = 0;
  33. Document doc;
  34. FileInfo info;
  35. //ftpOperater ftp;
  36. System.DateTime[] indate;
  37. StringBuilder sb = new StringBuilder();
  38. public Make_CustomLabelPrint()
  39. {
  40. InitializeComponent();
  41. }
  42. private void Make_CustomLablePrint_Load(object sender, EventArgs e)
  43. {
  44. dh = new DataHelper();
  45. pr_code.Focus();
  46. asc.controllInitializeSize(this);
  47. OperateResult.AppendText(">>请输入产品代码\n", Color.Black);
  48. InitPrint = new Thread(InPrint);
  49. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  50. BaseUtil.SetFormCenter(stw);
  51. stw.ShowDialog();
  52. }
  53. private void InPrint()
  54. {
  55. try
  56. {
  57. lbl = new ApplicationClass();
  58. BaseUtil.WriteLbl(lbl);
  59. }
  60. catch (Exception ex)
  61. {
  62. OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red);
  63. }
  64. }
  65. private void pr_code_KeyDown(object sender, KeyEventArgs e)
  66. {
  67. //按下enter键
  68. if (e.KeyCode == Keys.Enter)
  69. {
  70. //判断产品是否存在
  71. dt = (DataTable)dh.ExecuteSql("select pr_code,pr_detail,pr_spec from product where pr_code='" + pr_code.Text + "' and pr_statuscode ='AUDITED'", "select");
  72. //查询没有值,返回提示用户“产品:XXX,不存在或者为审核”,清空产品代码输入框中的值
  73. if (dt.Rows.Count == 0)
  74. {
  75. OperateResult.AppendText("<<产品:" + pr_code.Text + ",不存在或者为审核\n", Color.Red);
  76. pr_code.Text = "";
  77. return;
  78. }
  79. //查询产品是否有自定义的标签模板
  80. listA = (DataTable)dh.ExecuteSql("select pl_labelcode ||':'||pl_labelname pl_name,pl_id,pl_labeltype,pl_labelcode,pl_labelurl,pl_isdefault,pl_detno,pl_labelsoft,pl_status,pl_statuscode,pl_labelname,pl_indate from productlabel where pl_prodcode='" + pr_code.Text + "' and pl_labeltype='自定义' order by pl_isdefault desc", "select");
  81. if (listA.Rows.Count == 0)
  82. {
  83. OperateResult.AppendText("<<产品:" + pr_code.Text + ",没有维护自定义模板\n", Color.Red);
  84. pr_code.Text = "";
  85. return;
  86. }
  87. if (listA.Rows[0]["pl_isdefault"].ToString() != "1") {
  88. //说明没有默认的模板,那就取pl_id最大的
  89. listA = (DataTable)dh.ExecuteSql("select pl_labelcode ||':'||pl_labelname pl_name,pl_id,pl_labeltype,pl_labelcode,pl_labelurl,pl_isdefault,pl_detno,pl_labelsoft,pl_status,pl_statuscode,pl_labelname,pl_indate from productlabel where pl_prodcode='" + pr_code.Text + "' and pl_labeltype='自定义' order by pl_id desc", "select");
  90. }
  91. //下载标签
  92. getLabels();
  93. //标签显示
  94. PrintLabel.DataSource = listA;
  95. PrintLabel.DisplayMember = "pl_name";
  96. PrintLabel.ValueMember = "pl_labelcode";
  97. //有值,填充界面
  98. BaseUtil.SetFormValue(this.Controls, dt);
  99. //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");
  100. }
  101. }
  102. private void inputText_KeyDown(object sender, KeyEventArgs e)
  103. {
  104. //按下enter键
  105. if (e.KeyCode == Keys.Enter)
  106. {
  107. //产品编号为空
  108. if (pr_code.Text.Trim() == "")
  109. {
  110. OperateResult.AppendText("<<请先输入产品编号\n", Color.Red);
  111. return;
  112. }
  113. if (inputText.Text.Trim() == "")
  114. {
  115. OperateResult.AppendText("<<输入不能为空\n", Color.Red);
  116. return;
  117. }
  118. OperateResult.AppendText(">>"+inputText.Text+"\n", Color.Black);
  119. if (paramsInfo == null || paramsInfo.Rows.Count == 0)
  120. {
  121. OperateResult.AppendText("<<无可用标签\n", Color.Red,inputText);
  122. return;
  123. }
  124. else
  125. {
  126. if (doc==null) {
  127. OperateResult.AppendText(">>无可用标签\n", Color.Red,inputText);
  128. return;
  129. }
  130. //将值赋到doc对应的变量
  131. for (int i = 0; i < doc.Variables.FormVariables.Count; i++)
  132. {
  133. //找到对应的变量名
  134. if (doc.Variables.FormVariables.Item(i + 1).Name == paramsInfo.Rows[currentIndex - 1]["lp_name"].ToString())
  135. {
  136. //如果当时采集行的 lp_valuetype=’INPUT’,并且lp_sql 为空,则直接将采集的值赋给标签打印
  137. if (paramsInfo.Rows[currentIndex - 1]["lp_valuetype"].ToString() == "INPUT" && paramsInfo.Rows[currentIndex - 1]["lp_sql"].ToString() == "")
  138. {
  139. doc.Variables.FormVariables.Item(i + 1).Value = inputText.Text;
  140. }
  141. else
  142. {
  143. //取sql拿值赋给变量
  144. string sql = paramsInfo.Rows[currentIndex - 1]["lp_sql"].ToString();
  145. sql = sql.Substring(0, sql.IndexOf("{")) + "'" + inputText.Text + "'";
  146. doc.Variables.FormVariables.Item(i + 1).Value = dh.GetLabelParam(sql).ToString();
  147. sb.AppendLine("打印参数【" + doc.Variables.FormVariables.Item(i + 1).Name + "】赋值," + "取值SQL:" + sql + ",取到值" + doc.Variables.FormVariables.Item(i + 1).Value);
  148. }
  149. }
  150. }
  151. if (currentIndex == paramsInfo.Rows.Count)
  152. {
  153. OperateResult.AppendText("<<输入完成,正在打印\n", Color.Black,inputText);
  154. //打印
  155. LogManager.DoLog(sb.ToString());
  156. //保存本次赋值进行打印
  157. doc.Save();
  158. doc.Printer.SwitchTo(Printer.Text);
  159. doc.PrintDocument(int.Parse(printNum.Text));
  160. doc.Close();
  161. //讲文件最后写入时间改成数据库中拿到的时间
  162. info = new FileInfo(ftpOperater.DownLoadTo + PrintLabel.Text.Split(':')[1]);
  163. info.LastWriteTime = indate[PrintLabel.SelectedIndex];
  164. //打印成功
  165. OperateResult.AppendText("<<打印成功\n", Color.Green);
  166. //重置currenIndex为1
  167. currentIndex = 1;
  168. OperateResult.AppendText(">>请输入自定义模板的" + paramsInfo.Rows[0]["lp_name"].ToString() + "\n", Color.Black);
  169. doc=lbl.Documents.Open(ftpOperater.DownLoadTo + listA.Rows[PrintLabel.SelectedIndex]["pl_labelname"].ToString());
  170. }
  171. else
  172. {
  173. currentIndex++;
  174. OperateResult.AppendText("<<请输入自定义模板的" + paramsInfo.Rows[currentIndex - 1]["lp_name"].ToString() + "\n", Color.Green,inputText);
  175. }
  176. }
  177. }
  178. }
  179. private void getLabels()
  180. {
  181. //ftp = new ftpOperater();
  182. indate = new System.DateTime[listA.Rows.Count];
  183. for (int i = 0; i < listA.Rows.Count; i++)
  184. {
  185. BaseUtil.GetPrintLabel(listA.Rows[i]["pl_labelname"].ToString(), listA.Rows[i]["pl_labelurl"].ToString(), listA.Rows[i]["pl_indate"].ToString());
  186. indate[i] = Convert.ToDateTime(listA.Rows[i]["pl_indate"].ToString());
  187. }
  188. }
  189. private void PrintLabel_TextChanged(object sender, EventArgs e)
  190. {
  191. if (PrintLabel.Text.StartsWith("System")) {
  192. return;
  193. }
  194. 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]["pl_labelcode"].ToString() + "' order by lp_detno asc", "select");
  195. if (paramsInfo.Rows.Count > 0)
  196. {
  197. //根据查询出来的参数,一次提示用户, “<<SN001 “,
  198. //“>> 请输入自定义模板的(XXXX参数名称)lp_name”
  199. OperateResult.AppendText("<<" + listA.Rows[PrintLabel.SelectedIndex]["pl_labelcode"].ToString() + "\n", Color.Black);
  200. OperateResult.AppendText(">>请输入自定义模板" + paramsInfo.Rows[0]["lp_name"].ToString() + "的值\n", Color.Green);
  201. //打开模板路径
  202. //doc = lbl.Documents.Open(ftpOperater.DownLoadTo + PrintLabel.SelectedText.Split(':')[1]);
  203. if (doc != null)
  204. {
  205. doc.Close();
  206. }
  207. doc = lbl.Documents.Open(ftpOperater.DownLoadTo + listA.Rows[PrintLabel.SelectedIndex]["pl_labelname"].ToString());
  208. currentIndex = 1;
  209. //聚焦输入框
  210. inputText.Focus();
  211. }
  212. else
  213. {
  214. OperateResult.AppendText("<<标签编号:" + listA.Rows[0]["pl_labelcode"].ToString() + "无标签参数\n", Color.Red);
  215. }
  216. }
  217. private void Make_CustomLabelPrint_SizeChanged(object sender, EventArgs e)
  218. {
  219. asc.controlAutoSize(this);
  220. }
  221. private void PrintLabel_SelectedIndexChanged(object sender, EventArgs e)
  222. {
  223. }
  224. private void Make_CustomLabelPrint_FormClosing(object sender, FormClosingEventArgs e)
  225. {
  226. if (doc!=null)
  227. {
  228. doc.Close();
  229. }
  230. }
  231. private void pr_code_Leave(object sender, EventArgs e)
  232. {
  233. //pr_code_KeyDown(sender, new KeyEventArgs(Keys.Enter));
  234. }
  235. }
  236. }