using System;
using System.Data;
using bizFacade;
using MesServerWork;
//using SystemFramework;

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


        /// <summary>
        /// PLC传递机型处理
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void EngineTypeGetOver_PLC(object e)
        {
            lock(lockBarcodeEngineTypeGetOver) 
            {

                try
                {
                    DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                    string OpName = Convert.ToString(msgEvent.OpName);
                    int tagValue = Convert.ToInt32(msgEvent.TagValue);
                    //判断PLC机型与MES机型是否一致
                    if (tagValue == 1)
                    {
                        bool MesRead = false;

                        int EngineTypeID;//机型代码
                        int PartPallet_Code;//托盘编号

                        string EngineType;//机型
                        string EngineID;//工件编号

                        int EngineTypeID_MES;//机型代码
                        string EngineType_MES;//机型
                        string EngineID_MES;//工件编号
                        string OrderForm_MES;

                        //上线工位清MES传递机型信号
                        MIS_BASE.Write_EngineTypeGetOver_MES(OpName, false);

                        MIS_BASE.Read_QualityData_Read_CF(OpName, out MesRead);
                        //第一步，判断允许保存和保存完成信号是否全部被清空，若未清空，则通知复位MES&PLC
                        if (MesRead)
                        {
                            //未能清空信号，请求复位
                            string msg = "【" + OpName + "】" + "未能清空MesRead信号，请求复位！";
                            SendMessageFun.SendInfomation("AskReset", "1", OpName);
                            MyLog4Net.MyLogHelper.Error("EngineTypeGetOver", msg);
                            return;
                        }
                        string OrderForm;
                        MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code, out OrderForm);
                        MIS_BASE.GetEngineTypeFromPLC_MES(OpName, out EngineTypeID_MES, out EngineType_MES, out EngineID_MES, out OrderForm_MES);


                        SendMessageFun.SendMessage("123", "111", OpName);
                        // 读取MES区数据 校验MES区数据是否符合 同时判断是否此工件已工作过，若已工作过则弹出弹窗提示
                        if (EngineTypeID_MES != 0 & EngineID_MES != "" & EngineType_MES != "" & OrderForm_MES != "")
                        {
                            DataTable checkTable = BaseDataSystemMES.WorkOrderCheck(OpName, EngineID_MES, OrderForm_MES, EngineType_MES);
                            int result = Convert.ToInt32(checkTable.Rows[0]["result"]);
                            string resultMsg = checkTable.Rows[0]["msg"].ToString();
                            if (result != 2)
                            {
                                if (result == 3)
                                {
                                    string msg = "【" + OpName + "】" + resultMsg;
                                    MyLog4Net.MyLogHelper.Error("EngineTypeGetOver", msg);
                                    SendMessage.AsyncSendMessage_Notice(OpName, msg);
                                }
                                //写入到数据库的[北汽福田工位状态监控MOBY]表中
                                BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineTypeID, EngineTypeID_MES.ToString());
                                BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineType, EngineType_MES);
                                BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineID, EngineID_MES);
                                BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.OrderForm, OrderForm_MES);
                                BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.PartPallet_Code, PartPallet_Code.ToString());
                                BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineTypeID_MES, EngineTypeID_MES.ToString());
                                BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineType_MES, EngineType_MES);
                                BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineID_MES, EngineID_MES);
                                BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.OrderForm_MES, OrderForm_MES);
                                EngineTypeGetOver_PLC_Work(OpName, EngineTypeID_MES, EngineType_MES, EngineID_MES, PartPallet_Code, OrderForm_MES);

                                SendMessageFun.SendMessage("5", "1", OpName);

                                // 将工序数据转入临时表中
                                BaseDataSystemMES.ProcessDataToTemp(OpName, EngineTypeID_MES, OrderForm_MES, EngineID_MES);
                            }
                            else
                            {
                                MIS_BASE.WritePLC(34, OpName, true);
                                string msg = "【" + OpName + "】" + resultMsg;
                                MyLog4Net.MyLogHelper.Error("EngineTypeGetOver", msg);
                                SendMessage.AsyncSendMessage_Notice(OpName, msg);
                            }

                            return;
                        }
                        else
                        {
                            MIS_BASE.WritePLC(34, OpName, true);
                            string msg = "【" + OpName + "】" + "读取信息存在空值！";
                            MyLog4Net.MyLogHelper.Error("EngineTypeGetOver", msg);
                            SendMessage.AsyncSendMessage_Notice(OpName, msg);
                        }

                    }
                    else
                    {
                        MIS_BASE.Write_FixAllow(OpName, false);
                    }
                }
                catch (Exception err)
                {
                    ////ApplicationLog.WriteLog(err, "Function08:EngineTypeGetOver_PLC");
                }
            }
           
        }

        

    }
}
