using System;
using bizFacade;
using DtWebApiDT;
//using SystemFramework;
using MesServerWork;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 申请上线(针对上线工位)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void PartLoad(object e)
        {
            try
            {
                DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                string OpName = Convert.ToString(msgEvent.OpName);
                int tagValue = Convert.ToInt32(msgEvent.TagValue);

                if (tagValue == 1)
                {
                    // 读取上线机型，和订单的机型进行比较
                    string EngineType_PLC;
                    string Program_No;
                    MIS_BASE.Read_TagTypeID(38, OpName, out Program_No);

                    if (Program_No == "0")
                    {
                        // 信号10.4(29) = 1通知PLC
                        MIS_BASE.WritePLC(29, OpName, "1");
                        SendMessage.AsyncSendMessage_Notice(OpName, "PLC机型为0！");
                        return;
                    }
                    // 获取订单的机型
                    Db.GetEngineType_Order(OpName, Program_No, out int EngineTypeID, out string EngineType, out string EngineID, out string OrderForm, out int isCheckOk, out int EngineTypeID_lungu, out int EngineTypeID_qiaoke,out int backCode);
                    if(isCheckOk == 2)
                    {
                        // 信号10.4(29) = 1通知PLC
                        MIS_BASE.WritePLC(29, OpName, "1");
                        SendMessage.AsyncSendMessage_Notice(OpName, "PLC机型与MES订单机型不匹配！");
                        return;
                    }
                    else
                    {
                        BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.EngineID_MES, EngineID);

                        //string tagStartAdr;//= "DB411.10";
                        // 写入配方数据
                        //BaseDataSystemMES.PLCRcipeCurrent(OpName, out byte[] bytes, out tagStartAdr);
                        //DtWebApi.ReadWritePLC.Write_TagValueByTagTypeID("94", OpName, System.Text.Encoding.ASCII.GetString(bytes));
                        // 写入机型代码 （大机型） 机型 发动机号 订单号
                        MIS_BASE.Write_EngineTypeID_MES(OpName, EngineTypeID);
                        MIS_BASE.Write_EngineType_MES(OpName, EngineType);
                        MIS_BASE.Write_EngineID_MES(OpName, EngineID);
                        MIS_BASE.Write_OrderForm_MES(OpName, OrderForm);

                        // 写入MES防错程序号  轮毂程序号 或者 桥壳程序号 （小机型号） PLC干活用的
                        MIS_BASE.WritePLC(110, OpName, backCode);
                        // 写入MES转线比对用桥壳程序号 
                        MIS_BASE.WritePLC(200, OpName, EngineTypeID_qiaoke);
                        // MES传递机型
                        MIS_BASE.Write_EngineTypeGetOver_MES(OpName, true);
                        // 通知web显示
                        SendMessage.ShowMoby(OpName);
                    }
                    MyLog4Net.MyLogHelper.Error($"触发记录：", $"申请上线 工位号:{OpName} 工件编号:{EngineID}");
                }
                else
                {
                    //上线工位清MES传递机型信号
                    MIS_BASE.Write_EngineTypeGetOver_MES(OpName, false);

                    //申请上线信号消失  同时清空工作完成 (防错)
                    MIS_BASE.Write_MaterialVerifyOver(OpName, false);
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
            }
        }
    }
}
