using System;
using System.Data;
using MesServerWork;
using MisDataFunDll;
//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
                {
                    DeviceDriver_BasicData.CustomeEvetnArgs msgEvent = (DeviceDriver_BasicData.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);

                        // 读取MES区数据 校验MES区数据是否符合 同时判断是否此工件已工作过，若已工作过则弹出弹窗提示
                        if (EngineTypeID == EngineTypeID_MES & EngineID == EngineID_MES & EngineType == EngineType_MES & OrderForm  == OrderForm_MES)
                        {
                            if (EngineTypeID == 0 || EngineID == "" || EngineType == "" || OrderForm == "")
                            {
                                string msg = "【" + OpName + "】" + "传递机型数据为空！";
                                MyLog4Net.MyLogHelper.Error("EngineTypeGetOver", msg);
                                SendMessage.AsyncSendMessage_Notice(OpName, msg);
                                return;
                            }

                            DataTable checkTable = MisDataFun.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]表中
                                MisDataFun.OpName_MIS_MOBY_Insert(OpName, MisDataFun.MobyTagType.EngineTypeID, EngineTypeID.ToString());
                                MisDataFun.OpName_MIS_MOBY_Insert(OpName, MisDataFun.MobyTagType.EngineType, EngineType);
                                MisDataFun.OpName_MIS_MOBY_Insert(OpName, MisDataFun.MobyTagType.EngineID, EngineID);
                                MisDataFun.OpName_MIS_MOBY_Insert(OpName, MisDataFun.MobyTagType.OrderForm, OrderForm);
                                MisDataFun.OpName_MIS_MOBY_Insert(OpName, MisDataFun.MobyTagType.PartPallet_Code, PartPallet_Code.ToString());
                                //MisDataFun.OpName_MIS_MOBY_Insert(OpName,MisDataFun.MobyTagType.EngineTypeID, EngineTypeID.ToString());
                                //MisDataFun.OpName_MIS_MOBY_Insert(OpName,MisDataFun.MobyTagType.EngineType, EngineType);
                                //MisDataFun.OpName_MIS_MOBY_Insert(OpName,MisDataFun.MobyTagType.EngineID, EngineID);
                                //MisDataFun.OpName_MIS_MOBY_Insert(OpName,MisDataFun.MobyTagType.OrderForm, OrderForm);
                                EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType, EngineID, PartPallet_Code, OrderForm);

                                SendMessageFun.SendMessage("5123", "11111", OpName);

                                // 将工序数据转入临时表中
                                //BaseDataSystemMES.ProcessDataToTemp(OpName, EngineTypeID, OrderForm, EngineID);
                            }
                            else
                            {
                                string msg = "【" + OpName + "】" + resultMsg;
                                MyLog4Net.MyLogHelper.Error("EngineTypeGetOver", msg);
                                SendMessage.AsyncSendMessage_Notice(OpName, msg);
                            }
                            return;
                        }
                        else
                        {
                            string msg = "【" + OpName + "】" + "PLC与MES信息不一致！";
                            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");
                }
            }
           
        }

        

    }
}
