using System;
using System.Data;
using System.Windows.Forms;
using MesServerWork;
using MisDataFunDll;
using MisDataSaveDate;

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;
                    }
                    bool part1_IsOk = true;
                    bool part2_IsOk = true;

                    // 3.记录工件的进站数据
                    if (part1 != null && part1 != "" && part1.Length > 5)
                    {
                        // 查询是否 不允许工作
                        MisDataFun.Get_NotAllowWork(part1, OpName, out bool NotAllowWork);
                        if (NotAllowWork)
                        {
                            MIS_BASE.Write_NotAllowWork(OpName, true); // 不允许工作
                            part1_IsOk = false;
                        }
                        else
                        {
                            // 调用进站接口
                            CallWebService.Call_miFindCustomAndSfcData(OpName, part1, out string code, out string message);

                            if (code == "0")
                            {
                                string msg = "进站成功：" + part1;
                                MisDataFun.LogRecording_OP(OpName, "INFO", "MOM", msg);
                                MisDataFun.OpName_MIS_MOBY_Insert_JZ(OpName, part1);
                            }
                            else
                            {
                                part1_IsOk = false;
                                string msg = "进站失败：" + part1 + "，code：" + code + "，message：" + message;
                                MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", msg);
                            }
                        }
                       
                    }
                    if (part2 != null && part2 != "" && part2.Length > 5)
                    {
                        // 查询是否 不允许工作
                        MisDataFun.Get_NotAllowWork(part2, OpName, out bool NotAllowWork);
                        if (NotAllowWork)
                        {
                            MIS_BASE.Write_NotAllowWork(OpName, true); // 不允许工作
                            part2_IsOk = false;
                        }
                        else
                        {
                            // 调用进站接口
                            CallWebService.Call_miFindCustomAndSfcData(OpName, part2, out string code, out string message);

                            if (code == "0")
                            {
                                string msg = "进站成功：" + part2;
                                MisDataFun.LogRecording_OP(OpName, "INFO", "MOM", msg);
                                MisDataFun.OpName_MIS_MOBY_Insert_JZ(OpName, part2);
                            }
                            else
                            {
                                part2_IsOk = false;
                                string msg = "进站失败：" + part2 + "，code：" + code + "，message：" + message;
                                MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", msg);
                            }
                        }    
                    }
                    // 两个件都进站成功了 才允许工作
                    if(part1_IsOk && part2_IsOk)
                    {
                        MIS_BASE.Write_FixAllow(OpName, true); // 允许PLC工作（MES）
                    }
                }
                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");
            }
        }
        /// <summary>
        /// PLC传递机型处理
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void EngineTypeGetOver_PLC_part1Ng(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", "EngineTypeGetOver_PLC_part1Ng", "PLC产品1Ng传递机型！");
                    MIS_BASE.Write_TagValueByTagTypeID(34, OpName, false); // 复位报警（MES）

                    // 1.读取来件信息，有可能来一个件 有可能同时来两个件，来空托盘不交互
                    string part1;
                    MIS_BASE.Read_TagValueByTagTypeID(203, OpName, out part1);
                    // 2.判断PLC工件是否写入
                    if (part1 == "" || part1 == null || part1.Length < 5)
                    {
                        // MES报警
                        MIS_BASE.Write_TagValueByTagTypeID(34, OpName, true);
                        string msg = "【" + OpName + "】" + "PLC位置1产品信息为空，part1：" + part1;
                        SendMessage.AsyncSendMessage_Notice(OpName, msg);
                        MisDataFun.LogRecording_OP(OpName, "ERROR", "EngineTypeGetOver_PLC_part1Ng", msg);
                        return;
                    }

                    // 3.记录工件的进站数据
                    if (part1 != null && part1 != "" && part1.Length > 5)
                    {
                        string msg;
                        // 查询是否 不允许工作
                        MisDataFun.Get_NotAllowWork(part1, OpName, out bool NotAllowWork);
                        if (NotAllowWork)
                        {
                            MIS_BASE.Write_NotAllowWork(OpName, true); // 不允许工作
                            msg = "产品不合格，不允许工作！" + part1;
                            MisDataFun.LogRecording_OP(OpName, "INFO", "EngineTypeGetOver_PLC_part1Ng", msg);
                        }
                        else
                        {
                            msg = "产品1PLC中记录不合格，但PLCS中记录合格！" + part1;
                            MisDataFun.LogRecording_OP(OpName, "ERROR", "EngineTypeGetOver_PLC_part1Ng", msg);
                        }

                    }
                }
                else
                {
                    MisDataFun.LogRecording_OP(OpName, "INFO", "EngineTypeGetOver_PLC_part1Ng", "PLC产品1Ng传递机型取消！");
                    MIS_BASE.Write_NotAllowWork(OpName, false); // 不允许工作
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function08:EngineTypeGetOver_PLC");
            }


        }
        /// <summary>
        /// PLC传递机型处理
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void EngineTypeGetOver_PLC_part2Ng(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", "EngineTypeGetOver_PLC_part2Ng", "PLC产品2Ng传递机型！");
                    MIS_BASE.Write_TagValueByTagTypeID(34, OpName, false); // 复位报警（MES）

                    // 1.读取来件信息，有可能来一个件 有可能同时来两个件，来空托盘不交互
                    string part2;
                    MIS_BASE.Read_TagValueByTagTypeID(204, OpName, out part2);
                    // 2.判断PLC工件是否写入
                    if (part2 == "" || part2 == null || part2.Length < 5)
                    {
                        // MES报警
                        MIS_BASE.Write_TagValueByTagTypeID(34, OpName, true);
                        string msg = "【" + OpName + "】" + "PLC位置2产品信息为空，part2：" + part2;
                        SendMessage.AsyncSendMessage_Notice(OpName, msg);
                        MisDataFun.LogRecording_OP(OpName, "ERROR", "EngineTypeGetOver_PLC_part2Ng", msg);
                        return;
                    }

                    // 3.记录工件的进站数据
                    if (part2 != null && part2 != "" && part2.Length > 5)
                    {
                        string msg;
                        // 查询是否 不允许工作
                        MisDataFun.Get_NotAllowWork(part2, OpName, out bool NotAllowWork);
                        if (NotAllowWork)
                        {
                            MIS_BASE.Write_NotAllowWork(OpName, true); // 不允许工作
                            msg = "产品2不合格，不允许工作！" + part2;
                            MisDataFun.LogRecording_OP(OpName, "INFO", "EngineTypeGetOver_PLC_part2Ng", msg);
                        }
                        else
                        {
                            msg = "产品2PLC中记录不合格，但PLCS中记录合格！" + part2;
                            MisDataFun.LogRecording_OP(OpName, "ERROR", "EngineTypeGetOver_PLC_part2Ng", msg);
                        }
                    }
                }
                else
                {
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "PLC产品2Ng传递机型取消！");
                    MIS_BASE.Write_NotAllowWork(OpName, false); // 不允许工作
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function08:EngineTypeGetOver_PLC");
            }


        }

    }
}
