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

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

                            //保存数据到线首表 先保存到  北汽福田变速器序列号总线上线  用于之前判断是否重号
                            if (Db.UpLoadOpNameInsert(EngineID, EngineTypeID, EngineType, OpName))
                            {
                                //保存质量数据
                                int qualityMask = MisDataSaveDateMesRead.Funtion.MesRead(OpName, tagValue.ToString(), msgEvent.TagID.ToString());
                                //保存物料数据
                                BaseDataSystemMES.Material_Insert(OpName, qualityMask);
                               
                                //保存过站数据
                                BaseDataSystemMES.WorkStartInsert(OpName, qualityMask);
                                // 是否二次工作
                                int isTowWork = 0;
                                if (OpName == "J07PA040")
                                {
                                    isTowWork = Convert.ToInt32(MIS_BASE.ReadPLC(64, OpName));
                                }
                                // 根据二次压装来判断计数编号是哪个
                                // 6.该订单计数加一
                                BaseDataSystemMES.newAddOrderCount(OpName, EngineID, OrderForm, isTowWork);
                                SendMessageFun.SendInfomation("FlushOverWorkList", "1", OpName);

                                // 是否为5s工件  5s走二次压装
                                bool is5SWork = false;
                                if (EngineTypeID == 2 || EngineTypeID == 4) is5SWork = true;

                                //// 只有PA020 与 PA040 的5s 一次压装 不设为1
                                //if (OpName != "J07PA020" && !(OpName == "J07PA040" && isTowWork == 0 && is5SWork))
                                //{
                                //    // 在保存后才将托盘号置位为1
                                //    BaseDataSystemMES.Set_TrayID(OpName, EngineID+"-2");
                                //    // 同时将对于上一个设备将其托盘号设为1
                                //    BaseDataSystemMES.Set_TrayID(OpName, EngineID);
                                //    //BaseDataSystemMES.Set_TrayIDByBackWork(OpName, EngineID);
                                //}
                                //推送质量数据到MES的页面
                                SendMessageFun.SendInfomation("ShowTimelyQuality", "1&" + qualityMask.ToString(), OpName);
                                //统计在线产量，在线节拍
                                SendMessage.SendActuallyCount(OpName, tagValue);

                                //通知设备，MES的质量数据（和物料数据）保存完成
                                MIS_BASE.Write_MesReadOver(OpName, true);
                                //MES工作完成
                                MIS_BASE.Write_MaterialVerifyOver(OpName, true);
                            }
                            else
                            {
                                SendMessage.AsyncSendMessage_Notice(OpName, "保存上线数据失败！");
                            }
                        }
                        else
                        {
                            MIS_BASE.Write_MesReadOver(OpName, false);
                        }
                    }

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

        }
    }
}
