using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
//using SystemFramework;
using MesServerWork;
using MisDataFunDll;

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

                if (tagValue == 1)
                {
                    // 读取当前选择的计划中的机型，写入PLC
                    // 获取订单的机型
                    MisDataFun.GetEngineType_Order(OpName,  out int EngineTypeID, out string EngineType, out string EngineID, out string OrderForm, out int parType, out int isCheckOk,out string msg2);
                    if (isCheckOk == 2)
                    {
                        string msg = "【" + OpName + "】" + msg2;
                        // 判断到线体上有滞留工件 ，让去管理系统进行处理
                        SendMessage.AsyncSendMessage_Notice(OpName, msg);
                        MisDataSaveDate.Helper.ApiLogHelper.SaveMesLog(OpName, "PartLoad " + msg, MisDataSaveDate.Helper.MesLogType.ERROR);
                        return;
                    }
                    else
                    {
                        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);

                        // 写入机型代码 （大机型） 机型 发动机号 订单号
                        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);
                        MIS_BASE.WritePLC(115, OpName, parType); // 托盘类型 1 静 2 动 3 拐        

                        // MES传递机型
                        MIS_BASE.Write_EngineTypeGetOver_MES(OpName, true);
                    }
                }
                else
                {
                    //上线工位清MES传递机型信号
                    MIS_BASE.Write_EngineTypeGetOver_MES(OpName, false);
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
            }
        }
    }
}
