using System;
using System.Threading;
using System.Xml.Linq;
using bizFacade;

//using SystemFramework;
using MesServerWork;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        object lockBarcode = new object();

     

        ///第一步，判断条码类型，根据条码类型，执行对应的存储过程
        ///第二步，条码类型(将扫描的条码保存到对应的表中)
        ///第三步，扫描完成之后给出允许工作信号
        /// 处理页面扫描条码
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="barcode"></param>
        public void Barcode(object e)
        {
            lock(lockBarcode)
            {
                try
                {
                    DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                    string OpName = Convert.ToString(msgEvent.OpName);
                    string OpName_mes = Convert.ToString(msgEvent.OpName) + "_mes";
                    string barcode = Convert.ToString(msgEvent.TagValue);
                    //将随工单零件号写入PLC
                    MIS_BASE.Write_EngineType_MES(OpName_mes, "");
                    //将唯一标识写入PLC
                    MIS_BASE.Write_EngineID_MES(OpName_mes, "");
                    // 判断条码格式是否正确
                    if (barcode.Split(';').Length > 2)
                    {
                        //m003000214179;p23003;x0;g|JS8C.12.825|1|22-K-255-1/组件|ZM6|20230109134927285001740003
                        //[0]m003000214179
                        //[1]p23003
                        //[2]x0
                        //[3]g
                        //[4]JS8C.12.825
                        //[5]1
                        //[6]22-K-255-1/组件z
                        //[7]ZM6
                        //[8]20230109134927285001740003

                        var barArray = barcode.Split(new char[] { ';', '|' });
                        var orderNo = barArray[0].Substring(1); // 订单号
                        var batchNo = barArray[1].Substring(1); // 批次号
                        var partNo = barArray[4]; // 零件号/机型
                        var partName = barArray[6].Split('/')[1]; //零件名称
                        var partSort = barArray[6].Split('/')[0]; //零件类别
                        var partSortType = barArray[6].Split('/')[0]; //零件类别
                        var partSnNo = barArray[8]; // 零件唯一码

                        string EngineType = partNo;//机型
                        string EngineID = partSnNo.Substring(0,18);//发动机号

                        //为产品生成唯一标识(扫码时间戳)                       
                        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                        //EngineID = Convert.ToInt64(ts.TotalMilliseconds).ToString();

                        bool IsNotRepeatedNumber = false; // 是否重号
                        int IsTodayPlan = 0; // 是否是今天的计划


                      
                        //判断当前设备的模式：手动、自动、脉动
                        //自动模式:
                        MIS_BASE.Read_TagTypeID(63, OpName, out string workModel);
                        switch (workModel.ToLower())
                        {
                            case "true":
                            case "1":
                                workModel = "1";
                                break;
                            default:
                                workModel = "0";
                                    break;
                        }


                        //1:获得申请上线信息后，扫描条码才处理业务
                        bool ispartload = false;

                        MIS_BASE.Read_PartLoad(OpName, out ispartload);
                        if (!ispartload)
                        {
                            SendMessageFun.AsyncSendMessage_Notice(OpName, "等待人工扫码信号！！");
                            return;
                        }


                        if (workModel.ToString() == "1" && ispartload )
                        {
                            string opName_db = "";
                            //自动模式时,进行工位转换
                            if (OpName == "OP10")
                            {
                                OpName_mes = "OP10_mes";
                                opName_db = "OP20";
                            }
                            else if (OpName == "OP30")
                            {
                                OpName_mes = "OP30_mes";
                                opName_db = "OP40";
                            }
                            else
                            {
                                opName_db = OpName;
                            }

                            //判断该产品标识在该工位是否重复
                            IsNotRepeatedNumber = BaseDataSystemMES.EngineTypeID_IsNotRepeatedNumber(EngineID, opName_db);
                            if (IsNotRepeatedNumber)
                            {
                                // 判断是否是今天的计划
                                BaseDataSystemMES.GetEngineTypeUpLoadOpName(EngineID, partNo, opName_db, orderNo, batchNo, out IsTodayPlan);
                                if (IsTodayPlan == 1)
                                {
                                    BaseDataSystemMES.OpName_MIS_MOBY_Insert(opName_db, MobyTagType.WorkStartIn, DateTime.Now.ToString());
                                    //将唯一标识写入PLC
                                    //MIS_BASE.Write_EngineID_MES(OpName_mes, EngineID);
                                    MIS_BASE.WritePLC(74, OpName_mes, EngineID);
                                    //将随工单零件号写入PLC
                                    //MIS_BASE.Write_EngineType_MES(OpName_mes, EngineType);
                                    MIS_BASE.WritePLC(75, OpName_mes, EngineType);
                                    SendMessageFun.AsyncSendMessage_Notice(OpName, "收到扫码信号···········");
                                    //先把延时屏蔽掉看看效果如何
                                    //Thread.Sleep(5000);
                                    MIS_BASE.Write_EngineTypeGetOver_MES(OpName_mes, true);
                                    // MES传递机型
                                    //string WriteStr1 = MIS_BASE.ReadPLC(74, OpName_mes).ToString();
                                    //string WriteStr2 = MIS_BASE.ReadPLC(75, OpName_mes).ToString();

                                    //if (WriteStr1 != "" && WriteStr2 != "")
                                    //{
                                    //     MIS_BASE.Write_EngineTypeGetOver_MES(OpName_mes, true);
                                    //     SendMessageFun.AsyncSendMessage_Notice(OpName, "扫码成功·········");
                                    //}
                                    //else
                                    //{
                                    //    SendMessageFun.AsyncSendMessage_Notice(OpName, "写入传递机型时码值为空!!!!!!!!");
                                    //}
                                    //显示当前订单信息
                                    SendMessage.ShowMoby(opName_db);
                                    //通知web显示
                                    SendMessageFun.SendMessage("13", "111", opName_db);
                                    MIS_BASE.asyncSendMessage("MES|showBarcode|" + OpName + "|" + barcode);
                                }
                                else
                                {
                                    MIS_BASE.asyncSendMessage("MES|showBarcode|" + OpName + "|" + "");
                                    SendMessageFun.AsyncSendMessage_Notice(opName_db, "计划不匹配！！");
                                }
                            }
                            else
                            {
                                MIS_BASE.asyncSendMessage("MES|showBarcode|" + OpName + "|" + barcode);
                                SendMessageFun.AsyncSendMessage_Notice(opName_db, "工件零件号重号,请确认！");
                            }
                            return;

                        }

                        //获取脉动模式:
                        MIS_BASE.Read_TagTypeID(64, OpName, out string workModel_MD);
                        //获取手动模式:
                        MIS_BASE.Read_TagTypeID(62, OpName, out string workModel_SD);

                        switch (workModel_MD.ToLower())
                        {
                            case "true":
                            case "1":
                                workModel_MD = "1";
                                break;
                            default:
                                workModel_MD = "0";
                                break;
                        }
                        switch (workModel_SD.ToLower())
                        {
                            case "true":
                            case "1":
                                workModel_SD = "1";
                                break;
                            default:
                                workModel_SD = "0";
                                break;
                        }

                        //手动模式:
                        

                        if (workModel_MD.ToString() == "1" || workModel_SD.ToString() == "1")
                        { }
                        else
                        {
                            SendMessageFun.AsyncSendMessage_Notice(OpName, "PLC模式不正确！！");
                            return;
                        }

                        

                        //2:从数据库判断判断是否已经扫描过这个码 IsRepeatedNumber true 不重号；false 重号；

                        IsNotRepeatedNumber = BaseDataSystemMES.EngineTypeID_IsNotRepeatedNumber(EngineID, OpName);
                        if (IsNotRepeatedNumber)
                        {
                            // 判断是否是今天的计划
                            BaseDataSystemMES.GetEngineTypeUpLoadOpName(EngineID, partNo, OpName, orderNo, batchNo, out IsTodayPlan);
                            if (IsTodayPlan == 1)
                            {
                                //将随工单零件号写入PLC
                                MIS_BASE.Write_EngineType_MES(OpName_mes, EngineType);
                                //将唯一标识写入PLC 
                                MIS_BASE.Write_EngineID_MES(OpName_mes, EngineID);
                                Thread.Sleep(1000);
                                // MES传递机型
                                MIS_BASE.Write_EngineTypeGetOver_MES(OpName_mes, true);
                                //显示当前订单信息
                                SendMessage.ShowMoby(OpName);
                                //通知web显示
                                SendMessageFun.SendMessage("13", "111", OpName);
                                MIS_BASE.asyncSendMessage("MES|showBarcode|" + OpName + "|" + barcode);
                            }
                            else
                            {
                                SendMessageFun.AsyncSendMessage_Notice(OpName, "计划不匹配！！");
                                MIS_BASE.asyncSendMessage("MES|showBarcode|" + OpName + "|" + "");
                            }
                        }
                        else
                        {
                            SendMessageFun.AsyncSendMessage_Notice(OpName, "工件零件号重号,请确认！");
                            MIS_BASE.asyncSendMessage("MES|showBarcode|" + OpName + "|" + barcode);
                        }
                    }
                    else
                    {
                        SendMessageFun.AsyncSendMessage_Notice(OpName, "工件条码格式不正确,请确认！");
                        MIS_BASE.asyncSendMessage("MES|showBarcode|" + OpName + "|" + barcode);
                    }
                }
                catch (Exception err)
                {
                    ////ApplicationLog.WriteLog(err, "Function08:Barcode");
                }
            }

        }


    }
}
