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)
        {
            try
            {
                var msgEvent = (DeviceDriver_BasicData.CustomeEvetnArgs)e;
                string opName = Convert.ToString(msgEvent.OpName);
                int tagValue = Convert.ToInt32(msgEvent.TagValue);

                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 当前产品区已是最终 PACK 码，只补查订单和产品型号。
                MisDataFun.Moby_Select_PDC(engineID, out orderForm, out engineType, out engineTypeID);
                if (engineTypeID <= 0)
                {
                    WriteManualStaticBSaveError(opName, "未查询到产品执行信息：" + engineID);
                    return;
                }

                string message;
                if (!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("OP420", err.Message);
            }
        }

        /// <summary>
        /// 保存 OP420 静置B位开始记录，同一产品只保留最新一条。
        /// </summary>
        private bool SaveManualStaticBRecord(string engineID, string engineType, int engineTypeID, out string message)
        {
            message = "";
            SqlParameter[] sqlParameters = new SqlParameter[]
            {
                new SqlParameter("@产品编号", engineID),
                new SqlParameter("@产品型号", engineType),
                new SqlParameter("@产品型号代码", engineTypeID),
                new SqlParameter("@静置开始时间", DateTime.Now)
            };

            string errorMessage;
            DataAccess.ExecuteStoredProcedure("线下手动静置_记录_保存", ref sqlParameters, out errorMessage);
            if (!string.IsNullOrWhiteSpace(errorMessage))
            {
                message = "保存线下手动静置记录失败：" + errorMessage;
                return false;
            }

            return true;
        }

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