UsrNanRui.cs 20.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Interface;

namespace MES
{
    public partial class User : Asa.Face.BaseCtls, IUserControl
    {
        private string[] _code;
        private WebService web;
        private string[][] jsonData;
        private bool findQty = false;
        private bool findBatch = false;
        private Keys keyQty;     //用于查找qty的关键字
        private Keys keyBatch;   //用于查找batch的关键字
        private Dictionary<string, string> printText;
        private log4net.ILog LOG;

        private const string VENDER = "vender";
        private const string QTY = "qty";
        private const string BATCH = "batch";

        public event PrintEvent Printing;
        public event SerialNoEvent GetSN;

        public User()
        {
            InitializeComponent();
          
            printText = new Dictionary<string, string>();
            keyQty = new Keys();
            keyBatch = new Keys();

            LblVender.Text += string.Format(" ({0})", VENDER);
            LblQty.Text += string.Format(" ({0})", QTY);
            LblBatch.Text += string.Format(" ({0})", BATCH);
        }

        /// <summary>
        /// SuperDOG的特征ID
        /// </summary>
        public int ID => 1;  //固定,不允许改变

        /// <summary>
        /// MES是否连接
        /// </summary>
        public bool IsConn { get; set; } = false;

        /// <summary>
        /// 整个标签是否需要OCR
        /// </summary>
        public bool LabelOCR { get; set; } = false;

        /// <summary>
        /// 是否需要匹配模板
        /// </summary>
        public bool Match { set; get; } = false;




        public void SetLog(string log)
        {
            LOG = log4net.LogManager.GetLogger(log);
        }

        /// <summary>
        /// 获取控件
        /// </summary>
        /// <returns></returns>
        public Control GetControl()
        {
            return this;
        }

        /// <summary>
        /// 获取打印文本
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetPrint()
        {
            return printText;
        }

