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)
        {
            string opName = "";

            {
                try
                {
                    DeviceDriver_BasicData.CustomeEvetnArgs msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                    string OpName = Convert.ToString(msgEvent.OpName);
                    opName = OpName;
                    int tagValue = Convert.ToInt32(msgEvent.TagValue);
                    //判断PLC机型与MES机型是否一致
                    if (tagValue == 1)
                    {
                        bool MesRead = false;

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

                        //上线工位清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);
                            MisDataSaveDate.Helper.ApiLogHelper.SaveMesLog(OpName, "PLC传递机型 " + msg, MisDataSaveDate.Helper.MesLogType.ERROR);
                            return;
                        }
                        string OrderForm;
                        MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code, out OrderForm);

                        // 读取MES区数据 校验MES区数据是否符合 同时判断是否此工件已工作过，若已工作过则弹出弹窗提示
                        if (EngineID != "" & EngineType != "" & OrderForm != "")
                        {
                            DataTable checkTable = MisDataFun.WorkOrderCheck(OpName, EngineID, OrderForm, EngineType);
                            int result = Convert.ToInt32(checkTable.Rows[0]["result"]);
                            //int result = 1;
                            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);
                                    MisDataSaveDate.Helper.ApiLogHelper.SaveMesLog(OpName, "PLC传递机型 "+ msg, MisDataSaveDate.Helper.MesLogType.ERROR);
                                }
                                int parType = Convert.ToInt32(checkTable.Rows[0]["小机型"]);
                                EngineTypeID = Convert.ToInt32(checkTable.Rows[0]["型号代码"]);
                                MIS_BASE.Write_EngineTypeID_MES(OpName, EngineTypeID);

                                MIS_BASE.WritePLC(115, OpName, parType); // 托盘类型 1 静 2 动 3 拐

                                //写入到数据库的[北汽福田工位状态监控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_MES, EngineTypeID.ToString());
                                MisDataFun.OpName_MIS_MOBY_Insert(OpName, MisDataFun.MobyTagType.EngineType_MES, EngineType);
                                MisDataFun.OpName_MIS_MOBY_Insert(OpName, MisDataFun.MobyTagType.EngineID_MES, EngineID);
                                MisDataFun.OpName_MIS_MOBY_Insert(OpName, MisDataFun.MobyTagType.OrderForm_MES, OrderForm);
                                EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType, EngineID, PartPallet_Code, OrderForm);
                                SendMessage.ShowMoby(OpName);
                                SendMessageFun.SendMessage("5123", "1111", OpName);
                            }
                            else
                            {
                                string msg = "【" + OpName + "】" + resultMsg;
                                SendMessage.AsyncSendMessage_Notice(OpName, msg);
                                MisDataSaveDate.Helper.ApiLogHelper.SaveMesLog(OpName, "PLC传递机型 " + msg, MisDataSaveDate.Helper.MesLogType.ERROR);
                            }

                            return;
                        }
                        else
                        {
                            string msg = "【" + OpName + "】" + "读取信息存在空值！";
                            MisDataSaveDate.Helper.ApiLogHelper.SaveMesLog(OpName, "PLC传递机型 " + msg, MisDataSaveDate.Helper.MesLogType.ERROR);
                            SendMessage.AsyncSendMessage_Notice(OpName, msg);
                        }

                    }
                    else
                    {
                        MIS_BASE.Write_FixAllow(OpName, false);
                        MIS_BASE.Write_EngineTypeID_MES(OpName, 0);
                    }
                }
                catch (Exception err)
                {
                    string logOpName = string.IsNullOrWhiteSpace(opName) ? "Function1052_EngineGetOver" : opName;
                    string logText = $"EngineTypeGetOver异常：异常={err.Message}";
                    MisDataSaveDate.Helper.ApiLogHelper.SaveMesLog(logOpName, logText, MisDataSaveDate.Helper.MesLogType.ERROR);
                    MyLog4Net.MyLogHelper.Error("Function1052_EngineGetOver", $"{logText}{Environment.NewLine}{err}");
                }
            }


        }

    }
}
