using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using MesServerWork;
using MisDataFunDll;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// 申请提前上料
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void ReqEarlyFeeding(object e)
        {
            try
            {
                DeviceDriver_BasicData.CustomeEvetnArgs msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                string OpName = Convert.ToString(msgEvent.OpName);
                int tagValue = Convert.ToInt32(msgEvent.TagValue);


                if (tagValue == 1)
                {
                    var dt = MisDataFun.Get_ReqEarlyFeedingData(OpName);
                    int result = Convert.ToInt32(dt.Rows[0]["result"]);
                    string msg = dt.Rows[0]["msg"].ToString();
                    int EngineTypeID = Convert.ToInt32(dt.Rows[0]["产品型号代码"]);
                    if (result == 1)
                    {
                        MIS_BASE.WritePLC(96, OpName, EngineTypeID); // 产品型号
                        MIS_BASE.Write_EngineID_MES(OpName, dt.Rows[0]["工件编号"].ToString());
                        int ReadEngineID = Convert.ToInt32(MIS_BASE.ReadPLC(96, OpName));
                        if (EngineTypeID != ReadEngineID)
                        {
                            MisDataSaveDate.Helper.ApiLogHelper.SaveMesLog(OpName, "ReqEarlyFeeding " + $"写入提前上料机型{EngineTypeID.ToString()} 与 读取到的{ReadEngineID.ToString()}不一致", MisDataSaveDate.Helper.MesLogType.ERROR);
                            return;
                        }
                        if (EngineTypeID == 0 || ReadEngineID == 0)
                        {
                            MisDataSaveDate.Helper.ApiLogHelper.SaveMesLog(OpName, "ReqEarlyFeeding " + $"写入提前上料机型{EngineTypeID.ToString()} 与 读取到的{ReadEngineID.ToString()} 其中存在空值", MisDataSaveDate.Helper.MesLogType.ERROR);
                            return;
                        }
                        MIS_BASE.WritePLC(33, OpName, true); // 允许提前上料

                    }
                    else if (result == 2)
                    {
                        MIS_BASE.WritePLC(33, OpName, false);
                        string msg2 = "【" + OpName + "】" + msg;
                        SendMessage.AsyncSendMessage_Notice(OpName, msg);
                        MisDataSaveDate.Helper.ApiLogHelper.SaveMesLog(OpName, "ReqEarlyFeeding " + msg2, MisDataSaveDate.Helper.MesLogType.ERROR);
                        return;
                    }
                }
                else
                {
                    MIS_BASE.WritePLC(96, OpName, 0); // 提前上料位置
                    MIS_BASE.WritePLC(33, OpName, false); // 允许提前上料
                    MIS_BASE.Write_EngineID_MES(OpName, "");
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function08:MesReadOver");
            }
        }
    }
}