        /// <summary>
        /// MES连接
        /// </summary>
        /// <returns></returns>
        public bool Connect()
        {
            try
            {
                web = new WebService();
                string path = Application.StartupPath + "\\Plugin\\mes.csv";
                if (System.IO.File.Exists(path))
                {
                    string[] lines = System.IO.File.ReadAllLines(path);
                    for (int i = 0; i < lines.Length; i++)
                    {
                        string[] s = lines[i].Split(',');
                        if (s.Length != 2) continue;
                        switch (s[0])
                        {
                            case "MES_IP": web.IP = s[1]; break;
                            case "Qty_Start": keyQty.Starts = s[1].Split(';'); break;
                            case "Qty_End": keyQty.Ends = s[1].Split(';'); break;
                            case "Batch_Start": keyBatch.Starts = s[1].Split(';'); break;
                            case "Batch_End": keyBatch.Ends = s[1].Split(';'); break;
                        }
                    }
                }

                IsConn = web.Ping();
                LOG.Info(web.Log);

                if (IsConn)
                {
                    BtnConn.Text = "MES已连接";
                    BtnConn.StateColor = Color.Lime;
                }
                else
                {
                    BtnConn.Text = "MES未连接";
                    BtnConn.StateColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                IsConn = false;
                LOG.Error(ex.Message);
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return IsConn;
        }

        /// <summary>
        /// 设置条形码内容
        /// </summary>
        /// <param name="code"></param>
        public void SetCode(string[] code)
        {
            _code = code;
            if (code == null) return;
            CboCode.Text = "";
            CboCode.ItemClear();
            CboCode.ItemAdd(_code);
            TxtCode.Text = "";
            CboQty.ItemClear();
            CboBatch.ItemClear();
            findQty = false;
            findBatch = false;
            bool findCode = false;

            try
            {
                for (int i = 0; i < _code.Length; i++)
                {
                    FindKeys(_code[i]);
                    if (IsConn && !findCode)
                    {
                        bool rtn = web.UploadVender(_code[i], out jsonData);
                        LOG.Info(web.Log);
                        if (rtn)
                        {
                            CboCode.SelectedIndex = i;
                            CboOrder.ItemClear();
                            TxtOrderNum.Text = "";
                            for (int j = 0; j < jsonData.Length; j++)
                                CboOrder.ItemAdd(jsonData[j][0]);
                            if (CboOrder.Count > 0)
                                CboOrder.SelectedIndex = 0;
                            findCode = true;
                            break;
                        }
                        System.Threading.Thread.Sleep(50);
                    }
                }

                if (!findQty)
                {
                    for (int i = 0; i < _code.Length; i++)
                    {
                        if (int.TryParse(_code[i], out int result))
                        {
                            if (result % 100 == 0)
                            {
                                findQty = true;
                                break;
                            }
                        }
                    }
                }

                if (findQty && findBatch)
                    Match = false;
                else
                    Match = true;

                if (!findQty)
                    CboQty.ItemAdd(code);
                if (!findBatch)
                    CboBatch.ItemAdd(code);

                if (IsConn)
                {
                    if (!findCode)
                        Asa.Face.MessageBox.Show("没有匹配到供应商编号");
                }
            }
            catch (Exception ex)
            {
               LOG.Error(ex.Message);
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 设置OCR的文本
        /// </summary>
        /// <param name="text"></param>
        public void SetOcrText(Dictionary<string, string> text)
        {
            if (text == null) return;

            try
            {
                if (text.ContainsKey("qty")) CboQty.Text = text["qty"];
                if (text.ContainsKey("batch")) CboBatch.Text = text["batch"];
            }
            catch (Exception ex)
            {
               LOG.Error(ex.Message);
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 设置OCR的文本
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public bool SetOcrText(string text)
        {
            //if (text == null) return false;
            //if (findQty) return true;
            //Discern(text);
            //return findQty;

            return false;
        }

        /// <summary>
        /// 打印标签
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public bool Print(int time)
        {
            return false;
        }

        /// <summary>
        /// 标签预览
        /// </summary>
        public void Preview()
        {
            
        }

        /// <summary>
        /// 检查打印的二维码,true为找到
        /// </summary>
        /// <returns></returns>
        public bool Check()
        {
            return false;
        }












        private struct Keys
        {
            public string[] Starts;
            public string[] Ends;
        }

        private void FindKeys(string s)
        {
            if (!findQty)
            {
                for (int i = 0; i < keyQty.Starts.Length; i++)
                {
                    string val = keyQty.Starts[i];
                    if (s.StartsWith(val))
                    {
                        if (int.TryParse(val.Substring(val.Length), out int qty))
                        {
                            CboQty.Text = qty.ToString();
                            findQty = true;
                            continue;
                        }
                    }
                }
            }
            if (!findQty)
            {
                for (int i = 0; i < keyQty.Ends.Length; i++)
                {
                    string val = keyQty.Ends[i];
                    if (s.EndsWith(val))
                    {
                        if (int.TryParse(val.Substring(0, s.Length - val.Length), out int qty))
                        {
                            CboQty.Text = qty.ToString();
                            findQty = true;
                            continue;
                        }
                    }
                }
            }

            if (!findBatch)
            {
                for (int i = 0; i < keyBatch.Starts.Length; i++)
                {
                    string val = keyBatch.Starts[i];
                    if (s.StartsWith(val))
                    {
                        CboBatch.Text = val.Substring(val.Length);
                        findBatch = true;
                        continue;
                    }
                }
            }
            if (!findBatch)
            {
                for (int i = 0; i < keyBatch.Ends.Length; i++)
                {
                    string val = keyBatch.Ends[i];
                    if (s.EndsWith(val))
                    {
                        CboBatch.Text = val.Substring(0, s.Length - val.Length);
                        findBatch = true;
                        continue;
                    }
                }
            }
        }

        private void BtnVender_Click(object sender)
        {
            if (CboCode.Text.Length == 0) return;
            if (!IsConn) return;
            try
            {
                CboOrder.ItemClear();
                TxtOrderNum.Text = "";
                bool rtn = web.UploadVender(CboCode.Text, out jsonData);
                LOG.Info(web.Log);

                if (rtn)
                {
                    for (int i = 0; i < jsonData.Length; i++)
                        CboOrder.ItemAdd(jsonData[i][0]);
                    if (CboOrder.Count > 0)
                        CboOrder.SelectedIndex = 0;
                }
                else
                {
                    MessageBox.Show(web.ErrInfo, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
   
            catch (Exception ex)
            {
                LOG.Error(ex.Message);
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BtnBind_Click(object sender)
        {
            if (CboCode.Text == "" || TxtCode.Text == "") return;
            if (!IsConn) return;

            bool rtn = web.UploadMaterial(CboCode.Text, TxtCode.Text);
            LOG.Info(web.Log);
            if (rtn)
                Asa.Face.MessageBox.Show("绑定成功");
            else
                MessageBox.Show(web.ErrInfo, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        private void BtnUnbind_Click(object sender)
        {
            if (!IsConn) return;
        }

        private void CboOrder_SelectedIndexChanged(object sender)
        {
            try
            {
                TxtOrderNum.Text = jsonData[CboOrder.SelectedIndex][1];
                TxtCode.Text = jsonData[CboOrder.SelectedIndex][2];
            }
            catch (Exception ex)
            {
                LOG.Error(ex.Message);
            }
        }

        private void BtnUploadOrder_Click(object sender)
        {
            if (CboCode.Text.Length == 0 || CboOrder.Text.Length == 0 || TxtOrderNum.Text.Length == 0)
                return;
            if (!IsConn) return;
            try
            {
                bool rtn = web.UploadOrder(CboOrder.Text, TxtOrderNum.Text, CboQty.Text, out printText);
                LOG.Info(web.Log);

                if (rtn)
                {
                    int n = GetSN("sn");
                    printText.Add("FACTORYLOT", CboBatch.Text);
                    printText.Add("SN", string.Format("{0:0000}", n));
                    string s = Printing?.Invoke("sn");
                    if (string.IsNullOrWhiteSpace(s)) return;

                    rtn = web.UploadMessage(s, out string[] value);

                    LOG.Info(web.Log);
                    if (rtn)
                        Asa.Face.MessageBox.Show("上传完成");
                    else
                        Asa.Face.MessageBox.Show(web.ErrInfo);
                }
                else
                {
                    MessageBox.Show(web.ErrInfo, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                LOG.Error(ex.Message);
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BtnConn_Click(object sender)
        {
            IsConn = web.Ping();
            if (IsConn)
            {
                BtnConn.Text = "MES已连接";
                BtnConn.StateColor = Color.Lime;
                Asa.Face.MessageBox.Show("MES已连接");
            }
            else
            {
                BtnConn.Text = "MES未连接";
                BtnConn.StateColor = Color.Red;
                MessageBox.Show(web.ErrInfo, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void User_Load(object sender, EventArgs e)
        {

        }

        private void BtnDel1_Click(object sender)
        {
            TxtCode.Text = "";
        }

        private void BtnDel2_Click(object sender)
        {
            CboQty.Text = "";
        }

        private void BtnDel3_Click(object sender)
        {
            CboBatch.Text = "";
        }

        private void BtnDel4_Click(object sender)
        {
            CboOrder.Text = "";
        }

        private void BtnDel5_Click(object sender)
        {
            TxtOrderNum.Text = "";
        }


        //public void SetOcrText(string[] text)
        //{
        //    if (text == null) return;

        //    try
        //    {
        //        for (int i = 0; i < text.Length; i++)
        //        {
        //            LogOut.Invoke("ocr text index=" + i + ": " + text[i]);
        //            text[i] = text[i].Replace(" ", "");   //空格
        //            text[i] = text[i].Replace("11", "");  //数字1
        //            text[i] = text[i].Replace("ll", "");  //小写字母l
        //            text[i] = text[i].Replace("ii", "");  //小写字母i
        //            text[i] = text[i].Replace("II", "");  //大写字母I
        //            text[i] = text[i].Replace(":", "");
        //            text[i] = text[i].Replace(".", "");
        //            text[i] = text[i].ToLower();
        //            LogOut.Invoke("ocr text replace: " + text[i]);

        //            int index = -1, start = 0;

        //            if (!findQty)
        //            {
        //                start = 0;
        //                do
        //                {
        //                    index = text[i].IndexOf("QTY", start);
        //                    if (index >= 0)
        //                    {
        //                        if (int.TryParse(text[i].Substring(index + 3, 4), out int result))
        //                        {
        //                            TxtQty.Text = result.ToString();
        //                            findQty = true;
        //                            break;
        //                        }
        //                    }
        //                    start += index + 3;
        //                } while (index > 0);
        //            }
        //            if (!findQty)
        //            {
        //                start = 0;
        //                do
        //                {
        //                    index = text[i].IndexOf("PCS", start);
        //                    if (index >= 0)
        //                    {
        //                        if (int.TryParse(text[i].Substring(index - 4, 4), out int result))
        //                        {
        //                            TxtQty.Text = result.ToString();
        //                            findQty = true;
        //                            break;
        //                        }
        //                    }
        //                    start += index + 3;
        //                } while (index > 0);
        //            }
        //            if (findQty) break;
        //        }
        //        if (!findQty)
        //        {
        //            Asa.Face.MessageBox.Show("QTY 没有找到");
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        LogOut?.Invoke(ex.Message);
        //    }
        //}

        //private void Discern(string text)
        //{
        //    try
        //    {
        //        LogOut.Invoke("ocr text: " + text);
        //        text = text.Replace(" ", "");   //空格
        //        text = text.Replace("11", "");  //数字1
        //        text = text.Replace("l", "");   //小写字母l
        //        text = text.Replace("i", "");   //小写字母i
        //        text = text.Replace("I", "");   //大写字母I
        //        text = text.Replace(":", "");   //冒号
        //        text = text.Replace(".", "");   //点号
        //        text = text.ToLower();          //小写
        //        LogOut.Invoke("ocr text replace: " + text);

        //        int index, start;

        //        //前缀
        //        for (int i = 0; i < keyQty[0].Length; i++)
        //        {
        //            start = 0;
        //            do
        //            {
        //                index = text.IndexOf(keyQty[0][i], start);
        //                if (index > -1)
        //                {
        //                    if (int.TryParse(text.Substring(index + 3, 4), out int result))  //直接取4位
        //                    {
        //                        TxtQty.Text = result.ToString();
        //                        findQty = true;
        //                        break;
        //                    }
        //                    start = index + keyQty[0][i].Length;
        //                }
        //            } while (index > -1);
        //            if (findQty) break;
        //        }

        //        //后缀
        //        if (findQty) return;
        //        for (int i = 0; i < keyQty[1].Length; i++)
        //        {
        //            start = 0;
        //            do
        //            {
        //                index = text.IndexOf(keyQty[1][i], start);
        //                if (index > -1)
        //                {
        //                    if (int.TryParse(text.Substring(index - 4, 4), out int result))  //直接取4位
        //                    {
        //                        TxtQty.Text = result.ToString();
        //                        findQty = true;
        //                        break;
        //                    }
        //                    start = index + keyQty[1][i].Length;
        //                }
        //            } while (index > -1);
        //            if (findQty) break;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        LogOut?.Invoke(ex.Message);
        //    }
        //}

    }
}