using MesServerWork;
using MisDataFunDll;
using MisDataSaveDate;
using System;
using System.Data;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// OP431 保存：按 PLC 允许保存时间生成静置质量数据，正常过站后带质量数据出站。
        /// </summary>
        private void MesRead(object e)
        {
            try
            {
                var msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                string opName = Convert.ToString(msgEvent.OpName);
                int tagValue = Convert.ToInt32(msgEvent.TagValue);

                if (tagValue != 1)
                {
                    MIS_BASE.Write_MesReadOver(opName, false);
                    MIS_BASE.Write_MesWorkFinish(opName, false);
                    MIS_BASE.Write_OpNameIsOk(opName, -99);
                    MisDataFun.LogRecording_OP(opName, "INFO", "MIS", "PLC允许保存数据取消！");
                    return;
                }

                MisDataFun.LogRecording_OP(opName, "INFO", "MIS", "PLC允许保存OP431静置数据！");

                MIS_BASE.GetEngineTypeFromPLC(opName, out int engineTypeID, out string engineType, out string engineID, out int palletCode, out string orderForm);
                if (string.IsNullOrWhiteSpace(engineID))
                {
                    WriteManualStaticMesReadError(opName, "PLC产品码为空，无法保存静置数据");
                    return;
                }

                MisDataFun.Moby_Select_PDC(engineID, out orderForm, out engineType, out engineTypeID);
                if (engineTypeID <= 0)
                {
                    WriteManualStaticMesReadError(opName, "未查询到产品执行信息：" + engineID);
                    return;
                }

                MIS_BASE.Read_QualityMask(opName, out int qualityMask);
                qualityMask = NormalizeManualStaticQualityMask(qualityMask);

                if (!TryBuildManualStaticOutData(engineID, qualityMask, out ManualStaticOutData staticOutData, out string message))
                {
                    WriteManualStaticMesReadError(opName, message);
                    return;
                }

                if (!WriteManualStaticOutQualityData(opName, engineID, staticOutData, out message))
                {
                    WriteManualStaticMesReadError(opName, message);
                    return;
                }

                MisDataFun.WorkStartInsert(opName, qualityMask);
                MisDataFun.GetPartQualityGroupParams(opName, engineID, out DataTable dtQuality);
                if (dtQuality == null || dtQuality.Rows.Count == 0)
                {
                    WriteManualStaticMesReadError(opName, "未查询到OP431静置质量数据组，禁止静默出站：" + engineID);
                    return;
                }

                CallWebService.Call_dataCollectForSfcEx(opName, engineID, dtQuality, qualityMask, out string code, out string momMessage);
                if (code == "0")
                {
                    MIS_BASE.Write_OpNameIsOk(opName, qualityMask);
                }
                else if (code == "13921")
                {
                    qualityMask = 2;
                    MIS_BASE.Write_OpNameIsOk(opName, qualityMask);
                    MisDataFun.LogRecording_OP(opName, "INFO", "MOM", engineID + " 工厂MES质量判定不合格，按不合格完成OP431过站");
                }
                else
                {
                    WriteManualStaticMesReadError(opName, "出站失败：" + engineID + "，错误代码：" + code + "，消息：" + momMessage);
                    return;
                }

                MIS_BASE.Write_MesWorkFinish(opName, true);
                MIS_BASE.Write_MesReadOver(opName, true);

                MisDataFun.LogRecording_OP(opName, "INFO", "MIS", "MES保存OP431静置数据完成！");
                MisDataFun.LogRecording_OP(opName, "INFO", "MIS", "MES工作完成！");
            }
            catch (Exception err)
            {
                MyLog4Net.MyLogHelper.Error("Function1062.MesRead", err.Message);
                MisDataFun.LogRecording_OP(ManualStaticOutOpName, "ERROR", "MesRead", err.Message);
            }
        }

        private void WriteManualStaticMesReadError(string opName, string message)
        {
            MIS_BASE.Write_MesReadOver(opName, false);
            MIS_BASE.Write_MesWorkFinish(opName, false);
            MIS_BASE.Write_TagValueByTagTypeID(34, opName, true);
            SendMessage.AsyncSendMessage_Notice(opName, "【" + opName + "】" + message);
            MisDataFun.LogRecording_OP(opName, "ERROR", "MesRead", message);
        }
    }
}
