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）

                    // 1.读取来件信息，有可能来一个件 有可能同时来两个件，来空托盘不交互
                    string part1;
                    string part2;
                    MIS_BASE.Read_TagValueByTagTypeID(203, OpName, out part1);
                    MIS_BASE.Read_TagValueByTagTypeID(204, OpName, out part2);
                    // 2.判断PLC工件是否写入
                    if ((part2 == "" && part1 == "") || (part2 == null && part1 == null) || (part2.Length < 5 && part1.Length < 5))
                    {
                        //不允许工作
                        MIS_BASE.Write_FixAllow(OpName, false);
                        // MES报警
                        MIS_BASE.Write_TagValueByTagTypeID(34, OpName, true);
                        string msg = "【" + OpName + "】" + "PLC两个产品信息为空，part1：" + part1 + "，part2：" + part2;
                        SendMessage.AsyncSendMessage_Notice(OpName, msg);
                        MisDataFun.LogRecording_OP(OpName, "ERROR", "EngineTypeGetOver", msg);
                        return;
                    }
                    // 3.记录工件的进站数据
                    if (part1 != null && part1 != "" && part1.Length > 5)
                    {
                        MisDataFun.OpName_MIS_MOBY_Insert_JZ(OpName, part1);
                    }
                    if (part2 != null && part2 != "" && part2.Length > 5)
                    {
                        MisDataFun.OpName_MIS_MOBY_Insert_JZ(OpName, part2);
                    }
                    MIS_BASE.Write_FixAllow(OpName, true); // 允许PLC工作（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");
            }


        }

    }
}
