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);

                        MIS_BASE.Write_FixAllow(OpName, false);

                        if (tagValue == 1)
                        {
                            int qualityMask;
                            // 保存质量数据
                            qualityMask = MisDataSaveDateMesRead.Funtion.MesRead(OpName, tagValue.ToString(), msgEvent.TagID.ToString());


                            MIS_BASE.Read_EngineID(OpName, out string EngineID);
                            MIS_BASE.Read_TagTypeID(39, OpName, out string OrderForm);
                            // 分装线保存 扣数 
                            Db.SubLine_WorkFinish(EngineID, OpName, OrderForm, qualityMask);


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

                            // 测量数据发动机在线状态 过站信息

                            BaseDataSystemMES.WorkStartInsert(OpName, qualityMask);

                            // 读取当前选择的计划中的机型，写入PLC
                            // 获取订单的机型
                            string Program_No = "";
                            int planCount = 0; // 计划上料数量
                            int haveUplaodCount = 0; // 已上料数量
                            int remainingQuantity = 0; // 待上料数量
                            Db.GetEngineType_Order(OpName, Program_No, out int EngineTypeID_Program,
                                out string EngineType_Program, out string EngineID_Program, out string OrderForm_Program,
                                out int isCheckOk_Program, out planCount, out haveUplaodCount, out remainingQuantity);
                            if (isCheckOk_Program == 2)
                            {
                                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.WritePLC(95, OpName, planCount);
                                MIS_BASE.WritePLC(96, OpName, haveUplaodCount);
                                MIS_BASE.WritePLC(97, OpName, remainingQuantity);

                                //MIS_BASE.WritePLC(34, OpName, true);
                                string msg = "【" + OpName + "】" + "分装线无上线订单，请下达新订单！";
                                MyLog4Net.MyLogHelper.Error("PartLoad", msg);
                                SendMessage.AsyncSendMessage_Notice(OpName, msg);

                                Thread.Sleep(200);
                                MIS_BASE.Write_MaterialVerifyOver(OpName, true);
                                MIS_BASE.Write_MesReadOver(OpName, true);
                            }
                            else
                            {
                                // 写入机型代码 （大机型） 机型 发动机号 订单号
                                MIS_BASE.Write_EngineTypeID_MES(OpName, EngineTypeID_Program);
                                MIS_BASE.Write_EngineType_MES(OpName, EngineID_Program);
                                MIS_BASE.Write_OrderForm_MES(OpName, OrderForm_Program);

                                MIS_BASE.WritePLC(95, OpName, planCount);
                                MIS_BASE.WritePLC(96, OpName, haveUplaodCount);
                                MIS_BASE.WritePLC(97, OpName, remainingQuantity);

                                // 通知web显示
                                SendMessage.ShowMoby(OpName);
                                Thread.Sleep(200);
                                MIS_BASE.Write_MaterialVerifyOver(OpName, true);
                                MIS_BASE.Write_MesReadOver(OpName, true);
                            }

                            // 记录手动存储的物料数据
                            int byte1 = Convert.ToInt32(MIS_BASE.ReadPLC(211, OpName));
                            int byte2 = Convert.ToInt32(MIS_BASE.ReadPLC(202, OpName));
                            int byte3 = Convert.ToInt32(MIS_BASE.ReadPLC(203, OpName));
                            int byte4 = Convert.ToInt32(MIS_BASE.ReadPLC(204, OpName));
                            int byte5 = Convert.ToInt32(MIS_BASE.ReadPLC(205, OpName));
                            int byte6 = Convert.ToInt32(MIS_BASE.ReadPLC(206, OpName));
                            int byte7 = Convert.ToInt32(MIS_BASE.ReadPLC(207, OpName));
                            int byte8 = Convert.ToInt32(MIS_BASE.ReadPLC(208, OpName));
                            int byte9 = Convert.ToInt32(MIS_BASE.ReadPLC(209, OpName));
                            int byte10 = Convert.ToInt32(MIS_BASE.ReadPLC(210, OpName));
                            BaseDataSystemMES.HeLi_HMIMaterialFactory_Add(OpName, EngineID, OrderForm, byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8, byte9, byte10);

                            // 推送质量数据到MES的页面
                            // SendMessageFun.SendInfomation("ShowTimelyQuality", "1&" + qualityMask.ToString(), OpName);
                            // 对应手动工位的
                            // MIS_BASE.Write_ConfirmOkGo(OpName, true);


                            // 通知设备，MES的质量数据（和物料数据）保存完成

                            //统计在线产量，在线节拍
                            // SendMessage.SendActuallyCount(OpName, tagValue);
                        }
                        else
                        {
                            // 分装上线工位没有托盘到位离开，通过申请上线和保存完成代替
                            BaseDataSystemMES.OpName_MIS_MOBY_Insert(OpName, MobyTagType.WorkStartOut, DateTime.Now.ToString());
                            MIS_BASE.Write_MaterialVerifyOver(OpName, false);
                            MIS_BASE.Write_MesReadOver(OpName, false);
                            MIS_BASE.Write_ConfirmOkGo(OpName, false);
                            MIS_BASE.Write_FixAllow(OpName, false);
                            MIS_BASE.Write_ConfirmOkGo(OpName, false);

                            // 清传递机型时 去判断一下是否申请上线 还有 如果有就不清
                            int isEngineGetOver = Convert.ToInt32(MIS_BASE.ReadPLC(19, OpName));
                            if (isEngineGetOver == 0)
                            {
                                MIS_BASE.Write_EngineTypeGetOver_MES(OpName, false);
                            }
                            else
                            {
                                MyLog4Net.MyLogHelper.Error("MesRead", 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");
                }
            }

        }

    }
}
