using System;
using System.Data;
using MesServerWork;
using MisDataFunDll;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// PLC传递机型处理
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void EngineTypeGetOver_PLC(object e)
        {
            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);

                    // 判断是否返修件 是否需要继续工作
                    int isBackWork = 0;
                  

                    // 判断PLC机型是否写入
                    if (EngineTypeID <= 0 || EngineID == "")
                    {
                        //不允许工作
                        MIS_BASE.Write_FixAllow(OpName, false);
                        // MES报警
                        MIS_BASE.Write_TagValueByTagTypeID(34, OpName, true);
                        string msg = "【" + OpName + "】" + "PLC机型写入为空，EngineTypeID：" + EngineTypeID + "，EngineID：" + EngineID;
                        SendMessage.AsyncSendMessage_Notice(OpName, msg);
                        MisDataFun.LogRecording_OP(OpName, "ERROR", "EngineTypeGetOver", msg);
                        return;
                    }

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

                    // 机型信息写入数据库
                    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
                {
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "PLC传递机型取消！");
                    MIS_BASE.Write_FixAllow(OpName, false); // 允许PLC拧紧工作（MES）
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function08:EngineTypeGetOver_PLC");
            }


        }

    }
}
