Make_CartonBoxSNWeigh.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using LabelManager2;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.IO.Ports;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. using UAS_MES.DataOperate;
  13. using UAS_MES.Entity;
  14. using UAS_MES.PublicForm;
  15. using UAS_MES.PublicMethod;
  16. namespace UAS_MES.Make
  17. {
  18. public partial class Make_CartonBoxSNWeigh : Form
  19. {
  20. AutoSizeFormClass asc = new AutoSizeFormClass();
  21. DataHelper dh;
  22. DataTable dt;
  23. LogStringBuilder sql = new LogStringBuilder();
  24. ApplicationClass lbl;
  25. string outboxcode = "";
  26. Document doc;
  27. Thread thread;
  28. ModeBusTCPServer md;
  29. string PR_CHECKCARTONW = "0";
  30. //创建串口实例
  31. SerialPort serialPort1 = new SerialPort();
  32. //true的时候表示从串口读取数据
  33. bool GetData = true;
  34. //箱内总数
  35. int pa_totalqty;
  36. //称量的标准重量
  37. Double Weight;
  38. //最大重量
  39. Double MaxWeight;
  40. //最小重量
  41. Double MinWeight;
  42. DataTable ListC = new DataTable();
  43. Thread InitPrint;
  44. public Make_CartonBoxSNWeigh()
  45. {
  46. InitializeComponent();
  47. }
  48. protected override void OnVisibleChanged(EventArgs e)
  49. {
  50. base.OnVisibleChanged(e);
  51. if (!IsHandleCreated)
  52. {
  53. this.Close();
  54. }
  55. }
  56. private void 卡通箱称重_Load(object sender, EventArgs e)
  57. {
  58. CheckForIllegalCrossThreadCalls = false;
  59. asc.controllInitializeSize(this);
  60. ComList.Text = BaseUtil.GetCacheData("PortName").ToString();
  61. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  62. IP.Text = BaseUtil.GetCacheData("IP").ToString();
  63. Port.Text = BaseUtil.GetCacheData("IPPort").ToString();
  64. InitPrint = new Thread(InPrint);
  65. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  66. BaseUtil.SetFormCenter(stw);
  67. stw.ShowDialog();
  68. StartWeight.PerformClick();
  69. md = new ModeBusTCPServer();
  70. dh = SystemInf.dh;
  71. StepCount.StepCode = User.CurrentStepCode;
  72. StepCount.Source = User.UserSourceCode;
  73. StepCount.LineCode = User.UserLineCode;
  74. StepCount.Dh = dh;
  75. StepCount.Start();
  76. }
  77. private void InPrint()
  78. {
  79. try
  80. {
  81. lbl = new ApplicationClass();
  82. BaseUtil.WriteLbl(lbl);
  83. }
  84. catch (Exception)
  85. {
  86. OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red);
  87. }
  88. }
  89. private void 卡通箱称重_SizeChanged(object sender, EventArgs e)
  90. {
  91. weight.Location = new Point(weight_label.Location.X + weight_label.Size.Width - 10, weight_label.Location.Y - 20);
  92. asc.controlAutoSize(this);
  93. }
  94. private void sncode_KeyDown(object sender, KeyEventArgs e)
  95. {
  96. if (e.KeyCode == Keys.Enter)
  97. {
  98. sql.Clear();
  99. sql.Append("select max(ms_id) from makeserial where ms_sncode in (select '" + sncode.Text + "' from dual ");
  100. sql.Append("union select sn from makesnrelation where beforesn='" + sncode.Text + "' and sn<>' ' union select ");
  101. sql.Append("beforesn from makesnrelation where sn='" + sncode.Text + "' and beforesn<>' ') and ms_status in(1,2,0)");
  102. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  103. string MSID = dt.Rows[0][0].ToString();
  104. if (MSID == "")
  105. {
  106. OperateResult.AppendText(">>序列号" + sncode.Text + "不存在\n", Color.Red, sncode);
  107. return;
  108. }
  109. outboxcode = dh.getFieldDataByCondition("makeserial", "ms_outboxcode", "ms_id='" + MSID + "'").ToString();
  110. if (outboxcode == "")
  111. {
  112. OperateResult.AppendText(">>序列号" + sncode.Text + "尚未装箱\n", Color.Red, sncode);
  113. return;
  114. }
  115. sql.Clear();
  116. sql.Append("select pa_totalqty,ma_code,ma_qty,ma_salecode,PA_STANDARDQTY,PA_CURRENTQTY,pr_detail,pr_cartonunit,pr_code,pr_cartongw,");
  117. sql.Append("pr_cartonmaxw,pr_cartonminw,nvl(PR_CHECKCARTONW,'0') PR_CHECKCARTONW,nvl(pr_sendchecktype,'LineCode')pr_sendchecktype from package left join packagedetail ");
  118. sql.Append("on pa_id=pd_paid left join makeserial on ms_sncode=pd_barcode and ms_makecode=pa_makecode left join product on pr_code=pd_prodcode left join make on ma_code=pd_makecode ");
  119. sql.Append(" where pa_outboxcode='" + outboxcode + "' and pa_nextstep='" + User.CurrentStepCode + "'");
  120. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  121. //填充打印文件选项的DataGridView
  122. if (dt.Rows.Count > 0)
  123. {
  124. BaseUtil.SetFormValue(this.Controls, dt);
  125. string ErrorMessage;
  126. //重量的临时变量
  127. PR_CHECKCARTONW = dt.Rows[0]["PR_CHECKCARTONW"].ToString();
  128. string _weight = dt.Rows[0]["pr_cartongw"].ToString();
  129. string _maxweight = dt.Rows[0]["pr_cartonmaxw"].ToString();
  130. string _minweight = dt.Rows[0]["pr_cartonminw"].ToString();
  131. string pa_pr_cartonunit = dt.Rows[0]["pr_cartonunit"].ToString();
  132. string PA_STANDARDQTY = dt.Rows[0]["PA_STANDARDQTY"].ToString();
  133. string PA_CURRENTQTY = dt.Rows[0]["PA_CURRENTQTY"].ToString();
  134. Weight = double.Parse(_weight == "" ? "0" : _weight);
  135. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  136. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  137. pa_totalqty = int.Parse(dt.Rows[0]["pa_totalqty"].ToString());
  138. if (Weight - MinWeight == MaxWeight - Weight)
  139. pr_cartongw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_cartonunit"].ToString();
  140. else
  141. pr_cartongw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_cartonunit"].ToString();
  142. double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim());
  143. //需要检查称重重量
  144. if (PR_CHECKCARTONW != "0" && PA_STANDARDQTY == PA_CURRENTQTY)
  145. {
  146. //称量合格或不合格都记录重量
  147. if (ActualWeight >= MinWeight && ActualWeight <= MaxWeight && ActualWeight != 0)
  148. {
  149. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "卡通箱称重", "称量合格:" + weight.Text.Trim(), outboxcode, "");
  150. LogicHandler.RecordProdWeight(outboxcode, "CARTON", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
  151. OperateResult.AppendText(">>箱号" + outboxcode + "检测合格\n", Color.Green);
  152. }
  153. else
  154. {
  155. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "卡通箱称重", "称量不合格:" + weight.Text.Trim(), outboxcode, "");
  156. LogicHandler.RecordProdWeight(outboxcode, "CARTON", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
  157. OperateResult.AppendText(">>箱号" + outboxcode + "检测未通过\n", Color.Red, sncode);
  158. return;
  159. }
  160. }
  161. else
  162. {
  163. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "卡通箱称重", "称量完成:" + weight.Text.Trim(), outboxcode, "");
  164. LogicHandler.RecordProdWeight(outboxcode, "CARTON", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
  165. }
  166. if (AutoPrint.Checked)
  167. {
  168. if (PrintLabel.Items.Count > 0)
  169. {
  170. Confirm.PerformClick();
  171. }
  172. else
  173. {
  174. OperateResult.AppendText(">>产品" + pr_code.Text + "未维护卡通标签\n", Color.Red);
  175. }
  176. }
  177. OperateResult.AppendText(">>箱号" + outboxcode + "称重完成\n", Color.Green);
  178. if (LogicHandler.CartonBoxStepPass(ma_code.Text, User.UserSourceCode, outboxcode, User.UserCode, "卡通箱:" + outboxcode + "整箱过站", out ErrorMessage))
  179. {
  180. dh.UpdateByCondition("package", "pa_printcount= nvl(pa_printcount,0)+1,pa_weight='" + ActualWeight + "',pa_status=1,pa_packageqty=pa_currentqty,pa_totalqty=pa_currentqty", "pa_outboxcode='" + outboxcode + "'");
  181. dh.UpdateByCondition("package", "pa_nextstep='" + dh.getFieldDataByCondition("packagedetail left join makeserial on ms_sncode=pd_barcode and ms_makecode=pd_makecode", "max(ms_nextstepcode)", "pd_outboxcode='" + outboxcode + "'").ToString() + "' ,PA_CURRENTSTEP = '' ", "pa_outboxcode='" + outboxcode + "'");
  182. }
  183. else
  184. {
  185. OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red);
  186. }
  187. LoadCheckQTY();
  188. sncode.Clear();
  189. }
  190. else OperateResult.AppendText(">>箱号" + outboxcode + "当前执行工序不是" + User.CurrentStepCode + "\n", Color.Red, sncode);
  191. }
  192. }
  193. private void getSerialData()
  194. {
  195. if (serialPort1.IsOpen)
  196. {
  197. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  198. {
  199. SystemInf.OpenPort.Add(serialPort1.PortName);
  200. try
  201. {
  202. while (GetData)
  203. {
  204. try
  205. {
  206. weight.Text = serialPort1.ReadLine();
  207. }
  208. catch (Exception)
  209. {
  210. GetData = false;
  211. }
  212. }
  213. }
  214. catch (IOException ex) { OperateResult.AppendText(">>" + ex.Message + "\n", Color.Red, sncode); }
  215. }
  216. else OperateResult.AppendText(">>端口已被占用,请关闭其他窗口\n", Color.Red, sncode);
  217. }
  218. }
  219. //确认打印
  220. private void Confirm_Click(object sender, EventArgs e)
  221. {
  222. doc = lbl.Documents.Open(PrintLabel.Text);
  223. string ErrorMessage = "";
  224. if (!Print.CodeSoft(Tag.ToString(), doc, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), PrinterList.Text, outboxcode, int.Parse(PrintNum.Text), ma_code.Text, pr_code.Text, "卡通箱标", "0", out ErrorMessage))
  225. {
  226. OperateResult.AppendText(ErrorMessage + "\n", Color.Red);
  227. }
  228. }
  229. //停止进程,关闭串口
  230. private void StopWeight_Click(object sender, EventArgs e)
  231. {
  232. if (serialPort1.IsOpen)
  233. {
  234. GetData = false;
  235. serialPort1.Close();
  236. SystemInf.OpenPort.Remove(serialPort1.PortName);
  237. thread.Abort();
  238. }
  239. }
  240. //关闭窗口的时候停止进程,不再读取串口数据
  241. private void 卡通箱称重_FormClosing(object sender, FormClosingEventArgs e)
  242. {
  243. BaseUtil.ClosePrint(lbl);
  244. StopWeight.PerformClick();
  245. InitPrint.Abort();
  246. if (serialPort1.IsOpen)
  247. {
  248. GetData = false;
  249. serialPort1.Close();
  250. SystemInf.OpenPort.Remove(serialPort1.PortName);
  251. thread.Interrupt();
  252. }
  253. //thread.Abort();
  254. }
  255. private void StartWeight_Click(object sender, EventArgs e)
  256. {
  257. thread = new Thread(getSerialData);
  258. try
  259. {
  260. GetData = true;
  261. serialPort1.PortName = this.ComList.Text;
  262. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  263. serialPort1.Open();
  264. thread.Start();
  265. }
  266. catch (Exception mes)
  267. {
  268. if (BaudRate.Text == "" || ComList.Text == "")
  269. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  270. else
  271. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  272. }
  273. }
  274. private void pr_code_TextChanged(object sender, EventArgs e)
  275. {
  276. dt = (DataTable)dh.ExecuteSql("select la_id,la_url,la_isdefault from label where la_prodcode='" + pr_code.Text + "' and la_templatetype='卡通箱标' and la_statuscode='AUDITED' order by la_isdefault", "select");
  277. PrintLabel.DataSource = dt;
  278. PrintLabel.DisplayMember = "la_url";
  279. PrintLabel.ValueMember = "la_id";
  280. }
  281. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  282. {
  283. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  284. {
  285. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  286. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  287. }
  288. }
  289. private void SendCheck_Click(object sender, EventArgs e)
  290. {
  291. DataTable dt = (DataTable)dh.ExecuteSql("select wm_concat(pa_outboxcode) pa_outboxcode from package where pa_checkno='" + ob_checkno.Text + "' and pa_status=0 ", "select");
  292. if (dt.Rows[0][0].ToString() != "")
  293. {
  294. OperateResult.AppendText(">>批次" + ob_checkno.Text + "存在箱号" + dt.Rows[0][0].ToString() + "未封箱,请先进行封箱\n", Color.Red);
  295. return;
  296. }
  297. sql.Clear();
  298. sql.Append("update oqcbatch set ob_status='UNCHECK' where ob_checkno ='" + ob_checkno.Text + "'");
  299. dh.ExecuteSql(sql.GetString(), "select");
  300. ob_nowcheckqty.Text = "";
  301. ob_batchqty.Text = "";
  302. ob_nowcheckqty.ForeColor = Color.Black;
  303. SendCheck.Enabled = false;
  304. OperateResult.AppendText(">>批次" + ob_checkno.Text + "送检成功\n", Color.Green);
  305. LogicHandler.InsertMakeProcess("", ma_code.Text, User.UserSourceCode, "手动送检", "手动送检成功", User.UserCode);
  306. //记录操作日志
  307. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "手动送检", "手动送检成功", "", ob_checkno.Text);
  308. ob_checkno.Text = "";
  309. }
  310. bool AutoCut;
  311. private void LoadCheckQTY()
  312. {
  313. sql.Clear();
  314. string condition = "";
  315. int nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
  316. int batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
  317. if (nowcheckqty + 1 == batchqty && AutoCut)
  318. {
  319. condition = "and ob_status='UNCHECK' and ob_checkno='" + ob_checkno.Text + "'";
  320. }
  321. else
  322. {
  323. condition = "and ob_status='ENTERING' ";
  324. }
  325. if (pr_sendchecktype.Text == "SaleCode")
  326. {
  327. condition += " and ob_salecode='" + ma_salecode.Text + "'";
  328. }
  329. sql.Append("select ob_batchqty,ob_nowcheckqty,ob_checkno from oqcbatch where ");
  330. sql.Append("ob_linecode='" + User.UserLineCode + "' and ob_prodcode='" + pr_code.Text + "' and ");
  331. sql.Append("ob_stepcode='" + User.CurrentStepCode + "' " + condition);
  332. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  333. if (dt.Rows.Count > 0)
  334. {
  335. ob_batchqty.Text = dt.Rows[0]["ob_batchqty"].ToString();
  336. ob_nowcheckqty.Text = dt.Rows[0]["ob_nowcheckqty"].ToString();
  337. ob_checkno.Text = dt.Rows[0]["ob_checkno"].ToString();
  338. nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
  339. batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
  340. if (nowcheckqty >= batchqty)
  341. {
  342. ob_nowcheckqty.ForeColor = Color.Red;
  343. OperateResult.AppendText(">>当前采集数量已达到送检数量\n", Color.Red);
  344. if (AutoCut)
  345. {
  346. OperateResult.AppendText(">>批次" + ob_checkno.Text + "自动断批\n", Color.Green);
  347. }
  348. }
  349. }
  350. else
  351. {
  352. ob_batchqty.Text = "";
  353. ob_nowcheckqty.Text = "";
  354. ob_checkno.Text = "";
  355. SendCheck.Enabled = false;
  356. }
  357. if (ob_batchqty.Text != "")
  358. {
  359. SendCheck.Enabled = true;
  360. }
  361. else
  362. {
  363. SendCheck.Enabled = false;
  364. }
  365. }
  366. private void ob_checkno_TextChanged(object sender, EventArgs e)
  367. {
  368. if (ob_checkno.Text != "")
  369. {
  370. string Cut = dh.getFieldDataByCondition("product left join oqcbatch on ob_prodcode=pr_code", "pr_ifautocutcheckno", "ob_checkno='" + ob_checkno.Text + "'").ToString();
  371. if (Cut == "" || Cut == "0")
  372. AutoCut = false;
  373. else
  374. AutoCut = true;
  375. SendCheck.Enabled = true;
  376. }
  377. }
  378. private void RefreshWeigh_Click(object sender, EventArgs e)
  379. {
  380. sql.Clear();
  381. sql.Append("select pr_cartonmaxw,pr_cartonunit,pr_cartonminw,pr_cartongw from product where pr_code='" + pr_code.Text + "'");
  382. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  383. //填充打印文件选项的DataGridView
  384. if (dt.Rows.Count > 0)
  385. {
  386. string _weight = dt.Rows[0]["pr_cartongw"].ToString();
  387. string _maxweight = dt.Rows[0]["pr_cartonmaxw"].ToString();
  388. string _minweight = dt.Rows[0]["pr_cartonminw"].ToString();
  389. Weight = double.Parse(_weight == "" ? "0" : _weight);
  390. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  391. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  392. if (Weight - MinWeight == MaxWeight - Weight)
  393. pr_cartongw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_cartonunit"].ToString();
  394. else
  395. pr_cartongw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_cartonunit"].ToString();
  396. }
  397. }
  398. private void BuildServer_Click(object sender, EventArgs e)
  399. {
  400. if (!md.IsOpen)
  401. {
  402. md.IP = IP.Text;
  403. md.Port = Port.Text;
  404. if (md.Open())
  405. {
  406. BaseUtil.SetCacheData("IP", IP.Text);
  407. BaseUtil.SetCacheData("IPPort", Port.Text);
  408. OperateResult.AppendText(">>服务开启成功\n");
  409. }
  410. }
  411. else
  412. OperateResult.AppendText(">>服务已经开启\n");
  413. }
  414. private void CloseServer_Click(object sender, EventArgs e)
  415. {
  416. if (md.IsOpen)
  417. {
  418. md.Close();
  419. OperateResult.AppendText(">>服务关闭成功\n");
  420. }
  421. else
  422. OperateResult.AppendText(">>服务尚未开启\n");
  423. }
  424. }
  425. }