using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using MesServerWork;
using MisDataFunDll;

namespace MisDataSaveDate
{
    public static class CallWebService
    {
        /// <summary>
        /// 释放模组/PACK号
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="sfc"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        public static void Call_miReleaseSfcWithActivity(string opName,out string sfc, out string code, out string message)
        {
            sfc = "";
            string apiName = "miReleaseSfcWithActivity";
            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, "", out string enable, out DataTable dtEnable);
            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "该工位未配置申请条码接口，将采用离线！");
                code = "500";
                message = "该工位未配置申请条码接口 却意外调用";
                return;
            }
            if(enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "申请条码接口被禁用，将采用本地方式生成模组码/PACK码!");
                // 读取当前选择的计划中的机型, 发动机号，写入PLC
                MisDataFun.GetEngineType_EngineID(opName, out sfc, out int isCheckOk, out string msg2);
                code = isCheckOk.ToString();
                message = msg2;
                return;
            }
            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 获取接口operation/resource参数
            MisDataFun.GetApiParams_operation(opName, out string operation, out string resource, out code, out message);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 准备接口参数
            MiReleaseSfcWithActivityParams miReleaseSfcWithActivityParams = new MiReleaseSfcWithActivityParams();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString(); // 获取参数名称列的值
                object paramValue = row["默认值"];        // 使用默认值

                switch (paramName)
                {
                    case "site":
                        miReleaseSfcWithActivityParams.site = paramValue?.ToString() ?? "";
                        break;

                    case "resource":
                        miReleaseSfcWithActivityParams.resource = resource;
                        break;

                    case "operation":
                        miReleaseSfcWithActivityParams.operation = operation;
                        break;

                    case "operationRevision":
                        miReleaseSfcWithActivityParams.operationRevision = paramValue?.ToString() ?? "";
                        break;

                    case "sfcQty":
                        miReleaseSfcWithActivityParams.sfcQty = paramValue?.ToString() ?? "";
                        break;

                    case "activity":
                        miReleaseSfcWithActivityParams.activity = paramValue?.ToString() ?? "";
                        break;

                    case "user":
                        miReleaseSfcWithActivityParams.user = paramValue?.ToString() ?? "";
                        break;

                    case "modeProcessSFC":
                        if (Enum.TryParse(paramValue?.ToString(), out ModeProcessSfc mode))
                        {
                            miReleaseSfcWithActivityParams.modeProcessSFC = mode;
                        }
                        else
                        {
                            miReleaseSfcWithActivityParams.modeProcessSFC = ModeProcessSfc.MODE_NONE;
                        }
                        break;

                }
            }
            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "调用模组码/PACK码生成接口");
            ND_Api.miReleaseSfcWithActivity(apiName, opName, miReleaseSfcWithActivityParams, out sfc, out code, out message);
            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "调用模组码/PACK码生成接口成功！");
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", "调用模组码/PACK码生成接口失败！原因：" + message);
            }
        }
        /// <summary>
        /// 进站接口（目前没有用到）
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="sfc"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        public static void Call_sfcStart(string opName, string sfc, out string code, out string message) 
        {
            string apiName = "sfcStart";
            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, sfc, out string enable, out DataTable dtEnable);
            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{sfc}该工位未配置进站接口，将采用离线模式！");
                code = "0";
                message = "";
                return;
            }
            MisDataFun.GetApiStatus_EngineID(apiName, opName, sfc, out string initCode, out string initTime);
            if (initCode == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{sfc}该产品已经进站，进站时间：" + initTime);
                code = "0";
                message = "";
                return;
            }
            if (enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{sfc}进站接口被禁用，将采用离线模式进站!");
                code = "0";
                message = "";
                return;
            }
            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 获取接口operation/resource参数
            MisDataFun.GetApiParams_operation(opName, out string operation, out string resource, out code, out message);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 准备接口参数
            SfcStart sfcStart = new SfcStart();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString(); // 获取参数名称列的值
                object paramValue = row["默认值"];        // 使用默认值

                switch (paramName)
                {
                    case "site":
                        sfcStart.site = paramValue?.ToString() ?? "";
                        break;

                    case "resource":
                        sfcStart.resource = resource;
                        break;

                    case "operation":
                        sfcStart.operation = operation;
                        break;

                    case "operationRevision":
                        sfcStart.operationRevision = paramValue?.ToString() ?? "";
                        break;

                    case "user":
                        sfcStart.user = paramValue?.ToString() ?? "";
                        break;
                }
            }
            sfcStart.sfcArray = sfc;
            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}调用进站接口");
            ND_Api.sfcStart(apiName, opName,  sfcStart, out code, out message);
            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}调用进站接口成功！");
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", $"{sfc}调用进站接口失败！原因：" + message);
            }

        }
        /// <summary>
        /// 进站接口
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="sfc"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        public static void Call_miFindCustomAndSfcData(string opName, string sfc, out string code, out string message)
        {
            string apiName = "miFindCustomAndSfcData";
            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, sfc, out string enable, out DataTable dtEnable);
            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{sfc}该工位未配置进站接口，将采用离线模式！");
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}进站成功");
                code = "0";
                message = "";
                return;
            }
            // 获取接口进站状态
            MisDataFun.GetApiStatus_EngineID(apiName, opName, sfc, out string initCode, out string initTime);
            if (initCode == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "该产品已经进站 且未出站 跳过重新进站，进站时间：" + initTime);
                code = "0";
                message = "";
                return;
            }
            if (enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{sfc}进站接口被禁用，将采用离线模式进站 !");
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc} 离线模式进站成功");
                code = "0";
                message = "";
                return;
            }
            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 获取接口operation/resource参数
            MisDataFun.GetApiParams_operation(opName, out string operation, out string resource, out code, out message);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 准备接口参数
            MiFindCustomAndSfcData miFindCustomAndSfcData = new MiFindCustomAndSfcData();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString(); // 获取参数名称列的值
                object paramValue = row["默认值"];        // 使用默认值

                switch (paramName)
                {
                    case "site":
                        miFindCustomAndSfcData.site = paramValue?.ToString() ?? "";
                        break;
                    case "resource":
                        miFindCustomAndSfcData.resource = resource;
                        break;
                    case "operation":
                        miFindCustomAndSfcData.operation = operation;
                        break;
                    case "operationRevision":
                        miFindCustomAndSfcData.operationRevision = paramValue?.ToString() ?? "";
                        break;
                    case "user":
                        miFindCustomAndSfcData.user = paramValue?.ToString() ?? "";
                        break;
                    case "masterDataArray":
                        miFindCustomAndSfcData.masterDataArray = paramValue?.ToString() ?? "";
                        break;
                    case "activity":
                        miFindCustomAndSfcData.activity = paramValue?.ToString() ?? "";
                        break;
                    case "modeProcessSFC":
                        if (Enum.TryParse(paramValue?.ToString(), out ModeProcessSfc mode))
                        {
                            miFindCustomAndSfcData.modeProcessSfc = mode;
                        }
                        else
                        {
                            miFindCustomAndSfcData.modeProcessSfc = ModeProcessSfc.MODE_NONE;
                        }
                        break;

                }
            }
            miFindCustomAndSfcData.sfc = sfc;
            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}调用进站接口");
            ND_Api.miFindCustomAndSfcData(apiName, opName, miFindCustomAndSfcData, out code, out message);
            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}调用进站接口成功！");
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", $"{sfc}调用进站接口失败！原因：" + message);
            }
        }
        /// <summary>
        /// 进站接口（目前没有用到）
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="sfc"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        public static void Call_miCheckSFCStatusEx(string opName, string sfc, out string code, out string message)
        {
            string apiName = "miCheckSFCStatusEx";
            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, sfc, out string enable, out DataTable dtEnable);
            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "该工位未配置进站接口，将采用离线模式！");
                code = "500";
                message = "该工位未配置进站接口 却意外调用";
                return;
            }
            if (enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "进站接口被禁用，将采用离线模式进站!");
                code = "0";
                message = "";
                return;
            }
            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 获取接口operation/resource参数
            MisDataFun.GetApiParams_operation(opName, out string operation, out string resource, out code, out message);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 准备接口参数
            MiCheckSFCStatusEx miCheckSFCStatusEx = new MiCheckSFCStatusEx();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString(); // 获取参数名称列的值
                object paramValue = row["默认值"];        // 使用默认值

                switch (paramName)
                {
                    case "site":
                        miCheckSFCStatusEx.site = paramValue?.ToString() ?? "";
                        break;
                    case "operation":
                        miCheckSFCStatusEx.operation = operation;
                        break;
                    case "operationRevision":
                        miCheckSFCStatusEx.operationRevision = paramValue?.ToString() ?? "";
                        break;
                }
            }
            miCheckSFCStatusEx.sfc = sfc;
            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "调用进站接口");
            ND_Api.miCheckSFCStatusEx(apiName, opName, miCheckSFCStatusEx, out code, out message);
            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "调用进站接口成功！");
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", "调用进站接口失败！原因：" + message);
            }

        }
        /// <summary>
        /// 物料装配接口
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="sfc"></param>
        /// <param name="barcode"></param>
        /// <param name="inventory"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        public static void Call_miAssmebleAndCollectDataForSfc(string opName, string sfc, string barcode,string inventory, out string code, out string message) 
        {
            // 只有一个时使用BarCode
            if(barcode == "" && inventory != "")
            {
                barcode = inventory;
                inventory = "";
            }
            string apiName = "miAssmebleAndCollectDataForSfc";
            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, sfc, out string enable, out DataTable dtEnable);
            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{sfc}该工位未配置物料装配接口 却意外调用，将采用离线模式！");
                code = "0";
                message = "";
                return;
            }
            if (enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{sfc}物料装配接口被禁用，将采用离线模式进行校验!");
                code = "0";
                message = "";
                return;
            }
            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 获取接口operation/resource参数
            MisDataFun.GetApiParams_operation(opName, out string operation, out string resource, out code, out message);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 准备接口参数
            MiAssmebleAndCollectDataForSfc miAssmebleAndCollectDataForSfc = new MiAssmebleAndCollectDataForSfc();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString(); // 获取参数名称列的值
                object paramValue = row["默认值"];        // 使用默认值

                switch (paramName)
                {
                    case "site":
                        miAssmebleAndCollectDataForSfc.site = paramValue?.ToString() ?? "";
                        break;

                    case "resource":
                        miAssmebleAndCollectDataForSfc.resource = resource;
                        break;

                    case "operation":
                        miAssmebleAndCollectDataForSfc.operation = operation;
                        break;

                    case "operationRevision":
                        miAssmebleAndCollectDataForSfc.operationRevision = paramValue?.ToString() ?? "";
                        break;

                    case "user":
                        miAssmebleAndCollectDataForSfc.user = paramValue?.ToString() ?? "";
                        break;

                    case "activityId":
                        miAssmebleAndCollectDataForSfc.activityId = paramValue?.ToString() ?? "";
                        break;
                    case "modeProcessSFC":
                        if (Enum.TryParse(paramValue?.ToString(), out ModeProcessSfc mode))
                        {
                            miAssmebleAndCollectDataForSfc.modeProcessSFC = mode;
                        }
                        else
                        {
                            miAssmebleAndCollectDataForSfc.modeProcessSFC = ModeProcessSfc.MODE_NONE;
                        }
                        break;
                    case "partialAssembly":
                        miAssmebleAndCollectDataForSfc.partialAssembly = paramValue?.ToString() ?? "";
                        break;
                }
            }
            miAssmebleAndCollectDataForSfc.sfc = sfc;
            List<InventoryItem> inventoryArray = new List<InventoryItem>();
            if (inventory == "")
            {
                inventoryArray.Add(new InventoryItem() { inventory = barcode, qty = "1" });
            }
            else
            {
                AssemblyDataField assemblyDataField1 = new AssemblyDataField()
                {
                    attribute = "ATLID",
                    value = inventory,
                };
                AssemblyDataField assemblyDataField2 = new AssemblyDataField()
                {
                    attribute = "EXTERNAL_SERIAL",
                    value = barcode,
                };
                List<AssemblyDataField> assemblyDataFields = new List<AssemblyDataField>();
                assemblyDataFields.Add(assemblyDataField1);
                assemblyDataFields.Add(assemblyDataField2);
                inventoryArray.Add(new InventoryItem() { inventory = inventory, qty = "1",assemblyDataFields= assemblyDataFields });
            }
            miAssmebleAndCollectDataForSfc.inventoryArray = inventoryArray;
            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "调用物料装配接口"); // 只输出结果
            ND_Api.miAssmebleAndCollectDataForSfc(apiName, opName, miAssmebleAndCollectDataForSfc, out code, out message);
            if(code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}调用物料装配接口成功！");
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", $"{sfc}调用物料装配接口失败！原因："  + message);
            }
        }
        /// <summary>
        /// 批次物料校验接口
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="sfc"></param>
        /// <param name="inventory"></param>
        /// <param name="component"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        public static void Call_miCheckBOMInventory(string opName, string sfc, string inventory, string component, out string code, out string message)
        {
            string apiName = "miCheckBOMInventory";
            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, sfc, out string enable, out DataTable dtEnable);
            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{sfc}该工位未配置批次物料校验接口，将采用离线模式！");
                code = "500";
                message = "该工位未配置批次物料校验接口 却意外调用";
                return;
            }
            if (enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{sfc}批次物料校验接口被禁用!");
                code = "0";
                message = "";
                return;
            }
            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 获取接口operation/resource参数
            MisDataFun.GetApiParams_operation(opName, out string operation, out string resource, out code, out message);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 准备接口参数
            MiCheckBOMInventory miCheckBOMInventory = new MiCheckBOMInventory();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString(); // 获取参数名称列的值
                object paramValue = row["默认值"];        // 使用默认值

                switch (paramName)
                {
                    case "site":
                        miCheckBOMInventory.site = paramValue?.ToString() ?? "";
                        break;
                    case "user":
                        miCheckBOMInventory.user = paramValue?.ToString() ?? "";
                        break;
                    case "activity":
                        miCheckBOMInventory.activity = paramValue?.ToString() ?? "";
                        break;
                    case "resource":
                        miCheckBOMInventory.resource = resource;
                        break;
                    case "operation":
                        miCheckBOMInventory.operation = operation;
                        break;
                    case "operationRevision":
                        miCheckBOMInventory.operationRevision = paramValue?.ToString() ?? "";
                        break;
                    case "modeCheckOperation":
                        miCheckBOMInventory.modeCheckOperation = paramValue?.ToString() ?? "";
                        break;
                }
            }
            miCheckBOMInventory.sfc = sfc;
            miCheckBOMInventory.inventory = inventory;
            miCheckBOMInventory.component = component;
            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}调用批次物料校验接口");
            ND_Api.miCheckBOMInventory(apiName, opName, miCheckBOMInventory, out code, out message);
            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}调用批次物料校验接口成功！");
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", $"{sfc}调用批次物料校验接口失败！原因：" + message);
            }
        }
        /// <summary>
        /// 出站收数据接口 带质量数据
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="sfc"></param>
        /// <param name="dt_quality"></param>
        /// <param name="qualityMask"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        public static void Call_dataCollectForSfcEx(string opName, string sfc, DataTable dt_quality, int qualityMask, out string code, out string message)
        {

            string apiName = "dataCollectForSfcEx";
            if(dt_quality.Rows.Count == 0)
            {
                code = "99999";
                message = $"{sfc}出站失败，PLCS未查询到质量数据组！";
                return;
            }
            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, sfc, out string enable, out DataTable dtEnable);
            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{sfc}该工位未配置出站接口，将采用离线模式！");
                code = "0";
                message = "";
                return;
            }
            // 获取接口出站状态
            //MisDataFun.GetApiStatus_EngineID(apiName, opName, sfc, out string initCode, out string initTime);
            //if (initCode == "0")
            //{
            //    MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "该产品已经出站，出站时间：" + initTime);
            //    code = "0";
            //    message = "";
            //    return;
            //}
            //if (initCode == "13921")
            //{
            //    MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "该产品不合格出站 处理完NC后请不合格放行 NC时间：" + initTime);
            //    code = "0";
            //    message = "";
            //    return;
            //}
            if (enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{sfc}出站接口被禁用!");
                code = "0";
                message = "";
                return;
            }
            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 获取接口operation/resource参数
            MisDataFun.GetApiParams_operation(opName, out string operation, out string resource, out code, out message);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 准备接口参数
            DataCollectForSfcEx dataCollectForSfcEx = new DataCollectForSfcEx();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString(); // 获取参数名称列的值
                object paramValue = row["默认值"];        // 使用默认值

                switch (paramName)
                {
                    case "site":
                        dataCollectForSfcEx.site = paramValue?.ToString() ?? "";
                        break;
                    case "dcGroup":
                        dataCollectForSfcEx.dcGroup = paramValue?.ToString() ?? "";
                        break;
                    case "dcGroupRevision":
                        dataCollectForSfcEx.dcGroupRevision = paramValue?.ToString() ?? "";
                        break;
                    case "operation":
                        dataCollectForSfcEx.operation = operation;
                        break;
                    case "operationRevision":
                        dataCollectForSfcEx.operationRevision = paramValue?.ToString() ?? "";
                        break;
                    case "resource":
                        dataCollectForSfcEx.resource = resource;
                        break;
                    case "user":
                        dataCollectForSfcEx.user = paramValue?.ToString() ?? "";
                        break;
                    case "activityId":
                        dataCollectForSfcEx.activityId = paramValue?.ToString() ?? "";
                        break;
                    case "modeProcessSfc":
                        if (Enum.TryParse(paramValue?.ToString(), out ModeProcessSfc mode))
                        {
                            dataCollectForSfcEx.modeProcessSfc = mode;
                        }
                        else
                        {
                            dataCollectForSfcEx.modeProcessSfc = ModeProcessSfc.MODE_NONE;
                        }
                        break;
                }
            }
            dataCollectForSfcEx.sfc = sfc;

            if (opName == "OP540")
            {
                dataCollectForSfcEx.modeProcessSfc = ModeProcessSfc.MODE_NONE;
                bool isCapacityTest = false;
                foreach (DataRow qualityRow in dt_quality.Rows)
                {
                    if (qualityRow["name"].ToString().IndexOf("FDRL", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        isCapacityTest = true;
                        break;
                    }
                }
                // 判断是容量还是发包
                if (isCapacityTest)
                {
                    dataCollectForSfcEx.dcGroup = "*A"; // 容量测试
                    MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}OP540数据采集判断为容量测试");
                }
                else
                {
                    dataCollectForSfcEx.dcGroup = "*C"; // 发包测试
                    MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}OP540数据采集判断为发包测试");
                }
            }

            List<ParametricData> parametricDataArray = new List<ParametricData>();
            for (int i = 0; i < dt_quality.Rows.Count; i++)
            {
                parametricDataArray.Add(new ParametricData()
                {
                    name = dt_quality.Rows[i]["name"].ToString(),
                    value = dt_quality.Rows[i]["value"].ToString(),
                    dataType = dt_quality.Rows[i]["dataType"].ToString()
                });
            }
            dataCollectForSfcEx.parametricDataArray = parametricDataArray;
            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}调用出站接口");
            ND_Api.dataCollectForSfcEx(apiName, opName, dataCollectForSfcEx, out code, out message);
            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}调用出站接口成功！");
            }
            else if (code == "13921") // 质量判定不合格 走不合格放行
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", $"{sfc}调用出站接口成功 接口质量判定不合格！ " + message);
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", $"{sfc}调用出站接口失败！原因：" + message);
            }
        }
        /// <summary>
        /// 出站收数据接口（测试设备，目前没有用到）
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="sfc"></param>
        /// <param name="dt_quality"></param>
        /// <param name="qualityMask"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        public static void Call_dataCollectForMoudleTest(string opName, string sfc, DataTable dt_quality, int qualityMask, out string code, out string message)
        {

            string apiName = "dataCollectForMoudleTest";
            //if (qualityMask != 1)
            //{
            //    MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "不合格出站，不调用接口!");
            //    code = "0";
            //    message = "";
            //    return;
            //}
            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, sfc, out string enable, out DataTable dtEnable);
            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "该工位未配置出站接口，将采用离线模式！");
                code = "500";
                message = "该工位未配置出站接口 却意外调用";
                return;
            }
            // 获取接口出站状态
            MisDataFun.GetApiStatus_EngineID(apiName, opName, sfc, out string initCode, out string initTime);
            if (initCode == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "该产品已经出站，出站时间：" + initTime);
                code = "0";
                message = "";
                return;
            }
            enable = "1";
            if (enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "出站接口被禁用!");
                code = "0";
                message = "";
                return;
            }
           
            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 获取接口operation/resource参数
            MisDataFun.GetApiParams_operation(opName, out string operation, out string resource, out code, out message);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 准备接口参数
            DataCollectForMoudleTest dataCollectForMoudleTest = new DataCollectForMoudleTest();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString(); // 获取参数名称列的值
                object paramValue = row["默认值"];        // 使用默认值

                switch (paramName)
                {
                    case "site":
                        dataCollectForMoudleTest.site = paramValue?.ToString() ?? "";
                        break;
                    case "dcGroup":
                        dataCollectForMoudleTest.dcGroup = paramValue?.ToString() ?? "";
                        break;
                    case "dcGroupRevision":
                        dataCollectForMoudleTest.dcGroupRevision = paramValue?.ToString() ?? "";
                        break;
                    case "operation":
                        dataCollectForMoudleTest.operation = operation;
                        break;
                    case "operationRevision":
                        dataCollectForMoudleTest.operationRevision = paramValue?.ToString() ?? "";
                        break;
                    case "resource":
                        dataCollectForMoudleTest.resource = resource;
                        break;
                    case "user":
                        dataCollectForMoudleTest.user = paramValue?.ToString() ?? "";
                        break;
                    case "activityId":
                        dataCollectForMoudleTest.activityId = paramValue?.ToString() ?? "";
                        break;
                    case "modeProcessSfc":
                        if (Enum.TryParse(paramValue?.ToString(), out ModeProcessSfc mode))
                        {
                            dataCollectForMoudleTest.modeProcessSfc = mode;
                        }
                        else
                        {
                            dataCollectForMoudleTest.modeProcessSfc = ModeProcessSfc.MODE_NONE;
                        }
                        break;
                }
            }
            dataCollectForMoudleTest.sfc = sfc;
            List<ParametricData> parametricDataArray = new List<ParametricData>();
            for (int i = 0; i < dt_quality.Rows.Count; i++)
            {
                parametricDataArray.Add(new ParametricData() { 
                    name = dt_quality.Rows[i]["name"].ToString(), 
                    value = dt_quality.Rows[i]["value"].ToString(),
                    dataType = dt_quality.Rows[i]["dataType"].ToString()
                });
            }
            dataCollectForMoudleTest.parametricDataArray = parametricDataArray;
            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "调用出站接口");
            ND_Api.dataCollectForMoudleTest(apiName, opName, dataCollectForMoudleTest, out code, out message);
            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "调用出站接口成功！");
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", "调用出站接口失败！原因：" + message);
            }

        }
        /// <summary>
        /// 出站收数据接口（无质量数据用到）
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="sfc"></param>
        /// <param name="qualityMask"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        public static void Call_sfcComplete(string opName, string sfc, int qualityMask, out string code, out string message)
        {
            string apiName = "sfcComplete";
            //if (qualityMask != 1)
            //{
            //    MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "不合格出站，不调用接口!");
            //    code = "0";
            //    message = "";
            //    return;
            //}
            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, sfc, out string enable, out DataTable dtEnable);
            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", $"{sfc}该工位未配置 无质量数据 出站接口，却意外调用！");
                code = "0";
                message = "";
                return;
            }
            // 获取接口出站状态
                //MisDataFun.GetApiStatus_EngineID(apiName, opName, sfc, out string initCode, out string initTime);
                //if (initCode == "0")
                //{
                //    MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "该产品已经出站，出站时间：" + initTime);
                //    code = "0";
                //    message = "";
                //    return;
                //}
            if (enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{sfc}出站接口被禁用! 采用离线模式");
                code = "0";
                message = "";
                return;
            }
            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 获取接口operation/resource参数
            MisDataFun.GetApiParams_operation(opName, out string operation, out string resource, out code, out message);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 准备接口参数  
            SfcComplete sfcComplete = new SfcComplete();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString(); // 获取参数名称列的值
                object paramValue = row["默认值"];        // 使用默认值

                switch (paramName)
                {
                    case "site":
                        sfcComplete.site = paramValue?.ToString() ?? "";
                        break;
                    case "operation":
                        sfcComplete.operation = operation;
                        break;
                    case "operationRevision":
                        sfcComplete.operationRevision = paramValue?.ToString() ?? "";
                        break;
                    case "resource":
                        sfcComplete.resource = resource;
                        break;
                    case "user":
                        sfcComplete.user = paramValue?.ToString() ?? "";
                        break;
                }
            }
            sfcComplete.sfcArray = sfc;
            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}调用出站接口");
            ND_Api.sfcComplete(apiName, opName, sfcComplete, out code, out message);
            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{sfc}调用出站接口成功！");
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", $"{sfc}调用出站接口失败！原因：" + message);
            }

        }
        /// <summary>
        /// 电芯校验接口
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="inventory"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        public static void Call_miCheckInventoryAttributes(string opName, string inventory, out string code, out string message)
        {
            string apiName = "miCheckInventoryAttributes";
            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, "", out string enable, out DataTable dtEnable);
            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "该工位未配置电芯校验接口，将采用离线模式！");
                code = "500";
                message = "该工位未配置电芯校验接口 却意外调用";
                return;
            }
            if (enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "电芯校验接口被禁用，将默认全部合格！");
                code = "0";
                message = "";
                return;
            }
            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 获取接口operation/resource参数
            MisDataFun.GetApiParams_operation(opName, out string operation, out string resource, out code, out message);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 准备接口参数  
            MiCheckInventoryAttributes miCheckInventoryAttributes = new MiCheckInventoryAttributes();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString(); // 获取参数名称列的值
                object paramValue = row["默认值"];        // 使用默认值

                switch (paramName)
                {
                    case "sfc":
                        miCheckInventoryAttributes.sfc = paramValue?.ToString() ?? "";
                        break;
                    case "site":
                        miCheckInventoryAttributes.site = paramValue?.ToString() ?? "";
                        break;
                    case "operation":
                        miCheckInventoryAttributes.operation = operation;
                        break;
                    case "operationRevision":
                        miCheckInventoryAttributes.operationRevision = paramValue?.ToString() ?? "";
                        break;
                    case "resource":
                        miCheckInventoryAttributes.resource = resource;
                        break;
                    case "user":
                        miCheckInventoryAttributes.user = paramValue?.ToString() ?? "";
                        break;
                    case "activityId":
                        miCheckInventoryAttributes.activityId = paramValue?.ToString() ?? "";
                        break;
                    case "modeCheckInventory":
                        if (Enum.TryParse(paramValue?.ToString(), out ModeCheckInventory mode))
                        {
                            miCheckInventoryAttributes.modeCheckInventory = mode;
                        }
                        else
                        {
                            miCheckInventoryAttributes.modeCheckInventory = ModeCheckInventory.MODE_MARK_ONLY;
                        }
                        break;
                }
            }
            List<string> inventoryArray = new List<string>();
            inventoryArray.Add(inventory);
            miCheckInventoryAttributes.inventoryArray = inventoryArray;

            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{inventory}调用电芯校验接口");
            ND_Api.miCheckInventoryAttributes(apiName, opName, miCheckInventoryAttributes, out code, out message);
            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"{inventory}调用电芯校验成功！");
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", $"{inventory}调用电芯校验失败！原因：" + message);
            }

        }
        /// <summary>
        /// 电芯K值计算接口（目前没有用到，OCV设备自己写了）
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="inventoryArray"></param>
        /// <param name="dt_quality"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        /// <param name="checkResult"></param>
        public static void Call_cellCustomDCCheck(string opName, string [] inventoryArray, DataTable dt_quality, out string code, out string message, out Dictionary<string, string> checkResult) 
        {
            string apiName = "cellCustomDCCheck";
            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, "", out string enable, out DataTable dtEnable);
            checkResult = new Dictionary<string, string>();
            foreach (string key in inventoryArray)
            {
                checkResult[key] = "0";
            }
            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "该工位未配置电芯K值计算接口，将采用离线模式！");
                code = "500";
                message = "该工位未配置K值计算接口 却意外调用";
                return;
            }
            if (enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "电芯K值计算接口被禁用，将默认全部为0！");
                code = "0";
                message = "";
                foreach (string key in inventoryArray)
                {
                    checkResult.Add(key, "0");
                }
                return;
            }
            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 获取接口operation/resource参数
            MisDataFun.GetApiParams_operation(opName, out string operation, out string resource, out code, out message);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 准备接口参数  
            CellCustomDCCheck cellCustomDCCheck = new CellCustomDCCheck();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString(); // 获取参数名称列的值
                object paramValue = row["默认值"];        // 使用默认值

                switch (paramName)
                {
                    case "dcSequence":
                        cellCustomDCCheck.dcSequence = paramValue?.ToString() ?? "";
                        break;
                    case "site":
                        cellCustomDCCheck.site = paramValue?.ToString() ?? "";
                        break;
                    case "operation":
                        cellCustomDCCheck.operation = operation;
                        break;
                    case "Multispec":
                        cellCustomDCCheck.Multispec = paramValue?.ToString() ?? "";
                        break;
                    case "resource":
                        cellCustomDCCheck.resource = resource;
                        break;
                    case "user":
                        cellCustomDCCheck.user = paramValue?.ToString() ?? "";
                        break;
                       
                }
            }
            List<Inventory> inventoryList = new List<Inventory>();
            foreach (string key in inventoryArray)
            {
                List<InventoryData> inventoryDatalist = new List<InventoryData>();

                for (int i = 0; i < dt_quality.Rows.Count; i++) 
                {
                    string EngineID = dt_quality.Rows[i]["EngineID"].ToString();
                    string name = dt_quality.Rows[i]["name"].ToString();
                    string value = dt_quality.Rows[i]["value"].ToString();
                    if (EngineID == key)
                    {
                        inventoryDatalist.Add(new InventoryData() { name = name, value = value });
                    }
                }

                inventoryList.Add(new Inventory() { inventoryId = key, marking = "", inventoryDatalist = inventoryDatalist });
            }
            cellCustomDCCheck.inventoryList = inventoryList;
            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "调用电芯接口");
            ND_Api.cellCustomDCCheck(apiName, opName, cellCustomDCCheck, out code, out message);
            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "调用电芯接口成功！");
                foreach (string key in inventoryArray)
                {
                    checkResult[key] = "1";
                }
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", "调用电芯接口失败！原因：" + message);
            }
        }

        /// <summary>
        /// 电芯K值计算接口（单颗电芯版本）
        /// </summary>
        /// <param name="opName">工位名称</param>
        /// <param name="inventoryId">单个电芯ID</param>
        /// <param name="dt_quality">质量数据表</param>
        /// <param name="code">返回代码</param>
        /// <param name="message">返回消息</param>
        /// <param name="checkResult">检查结果（0或1）</param>
        public static void Call_cellCustomOneDCCheck(string opName, string inventoryId, DataTable dt_quality, out string code, out string message, out string checkResult)
        {
            string apiName = "cellCustomDCCheck";
            checkResult = "0";

            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, "", out string enable, out DataTable dtEnable);

            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "该工位未配置电芯K值计算接口，将采用离线模式！");
                code = "500";
                message = "该工位未配置电芯K值计算接口 却意外调用";
                return;
            }

            if (enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "电芯K值计算接口被禁用，将默认为0！");
                code = "0";
                message = "";
                checkResult = "0";
                return;
            }

            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }

            // 获取接口operation/resource参数
            MisDataFun.GetApiParams_operation(opName, out string operation, out string resource, out code, out message);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }

            // 准备接口参数  
            CellCustomDCCheck cellCustomDCCheck = new CellCustomDCCheck();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString();
                object paramValue = row["默认值"];

                switch (paramName)
                {
                    case "dcSequence":
                        cellCustomDCCheck.dcSequence = paramValue?.ToString() ?? "";
                        break;
                    case "site":
                        cellCustomDCCheck.site = paramValue?.ToString() ?? "";
                        break;
                    case "operation":
                        cellCustomDCCheck.operation = operation;
                        break;
                    case "Multispec":
                        cellCustomDCCheck.Multispec = paramValue?.ToString() ?? "";
                        break;
                    case "resource":
                        cellCustomDCCheck.resource = resource;
                        break;
                    case "user":
                        cellCustomDCCheck.user = paramValue?.ToString() ?? "";
                        break;
                }
            }

            // 为单个电芯构建数据
            List<InventoryData> inventoryDatalist = new List<InventoryData>();
            for (int i = 0; i < dt_quality.Rows.Count; i++)
            {
                string name = dt_quality.Rows[i]["name"].ToString();
                string value = dt_quality.Rows[i]["value"].ToString();
                inventoryDatalist.Add(new InventoryData() { name = name, value = value });
            }

            List<Inventory> inventoryList = new List<Inventory>
            {
                new Inventory()
                {
                    inventoryId = inventoryId,
                    marking = "",
                    inventoryDatalist = inventoryDatalist
                }
            };

            cellCustomDCCheck.inventoryList = inventoryList;

            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"调用电芯接口，电芯ID：{inventoryId}");
            ND_Api.cellCustomDCCheck(apiName, opName, cellCustomDCCheck, out code, out message);
            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"调用电芯接口成功！电芯ID：{inventoryId}");
                checkResult = "1";
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", $"调用电芯接口失败！电芯ID：{inventoryId}，原因：{message}");
                checkResult = "0";
            }
        }

        /// <summary>
        /// 获取打印配置接口
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="sfc"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        /// <param name="labelContent"></param>
        public static void Call_getAllLabelContents(string opName, string sfc, out string code, out string message, out LabelContent labelContent) 
        {
            string apiName = "getAllLabelContents";
            labelContent = new LabelContent();
            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, sfc, out string enable, out DataTable dtEnable);
            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "该工位未配置打印配置接口，将采用离线模式！");
                code = "500";
                message = "该工位未配置打印配置接口 却意外调用";
                return;
            }
            if (enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "打印配置接口被禁用!");
                code = "0";
                message = "";
                labelContent.Sfc = sfc;
                LabelField labelField1 = new LabelField() { Field="TEXT1",Value="1P4S",Formula= "#ITEM" };
                LabelField labelField2 = new LabelField() { Field="TEXT2",Value="LFP",Formula= "#ITEM" };
                LabelField labelField3 = new LabelField() { Field="TEXT3",Value= "P000103005", Formula= "#ITEM" };
                if(opName == "OP250")
                {
                    labelField1 = new LabelField() { Field = "TEXT1", Value = "PDC2.0", Formula = "#ITEM" };
                    labelField2 = new LabelField() { Field = "TEXT2", Value = "PACK", Formula = "#ITEM" };
                    labelField3 = new LabelField() { Field = "TEXT3", Value = "P000107123", Formula = "#ITEM" }; ;
                }
                if (opName == "OP490-3")
                {
                    labelContent.LabelFields.Add(new LabelField() { Field = "TEXT9", Value = "999", Formula = "#ITEM" });
                    labelContent.LabelFields.Add(new LabelField() { Field = "TEXT14", Value = "999", Formula = "#ITEM" });
                    labelContent.LabelFields.Add(new LabelField() { Field = "TEXT16", Value = "999", Formula = "#ITEM" });
                }
                labelContent.LabelFields.Add(labelField1);
                labelContent.LabelFields.Add(labelField2);
                labelContent.LabelFields.Add(labelField3);
                return;
            }
            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 准备接口参数  
            GetAllLabelContents getAllLabelContents = new GetAllLabelContents();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString(); // 获取参数名称列的值
                object paramValue = row["默认值"];        // 使用默认值

                switch (paramName)
                {
                    case "site":
                        getAllLabelContents.site = paramValue?.ToString() ?? "";
                        break;
                    case "item":
                        getAllLabelContents.item = paramValue?.ToString() ?? "";
                        break;
                    case "languageCode":
                        getAllLabelContents.languageCode = paramValue?.ToString() ?? "";
                        break;
                    case "Y":
                        getAllLabelContents.Y = paramValue?.ToString() ?? "";
                        break;
                    case "activity":
                        getAllLabelContents.activity = paramValue?.ToString() ?? "";
                        break;

                }
            }
            getAllLabelContents.sfcList = sfc;
            if (opName == "OP490-3") getAllLabelContents.Y = "2";
            if (opName.StartsWith("OP450")) getAllLabelContents.Y = "3";
            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "调用打印配置接口");
            ND_Api.getAllLabelContents(apiName, opName, getAllLabelContents,out string responseContent, out code, out message);
            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "调用打印配置接口成功！");

                List<LabelContent> labelContents = ParseLabelContents(responseContent);
                if(labelContents.Count > 0)
                {
                    labelContent = labelContents[0];
                }
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", "调用打印配置接口失败！原因：" + message);
            }
        }

        /// <summary>
        /// 解析打印配置
        /// </summary>
        /// <param name="xmlResponse"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static List<LabelContent> ParseLabelContents(string xmlResponse)
        {
            // 1. 定义所有用到的命名空间
            XNamespace soapEnv = "http://schemas.xmlsoap.org/soap/envelope/";
            XNamespace ns2 = "http://miGetAllLabelContentsBySfc.ws.atlmes.com/";
            XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";

            // 2. 加载XML并忽略注释（避免干扰）
            var doc = XDocument.Parse(xmlResponse, LoadOptions.SetLineInfo);

            // 3. 获取SOAP Body下的所有responseList节点（必须指定完整路径）
            var returnElement = doc.Element(soapEnv + "Envelope")?
                                  .Element(soapEnv + "Body")?
                                  .Element(ns2 + "getAllLabelContentsResponse")?
                                  .Element("return");

            if (returnElement == null)
                throw new Exception("Invalid XML: Missing SOAP Envelope/Body/return structure");

            var result = new List<LabelContent>();

            // 4. 遍历每个responseList
            foreach (var response in returnElement.Elements("responseList"))
            {
                var labelContent = new LabelContent
                {
                    // 注意：子节点不需要命名空间（因为它们继承父节点的ns2）
                    Code = (string)response.Element("code") ?? string.Empty,
                    Message = (string)response.Element("message") ?? string.Empty,
                    TemplateVersion = (string)response.Element("templateVersion") ?? string.Empty,
                    Sfc = (string)response.Element("sfc") ?? string.Empty,
                    SysPn = (string)response.Element("sysPn") ?? string.Empty
                };

                // 5. 提取labelListArray中的字段
                foreach (var field in response.Elements("labelListArray"))
                {
                    labelContent.LabelFields.Add(new LabelField
                    {
                        Field = (string)field.Element("field") ?? string.Empty,
                        Value = (string)field.Element("value") ?? string.Empty,
                        Formula = (string)field.Element("formula") ?? string.Empty
                    });
                }

                result.Add(labelContent);
            }

            return result;
        }

        public static void Call_dataCollectForResourceInspect(string opName, DataTable dt_quality, out string code, out string message)
        {

            string apiName = "dataCollectForResourceInspectTask";
            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, "", out string enable, out DataTable dtEnable);
            //if (dtEnable.Rows.Count == 0)
            //{
            //    MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "该工位未配置首件数据上传接口，将采用离线模式！");
            //    code = "0";
            //    message = "";
            //    return;
            //}
            //if (enable == "0")
            //{
            //    MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", "出站接口被禁用!");
            //    code = "0";
            //    message = "";
            //    return;
            //}
            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 获取接口operation/resource参数
            MisDataFun.GetApiParams_operation(opName, out string operation, out string resource, out code, out message);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }
            // 准备接口参数
            DataCollectForResourceInspect dataCollectForResourceInspect = new DataCollectForResourceInspect();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString(); // 获取参数名称列的值
                object paramValue = row["默认值"];        // 使用默认值

                switch (paramName)
                {
                    case "site":
                        dataCollectForResourceInspect.site = paramValue?.ToString() ?? "";
                        break;
                    case "dcGroup":
                        dataCollectForResourceInspect.dcGroup = paramValue?.ToString() ?? "";
                        break;
                    case "dcMode":
                        dataCollectForResourceInspect.dcMode = paramValue?.ToString() ?? "";
                        break;
                    case "dcGroupRevision":
                        dataCollectForResourceInspect.dcGroupRevision = paramValue?.ToString() ?? "";
                        break;
                    case "executeMode":
                        dataCollectForResourceInspect.executeMode = paramValue?.ToString() ?? "";
                        break;
                    case "operation":
                        dataCollectForResourceInspect.operation = operation;
                        break;
                    case "operationRevision":
                        dataCollectForResourceInspect.operationRevision = paramValue?.ToString() ?? "";
                        break;
                    case "resource":
                        dataCollectForResourceInspect.resource = resource;
                        break;
                    case "user":
                        dataCollectForResourceInspect.user = paramValue?.ToString() ?? "";
                        break;
                    case "dcSequence":
                        dataCollectForResourceInspect.dcSequence = paramValue?.ToString() ?? "";
                        break;
                }
            }

            int EngineTypeID = Convert.ToInt32(dt_quality.Rows[0]["产品代码"]);
            if (opName == "OP320")
            {
                if (EngineTypeID == 2) dataCollectForResourceInspect.dcSequence = "B1";
                if (EngineTypeID == 3) dataCollectForResourceInspect.dcSequence = "B2";
                if (EngineTypeID == 4) dataCollectForResourceInspect.dcSequence = "B3";
                if (EngineTypeID == 5) dataCollectForResourceInspect.dcSequence = "B5";
                if (EngineTypeID == 6) dataCollectForResourceInspect.dcSequence = "B4";
                if (EngineTypeID == 7) dataCollectForResourceInspect.dcSequence = "B4";
            }

            List<ParametricData> parametricDataArray = new List<ParametricData>();
            for (int i = 0; i < dt_quality.Rows.Count; i++)
            {
                parametricDataArray.Add(new ParametricData()
                {
                    name = dt_quality.Rows[i]["检测项目代码"].ToString(),
                    value = dt_quality.Rows[i]["测量值"].ToString(),
                    dataType = dt_quality.Rows[i]["dataType"].ToString()
                });
            }
            dataCollectForResourceInspect.parametricDataArray = parametricDataArray;

            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "调用首件数据上传接口");
            ND_Api.dataCollectForResourceInspect(apiName, opName, dataCollectForResourceInspect, out code, out message);
            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", "调用首件数据上传接口成功！");
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", "调用首件数据上传接口失败！原因：" + message);
                throw new Exception("首件数据上传失败！原因：" + message);
            }
        }

        /// <summary>
        /// 获取产品生产状态
        /// </summary>
        /// <param name="barcode">产品条码</param>
        /// <param name="statusBO">返回的状态代码</param>
        /// <param name="operation">返回的操作编号</param>
        /// <param name="productStatus">转换后的产品状态</param>
        /// <param name="opStation">转换后的工位号</param>
        /// <param name="code">返回码</param>
        /// <param name="message">返回消息</param>
        public static void Call_getSfcStatusAndOperation(string barcode, out string statusBO, out string operation, out string productStatus, out string opStation, out string code, out string message)
        {
            statusBO = "";
            operation = "";
            productStatus = "";
            opStation = "";
            string apiName = "getSfcStatusAndOperation";
            string opName = "API";

            // 获取接口是否禁用
            MisDataFun.GetApiEnable(apiName, opName, "", out string enable, out DataTable dtEnable);
            if (dtEnable.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{barcode}该接口未配置，无法查询产品状态！");
                code = "500";
                message = "该接口未配置";
                return;
            }
            if (enable == "0")
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MOM", $"{barcode}产品状态查询接口被禁用!");
                code = "500";
                message = "接口被禁用";
                return;
            }

            // 获取接口预置参数
            MisDataFun.GetApiParams(apiName, opName, out code, out message, out DataTable dt);
            if (code != "0")
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", message);
                return;
            }

            // 准备接口参数
            GetSfcStatusAndOperationParams getSfcStatusAndOperationParams = new GetSfcStatusAndOperationParams();
            foreach (DataRow row in dt.Rows)
            {
                string paramName = row["参数名称"].ToString();
                object paramValue = row["默认值"];

                switch (paramName)
                {
                    case "site":
                        getSfcStatusAndOperationParams.site = paramValue?.ToString() ?? "";
                        break;
                }
            }
            getSfcStatusAndOperationParams.barcode = barcode;

            MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"调用产品状态查询接口，barcode={barcode}");
            ND_Api.getSfcStatusAndOperation(apiName, opName, getSfcStatusAndOperationParams, out string responseContent, out code, out message);

            if (code == "0")
            {
                MisDataFun.LogRecording_OP(opName, "INFO", "MOM", $"调用产品状态查询接口成功！barcode={barcode}");

                // 解析XML提取 Status_BO 和 Operation
                try
                {
                    System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Parse(responseContent);
                    foreach (var element in doc.Descendants())
                    {
                        if (element.Name.LocalName == "Status_BO")
                        {
                            statusBO = element.Value;
                        }
                        if (element.Name.LocalName == "Operation")
                        {
                            operation = element.Value;
                        }
                    }

                    // 调用存储过程转换数据
                    if (!string.IsNullOrEmpty(statusBO) && !string.IsNullOrEmpty(operation))
                    {
                        System.Data.SqlClient.SqlParameter[] param = new System.Data.SqlClient.SqlParameter[2];
                        param[0] = new System.Data.SqlClient.SqlParameter("@产品状态代码", statusBO);
                        param[1] = new System.Data.SqlClient.SqlParameter("@操作编号", operation);
                        DataLinkMesWork1.DataAccess.ExecuteStoredProcedure("MOM_产品状态转换_查询", ref param, out DataTable dtResult, out string errorMessage);

                        if (string.IsNullOrEmpty(errorMessage) && dtResult.Rows.Count > 0)
                        {
                            productStatus = dtResult.Rows[0]["产品状态"]?.ToString() ?? "";
                            opStation = dtResult.Rows[0]["工位号"]?.ToString() ?? "";
                        }
                        else if (!string.IsNullOrEmpty(errorMessage))
                        {
                            MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", $"调用存储过程转换失败：{errorMessage}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", $"解析返回结果失败：{ex.Message}");
                }
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "MOM", $"调用产品状态查询接口失败！barcode={barcode}，原因：" + message);
            }
        }
    } 
}
