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

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

                if (tagValue == 1)
                {
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "申请上线！");
                    if (!MisDataFun.GetPartLoadIsAutoOnLineModel(OpName)) return;  // 判断 申请模式是否为自动申请

                    // 调用接口/或者本地生成模组码 或者 PACK码
                    CallWebService.Call_miReleaseSfcWithActivity(OpName, out string sfc, out string code, out string message);
                    string msg1 = "申请条码完成：" + sfc + "，code：" + code + "，message：" + message;
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MOM", msg1);
                    if (code != "0")
                    {
                        MIS_BASE.Write_TagValueByTagTypeID(34, OpName, true); // MES报警
                        string msg = "【" + OpName + "】" + msg1;
                        SendMessage.AsyncSendMessage_Notice(OpName, msg);
                        MisDataFun.LogRecording_OP(OpName, "ERROR", "PackPartLoad", msg);
                    }
                    else
                    {
                        MIS_BASE.Write_TagValueByTagTypeID(34, OpName, false); // 清除报警

                        // 这里就是Pack码~
                        string EngineID = sfc;

                        // 读取当前选择的计划中的机型, 发动机号，写入PLC
                        MisDataFun.GetEngineType_PackOrder(sfc, out int EngineTypeID, out string EngineType, out string OrderForm, out int isCheckOk, out string msg2);

                        // 1.机型信息写入数据库,记录工位开始
                        MIS_BASE.Read_PalletCode(OpName, out int PartPallet_Code);
                        MisDataFun.OpName_MIS_MOBY_Insert_FirstOpName(OpName, EngineTypeID, EngineTypeID, EngineType, EngineType, EngineID, EngineID, PartPallet_Code, OrderForm, OrderForm);

                        //第一步，将操作指南转移到操作指南_临时表中
                        MisDataFun.MesServer_ProcessList_EngineTypeOver(OpName);

                        //第二步，将零件信息转移到零件_临时表中
                        MisDataFun.MesServer_Process_EngineTypeOver(OpName);

                        //5 显示订单、机型信息
                        SendMessage.ShowMoby_FirstOpName(OpName);

                        // 3显示操作指导图片 PicShow_String_FirstOpName
                        // SendMessage.PicShow_FirstOpName(OpName);

                        //1 工序内容
                        SendMessage.ProcessList_FirstOpName(OpName);

                        // 2界面显示物料信息
                        SendMessage.MaterialList_FirstOpName(OpName);

                        string msg = "请扫描物料码！";
                        MisDataFun.LogRecording_OP(OpName, "INFO", "PackPartLoad", msg);
                        SendMessageFun.AsyncSendMessage_Notice(OpName, msg);

                        MIS_BASE.Write_EngineTypeID_MES(OpName, EngineTypeID); // 机型代码（MES）
                        MIS_BASE.Write_EngineID_MES(OpName, EngineID); // 发动机号（MES）
                        MIS_BASE.Write_EngineType_MES(OpName, EngineType); // 机型（MES）
                        MIS_BASE.Write_OrderForm_MES(OpName, OrderForm); // 订单号（MES）

                        MIS_BASE.Write_EngineTypeGetOver_MES(OpName, true);// 传递机型（MES）
                    }
                }
                else
                {
                    MisDataFun.LogRecording_OP(OpName, "INFO", "MIS", "申请上线取消！");
                    MIS_BASE.Write_EngineTypeGetOver_MES(OpName, false);// 传递机型（MES）
                }
            }
            catch (Exception err)
            {
                MyLog4Net.MyLogHelper.Error("PartLoad", err.Message.ToString());
                MisDataFun.LogRecording_OP("Function1051", "INFO", "PartLoad", err.Message.ToString());
            }
        }
    }
}
