Make_CustomLabelPrint.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_NEW.DataOperate;
  15. using UAS_MES_NEW.Entity;
  16. using UAS_MES_NEW.PublicForm;
  17. using UAS_MES_NEW.PublicMethod;
  18. namespace UAS_MES_NEW.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. string filechangetime = PrintFile.LastWriteTime.ToString();
  142. if (filechangetime != filelastwritetime)
  143. {
  144. lbl.Quit();
  145. lbl = new ApplicationClass();
  146. BaseUtil.WriteLbl();
  147. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  148. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  149. dh.UpdateByCondition("label", update, "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'");
  150. }
  151. doc = lbl.Documents.Open(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString(), true);
  152. }
  153. //将值赋到doc对应的变量
  154. for (int i = 0; i < doc.Variables.FormVariables.Count; i++)
  155. {
  156. //找到对应的变量名
  157. if (doc.Variables.FormVariables.Item(i + 1).Name == paramsInfo.Rows[currentIndex - 1]["lp_name"].ToString())
  158. {
  159. //如果当时采集行的 lp_valuetype=’INPUT’,并且lp_sql 为空,则直接将采集的值赋给标签打印
  160. if (paramsInfo.Rows[currentIndex - 1]["lp_valuetype"].ToString() == "INPUT" && paramsInfo.Rows[currentIndex - 1]["lp_sql"].ToString() == "")
  161. {
  162. doc.Variables.FormVariables.Item(i + 1).Value = inputText.Text;
  163. }
  164. else
  165. {
  166. //取sql拿值赋给变量
  167. string sql = paramsInfo.Rows[currentIndex - 1]["lp_sql"].ToString();
  168. Regex ConnoteA = new Regex("{\\w+}");
  169. foreach (Match mch in ConnoteA.Matches(sql))
  170. {
  171. string x = mch.Value.Trim();
  172. sql = sql.Replace(x, "'" + inputText.Text + "'");
  173. }
  174. doc.Variables.FormVariables.Item(i + 1).Value = dh.GetLabelParam(sql).ToString();
  175. sb.AppendLine("打印参数【" + doc.Variables.FormVariables.Item(i + 1).Name + "】赋值," + "取值SQL:" + sql + ",取到值" + doc.Variables.FormVariables.Item(i + 1).Value);
  176. }
  177. inputValues[currentIndex - 1] = doc.Variables.FormVariables.Item(i + 1).Value;
  178. }
  179. }
  180. if (currentIndex == paramsInfo.Rows.Count)
  181. {
  182. OperateResult.AppendText("<<输入完成,正在打印\n", Color.Black, inputText);
  183. canPrint = true;
  184. //打印
  185. LogManager.DoLog(sb.ToString());
  186. //保存本次赋值进行打印
  187. doc.Printer.SwitchTo(Printer.Text);
  188. doc.PrintDocument(int.Parse(PrintNum.Text));
  189. for (int i = 0; i < doc.Variables.FormVariables.Count; i++)
  190. {
  191. doc.Variables.FormVariables.Item(i + 1).Value = null;
  192. }
  193. //讲文件最后写入时间改成数据库中拿到的时间
  194. //记录打印日志
  195. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "自定义标打印", "成功打印", "", "");
  196. //打印成功
  197. OperateResult.AppendText("<<打印成功\n", Color.Green);
  198. //重置currenIndex为1
  199. currentIndex = 1;
  200. OperateResult.AppendText(">>请输入自定义模板的" + paramsInfo.Rows[0]["lp_name"].ToString() + "所需参数\n", Color.Black);
  201. }
  202. else
  203. {
  204. canPrint = false;
  205. currentIndex++;
  206. OperateResult.AppendText("<<请输入自定义模板的" + paramsInfo.Rows[currentIndex - 1]["lp_name"].ToString() + "所需参数\n", Color.Green, inputText);
  207. }
  208. }
  209. }
  210. }
  211. private void PrintLabel_TextChanged(object sender, EventArgs e)
  212. {
  213. if (PrintLabel.Text.StartsWith("System"))
  214. {
  215. return;
  216. }
  217. 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");
  218. if (paramsInfo.Rows.Count > 0)
  219. {
  220. //根据查询出来的参数,一次提示用户, “<<SN001 “,
  221. //“>> 请输入自定义模板的(XXXX参数名称)lp_name”
  222. OperateResult.AppendText("<<" + listA.Rows[PrintLabel.SelectedIndex]["la_code"].ToString() + "\n", Color.Black);
  223. OperateResult.AppendText(">>请输入自定义模板" + paramsInfo.Rows[0]["lp_name"].ToString() + "所需参数\n", Color.Green);
  224. //打开模板路径
  225. currentIndex = 1;
  226. //聚焦输入框
  227. inputText.Focus();
  228. }
  229. else
  230. {
  231. OperateResult.AppendText("<<标签编号:" + listA.Rows[0]["la_code"].ToString() + "无标签参数\n", Color.Red);
  232. }
  233. }
  234. private void Make_CustomLabelPrint_SizeChanged(object sender, EventArgs e)
  235. {
  236. asc.controlAutoSize(this);
  237. }
  238. private void Make_CustomLabelPrint_FormClosing(object sender, FormClosingEventArgs e)
  239. {
  240. BaseUtil.ClosePrint(lbl);
  241. }
  242. private void pr_code_Leave(object sender, EventArgs e)
  243. {
  244. //pr_code_KeyDown(sender, new KeyEventArgs(Keys.Enter));
  245. }
  246. private void printButton_Click(object sender, EventArgs e)
  247. {
  248. if (canPrint)
  249. {
  250. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'").ToString();
  251. FileInfo PrintFile = new FileInfo(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString());
  252. if (PrintFile == null)
  253. {
  254. MessageBox.Show("打印文件不存在");
  255. return;
  256. }
  257. string filechangetime = PrintFile.LastWriteTime.ToString();
  258. if (filechangetime != filelastwritetime)
  259. {
  260. lbl.Quit();
  261. lbl = new ApplicationClass();
  262. BaseUtil.WriteLbl();
  263. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  264. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  265. dh.UpdateByCondition("label", update, "la_id = '" + listA.Rows[PrintLabel.SelectedIndex]["la_id"].ToString() + "'");
  266. }
  267. doc = lbl.Documents.Open(listA.Rows[PrintLabel.SelectedIndex]["la_url"].ToString(), true);
  268. sb = new StringBuilder();
  269. //将值赋到doc对应的变量
  270. for (int i = 0; i < doc.Variables.FormVariables.Count; i++)
  271. {
  272. for (int j = 0; j < paramsInfo.Rows.Count; j++)
  273. {
  274. //找到对应的变量名
  275. if (doc.Variables.FormVariables.Item(i + 1).Name == paramsInfo.Rows[j]["lp_name"].ToString())
  276. {
  277. //直接赋值
  278. doc.Variables.FormVariables.Item(i + 1).Value = inputValues[j];
  279. }
  280. }
  281. }
  282. //打印
  283. LogManager.DoLog(sb.ToString());
  284. //保存本次赋值进行打印
  285. doc.Printer.SwitchTo(Printer.Text);
  286. doc.PrintDocument(int.Parse(PrintNum.Text));
  287. for (int i = 0; i < doc.Variables.FormVariables.Count; i++)
  288. {
  289. doc.Variables.FormVariables.Item(i + 1).Value = null;
  290. }
  291. //讲文件最后写入时间改成数据库中拿到的时间
  292. //记录打印日志
  293. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "自定义标打印", "成功打印", "", "");
  294. //打印成功
  295. OperateResult.AppendText("<<打印成功\n", Color.Green);
  296. }
  297. else
  298. {
  299. OperateResult.AppendText("<<标签参数未全部输入完成\n", Color.Red);
  300. }
  301. }
  302. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  303. {
  304. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  305. {
  306. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  307. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  308. }
  309. }
  310. }
  311. }