using DataLinkMesWork1;
using MesServerWork;
using MisDataFunDll;
using System;
using System.Data.SqlClient;

namespace MesServerFunctions
{
    partial class FunctionMES
    {
        /// <summary>
        /// OP420 静置B位产品信息保存：64=1 保存开始静置信息，64=0 复位保存完成点位。
        /// </summary>
        private void ManualStaticBSave(object e)
        {
            var msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
            string opName = Convert.ToString(msgEvent.OpName);
            int tagValue = Convert.ToInt32(msgEvent.TagValue);

            try
            {
                if (opName != "OP420")
                {
                    return;
                }

                if (tagValue != 1)
                {
                    MIS_BASE.Write_TagValueByTagTypeID(31, opName, false);
                    MisDataFun.LogRecording_OP(opName, "INFO", "ManualStaticBSave", "PLC请求保存静置B位产品信息取消！");
                    return;
                }

                int engineTypeID;
                string engineType;
                string engineID;
                int palletCode;
                string orderForm;

                // 从 PLC 当前产品区读取 B 位产品信息，作为线下手动静置开始记录。
                MIS_BASE.GetEngineTypeFromPLC(opName, out engineTypeID, out engineType, out engineID, out palletCode, out orderForm);
                if (string.IsNullOrWhiteSpace(engineID))
                {
                    WriteManualStaticBSaveError(opName, "PLC当前产品码为空，无法记录静置B位产品信息");
                    return;
                }

                // PLC 可能只给产品码和机型代码，补查数据库中的产品型号。
                string dbEngineType;
                int dbEngineTypeID;
                MisDataFun.Moby_Select_PDC(engineID, out orderForm, out dbEngineType, out dbEngineTypeID);
                if (dbEngineTypeID > 0)
                {
                    engineTypeID = dbEngineTypeID;
                }
                if (!string.IsNullOrWhiteSpace(dbEngineType))
                {
                    engineType = dbEngineType;
                }

                string message;
                if (!MisDataFun.SaveManualStaticBRecord(engineID, engineType, engineTypeID, out message))
                {
                    WriteManualStaticBSaveError(opName, message);
                    return;
                }

                MIS_BASE.Write_TagValueByTagTypeID(31, opName, true);
                MisDataFun.LogRecording_OP(opName, "INFO", "ManualStaticBSave", "静置B位产品信息保存完成：" + engineID);
            }
            catch (Exception err)
            {
                MyLog4Net.MyLogHelper.Error("Function1055.ManualStaticBSave", err.Message);
                WriteManualStaticBSaveError(opName, err.Message);
            }
        }

       

        /// <summary>
        /// OP420 静置B位保存失败时复位完成点位并通知页面。
        /// </summary>
        private void WriteManualStaticBSaveError(string opName, string message)
        {
            MIS_BASE.Write_TagValueByTagTypeID(31, opName, false);
            MIS_BASE.Write_TagValueByTagTypeID(34, opName, true);
            SendMessage.AsyncSendMessage_Notice(opName, "【" + opName + "】" + message);
            MisDataFun.LogRecording_OP(opName, "ERROR", "ManualStaticBSave", message);
        }
    }
}