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;//托盘编号

                        MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code, out OrderForm);
                        // PDC产线plc只写了发动机号和机型代码，其余要从数据库中读取
                        MisDataFun.Moby_Select_PDC(EngineID, out OrderForm, out EngineType, out int EngineTypeID1);

                        if (!MisDataBaseFun.EngineTypeVerify(OpName, EngineID, EngineTypeID)) return; // 机型校验
                        if (!MisDataBaseFun.NotAllowWorkVerify(OpName, EngineID)) 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(OpName, EngineTypeID, EngineType, EngineID, PartPallet_Code, OrderForm, 0, 0, "");
                            EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType, EngineID, PartPallet_Code, OrderForm);

                        }
                        else
                        {
                            string msg = "进站失败：" + EngineID + "，code：" + code + "，message：" + message;
                            MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", msg);
                        }
                    }
                    else
                    {
                        MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "PLC传递机型取消！");
                        MIS_BASE.Write_FixAllow(OpName, false); // 允许PLC拧紧工作（MES）
                        MIS_BASE.Write_NotAllowWork(OpName, false); // 不允许工作
                    }
                }
                catch (Exception err)
                {
                    ////ApplicationLog.WriteLog(err, "Function08:EngineTypeGetOver_PLC");
                }
            }
           
        }

        

    }
}
