using System;
using MesServerWork;
using MisDataFunDll;
using MisDataSaveDate;
//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
                {
                    var msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                    string OpName = Convert.ToString(msgEvent.OpName);
                    int tagValue = Convert.ToInt32(msgEvent.TagValue);
                    //判断PLC机型与MES机型是否一致
                    if (tagValue == 1)
                    {
                        MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "PLC传递机型！");
                        MIS_BASE.Write_TagValueByTagTypeID(34, OpName, false); // 复位报警（MES）

                  
                        int EngineTypeID;//机型代码
                        string EngineType;//机型
                        string EngineID;//工件编号
                        string OrderForm; // 工单号
                        int PartPallet_Code;//托盘编号

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

                        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);

                        // 判断PLC机型是否写入
                        if (!MisDataBaseFun.EngineTypeVerifyOnLine(OpName, EngineID, EngineTypeID, EngineID_MES, EngineTypeID_MES)) return;


                        // 调用进站接口
                        CallWebService.Call_miFindCustomAndSfcData(OpName, EngineID, out string code, out string message);
                        if (code == "0")
                        {
                            string msg = "进站成功：" + EngineID;
                            MisDataFun.LogRecording_OP(OpName, "INFO", "MOM", msg);
                            // 机型信息写入数据库
                            MisDataFun.OpName_MIS_MOBY_Insert_FirstOpName(OpName, EngineTypeID, EngineTypeID_MES, EngineType_MES, EngineType_MES, EngineID, EngineID_MES, PartPallet_Code, OrderForm_MES, OrderForm_MES);
                            EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType_MES, EngineID, PartPallet_Code, OrderForm_MES);

                        }
                        else
                        {
                            string msg = "进站失败：" + EngineID + "，code：" + code + "，message：" + message;
                            MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", msg);
                        }
                        //// 机型信息写入数据库
                        //MisDataFun.OpName_MIS_MOBY_Insert_FirstOpName(OpName, EngineTypeID, EngineTypeID_MES, EngineType_MES, EngineType_MES, EngineID, EngineID_MES, PartPallet_Code, OrderForm_MES, OrderForm_MES);
                        //EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType_MES, EngineID, PartPallet_Code, OrderForm_MES);
                    }
                    else
                    {
                        MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "PLC传递机型取消！");
                        MIS_BASE.Write_FixAllow(OpName, false); // 允许PLC拧紧工作（MES）
                    }
                }
                catch (Exception err)
                {
                    ////ApplicationLog.WriteLog(err, "Function08:EngineTypeGetOver_PLC");
                }
            }
           
        }

        

    }
}
