using MesServerWork;
using MesWork;
using MisDataFunDll;
using System;
using System.Data;
using System.Threading;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 允许保存(针对半自动和手动压装工位)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void MesRead(object e)
        {
            {
                try
                {
                    DeviceDriver_BasicData.CustomeEvetnArgs msgEvent = (DeviceDriver_BasicData.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;
                    MisDataFun.MobySendWebAndSaveData(OpName, MisDataFun.MobyTagType.MesRead, "0");
                    Thread.Sleep(500);
                    MisDataFun.MobySendWebAndSaveData(OpName, MisDataFun.MobyTagType.MesRead, "1");
                    if (tagValue == 1)
                    {
                        bool isEngineGetOver;
                        DataRow mobyData = MisDataFun.GetMobyTableByOpName(OpName).Rows[0];
                        EngineTypeID_MES = Convert.ToInt32(mobyData["产品型号代码_MES"]);
                        EngineType_MES = mobyData["发动机型号_MES"].ToString();
                        EngineID_MES = mobyData["变速器序列号_MES"].ToString();
                        OrderForm_MES = mobyData["订单号_MES"].ToString();
                        isEngineGetOver = Convert.ToBoolean(mobyData["允许工作"]);

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

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

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

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

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

                        MisDataFun.MobySendWebAndSaveData(OpName, MisDataFun.MobyTagType.MesReadOver, "1");
                        Thread.Sleep(1000 * 1);                  
                        MisDataFun.MobySendWebAndSaveData(OpName, MisDataFun.MobyTagType.WorkStart, "0");
                        MisDataFun.MobySendWebAndSaveData(OpName, MisDataFun.MobyTagType.EngineTypeGetOverPLC, "0");
                        MisDataFun.MobySendWebAndSaveData(OpName, MisDataFun.MobyTagType.PartLoad, "0");
                        MisDataFun.MobySendWebAndSaveData(OpName, MisDataFun.MobyTagType.MesRead, "0");
                        MisDataFun.MobySendWebAndSaveData(OpName, MisDataFun.MobyTagType.MesReadOver, "0");
                        MisDataFun.MobySendWebAndSaveData(OpName, MisDataFun.MobyTagType.FixAllow, "0");


                        string msg = "【" + OpName + "】" + "工件数据保存完成!";
                        SendMessage.AsyncSendMessage_Notice(OpName, msg);
                    }
                }
                catch (Exception err)
                {
                    // 保存出错日志 1.申请上线 2.传递机型 3.允许保存 4.扫码
                    MisDataSaveDate.FuntionMesRead.Alarm_SaveFileLog4Error(System.Reflection.MethodBase.GetCurrentMethod().Name, e, "Fun1053", err);
                    ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
                }
            }

        }

    }
}
