Packing_BigBoxWeight.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using DevExpress.Printing.Core.PdfExport.Metafile;
  2. using LabelManager2;
  3. using Seagull.BarTender.Print;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.IO.Ports;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Threading;
  15. using System.Windows.Forms;
  16. using UAS_MES_NEW.DataOperate;
  17. using UAS_MES_NEW.Entity;
  18. using UAS_MES_NEW.PublicForm;
  19. using UAS_MES_NEW.PublicMethod;
  20. using static DevExpress.XtraBars.Painters.PrimitivesPainterWindowsXP;
  21. using static System.Runtime.CompilerServices.RuntimeHelpers;
  22. namespace UAS_MES_NEW.Packing
  23. {
  24. public partial class Packing_BigBoxWeigh : Form
  25. {
  26. AutoSizeFormClass asc = new AutoSizeFormClass();
  27. DataHelper dh;
  28. Document doc;
  29. //true的时候表示从串口读取数据
  30. bool GetData = true;
  31. Engine engine;
  32. DataTable dt;
  33. LogStringBuilder sql = new LogStringBuilder();
  34. ApplicationClass lbl;
  35. Thread thread;
  36. Thread InitPrint;
  37. //打印计数
  38. int printcount;
  39. //创建串口实例
  40. SerialPort serialPort1 = new SerialPort();
  41. //存储当前大箱号
  42. string outboxcode = "";
  43. //记录称重串口号和波特率
  44. string Comlist = "";
  45. string Baurate = "";
  46. public Packing_BigBoxWeigh()
  47. {
  48. InitializeComponent();
  49. }
  50. protected override void OnVisibleChanged(EventArgs e)
  51. {
  52. base.OnVisibleChanged(e);
  53. if (!IsHandleCreated)
  54. {
  55. this.Close();
  56. }
  57. }
  58. private void Make_BigBoxWeight_Load(object sender, EventArgs e)
  59. {
  60. asc.controllInitializeSize(this);
  61. CheckForIllegalCrossThreadCalls = false;
  62. Comlist = BaseUtil.GetCacheData("PortName").ToString();
  63. Baurate = BaseUtil.GetCacheData("BaudRate").ToString();
  64. InitPrint = new Thread(InPrint);
  65. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  66. BaseUtil.SetFormCenter(stw);
  67. stw.ShowDialog();
  68. pa_outboxcode.Focus();
  69. OperateResult.AppendText(">>请输入大箱号\n");
  70. printcount = 0;
  71. weightsum.Text = printcount + "";
  72. StartWeight.PerformClick();
  73. dh = SystemInf.dh;
  74. }
  75. private void InPrint()
  76. {
  77. try
  78. {
  79. engine = new Engine(true);
  80. }
  81. catch
  82. {
  83. OperateResult.AppendText("未正确安装BarTender软件\n", Color.Red);
  84. }
  85. }
  86. private void Make_BigBoxWeight_SizeChanged(object sender, EventArgs e)
  87. {
  88. asc.controlAutoSize(this);
  89. }
  90. private void pa_outboxcode_KeyDown(object sender, KeyEventArgs e)
  91. {
  92. if (e.KeyCode == Keys.Enter)
  93. {
  94. sql.Clear();
  95. sql.Append("select pa_weight,pr_bigboxmaxw,pr_bigboxminw,PA_STANDARDQTY,PA_CURRENTQTY,pr_colorboxgw,pa_id,pa_prodcode,pr_spec,pr_detail,pa_makecode,pa_salecode,pa_totalqty,");
  96. sql.Append("pa_packageqty from package left join product on pr_code=pa_prodcode where ");
  97. sql.Append("pa_outboxcode='" + pa_outboxcode.Text + "' and pa_type=2");
  98. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  99. outboxcode = pa_outboxcode.Text;
  100. weightsum.Text = printcount + "";
  101. if (dt.Rows.Count > 0)
  102. {
  103. BaseUtil.SetFormValue(this.Controls, dt);
  104. pa_outboxcode.Text = outboxcode;
  105. }
  106. else
  107. {
  108. OperateResult.AppendText(">>箱号不存在\n", Color.Red);
  109. return;
  110. }
  111. double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim());
  112. if (ActualWeight > 0)
  113. {
  114. string _weight = dt.Rows[0]["pr_colorboxgw"].ToString();
  115. string _maxweight = dt.Rows[0]["pr_bigboxmaxw"].ToString();
  116. string _minweight = dt.Rows[0]["pr_bigboxminw"].ToString();
  117. string pa_weight = dt.Rows[0]["pa_weight"].ToString();
  118. string PA_STANDARDQTY = dt.Rows[0]["PA_STANDARDQTY"].ToString();
  119. string PA_CURRENTQTY = dt.Rows[0]["PA_CURRENTQTY"].ToString();
  120. if (pa_weight != "")
  121. {
  122. OperateResult.AppendText(">>箱号" + pa_outboxcode.Text + "已经称重\n", Color.Red);
  123. return;
  124. }
  125. //赋值重量单位
  126. Weight = double.Parse(_weight == "" ? "0" : _weight);
  127. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  128. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  129. if (Weight - MinWeight == MaxWeight - Weight)
  130. pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight);
  131. else
  132. pr_colorboxgw.Text = MinWeight + "-" + MaxWeight;
  133. if (PA_STANDARDQTY == PA_CURRENTQTY)
  134. {
  135. if ((ActualWeight >= MinWeight) && (ActualWeight <= MaxWeight))
  136. {
  137. OperateResult.AppendText(">>大箱" + pa_outboxcode.Text + "重量检测检测合格\n", Color.Green);
  138. }
  139. else
  140. {
  141. OperateResult.AppendText(">>大箱" + pa_outboxcode.Text + "重量检测未通过\n", Color.Red);
  142. return;
  143. }
  144. }
  145. //更新大箱重量
  146. sql.Clear();
  147. sql.Append("update package set pa_weight='" + ActualWeight + "' where pa_outboxcode= '" + pa_outboxcode.Text + "'");
  148. dh.ExecuteSql(sql.GetString(), "update");
  149. OperateResult.AppendText("<<<箱号:" + pa_outboxcode.Text + ",重量:" + weight.Text + "\n", Color.Green);
  150. LogicHandler.InsertMakeProcess(pa_outboxcode.Text, pa_makecode.Text, User.UserSourceCode, "大箱称量", "大箱称量合格", User.UserCode);
  151. OperateResult.AppendText("<<<更新成功\n", Color.Green);
  152. //刷新grid的历史称重信息;
  153. recordResult(pa_outboxcode.Text, weight.Text, System.DateTime.Now.ToString());
  154. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, pa_makecode.Text, User.UserLineCode, User.UserSourceCode, "大箱" + pa_outboxcode.Text + "称重:" + weight.Text, "称重成功", pa_outboxcode.Text, "");
  155. LogicHandler.RecordProdWeight(pa_outboxcode.Text, "BIGBOX", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pa_prodcode.Text, User.UserSourceCode, User.UserName);
  156. if (autoprint.Checked == true)
  157. {
  158. try
  159. {
  160. //doc = lbl.Documents.Open(ftpOperater.DownLoadTo + PrintLabel.Text);
  161. string ErrorMessage = "";
  162. if (!Print.BarTender(Tag.ToString(), ref engine, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, pa_outboxcode.Text, int.Parse(PrintNum.Text), pa_makecode.Text, pa_prodcode.Text, "大箱标", "0", out ErrorMessage))
  163. {
  164. OperateResult.AppendText(ErrorMessage + "\n", Color.Red);
  165. }
  166. dh.ExecuteSql("update package set pa_printcount= pa_printcount+1 where pa_outboxcode='" + pa_outboxcode.Text + "'", "update");
  167. printcount++;
  168. weightsum.Text = printcount + "";
  169. //清空输入框中的值,扫描框置空定焦
  170. pa_outboxcode.Text = "";
  171. pa_outboxcode.Focus();
  172. }
  173. catch (Exception eb)
  174. {
  175. if (PrintLabel.SelectedValue == null)
  176. {
  177. OperateResult.AppendText(">>产品:" + pa_prodcode.Text + "未维护大箱标签模板\n", Color.Red, pa_outboxcode);
  178. pa_outboxcode.Focus();
  179. }
  180. else OperateResult.AppendText("<<<打印失败:" + eb.Message + "\n", Color.Red);
  181. }
  182. }
  183. }
  184. else
  185. {
  186. OperateResult.AppendText("<<<大箱重量应大于0\n", Color.Red);
  187. }
  188. }
  189. }
  190. private void recordResult(string pa_outboxcode, string weight, string dateTime)
  191. {
  192. //创建一个item
  193. ListViewItem lvi = new ListViewItem();
  194. //分条赋值
  195. lvi.SubItems.Add(pa_outboxcode);
  196. lvi.SubItems.Add(weight);
  197. lvi.SubItems.Add(dateTime);
  198. //添加结果的信息进去
  199. showResult.Items.Add(lvi);
  200. }
  201. private void pa_prodcode_TextChanged(object sender, EventArgs e)
  202. {
  203. DataTable _dt = (DataTable)dh.ExecuteSql("select la_id,la_url,la_isdefault from label where la_prodcode='" + pa_prodcode.Text + "' and la_templatetype='大箱标' and la_statuscode='AUDITED' order by la_isdefault", "select");
  204. PrintLabel.DataSource = _dt;
  205. PrintLabel.DisplayMember = "la_url";
  206. PrintLabel.ValueMember = "la_id";
  207. }
  208. private void StartWeight_Click(object sender, EventArgs e)
  209. {
  210. thread = new Thread(getSerialData);
  211. try
  212. {
  213. GetData = true;
  214. serialPort1.PortName = Comlist;
  215. serialPort1.BaudRate = int.Parse(Baurate);
  216. serialPort1.Open();
  217. thread.Start();
  218. }
  219. catch (Exception mes)
  220. {
  221. if (Baurate == "" || Comlist == "")
  222. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  223. else
  224. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  225. }
  226. }
  227. Regex re = new Regex("\\d+.\\w+");
  228. private void getSerialData()
  229. {
  230. if (serialPort1.IsOpen)
  231. {
  232. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  233. {
  234. SystemInf.OpenPort.Add(serialPort1.PortName);
  235. try
  236. {
  237. while (GetData)
  238. {
  239. try
  240. {
  241. weight.Text = re.Match(serialPort1.ReadLine().Trim()).Groups[0].Value;
  242. }
  243. catch (Exception)
  244. {
  245. GetData = false;
  246. }
  247. }
  248. }
  249. catch (IOException ex) { MessageBox.Show(ex.Message); }
  250. }
  251. else
  252. MessageBox.Show("端口已被占用,请关闭其他窗口");
  253. }
  254. }
  255. private void StopWeight_Click(object sender, EventArgs e)
  256. {
  257. if (serialPort1.IsOpen)
  258. {
  259. GetData = false;
  260. serialPort1.Close();
  261. SystemInf.OpenPort.Remove(serialPort1.PortName);
  262. thread.Abort();
  263. }
  264. }
  265. private void Make_BigBoxWeight_FormClosing(object sender, FormClosingEventArgs e)
  266. {
  267. BaseUtil.ClosePrint(lbl);
  268. StopWeight.PerformClick();
  269. InitPrint.Abort();
  270. if (serialPort1.IsOpen)
  271. {
  272. GetData = false;
  273. serialPort1.Close();
  274. SystemInf.OpenPort.Remove(serialPort1.PortName);
  275. thread.Interrupt();
  276. }
  277. //thread.Abort();
  278. }
  279. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  280. {
  281. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  282. {
  283. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  284. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  285. }
  286. }
  287. Double Weight;
  288. //最大重量
  289. Double MaxWeight;
  290. //最小重量
  291. Double MinWeight;
  292. private void RefreshWeigh_Click(object sender, EventArgs e)
  293. {
  294. sql.Clear();
  295. sql.Append("select pr_colorboxgw,pr_bigboxmaxw,pr_bigboxminw,pr_colorboxunit from product where pr_code='" + pa_prodcode.Text + "'");
  296. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  297. if (dt.Rows.Count > 0)
  298. {
  299. //重量的临时变量
  300. string _weight = dt.Rows[0]["pr_colorboxgw"].ToString();
  301. string _maxweight = dt.Rows[0]["pr_bigboxmaxw"].ToString();
  302. string _minweight = dt.Rows[0]["pr_bigboxminw"].ToString();
  303. //赋值重量单位
  304. Weight = double.Parse(_weight == "" ? "0" : _weight);
  305. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  306. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  307. if (Weight - MinWeight == MaxWeight - Weight)
  308. pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_colorboxunit"].ToString();
  309. else
  310. pr_colorboxgw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_colorboxunit"].ToString();
  311. }
  312. }
  313. private void Packing_Click(object sender, EventArgs e)
  314. {
  315. if (pa_outboxcode.Text != "")
  316. {
  317. sql.Clear();
  318. sql.Append("select nvl(pa_status,0) pa_status from packagedetail left join package ");
  319. sql.Append("on pa_id=pd_paid where pd_outboxcode='" + pa_outboxcode.Text + "'");
  320. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  321. if (dt.Rows.Count > 0)
  322. {
  323. if (dt.Rows[0]["pa_status"].ToString() == "0")
  324. {
  325. string Seal = MessageBox.Show(this.ParentForm, "是否确认封箱?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  326. if (Seal == "Yes")
  327. {
  328. OperateResult.AppendText(">>大箱号" + pa_outboxcode.Text + "封大箱成功\n", Color.Green);
  329. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, pa_makecode.Text, User.UserLineCode, User.UserSourceCode, "大箱采集", "大箱封箱" + pa_outboxcode.Text + "成功", pa_outboxcode.Text, "");
  330. dh.ExecuteSql("update package set pa_status=1 where pa_outboxcode='" + pa_outboxcode.Text + "'", "update");
  331. }
  332. }
  333. else OperateResult.AppendText(">>大箱号" + pa_outboxcode.Text + "已封大箱\n", Color.Red);
  334. }
  335. else OperateResult.AppendText(">>大箱号" + pa_outboxcode.Text + "未采集内容\n", Color.Red);
  336. }
  337. else OperateResult.AppendText(">>大箱号不能为空\n", Color.Red);
  338. }
  339. }
  340. }