Make_CustomLabelPrint.cs 12 KB

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