Make_CustomLabelPrint.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.Text.RegularExpressions;
  12. using System.Threading;
  13. using System.Windows.Forms;
  14. using UAS_MES.DataOperate;
  15. using UAS_MES.Entity;
  16. using UAS_MES.PublicForm;
  17. using UAS_MES.PublicMethod;
  18. namespace UAS_MES.Make
  19. {
  20. public partial class Make_CustomLabelPrint : Form
  21. {
  22. DataHelper dh;
  23. LogStringBuilder sql = new LogStringBuilder();
  24. AutoSizeFormClass asc = new AutoSizeFormClass();
  25. ApplicationClass lbl;
  26. Thread InitPrint;
  27. //存放产品信息
  28. DataTable dt;
  29. //存放标签信息
  30. DataTable listA;
  31. //存放标签参数列表信息
  32. DataTable paramsInfo;
  33. //记录当前的步骤数
  34. int currentIndex = 0;
  35. Document doc;
  36. FileInfo info;
  37. //ftpOperater ftp;
  38. StringBuilder sb = new StringBuilder();
  39. bool canPrint = false;
  40. string[] inputValues;//存放用户输入的参数值
  41. public Make_CustomLabelPrint()
  42. {
  43. InitializeComponent();
  44. }
  45. private void Make_CustomLablePrint_Load(object sender, EventArgs e)
  46. {
  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. pr_code.Focus();
  54. dh = SystemInf.dh;
  55. }
  56. private void InPrint()
  57. {
  58. try
  59. {
  60. lbl = new ApplicationClass();
  61. BaseUtil.WriteLbl();
  62. }
  63. catch (Exception ex)
  64. {
  65. OperateResult.AppendText("未正确安装CodeSoft软件\n", 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. if (PrintFile.LastWriteTime.ToString() != filelastwritetime)
  142. {
  143. lbl.Quit();
  144. lbl = new ApplicationClass();
  145. BaseUtil.WriteLbl();
  146. dh.UpdateByCondition("label", "la_lastwritetime = to_date('" + PrintFile.LastWriteTime.ToString() + "', 'yyyy-mm-dd hh24:mi:ss') ", "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'");
  147. }
  148. doc = lbl.Documents.Open(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString(), true);
  149. }
  150. //将值赋到doc对应的变量
  151. for (int i = 0; i < doc.Variables.FormVariables.Count; i++)
  152. {
  153. //找到对应的变量名
  154. if (doc.Variables.FormVariables.Item(i + 1).Name == paramsInfo.Rows[currentIndex - 1]["lp_name"].ToString())
  155. {
  156. //如果当时采集行的 lp_valuetype=’INPUT’,并且lp_sql 为空,则直接将采集的值赋给标签打印
  157. if (paramsInfo.Rows[currentIndex - 1]["lp_valuetype"].ToString() == "INPUT" && paramsInfo.Rows[currentIndex - 1]["lp_sql"].ToString() == "")
  158. {
  159. doc.Variables.FormVariables.Item(i + 1).Value = inputText.Text;
  160. }
  161. else
  162. {
  163. //取sql拿值赋给变量
  164. string sql = paramsInfo.Rows[currentIndex - 1]["lp_sql"].ToString();
  165. Regex ConnoteA = new Regex("{\\w+}");
  166. foreach (Match mch in ConnoteA.Matches(sql))
  167. {
  168. string x = mch.Value.Trim();
  169. sql = sql.Replace(x, "'" + inputText.Text + "'");
  170. }
  171. doc.Variables.FormVariables.Item(i + 1).Value = dh.GetLabelParam(sql).ToString();
  172. sb.AppendLine("打印参数【" + doc.Variables.FormVariables.Item(i + 1).Name + "】赋值," + "取值SQL:" + sql + ",取到值" + doc.Variables.FormVariables.Item(i + 1).Value);
  173. }
  174. inputValues[currentIndex - 1] = doc.Variables.FormVariables.Item(i + 1).Value;
  175. }
  176. }
  177. if (currentIndex == paramsInfo.Rows.Count)
  178. {
  179. OperateResult.AppendText("<<输入完成,正在打印\n", Color.Black, inputText);
  180. canPrint = true;
  181. //打印
  182. LogManager.DoLog(sb.ToString());
  183. //保存本次赋值进行打印
  184. doc.Printer.SwitchTo(Printer.Text);
  185. doc.PrintDocument(int.Parse(PrintNum.Text));
  186. for (int i = 0; i < doc.Variables.FormVariables.Count; i++)
  187. {
  188. doc.Variables.FormVariables.Item(i + 1).Value = null;
  189. }
  190. //讲文件最后写入时间改成数据库中拿到的时间
  191. //记录打印日志
  192. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "自定义标打印", "成功打印", "", "");
  193. //打印成功
  194. OperateResult.AppendText("<<打印成功\n", Color.Green);
  195. //重置currenIndex为1
  196. currentIndex = 1;
  197. OperateResult.AppendText(">>请输入自定义模板的" + paramsInfo.Rows[0]["lp_name"].ToString() + "\n", Color.Black);
  198. }
  199. else
  200. {
  201. canPrint = false;
  202. currentIndex++;
  203. OperateResult.AppendText("<<请输入自定义模板的" + paramsInfo.Rows[currentIndex - 1]["lp_name"].ToString() + "\n", Color.Green, inputText);
  204. }
  205. }
  206. }
  207. }
  208. private void PrintLabel_TextChanged(object sender, EventArgs e)
  209. {
  210. if (PrintLabel.Text.StartsWith("System"))
  211. {
  212. return;
  213. }
  214. 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() + "' order by lp_detno asc", "select");
  215. if (paramsInfo.Rows.Count > 0)
  216. {
  217. //根据查询出来的参数,一次提示用户, “<<SN001 “,
  218. //“>> 请输入自定义模板的(XXXX参数名称)lp_name”
  219. OperateResult.AppendText("<<" + listA.Rows[PrintLabel.SelectedIndex]["la_code"].ToString() + "\n", Color.Black);
  220. OperateResult.AppendText(">>请输入自定义模板" + paramsInfo.Rows[0]["lp_name"].ToString() + "的值\n", Color.Green);
  221. //打开模板路径
  222. currentIndex = 1;
  223. //聚焦输入框
  224. inputText.Focus();
  225. }
  226. else
  227. {
  228. OperateResult.AppendText("<<标签编号:" + listA.Rows[0]["la_code"].ToString() + "无标签参数\n", Color.Red);
  229. }
  230. }
  231. private void Make_CustomLabelPrint_SizeChanged(object sender, EventArgs e)
  232. {
  233. asc.controlAutoSize(this);
  234. }
  235. private void Make_CustomLabelPrint_FormClosing(object sender, FormClosingEventArgs e)
  236. {
  237. BaseUtil.ClosePrint(lbl);
  238. }
  239. private void pr_code_Leave(object sender, EventArgs e)
  240. {
  241. //pr_code_KeyDown(sender, new KeyEventArgs(Keys.Enter));
  242. }
  243. private void printButton_Click(object sender, EventArgs e)
  244. {
  245. if (canPrint)
  246. {
  247. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'").ToString();
  248. FileInfo PrintFile = new FileInfo(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString());
  249. if (PrintFile == null)
  250. {
  251. MessageBox.Show("打印文件不存在");
  252. return;
  253. }
  254. if (PrintFile.LastWriteTime.ToString() != filelastwritetime)
  255. {
  256. lbl.Quit();
  257. lbl = new ApplicationClass();
  258. BaseUtil.WriteLbl(lbl);
  259. dh.UpdateByCondition("label", "la_lastwritetime = to_date('" + PrintFile.LastWriteTime.ToString() + "', 'yyyy-mm-dd hh24:mi:ss') ", "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'");
  260. }
  261. doc = lbl.Documents.Open(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString());
  262. sb = new StringBuilder();
  263. //将值赋到doc对应的变量
  264. for (int i = 0; i < doc.Variables.FormVariables.Count; i++)
  265. {
  266. for (int j = 0; j < paramsInfo.Rows.Count; j++)
  267. {
  268. //找到对应的变量名
  269. if (doc.Variables.FormVariables.Item(i + 1).Name == paramsInfo.Rows[j]["lp_name"].ToString())
  270. {
  271. //直接赋值
  272. doc.Variables.FormVariables.Item(i + 1).Value = inputValues[j];
  273. }
  274. }
  275. }
  276. //打印
  277. LogManager.DoLog(sb.ToString());
  278. //保存本次赋值进行打印
  279. doc.Printer.SwitchTo(Printer.Text);
  280. doc.PrintDocument(int.Parse(PrintNum.Text));
  281. for (int i = 0; i < doc.Variables.FormVariables.Count; i++)
  282. {
  283. doc.Variables.FormVariables.Item(i + 1).Value = null;
  284. }
  285. //讲文件最后写入时间改成数据库中拿到的时间
  286. //记录打印日志
  287. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "自定义标打印", "成功打印", "", "");
  288. //打印成功
  289. OperateResult.AppendText("<<打印成功\n", Color.Green);
  290. }
  291. else
  292. {
  293. OperateResult.AppendText("<<标签参数未全部输入完成\n", Color.Red);
  294. }
  295. }
  296. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  297. {
  298. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  299. {
  300. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  301. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  302. }
  303. }
  304. }
  305. }