UsrNanRui.cs 9.0 KB
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 MES.Interface;

namespace MES
{
    public partial class UsrNanRui : Asa.Face.BaseCtls, IUserControl
    {
        private WebService web;
        private string[][] jsonData;
        private Dictionary<string, string> printText;

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

        public UsrNanRui()
        {
            InitializeComponent();
            printText = new Dictionary<string, string>();
            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 event Log LogOut;
        public event PrintEvent Printing;
        public event SerialNoEvent GetSN;
        public event PreviewEvent Preview;


        /// <summary>
        /// 连接MES
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool Connect(string ip)
        {
            try
            {
                web = new WebService(ip);
                IsConn = web.Ping();
                LogOut?.Invoke(web.Log);
                if (IsConn)
                {
                    BtnConn.Text = "MES已连接";
                    BtnConn.StateColor = Color.Lime;
                }
                else
                {
                    BtnConn.Text = "MES未连接";
                    BtnConn.StateColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                IsConn = false;
                LogOut?.Invoke(ex.Message);
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return IsConn;
        }

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

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

        public void SetCode(string[] code)
        {
            if (code == null) return;
            CboCode.Text = "";
            CboCode.ItemClear();
            CboCode.ItemAdd(code);
            CboCode.SelectedIndex = 0;

            CboBatch.Text = "";
            CboBatch.ItemClear();
            CboBatch.ItemAdd(code);
            CboBatch.SelectedIndex = 0;
        }

        public void SetCode(Dictionary<string, string> code)
        {
            if (code == null) return;

            bool rtn = code.TryGetValue(VENDER, out string value);
            if (rtn)
            {
                CboCode.Text = "";
                CboCode.ItemClear();
                CboCode.ItemAdd(value);
                CboCode.SelectedIndex = 0;
                printText.Add(VENDER, value);
            }

            rtn = code.TryGetValue(QTY, out value);
            if (rtn)
            {
                TxtQty.Text = value;
                printText.Add(QTY, value);
            }

            rtn = code.TryGetValue(BATCH, out value);
            if (rtn)
            {
                CboBatch.Text = "";
                CboBatch.ItemClear();
                CboBatch.ItemAdd(value);
                CboBatch.SelectedIndex = 0;
                printText.Add(BATCH, value);
            }

        }

        public void SetOcrText(Dictionary<string, string> text)
        {
            if (text == null) return;

            bool rtn = text.TryGetValue(VENDER, out string value);
            if (rtn)
            {
                CboCode.Text = "";
                CboCode.ItemClear();
                CboCode.ItemAdd(value);
                printText.Add(VENDER, value);
            }

            rtn = text.TryGetValue(QTY, out value);
            if (rtn)
            {
                TxtQty.Text = value;
                printText.Add(QTY, value);
            }

            rtn = text.TryGetValue(BATCH, out value);
            if (rtn)
            {
                CboBatch.Text = "";
                CboBatch.ItemClear();
                CboBatch.ItemAdd(value);
                CboBatch.SelectedIndex = 0;
                printText.Add(BATCH, value);
            }
        }

        public bool Print(int time)
        {
            for (int i = 0; i < time; i++)
            {
                Printing?.Invoke();
                System.Threading.Thread.Sleep(500);
            }
            return true;
        }





        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;
                Asa.Face.MessageBox.ShowError(web.ErrInfo);
            }
        }

        private void BtnVender_Click(object sender)
        {
            if (!IsConn) return;
            if (CboCode.Text.Length == 0) return;
          
            try
            {
                CboOrder.ItemClear();
                TxtOrderNum.Text = "";
                bool rtn = web.UploadVender(CboCode.Text, out jsonData);
                LogOut?.Invoke(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
                {
                    Asa.Face.MessageBox.Show(web.ErrInfo);
                }
            }

            catch (Exception ex)
            {
                LogOut?.Invoke(ex.Message);
                Asa.Face.MessageBox.ShowError(ex.Message);
            }
        }

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

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

        }

        private void CboOrder_SelectedIndexChanged(object sender)
        {
            //try
            //{
            //    TxtOrderNum.Text = jsonData[CboOrder.SelectedIndex][1];
            //}
            //catch (Exception ex)
            //{
            //    LogOut?.Invoke(ex.Message);
            //}
        }

        private void BtnUpload_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, TxtQty.Text, out printText);
                LogOut?.Invoke(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);
                //    LogOut?.Invoke(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)
            {
                LogOut?.Invoke(ex.Message);
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BtnPreview_Click(object sender)
        {
            Preview?.Invoke();
        }
    }
}