using bizFacade;
using MesServerWork;
using MesWork;
using System;
using System.Threading;
//using SystemFramework;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 允许保存(针对半自动和手动压装工位)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void MesRead(object e)
        {
            {
                try
                {
                    DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                    string OpName = Convert.ToString(msgEvent.OpName);
                    int tagValue = Convert.ToInt32(msgEvent.TagValue);

                    int EngineTypeID_MES;//机型代码
                    string EngineType_MES;//机型
                    string EngineID_MES;//工件编号
                    string OrderForm_MES;

                    if (tagValue == 1)
                    {
                        MIS_BASE.Read_EngineGetOver_PLC(OpName,out bool isEngineGetOver);
                        if (!isEngineGetOver)
                        {
                            string msg2 = "【" + OpName + "】" + "请先开始订单生产后再进行保存，当前未允许工作!";
                            SendMessage.AsyncSendMessage_Notice(OpName, msg2);
                            return;
                        }

                        MIS_BASE.GetEngineTypeFromPLC_MES(OpName, out EngineTypeID_MES, out EngineType_MES, out EngineID_MES, out OrderForm_MES);

                        MIS_BASE.Write_MesRead_PLC(OpName, true);

                        // 存追溯表数据
                        BaseDataSystemMES.WorkOrderOpWorkOver(OpName, EngineID_MES, OrderForm_MES);

                        // 保存物料数据
                        BaseDataSystemMES.Material_Insert(OpName, 1);

                        // 测量数据发动机在线状态
                        BaseDataSystemMES.WorkStartInsert(OpName, 1);

                        //对应手动工位的
                        //MIS_BASE.Write_ConfirmOkGo(OpName, true);
                        //通知设备，MES的质量数据（和物料数据）保存完成
                        //统计在线产量，在线节拍
                        SendMessage.SendActuallyCount(OpName, tagValue);

                        MIS_BASE.Write_MesReadOver(OpName, true);
                        MIS_BASE.Write_MaterialVerifyOver(OpName, true);

                        Thread.Sleep(1000 * 1);
                        MIS_BASE.Write_MesRead_PLC(OpName, false);

                        string msg = "【" + OpName + "】" + "工件数据保存完成!";
                        SendMessage.AsyncSendMessage_Notice(OpName, msg);
                    }
                    else
                    {
                        BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.WorkStartOut, DateTime.Now.ToString());
                        MIS_BASE.Write_MesReadOver(OpName, false);
                        MIS_BASE.Write_ConfirmOkGo(OpName, false);
                        MIS_BASE.Write_FixAllow(OpName, false);
                        MIS_BASE.Write_EngineTypeGetOver_MES(OpName, false);
                        //工作完成
                        MIS_BASE.Write_MaterialVerifyOver(OpName, false);
                        MIS_BASE.Write_EngineTypeID_MES(OpName, 0);
                        MIS_BASE.Write_EngineType_MES(OpName, "");
                        MIS_BASE.Write_EngineID_MES(OpName, "");
                        MIS_BASE.Write_OrderForm_MES(OpName, "");
                        MIS_BASE.Write_EngineTypeGetOver_PLC(OpName, false);
                        MIS_BASE.Write_WorkStart(OpName, false);

                        SendMessageFun.SendMessage("5", "1", OpName);
                    }
                }
                catch (Exception err)
                {
                    // 保存出错日志 1.申请上线 2.传递机型 3.允许保存 4.扫码
                    MisDataSaveDate.FuntionMesRead.Alarm_SaveFileLog4Error(System.Reflection.MethodBase.GetCurrentMethod().Name, e, "Fun1053", err);
                    ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
                }
            }

        }

    }
}
