
using DataLinkMesWork1;
using MesDataFunction;
using MesServerFunctions;
using MesServerWork;
using MisDataSaveDate;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Windows.Forms;

namespace MisDataFunDll
{
    public partial class MisDataFun
    {
        /// <summary>
        /// 产品上线工位_获取上线总成号
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="EngineTypeID"></param>
        /// <param name="EngineID"></param>
        /// <param name="isCheckOk"></param>
        /// <param name="msg2"></param>
        /// <returns></returns>
        public static void GetEngineType_EngineID(string opName, out string EngineID, out int isCheckOk, out string msg2)
        {
            EngineID = "";
            isCheckOk = 2;
            msg2 = "未获取到正在执行的生产计划！";
            string produceName = "生产计划_派工订单_获取上线产品码";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            if (DataAccess.ExecuteStoredProcedure(produceName, ref thisParms, out DataTable dt))
            {
                if (dt.Rows.Count > 0)
                {
                    EngineID = dt.Rows[0]["变速箱总成号"].ToString();
                    isCheckOk = Convert.ToInt32(dt.Rows[0]["isCheckOk"]); ;
                    msg2 = dt.Rows[0]["msg2"].ToString();
                }
            }
        }

        /// <summary>
        /// 产品上线工位_获取上线信息
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="EngineTypeID"></param>
        /// <param name="EngineID"></param>
        /// <param name="isCheckOk"></param>
        /// <param name="msg2"></param>
        /// <returns></returns>
        public static DataTable GetEngineType_Order(string opName, string EngineID, out int EngineTypeID, out string EngineType, out string OrderForm, out int isCheckOk, out string msg2)
        {
            DataTable dt = new DataTable();
            EngineTypeID = 0;
            EngineType = "";
            OrderForm = "";

            string produceName = "生产计划_派工订单_上线信息查询";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@EngineID", EngineID);
            if (DataAccess.ExecuteStoredProcedure(produceName, ref thisParms, out dt))
            {
                if (dt.Rows.Count > 0)
                {
                    EngineTypeID = Convert.ToInt32(dt.Rows[0]["机型代码"]);
                    EngineType = dt.Rows[0]["机型"].ToString();
                    OrderForm = dt.Rows[0]["订单号"].ToString();
                    isCheckOk = 1;
                    msg2 = "";
                    return dt;
                }
            }
            isCheckOk = 2;
            msg2 = "未获取到正在执行的生产计划！";
            return dt;
        }

        /// <summary>
        /// 产品上线工位_获取Pack上线信息
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="EngineTypeID"></param>
        /// <param name="EngineID"></param>
        /// <param name="isCheckOk"></param>
        /// <param name="msg2"></param>
        /// <returns></returns>
        public static DataTable GetEngineType_PackOrder(string packCode, out int EngineTypeID, out string EngineType, out string OrderForm, out int isCheckOk, out string msg2)
        {
            DataTable dt = new DataTable();
            EngineTypeID = 0;
            EngineType = "";
            OrderForm = "";

            string produceName = "生产计划_派工订单_Pack上线信息查询";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@Pack码", packCode);

            if (DataAccess.ExecuteStoredProcedure(produceName, ref thisParms, out dt))
            {
                if (dt.Rows.Count > 0)
                {
                    EngineTypeID = Convert.ToInt32(dt.Rows[0]["机型代码"]);
                    EngineType = dt.Rows[0]["机型"].ToString();
                    OrderForm = dt.Rows[0]["订单号"].ToString();
                    isCheckOk = 1;
                    msg2 = "";
                    return dt;
                }
            }
            isCheckOk = 2;
            msg2 = "未获取到此条码Pack信息！";
            return dt;
        }
        /// <summary>
        /// 测量数据工位_监控系统_MOBY_线首_增加
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="engineTypeID"></param>
        /// <param name="engineTypeID_MES"></param>
        /// <param name="engineType"></param>
        /// <param name="engineType_MES"></param>
        /// <param name="engineID"></param>
        /// <param name="engineID_MES"></param>
        /// <param name="palletCode"></param>
        /// <param name="orderForm"></param>
        /// <param name="orderForm_MES"></param>
        /// <returns></returns>
        public static bool OpName_MIS_MOBY_Insert_FirstOpName(string opName, int engineTypeID, int engineTypeID_MES, string engineType, string engineType_MES, string engineID, string engineID_MES, int palletCode, string orderForm, string orderForm_MES)
        {
            string produceName = "测量数据工位_监控系统_MOBY_线首_增加";
            SqlParameter[] thisParms = new SqlParameter[10];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@产品型号代码", engineTypeID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@产品型号代码_MES", engineTypeID_MES);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@发动机型号", engineType);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@发动机型号_MES", engineType_MES);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@发动机号", engineID);
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@发动机号_MES", engineID_MES);
            thisParms[7] = new System.Data.SqlClient.SqlParameter("@订单号", orderForm);
            thisParms[8] = new System.Data.SqlClient.SqlParameter("@订单号_MES", orderForm_MES);
            thisParms[9] = new System.Data.SqlClient.SqlParameter("@托盘号", palletCode);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }
        /// <summary>
        /// 测量数据工位_监控系统_MOBY_增加
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="engineTypeID"></param>
        /// <param name="engineType"></param>
        /// <param name="engineID"></param>
        /// <param name="palletCode"></param>
        /// <param name="orderForm"></param>
        /// <param name="repairCode"></param>
        /// <param name="Is_repairBarcode"></param>
        /// <param name="repairBarcode"></param>
        /// <returns></returns>
        public static bool OpName_MIS_MOBY_Insert(string opName, int engineTypeID, string engineType, string engineID, int palletCode, string orderForm, int repairCode, int Is_repairBarcode, string repairBarcode)
        {
            string produceName = "测量数据工位_监控系统_MOBY_增加";
            SqlParameter[] thisParms = new SqlParameter[9];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@订单号", orderForm);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@产品型号代码", engineTypeID);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@发动机型号", engineType);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@发动机号", engineID);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@托盘号", palletCode);
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@返修标志", repairCode);
            thisParms[7] = new System.Data.SqlClient.SqlParameter("@拆解标志", Is_repairBarcode);
            thisParms[8] = new System.Data.SqlClient.SqlParameter("@拆解返修条码", repairBarcode);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }

        /// <summary>
        /// 测量数据工位_监控系统_MOBY_托盘到位
        /// </summary>
        static public void MOBY_TrayStart_Update(string opName)
        {
            string procedureName;
            procedureName = "测量数据工位_监控系统_MOBY_托盘到位";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }

        /// <summary>
        /// 测量数据工位_监控系统_MOBY_增加
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="engineTypeID"></param>
        /// <param name="engineType"></param>
        /// <param name="engineID"></param>
        /// <param name="palletCode"></param>
        /// <param name="orderForm"></param>
        /// <param name="repairCode"></param>
        /// <param name="Is_repairBarcode"></param>
        /// <param name="repairBarcode"></param>
        /// <returns></returns>
        public static bool OpName_MIS_MOBY_FX_Insert(string opName, int engineTypeID, string engineType, string engineID, int palletCode, string orderForm)
        {
            string produceName = "测量数据工位_监控系统_返修_MOBY_增加";
            SqlParameter[] thisParms = new SqlParameter[6];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@产品型号代码", engineTypeID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@发动机型号", engineType);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@发动机号", engineID);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@托盘号", palletCode);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@订单号", orderForm);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }
        /// <summary>
        /// 测量数据工位_监控系统_MOBY_增加
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="engineTypeID"></param>
        /// <param name="engineType"></param>
        /// <param name="engineID"></param>
        /// <param name="palletCode"></param>
        /// <param name="orderForm"></param>
        /// <param name="repairCode"></param>
        /// <param name="Is_repairBarcode"></param>
        /// <param name="repairBarcode"></param>
        /// <returns></returns>
        public static bool OpName_MIS_MOBY_Insert_JZ(string opName, string engineID)
        {
            string produceName = "测量数据工位_监控系统_MOBY_静置工位_增加";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@发动机号", engineID);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }

        public static void BarCodeIsSecondVerification(string opName, out bool isSecondVerification)
        {
            isSecondVerification = false;
            DataTable dt = new DataTable();
            string produceName = "测量数据工位_监控系统_是否二次验证_查询";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            DataAccess.ExecuteStoredProcedure(produceName, ref thisParms, out dt);
            if (dt.Rows.Count > 0)
            {
                string isVerification = dt.Rows[0]["是否验证"].ToString();
                if(isVerification == "1")
                {
                    isSecondVerification = true;
                }
            }
        }
        /// <summary>
        /// 日志_工位操作_增加
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="type"></param>
        /// <param name="msgType"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool LogRecording_OP(string OpName, string type, string msgType, string content)
        {
           if (type == "ERROR")
           {
                if(msgType == "MOM" && content.IndexOf("调用首件数据上传接口失败") == -1 && content.IndexOf("却意外调用") == -1 && content.IndexOf("接口质量判定不合格") == -1) MIS_BASE.Write_MesErrorType(OpName, 1000);  // 写入MES报警类型 写入PLC接口调用失败报警
           }

            string produceName = "日志_工位操作_增加";
            SqlParameter[] thisParms = new SqlParameter[4];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", OpName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@消息类型", type);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@操作类型", msgType);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@内容", content);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }

        /// <summary>
        ///  判断类型是否存在该工位
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool GetSpecialOpExist(string opName, string type)
        {
            if (PP.SpecialOpList.ContainsKey(type))
            {
                return PP.SpecialOpList[type].Contains(opName);
            }
            return false;
        }

        /// <summary>
        /// 这是一个防错方法 判断200扫码工位 如果出现 产品到位 传递机型未完成时扫码 无物料与拧紧的情况下 会直接跳到出站 给两秒缓冲时间 确保物料已入库
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="engineID"></param>
        /// <returns></returns>
        public static int Moby_Select_OP200_DelayResult(string opName, string engineID)
        {
            DataTable dt = new DataTable();
            int result = 2; // 默认不需要延时

            try
            {
                string procedureName = "MOM_接口_Moby_执行状态_OP200延时判断_查询";
                SqlParameter[] thisParms = new SqlParameter[2];
                thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
                thisParms[1] = new System.Data.SqlClient.SqlParameter("@产品编号", engineID);

                DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);

                if (dt != null && dt.Rows.Count > 0)
                {
                    result = Convert.ToInt32(dt.Rows[0]["result"]);
                }
            }
            catch
            {
                result = 2;
            }

            return result;
        }

        /// <summary>
        /// 产品离站时清空拧紧临时表和物料临时表
        /// </summary>
        /// <param name="opName">工位号</param>
        public static void TightenStation_ClearTempTables(string opName)
        {
            string procedureName = "拧紧工位_产品离站_清空临时表";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }

        /// <summary>
        /// 扫码前检查拧紧+物料临时表数据是否已正确写入，未就绪时自动发送提示并记录日志
        /// </summary>
        /// <param name="opName">工位号</param>
        /// <param name="engineID">产品编号</param>
        /// <returns>true=就绪 false=未就绪(已发送提示)</returns>
        public static bool TightenStation_CheckDataReady(string opName, string engineID)
        {
            try
            {
                string procedureName = "拧紧工位_扫码前_数据就绪检查";
                SqlParameter[] thisParms = new SqlParameter[2];
                thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
                thisParms[1] = new System.Data.SqlClient.SqlParameter("@产品编号", engineID);
                DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out DataTable dt);
                if (dt != null && dt.Rows.Count > 0)
                {
                    int result = Convert.ToInt32(dt.Rows[0]["result"]);
                    string msg = dt.Rows[0]["msg"].ToString();
                    if (result == 1) return true;

                    // 未就绪：发送提示 + 记录日志
                    string notice = $"【{opName}】{msg}";
                    SendMessageFun.AsyncSendMessage_Notice(opName, notice);
                    LogRecording_OP(opName, "WARNING", "barcode", notice);
                }
            }
            catch { }
            return false;
        }

        /// <summary>
        /// 生产计划_派工订单_转线标注
        /// </summary>
        /// <returns></returns>
        public static bool WorkPlan_TurnLine(string EngineID, string TurnEngineID, int type)
        {
            string produceName = "生产计划_派工订单_转线标注";
            SqlParameter[] thisParms = new SqlParameter[3];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工件编号", EngineID);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@标注编号", TurnEngineID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@type", type);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }
        /// <summary>
        /// 电子看板_工序内容_临时表_增加
        /// </summary>
        /// <param name="opName"></param>
        /// <returns></returns>
        public static bool MesServer_ProcessList_EngineTypeOver(string opName)
        {
            string produceName = "电子看板_工序内容_临时表_增加";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }
        /// <summary>
        /// 电子看板_装配零件_临时表_增加
        /// </summary>
        /// <param name="opName"></param>
        /// <returns></returns>
        public static bool MesServer_Process_EngineTypeOver(string opName)
        {
            string produceName = "电子看板_装配零件_临时表_增加";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }
        /// <summary>
        /// 测量数据工位_监控系统_MOBY_查询
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="OrderForm"></param>
        /// <param name="EngineTypeID"></param>
        /// <param name="EngineType"></param>
        /// <param name="EngineID"></param>
        /// <param name="PartPallet_Code"></param>
        /// <param name="IsrepairBarcode"></param>
        /// <param name="repairBarcode"></param>
        /// <param name="EngineTypeString"></param>
        public static void Moby_Select(string opName, out string OrderForm, out int EngineTypeID, out string EngineType, out string EngineID, out string PartPallet_Code, out int IsrepairBarcode, out string repairBarcode, out string EngineTypeString)
        {
            DataTable dt = new DataTable();
            OrderForm = "";
            EngineTypeID = 0;
            EngineType = "";
            EngineID = "";
            PartPallet_Code = "0";
            IsrepairBarcode = 0;
            repairBarcode = "0";
            EngineTypeString = "";
            try
            {
                string procedureName;
                procedureName = "测量数据工位_监控系统_MOBY_查询";
                SqlParameter[] thisParms = new SqlParameter[1];
                thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);

                DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
                if (dt.Rows.Count > 0)
                {
                    OrderForm = dt.Rows[0]["订单号"].ToString();
                    EngineTypeID = Convert.ToInt32(dt.Rows[0]["机型代码"]); ;
                    EngineType = dt.Rows[0]["发动机型号"].ToString();
                    EngineID = dt.Rows[0]["变速器序列号"].ToString();
                    PartPallet_Code = dt.Rows[0]["托盘号"].ToString();
                }
            }
            catch
            {
                OrderForm = "";
                EngineTypeID = 0;
                EngineType = "";
                EngineID = "";
                PartPallet_Code = "0";
                IsrepairBarcode = 0;
                repairBarcode = "";
                EngineTypeString = "";
            }
        }
        /// <summary>
        /// 电子看板_工序内容_临时表_查询
        /// </summary>
        /// <param name="opName">工位号</param>
        /// <param name="engineTypeID">产品型号代码</param>
        /// <param name="dt"></param>
        static public void Process_Select(string opName, int engineTypeID, out DataTable dt)
        {
            dt = null;
            string procedureName;
            procedureName = "电子看板_工序内容_临时表_查询";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@产品型号代码", engineTypeID);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
        }
        /// <summary>
        /// 电子看板_装配零件_物料验证_查询 
        /// </summary>
        /// <param name="opName">工位号</param>
        /// <param name="engineID">发动机号</param>
        /// <param name="dt"></param>
        static public bool PartMaterialVerify_Select(string opName, int engineTypeID, string engineType, out DataTable dt)
        {
            dt = null;
            string procedureName;
            procedureName = "电子看板_装配零件_临时表_查询";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            return DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
        }
        /// <summary>
        /// 电子看板_装配零件_临时表_判断_是否允许工作
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="IsAllowWork"></param>
        /// <returns></returns>
        public static bool MesServer_Judge_IsCanWork(string opName, out int IsAllowWork)
        {
            IsAllowWork = 0;
            string produceName = "电子看板_装配零件_临时表_判断_是否允许工作";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@是否允许工作", IsAllowWork);
            thisParms[1].Direction = ParameterDirection.Output;
            if (DataAccess.ExecuteStoredProcedure(produceName, ref thisParms))
            {
                IsAllowWork = Convert.ToInt32(thisParms[1].Value);
                return true;
            }
            return false;
        }


        public static string GetMesClientTagValue(string OpName, string EngineID, int EngineTypeID)
        {
            // 机型代码 对应客户标签字段映射
            string targetField;
            if (OpName.StartsWith("OP450"))
            {
                targetField = "TEXT2";
            }
            else
            {
                switch (EngineTypeID)
                {
                    case 1: case 5: targetField = "TEXT9"; break;
                    case 2: case 3:  targetField = "TEXT14"; break;
                    case 4: case 6: case 7:  targetField = "TEXT16"; break;
                    default: targetField = ""; break;
                }
            }

            if (!string.IsNullOrEmpty(targetField))
            {
                CallWebService.Call_getAllLabelContents(OpName, EngineID, out string res_code, out string res_message, out LabelContent labelContent);
                List<LabelField> LabelFields = labelContent.LabelFields;
                for (int i = 0; i < LabelFields.Count; i++)
                {
                    if (LabelFields[i].Field == targetField)
                    {
                        MisDataFun.LogRecording_OP(OpName, "INFO", "MOM", $"{EngineID} 获取{targetField}的客户标签成功 内容：{LabelFields[i].Value}");
                        return LabelFields[i].Value;
                    }
                }
            }

            MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", $"{EngineID}获取客户标签码失败 无可用客户标签码！");
            return "";
        }


        public static DataTable GetMesModelPrintData(string OpName,string EngineID)
        {
            string produceName = "工位_打码记录_产品打印记录_查询";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工件编号", EngineID);
            DataAccess.ExecuteStoredProcedure(produceName, ref thisParms, out DataTable dt);
            if(dt.Rows.Count == 0)
            {
                MisDataFun.LogRecording_OP(OpName, "ERROR", "MOM", $"{EngineID}获取模组码打印记录失败 无记录的模组码打印记录！");
            }
            return dt;
        }
        /// <summary>
        /// 电子看板_装配零件_临时表_修改_零件条码
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="barCode"></param>
        /// <param name="IsSuccess"></param>
        /// <returns></returns>
        public static bool PartMaterial_DM_Update(int ID,int isClear,string opName,string batchCode,string materialsCode)
        {
            string produceName = "电子看板_装配零件_临时表_修改_零件条码";
            SqlParameter[] thisParms = new SqlParameter[5];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@ID", ID);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@是否清空", isClear);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@批次号", batchCode);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@物料条码", materialsCode);
            if (DataAccess.ExecuteStoredProcedure(produceName, ref thisParms))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 电子看板_装配零件_临时表_修改_零件条码
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="barCode"></param>
        /// <param name="IsSuccess"></param>
        /// <returns></returns>
        public static void  PartMaterial_DM_Check(string opName, string barCode,string EngineID_MES, out int isOk,out bool MesCheck,out bool isAllBatchCheck,out bool ShowBox,out string msg,out string MaterialsCode,out string batchCode,out int ID,out bool isBatch)
        {
            string produceName = "电子看板_装配零件_临时表_校验_零件条码";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@条码", barCode);
            DataAccess.ExecuteStoredProcedure(produceName, ref thisParms, out DataTable dt);
            DataRow row = dt.Rows[0];
            isOk = Convert.ToInt32(row["isOk"]);
            MesCheck = Convert.ToBoolean(row["MesCheck"]);
            isAllBatchCheck = Convert.ToBoolean(row["isAllBatchCheck"]);
            ShowBox = Convert.ToBoolean(row["showBox"]);
            msg = row["msg"].ToString();
            MaterialsCode = row["物料条码"].ToString();
            batchCode = row["批次号"].ToString();
            ID = Convert.ToInt32(row["ID"]);
            isBatch = Convert.ToBoolean(row["isBatch"]);

            // 判断是否成功
            if (isOk == 1)
            {
                // 判断是否工厂MES校验
                if (MesCheck)
                {
                    // 调用接口进行物料装配验证
                    CallWebService.Call_miAssmebleAndCollectDataForSfc(opName, EngineID_MES, MaterialsCode,batchCode, out string code, out string message);
                    if (code == "0")
                    {
                        // 更新到工厂校验通过后再更新到临时表~
                        MisDataFun.PartMaterial_DM_Update(ID, 0, opName, batchCode, MaterialsCode);
                        SendMessageFun.SendInfomation("MaterialList", "1", opName);//通知界面更新零件信息
                        SendMessageFun.AsyncSendMessage_Materials_BarCode_Box(opName, 0, "", "", "");
                    }
                    else
                    {
                        SendMessageFun.AsyncSendMessage_Notice_Error(opName, message, "物料验证");
                        //if (isBatch)  // 这是一个带 物料、批次的条码 失败了需要清空物料校验状态
                        //{
                            MisDataFun.PartMaterial_DM_Update(ID, 1, opName, "", "");
                            SendMessageFun.AsyncSendMessage_Materials_BarCode_Box(opName, 0, "", "", "");
                        //}
                    }
                }else if (isAllBatchCheck) // 只剩仅批追了 读取批追列表 开始遍历
                {
                    DataTable batchList = MaterialsCheck_Get_BatchList(opName);
                    bool batchCheckIsOk = false;
                    foreach (DataRow item in batchList.Rows)
                    {
                        string itemCode = item["物料号"].ToString();
                        int itemID = Convert.ToInt32(item["ID"]);
                        CallWebService.Call_miCheckBOMInventory(opName, EngineID_MES, barCode, itemCode, out string code, out string message);
                        if (code == "0") {
                            // 匹配成功进行物料绑定~
                            CallWebService.Call_miAssmebleAndCollectDataForSfc(opName, EngineID_MES, barCode, barCode, out string code2, out string message2);
                            if(code2 == "0")
                            {
                                MisDataFun.PartMaterial_DM_Update(itemID, 0,opName, barCode, "");
                                SendMessageFun.AsyncSendMessage_Materials_BarCode_Box(opName, 0, "", "", "");
                                SendMessageFun.SendInfomation("MaterialList", "1", opName); //通知界面更新零件信息
                                batchCheckIsOk = true;
                                break; // 成功就退出遍历
                            }
                        }
                    }
                    if (!batchCheckIsOk)
                    {
                        SendMessageFun.AsyncSendMessage_Notice_Error(opName, "批次条码校验失败", "物料验证");
                    }
                }
                else if (ShowBox) // 需要前端弹窗继续指导扫码
                {
                    SendMessageFun.AsyncSendMessage_Materials_BarCode_Box(opName, 1, MaterialsCode, batchCode, msg);
                }
            }
            else
            {
                MisDataFun.LogRecording_OP(opName, "ERROR", "Barcode", msg);
                SendMessageFun.AsyncSendMessage_Notice_Error(opName, msg, "条码扫描");
            }
        }

        public static DataTable MaterialsCheck_Get_BatchList(string OpName)
        {
            string procedureName;
            procedureName = "电子看板_装配零件_临时表_校验_获取批追列表";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", OpName);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out DataTable dt);
            return dt;
        }

        static public int CheckTJTimeAndSave(string opName, string EngineID,string barCode,int isSave,int checkTpye = 1)
        {
            string procedureName;
            DataTable dt = new DataTable();
            procedureName = "工位_涂胶时间差_校验";
            SqlParameter[] thisParms = new SqlParameter[5];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@工件编号", EngineID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@物料码值", barCode);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@是否保存", isSave);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@checkTpye", checkTpye);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
            if (dt != null && dt.Rows.Count > 0)
            {
                int result = Convert.ToInt32(dt.Rows[0]["result"]);
                string msg = dt.Rows[0]["msg"].ToString();
                if (result != 1)
                {
                    MisDataFun.LogRecording_OP(opName, "ERROR", "Barcode", msg);
                    //SendMessageFun.AsyncSendMessage_Notice_Error(opName, msg, "涂胶清洗时间差校验");
                }
                else
                {
                    if (msg != "" ) MisDataFun.LogRecording_OP(opName, "INFO", "Barcode", msg);
                }
                return result;
            }
            return 0;

        }

        static public int CheckLineOutDataIsAllOk(string opName, string EngineID)
        {
            string procedureName;
            DataTable dt = new DataTable();
            procedureName = "测量数据合格_产品数据合格校验";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@产品编号", EngineID);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
            if (dt != null && dt.Rows.Count > 0)
            {
                int result = Convert.ToInt32(dt.Rows[0]["result"]);
                string msg = dt.Rows[0]["msg"].ToString();
                if (result != 1)
                {
                    MisDataFun.LogRecording_OP(opName, "ERROR", "Barcode", msg);
                    SendMessageFun.AsyncSendMessage_Notice_Error(opName, msg, "装配物料 质量数据判断");
                }
                else
                {
                    if (msg != "") MisDataFun.LogRecording_OP(opName, "ERRPR", "Barcode", msg);
                }
                return result;
            }
            return 2;

        }

        /// <summary>
        /// 测量数据合格索引_增加
        /// </summary>
        /// <param name="EngineID"></param>
        /// <param name="tagID"></param>
        /// <param name="EngineType"></param>
        /// <param name="EngineTypeID"></param>
        /// <param name="opName"></param>
        /// <param name="isGoodBad"></param>
        /// <param name="PartPalletCode"></param>
        /// <param name="Id_tagCF"></param>
        /// <returns></returns>
        public static bool BaseData_tagTable_QualityData_B5A_IndexInsert(string EngineID, string tagID, string EngineType, int EngineTypeID, string opName, int isGoodBad, string PartPalletCode, out int Id_tagCF)
        {
            string procedureName;
            procedureName = "测量数据合格索引_增加";
            Id_tagCF = 0;
            SqlParameter[] thisParms = new SqlParameter[8];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@EngineID", EngineID);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@TagID_CF", tagID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@EngineType", EngineType);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@EngineTypeID", EngineTypeID);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@托盘编号", PartPalletCode);
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@合格标志", isGoodBad);
            thisParms[7] = new System.Data.SqlClient.SqlParameter("@Id_tagCF", Id_tagCF);
            thisParms[7].Direction = ParameterDirection.Output;

            if (DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms))
            {
                Id_tagCF = Convert.ToInt32(thisParms[7].Value.ToString());
                return true;
            }
            return false;
        }

        public static bool BaseData_tagTable_QualityData_B5A_Insert_OP170Status(int Id_tagCF, int engineTypeID, string engineID, string TagID, string TagValue, int IsGoodBad, string opName)
        {
            string procedureName;
            procedureName = "测量数据合格_增加_170状态";
            SqlParameter[] thisParms = new SqlParameter[7];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@Id_tagCF", Id_tagCF);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@EngineTypeID", engineTypeID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@EngineID", engineID);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@TagID", TagID);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@TagValue", TagValue);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@IsGoodBad", IsGoodBad);
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@工位号", opName);

            if (DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms))
            {
                return true;
            }
            return false;
        }

        public static void SaveOP100_CellCode(string opName, string EngineID)
        {
            string procedureName;
            procedureName = "测量数据合格_电芯堆叠电芯码记录";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@产品编号", EngineID);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out DataTable dt, out string err);
        }

        public static bool CheckCleanoutTime(string opName,string EngineID,out int MaxUseTime)
        {
            string procedureName;
            MaxUseTime = -1;
            procedureName = "工位_电芯清洗时间_校验";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@产品编号", EngineID);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out DataTable dt,out string err);
            if(dt.Rows.Count > 0)
            {
                int result = Convert.ToInt32(dt.Rows[0]["result"]);
                string msg = dt.Rows[0]["msg"].ToString();
                MaxUseTime = Convert.ToInt32(dt.Rows[0]["maxTime"]);
                if (result == 1) return true;
                else
                {
                    MisDataFun.LogRecording_OP(opName, "ERROR", "TimeCheck", msg);
                    SendMessageFun.AsyncSendMessage_Notice(opName, msg);
                }
            }

            return false;
        }
        /// <summary>
        /// 测量数据合格_增加
        /// </summary>
        /// <param name="Id_tagCF"></param>
        /// <param name="engineTypeID"></param>
        /// <param name="engineID"></param>
        /// <param name="opName"></param>
        /// <param name="dt"></param>
        /// <param name="IsCheck"></param>
        /// <returns></returns>
        public static bool BaseData_tagTable_QualityData_B5A_Insert(int Id_tagCF, int engineTypeID, string engineID, string opName, DataTable dt, bool IsCheck = false)
        {
            string procedureName;
            procedureName = "测量数据合格_增加";
            SqlParameter[] thisParms = new SqlParameter[15];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@Id_tagCF", Id_tagCF);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@EngineTypeID", engineTypeID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@EngineID", engineID);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@TagID", SqlDbType.Int, 4, "TagID");
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@TagValue", SqlDbType.NVarChar, 100, "TagValue");
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@IsGoodBad", SqlDbType.Int, 4, "IsGoodBad");
            //是否保存无合格标志数据数据
            //thisParms[6] = new System.Data.SqlClient.SqlParameter("@IsCheck", IsCheck);
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@测量位置", SqlDbType.NVarChar, 50, "测量位置");
            thisParms[7] = new System.Data.SqlClient.SqlParameter("@测量项目", SqlDbType.NVarChar, 50, "测量项目");
            thisParms[8] = new System.Data.SqlClient.SqlParameter("@工位号", opName);  //为了给J07MA020 保留四位精度而设置
            thisParms[9] = new System.Data.SqlClient.SqlParameter("@理论值", SqlDbType.NVarChar, 50, "理论值");
            thisParms[10] = new System.Data.SqlClient.SqlParameter("@上限值", SqlDbType.NVarChar, 50, "上限值");
            thisParms[11] = new System.Data.SqlClient.SqlParameter("@下限值", SqlDbType.NVarChar, 50, "下限值");
            thisParms[12] = new System.Data.SqlClient.SqlParameter("@测量单位", SqlDbType.NVarChar, 50, "测量单位");
            thisParms[13] = new System.Data.SqlClient.SqlParameter("@列位置", SqlDbType.Int, 4, "列位置");
            thisParms[14] = new System.Data.SqlClient.SqlParameter("@变量排序", SqlDbType.Int, 4, "变量排序");
            //table.Columns.Add("TagID", typeof(System.Int32));
            //table.Columns.Add("TagValue", typeof(System.String));
            //table.Columns.Add("IsGoodBad", typeof(System.Int32));
            //table.Columns.Add("测量位置", typeof(System.String));
            //table.Columns.Add("测量项目", typeof(System.String));
            //table.Columns.Add("工位号", typeof(System.String));
            //table.Columns.Add("理论值", typeof(System.String));
            //table.Columns.Add("上限值", typeof(System.String));
            //table.Columns.Add("下限值", typeof(System.String));
            //table.Columns.Add("测量单位", typeof(System.String));
            //table.Columns.Add("列位置", typeof(System.Int32));
            //table.Columns.Add("变量排序", typeof(System.Int32));
            return DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, dt);
        }

        public static bool BaseData_tagTable_QualityData_B5A_Insert_table(int Id_tagCF, int engineTypeID, string engineID, string opName, DataTable dt, bool IsCheck = false)
        {
            string TableName = "测量数据合格";
            Get_TableStructure(TableName, out DataTable dt_target);
            dt_target.TableName = TableName;
            foreach (DataRow dr in dt.Rows)
            {
                DataRow drNew = dt_target.NewRow();
                drNew["Id_tagCF"] = Id_tagCF;
                drNew["EngineTypeID"] = engineTypeID;
                drNew["EngineID"] = engineID;
                drNew["TagID"] = dr["TagID"].ToString();
                drNew["TagValue"] = dr["TagValue"].ToString();
                drNew["IsGoodBad"] = dr["IsGoodBad"].ToString();
                drNew["测量位置"] = dr["测量位置"].ToString();
                drNew["测量项目"] = dr["测量项目"].ToString();
                drNew["工位号"] = opName;
                drNew["理论值"] = dr["理论值"].ToString();
                drNew["上限值"] = dr["上限值"].ToString();
                drNew["下限值"] = dr["下限值"].ToString();
                drNew["测量单位"] = dr["测量单位"].ToString();
                drNew["列位置"] = dr["列位置"].ToString();
                drNew["变量排序"] = dr["变量排序"].ToString();
                drNew["是否禁用"] = 0;
                drNew["工位类型"] = 0;
                drNew["接口输出"] = 0;
                drNew["是否备份"] = 0;
                dt_target.Rows.Add(drNew);
            }
    ;
            return DataAccess.SqlbulkcopyInsert(dt_target, out string errMsg);
        }

        public static bool Get_TableStructure(string tableName, out DataTable dt)
        {
            dt = null;
            var sql = "select * from " + tableName + " where 1=2";
            return DataAccess.ExecuteDataTable(sql, out dt);

        }

        /// <summary>
        /// 测量数据合格_手动拧紧_增加
        /// </summary>
        /// <param name="engineTypeID"></param>
        /// <param name="engineType"></param>
        /// <param name="engineID"></param>
        /// <param name="tagValue"></param>
        /// <param name="isGoodBad"></param>
        /// <param name="opName"></param>
        /// <param name="partPallet"></param>
        /// <param name="tagID"></param>
        /// <param name="tagID_CF"></param>
        /// <returns></returns>
        public static bool BaseData_tagTable_QualityData_B5A_Insert(int engineTypeID, string engineType, string engineID, string tagValue, int isGoodBad, string opName, string partPallet, int tagID, int tagID_CF, string MaxValue, string MinValue,int step,int workType)
        {
            string procedureName;
            procedureName = "测量数据合格_手动拧紧_增加";
            SqlParameter[] thisParms = new SqlParameter[13];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@EngineTypeID", engineTypeID);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@EngineType", engineType);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@EngineID", engineID);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@TagValue", tagValue);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@合格标志", isGoodBad);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@托盘编号", partPallet);
            thisParms[7] = new System.Data.SqlClient.SqlParameter("@TagID", tagID);
            thisParms[8] = new System.Data.SqlClient.SqlParameter("@TagID_CF", tagID_CF);
            thisParms[9] = new System.Data.SqlClient.SqlParameter("@上限值", MaxValue);
            thisParms[10] = new System.Data.SqlClient.SqlParameter("@下限值", MinValue);
            thisParms[11] = new System.Data.SqlClient.SqlParameter("@拧紧顺序号", step);
            thisParms[12] = new System.Data.SqlClient.SqlParameter("@拧紧类型", workType);
            return DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }
        /// <summary>
        /// 测量数据合格索引_增加_电芯
        /// </summary>
        /// <param name="EngineID"></param>
        /// <param name="tagID"></param>
        /// <param name="EngineType"></param>
        /// <param name="EngineTypeID"></param>
        /// <param name="opName"></param>
        /// <param name="isGoodBad"></param>
        /// <param name="PartPalletCode"></param>
        /// <param name="Id_tagCF"></param>
        /// <returns></returns>
        public static bool BaseData_tagTable_QualityData_B5A_IndexInsert_cell(string EngineID, string tagID, string EngineType, int EngineTypeID, string opName, int isGoodBad, string PartPalletCode, out int Id_tagCF)
        {
            string procedureName;
            procedureName = "测量数据合格索引_增加_电芯";
            Id_tagCF = 0;
            SqlParameter[] thisParms = new SqlParameter[8];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@EngineID", EngineID);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@TagID_CF", tagID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@EngineType", EngineType);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@EngineTypeID", EngineTypeID);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@托盘编号", PartPalletCode);
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@合格标志", isGoodBad);
            thisParms[7] = new System.Data.SqlClient.SqlParameter("@Id_tagCF", Id_tagCF);
            thisParms[7].Direction = ParameterDirection.Output;

            if (DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms))
            {
                Id_tagCF = Convert.ToInt32(thisParms[7].Value.ToString());
                return true;
            }
            return false;
        }
        /// <summary>
        /// 测量数据合格_增加_电芯
        /// </summary>
        /// <param name="Id_tagCF"></param>
        /// <param name="engineTypeID"></param>
        /// <param name="engineID"></param>
        /// <param name="opName"></param>
        /// <param name="dt"></param>
        /// <param name="IsCheck"></param>
        /// <returns></returns>
        public static bool BaseData_tagTable_QualityData_B5A_Insert_cell(int Id_tagCF, int engineTypeID, string engineID, string opName, DataTable dt, bool IsCheck = false)
        {
            string procedureName;
            procedureName = "测量数据合格_增加_电芯";
            SqlParameter[] thisParms = new SqlParameter[15];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@Id_tagCF", Id_tagCF);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@EngineTypeID", engineTypeID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@EngineID", engineID);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@TagID", SqlDbType.Int, 4, "TagID");
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@TagValue", SqlDbType.NVarChar, 100, "TagValue");
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@IsGoodBad", SqlDbType.Int, 4, "IsGoodBad");
            //是否保存无合格标志数据数据
            //thisParms[6] = new System.Data.SqlClient.SqlParameter("@IsCheck", IsCheck);
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@测量位置", SqlDbType.NVarChar, 50, "测量位置");
            thisParms[7] = new System.Data.SqlClient.SqlParameter("@测量项目", SqlDbType.NVarChar, 50, "测量项目");
            thisParms[8] = new System.Data.SqlClient.SqlParameter("@工位号", opName);  //为了给J07MA020 保留四位精度而设置
            thisParms[9] = new System.Data.SqlClient.SqlParameter("@理论值", SqlDbType.NVarChar, 50, "理论值");
            thisParms[10] = new System.Data.SqlClient.SqlParameter("@上限值", SqlDbType.NVarChar, 50, "上限值");
            thisParms[11] = new System.Data.SqlClient.SqlParameter("@下限值", SqlDbType.NVarChar, 50, "下限值");
            thisParms[12] = new System.Data.SqlClient.SqlParameter("@测量单位", SqlDbType.NVarChar, 50, "测量单位");
            thisParms[13] = new System.Data.SqlClient.SqlParameter("@列位置", SqlDbType.Int, 4, "列位置");
            thisParms[14] = new System.Data.SqlClient.SqlParameter("@变量排序", SqlDbType.Int, 4, "变量排序");
            //table.Columns.Add("TagID", typeof(System.Int32));
            //table.Columns.Add("TagValue", typeof(System.String));
            //table.Columns.Add("IsGoodBad", typeof(System.Int32));
            //table.Columns.Add("测量位置", typeof(System.String));
            //table.Columns.Add("测量项目", typeof(System.String));
            //table.Columns.Add("工位号", typeof(System.String));
            //table.Columns.Add("理论值", typeof(System.String));
            //table.Columns.Add("上限值", typeof(System.String));
            //table.Columns.Add("下限值", typeof(System.String));
            //table.Columns.Add("测量单位", typeof(System.String));
            //table.Columns.Add("列位置", typeof(System.Int32));
            //table.Columns.Add("变量排序", typeof(System.Int32));
            return DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, dt);
        }

        /// <summary>
        /// MES_装配数据_零部件追溯_增加
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="m_ConfirmOkGo"></param>
        /// <returns></returns>
        public static bool Material_Insert(string opName, int m_ConfirmOkGo)
        {
            string produceName = "MES_装配数据_零部件追溯_增加";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@放行方式", m_ConfirmOkGo);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }

        /// <summary>
        /// 测量数据发动机在线状态_增加数据
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="isOK"></param>
        static public void WorkStartInsert_JZ(string opName, string engineID, int isOK)
        {
            string procedureName;
            procedureName = "测量数据发动机在线状态_增加数据_JZ";
            SqlParameter[] thisParms = new SqlParameter[3];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@发动机号", engineID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@是否合格", isOK.ToString());
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }
        /// <summary>
        /// 测量数据发动机在线状态_增加数据
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="isOK"></param>
        static public void WorkStartInsert_FX(string opName, string opName1, string engineID, int isOK)
        {
            string procedureName;
            procedureName = "测量数据发动机在线状态_增加数据_FX";
            SqlParameter[] thisParms = new SqlParameter[4];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@上线工位号", opName1);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@发动机号", engineID);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@是否合格", isOK.ToString());
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }
        /// <summary>
        /// 测量数据发动机在线状态_增加数据
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="isOK"></param>
        static public void WorkStartInsert(string opName, int isOK)
        {
            string procedureName;
            procedureName = "测量数据发动机在线状态_增加数据";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@是否合格", isOK.ToString());
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }

        /// <summary>
        /// 保存数据至收集组临时表
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="isOK"></param>
        static public void MOM_SaveQDataToUpload(string opName, string EngineID)
        {
            string procedureName;
            procedureName = "MOM_数据收集组_实时表_增加";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@工件编号", EngineID);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }
        /// <summary>
        /// 测量数据工位_监控系统_MOBY_查询_PDC
        /// </summary>
        /// <param name="EngineID"></param>
        /// <param name="OrderForm"></param>
        /// <param name="EngineType"></param>
        public static void Moby_Select_PDC(string EngineID, out string OrderForm, out string EngineType, out int EngineTypeCode)
        {
            DataTable dt = new DataTable();
            OrderForm = "";
            EngineType = "";
            EngineTypeCode = 0;
            try
            {
                string procedureName;
                procedureName = "测量数据工位_监控系统_MOBY_查询_PDC";
                SqlParameter[] thisParms = new SqlParameter[1];
                thisParms[0] = new System.Data.SqlClient.SqlParameter("@发动机号", EngineID);

                DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
                if (dt.Rows.Count > 0)
                {
                    OrderForm = dt.Rows[0]["订单号"].ToString();
                    EngineType = dt.Rows[0]["产品型号"].ToString();
                    EngineTypeCode = Convert.ToInt32(dt.Rows[0]["机型代码"].ToString());
                }
            }
            catch
            {
                OrderForm = "";
                EngineType = "";
                EngineTypeCode = 0;
            }
        }

        /// <summary>
        /// 基本数据_工作日历_班次产量查询_Web页面显示_初始化查询
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="IsAllowWork"></param>
        /// <returns></returns>
        public static void InitOpNameOuputCount(string opName, out int outputValue, out int nowOutputValue,
            out int opNameBeatValue, out int NowOpNameBeatValue, out int classNumber)
        {
            outputValue = 0;//工位产量
            nowOutputValue = 0;//在线产量
            opNameBeatValue = 0;//工位节拍
            NowOpNameBeatValue = 0;//在线节拍
            classNumber = 0;//班次
            try
            {
                string procedureName;
                procedureName = "Web页面显示_计划产量初始化查询";
                SqlParameter[] thisParms = new SqlParameter[6];
                thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
                thisParms[1] = new System.Data.SqlClient.SqlParameter("@工位产量", SqlDbType.Int, 4, "工位产量");
                thisParms[1].Direction = ParameterDirection.Output;
                thisParms[2] = new System.Data.SqlClient.SqlParameter("@工位节拍", SqlDbType.Int, 4, "工位节拍");
                thisParms[2].Direction = ParameterDirection.Output;
                thisParms[3] = new System.Data.SqlClient.SqlParameter("@在线产量", SqlDbType.Int, 4, "在线产量");
                thisParms[3].Direction = ParameterDirection.Output;
                thisParms[4] = new System.Data.SqlClient.SqlParameter("@在线节拍", SqlDbType.Int, 4, "在线节拍");
                thisParms[4].Direction = ParameterDirection.Output;
                thisParms[5] = new System.Data.SqlClient.SqlParameter("@班次代码", SqlDbType.Int, 4, "班次代码");
                thisParms[5].Direction = ParameterDirection.Output;
                DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
                if (thisParms[1].Value != null && thisParms[2].Value != null && thisParms[3].Value != null && thisParms[4].Value != null && thisParms[5].Value != null)
                {
                    outputValue = Convert.ToInt32(thisParms[1].Value);
                    opNameBeatValue = Convert.ToInt32(thisParms[2].Value);
                    nowOutputValue = Convert.ToInt32(thisParms[3].Value);
                    NowOpNameBeatValue = Convert.ToInt32(thisParms[4].Value);
                    classNumber = Convert.ToInt32(thisParms[5].Value);
                }
            }
            catch
            {
                outputValue = 0;
                opNameBeatValue = 0;
                nowOutputValue = 0;
                NowOpNameBeatValue = 0;
                classNumber = 0;
            }
        }

        static public void GetImageFile(string opName, int engineTypeID, out byte[] pic)
        {
            string sql;
            DataTable dt;
            pic = null;
            sql = "exec 电子看板_工位名称与产品型号产品图片_查询 '" + opName + "'," + engineTypeID.ToString();
            DataAccess.ExecuteDataTable(sql, out dt);
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    pic = (byte[])dt.Rows[0]["产品图片"];
                }
            }
        }

        /// <summary>
        /// 电子看板_工位机型_质量数据测量位置_查询_工作顺序
        /// </summary>
        /// <param name="opNum">工位号</param>
        /// <param name="EngineTypeID">产品型号代码</param>
        /// <param name="dt"></param>
        static public bool ShaftPositionSelect(string opNum, int EngineTypeID, out DataTable dt)
        {
            string procedureName = "电子看板_工位机型_质量数据测量位置_临时表_查询";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opNum);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@产品型号代码", EngineTypeID);
            return DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
        }

        /// <summary>
        /// 生产计划_派工订单_入站扫码验证
        /// </summary>
        /// <param name="dt"></param>
        static public string GetPartCodeByBarCodde(string OpName, string XTCode)
        {
            string procedureName = "生产计划_派工订单_入站扫码验证";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@箱体码", XTCode);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@工位号", OpName);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out DataTable dt);
            if(dt.Rows.Count > 0)
            {
                return dt.Rows[0]["产品码"].ToString();
            }
            return "";
        }


        /// <summary>
        /// 测量数据合格_查询
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="engineID"></param>
        /// <param name="tb"></param>
        /// <returns></returns>
        static public bool QualitySelect(string opName, string engineID, out DataTable dt)
        {
            string procedureName = "测量数据合格_查询";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@发动机号", engineID);
            return DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
        }
        /// <summary>
        /// 生产计划_派工订单_查询_工位执行_完工判断
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="isCompleted"></param>
        /// <param name="num_plan"></param>
        /// <param name="num_complete"></param>
        public static void Select_OrderFormList_OrderComplete(string opName, out int isCompleted, out int num_plan, out int num_complete)
        {
            isCompleted = 0;//订单数量是否完全上线
            num_plan = 0;//计划数量
            num_complete = 0;//上线数量
            try
            {
                string procedureName;
                procedureName = "生产计划_派工订单_查询_工位执行_完工判断";
                SqlParameter[] thisParms = new SqlParameter[4];
                thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
                thisParms[1] = new System.Data.SqlClient.SqlParameter("@ISCOMPLETE", SqlDbType.Int, 4, "ISCOMPLETE");
                thisParms[1].Direction = ParameterDirection.Output;
                thisParms[2] = new System.Data.SqlClient.SqlParameter("@NUM_PLAN", SqlDbType.Int, 4, "NUM_PLAN");
                thisParms[2].Direction = ParameterDirection.Output;
                thisParms[3] = new System.Data.SqlClient.SqlParameter("@NUM_COMPLETE", SqlDbType.Int, 4, "NUM_COMPLETE");
                thisParms[3].Direction = ParameterDirection.Output;

                DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
                if (thisParms[1].Value != null && thisParms[2].Value != null && thisParms[3].Value != null)
                {
                    isCompleted = Convert.ToInt32(thisParms[1].Value);
                    num_plan = Convert.ToInt32(thisParms[2].Value);
                    num_complete = Convert.ToInt32(thisParms[3].Value);
                }
            }
            catch
            {
                isCompleted = 0;
                num_plan = 0;
                num_complete = 0;
            }
        }
        /// <summary>
        /// 生产计划_派工订单_查询_工位执行
        /// </summary>
        /// <param name="opName">工位号</param>
        /// <param name="dt"></param>
        static public void OrderFormList_Select(string opName, out DataTable dt)
        {
            dt = null;
            string sql = "exec 生产计划_派工订单_查询_工位执行 '" + opName + "'";
            DataAccess.ExecuteDataTable(sql, out dt);
        }
        /// <summary>
        /// MES界面显示_弹窗消息_查询
        /// </summary>
        /// <param name="opname"></param>
        /// <param name="tb"></param>
        /// <returns></returns>
        public static bool ShowWindowNotice(string opname, out DataTable dt)
        {
            string procedureName = "MES界面显示_弹窗消息_查询";
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@工位号", opname);
            return DataAccess.ExecuteStoredProcedure(procedureName, ref sqlParameters, out dt);
        }

        /// <summary>
        /// MES界面显示_消息滚动_查询
        /// </summary>
        /// <param name="opname"></param>
        /// <param name="tb"></param>
        /// <returns></returns>
        public static bool ShowLEDNotice(string opname, out DataTable dt)
        {
            string procedureName = "MES界面显示_消息滚动_查询";
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@工位号", opname);
            return DataAccess.ExecuteStoredProcedure(procedureName, ref sqlParameters, out dt);
        }
        /// <summary>
        /// 电子看板_工位机型_物料检测测量位置_查询_物料检测
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        static public bool MaterialCheckSelect(string opName, out DataTable dt)
        {
            string procedureName = "电子看板_工位机型_物料检测测量位置_查询_物料检测";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            return DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
        }
        /// <summary>
        /// 电芯进站 离站
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="notice"></param>
        /// <returns></returns>
        public static bool Cell_CheckDataAdd(string opName, string EngineID, string operationTime, int isOK)
        {
            string produceName = "测量数据合格_增加数据_电芯";
            SqlParameter[] thisParms = new SqlParameter[4];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@发动机号", EngineID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@操作时间", operationTime);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@合格标志", isOK);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }
        public static bool Cell_PassStation(string opName, string EngineID, string operationTime, int passType, int isOK = 1)
        {
            string produceName = "测量数据发动机在线状态_增加数据_电芯";
            SqlParameter[] thisParms = new SqlParameter[5];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@发动机号", EngineID);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@操作时间", operationTime);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@合格标志", isOK);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@过站类型", passType);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }

        public static bool GetCellParamsForK(string Id_tagCF_DC_Str, out DataTable dt)
        {
            string procedureName = "MOM_质量数据_上传_电池K_查询";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@Id_tagCF_DC_Str", Id_tagCF_DC_Str);
            return DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
        }
        public static bool GetPartQualityGroupParams(string opName, string EngineID, out DataTable dt)
        {
            string procedureName = "MOM_质量数据_上传_查询";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@发动机号", EngineID);
            return DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
        }

        public static bool SetPartQualityGroupParams(string opName, string EngineID)
        {
            string procedureName = "MOM_数据收集组_实时表_增加";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@工件编号", EngineID);
            return DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }

        /// <summary>
        /// 报警接口记录 存储
        /// 关机（0）/开机（1）/报警（2）/故障（3） 运行（4） 待机（5）
        /// 注意事项：当设备状态发生变化时，给表里插入一条数据。状态时间字段精确到年月日时分秒。当状态没变化时，不用重复去存数据，不同的故障告警，认为是不同的状态，当前如果是报警1，先存一条报警1，如果一直是报警1，就不用重复存报警1，当又产生了报警2时，需要给表里再存一条报警2。场景如下：
        ////1.开机后，状态先存一条开机，3秒后，再继续存一条待机
        ////2.设备运行后，存一条运行
        ////3.有故障1时，存一条故障，内容为故障1
        ////4.有故障2时，存一条故障，内容为故障2
        ////5.故障1解除后，先存一条运行，如果还有故障2, 继续再存一条故障，内容为故障2
        ////6.故障2解除后，存一条运行或者待机（看设备具体是运行还是待机状态）
        /// </summary>
        /// <param name="opName">工位号</param>
        /// <param name="DeviceStatus">状态代码</param>
        /// <param name="MalfunctionCode">故障代码 如状态为故障/报警，记录故障/报警代码。</param>
        /// <param name="MalfunctionName">故障名称 如状态为故障/报警，记录故障/报警名称</param>
        /// <param name="Malfunctionexplain">故障说明 如状态为故障/报警，记录故障/报警说明。</param>
        /// <param name="Remark">备注</param>
        /// <returns></returns>
        public static bool errorPortSave(string opName, string DeviceStatus, string MalfunctionCode = null, string MalfunctionName = null, string Malfunctionexplain = null, string Remark = null)
        {
            string produceName = "接口_工位运行状态_增加";
            SqlParameter[] thisParms = new SqlParameter[6];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@状态代码", DeviceStatus);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@故障代码", MalfunctionCode);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@故障名称", MalfunctionName);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@故障说明", Malfunctionexplain);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@备注", Remark);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }
        /// <summary>
        /// MES界面显示_弹窗消息_增加
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="notice"></param>
        /// <returns></returns>
        public static bool Web_Notice_Insert(string opName, string notice)
        {
            string produceName = "MES界面显示_弹窗消息_增加";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@Notice", notice);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }
        /// <summary>
        /// 基本数据_工作日历_班次产量查询_New
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="opNameCount">在线产量</param>
        /// <param name="opNameCountTime">在线节拍</param>
        static public void OpNameCount_New(string opName, out int opNameCount, out int opNameCountTime)
        {
            opNameCount = 0;
            opNameCountTime = 0;
            string procedureName;
            procedureName = "基本数据_工作日历_班次产量查询_New";
            SqlParameter[] thisParms = new SqlParameter[3];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@在线产量", 0);
            thisParms[1].Direction = ParameterDirection.Output;
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@在线节拍", 0);
            thisParms[2].Direction = ParameterDirection.Output;
            if (DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms))
            {
                opNameCount = Convert.ToInt32(thisParms[1].Value);
                opNameCountTime = Convert.ToInt32(thisParms[2].Value);
            }
        }
        /// <summary>
        /// 基本数据_工作日历_班次产量查询_New
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="opNameCount">在线产量</param>
        /// <param name="opNameCountTime">在线节拍</param>
        static public void OpNameCount_BQ(string opName, out int opNameCount, out int opNameCountTime)
        {
            opNameCount = 0;
            opNameCountTime = 0;
            string procedureName;
            procedureName = "基本数据_工作日历_班次产量查询_北汽";
            SqlParameter[] thisParms = new SqlParameter[3];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@在线产量", 0);
            thisParms[1].Direction = ParameterDirection.Output;
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@在线节拍", 0);
            thisParms[2].Direction = ParameterDirection.Output;
            if (DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms))
            {
                opNameCount = Convert.ToInt32(thisParms[1].Value);
                opNameCountTime = Convert.ToInt32(thisParms[2].Value);
            }
        }
        /// <summary>
        /// 电子看板_工位机型_质量数据测量位置_临时表_增加
        /// </summary>
        /// <param name="opName"></param>
        /// <returns></returns>
        public static bool MesServer_ShaftPosition_EngineTypeOver(string opName)
        {
            string produceName = "电子看板_工位机型_质量数据测量位置_临时表_增加";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }

        /// <summary>
        /// 电子看板_工位机型_质量数据测量位置_临时表_查询_完成情况
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="workGroupIndex"></param>
        /// <param name="BoltCode"></param>
        /// <param name="sn_WorkingStepIsOver"></param>
        /// <returns></returns>
        public static bool MesServer_ShaftPosition_Select_FinishResult(string opName, out int workGroupIndex, out string BoltCode, out bool sn_WorkingStepIsOver, out bool isHaveNg)
        {
            workGroupIndex = 0;
            BoltCode = "";
            sn_WorkingStepIsOver = true;
            isHaveNg = false;

            string produceName = "电子看板_工位机型_质量数据测量位置_临时表_查询_完成情况";
            SqlParameter[] thisParms = new SqlParameter[5];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@组号", SqlDbType.Int, 4);
            thisParms[1].Direction = ParameterDirection.Output;
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@套筒号", SqlDbType.NVarChar, 50);
            thisParms[2].Direction = ParameterDirection.Output;
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@是否全部完成", SqlDbType.Int, 4);
            thisParms[3].Direction = ParameterDirection.Output;
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@是否全部完成", SqlDbType.Int, 4);
            thisParms[4].Direction = ParameterDirection.Output;
            if (DataAccess.ExecuteStoredProcedure(produceName, ref thisParms))
            {
                workGroupIndex = Convert.ToInt32(thisParms[1].Value);
                BoltCode = thisParms[2].Value.ToString();
                sn_WorkingStepIsOver = Convert.ToBoolean(thisParms[3].Value);
                isHaveNg = Convert.ToBoolean(thisParms[3].Value);
                return true;
            }
            return false;
        }


        /// <summary>
        /// 电子看板_工位机型_质量数据测量位置_临时表_修改
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="workGroupIndex"></param>
        /// <param name="IsOK"></param>
        /// <param name="IsFinish"></param>
        /// <returns></returns>
        public static bool MesServer_ShaftPosition_Update(string opName, int workGroupIndex, int IsOK, out bool IsFinish)
        {
            IsFinish = true;
            string produceName = "电子看板_工位机型_质量数据测量位置_临时表_修改";
            SqlParameter[] thisParms = new SqlParameter[4];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@组号", workGroupIndex);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@合格标志", IsOK);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@是否全部完成", SqlDbType.Int, 4);
            thisParms[3].Direction = ParameterDirection.Output;
            if (DataAccess.ExecuteStoredProcedure(produceName, ref thisParms))
            {
                IsFinish = Convert.ToBoolean(thisParms[3].Value);
                return true;
            }
            return false;
        }
        /// <summary>
        /// 电子看板_工位机型_质量数据测量位置_临时表_查询_完成情况
        /// </summary>
        /// <param name="opName">工位号</param>
        /// <param name="ProcessCode">拧紧序号</param>
        /// <param name="BoltCode">套筒号</param>
        /// <param name="workStepProperty">工步属性</param>
        /// <param name="sn_WorkingStepIsOver">是否全部完成</param>
        /// <returns></returns>
        public static bool MesServer_ShaftPosition_Select_FinishResult(string opName, out int ProcessCode, out int BoltCode, out int workStepProperty, out bool sn_WorkingStepIsOver, out bool isHaveNg)
        {
            BoltCode = 0;
            ProcessCode = 512;
            workStepProperty = 0;
            sn_WorkingStepIsOver = true;
            isHaveNg = false;

            string produceName = "电子看板_工位机型_质量数据测量位置_临时表_查询_完成情况";
            SqlParameter[] thisParms = new SqlParameter[6];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@拧紧序号", SqlDbType.Int, 4);
            thisParms[1].Direction = ParameterDirection.Output;
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@套筒号", SqlDbType.Int, 4);
            thisParms[2].Direction = ParameterDirection.Output;
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@工步属性", SqlDbType.Int, 4);
            thisParms[3].Direction = ParameterDirection.Output;
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@是否全部完成", SqlDbType.Int, 4);
            thisParms[4].Direction = ParameterDirection.Output;
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@是否存在不合格", SqlDbType.Int, 4);
            thisParms[5].Direction = ParameterDirection.Output;
            if (DataAccess.ExecuteStoredProcedure(produceName, ref thisParms))
            {
                ProcessCode = Convert.ToInt32(thisParms[1].Value);
                BoltCode = Convert.ToInt32(thisParms[2].Value);
                workStepProperty = Convert.ToInt32(thisParms[3].Value);
                sn_WorkingStepIsOver = Convert.ToBoolean(thisParms[4].Value);
                isHaveNg = Convert.ToBoolean(thisParms[5].Value);
                return true;
            }
            return false;
        }


        /// <summary>
        /// 电子看板_工位机型_质量数据测量位置_临时表_修改_重新拧紧
        /// </summary>
        /// <param name="opName">工位号</param>
        /// <param name="ProcessCode">拧紧序号</param>
        /// <returns></returns>
        public static bool MesServer_ShaftPosition_FixAllow_Again(string opName, int ProcessCode)
        {
            string produceName = "电子看板_工位机型_质量数据测量位置_临时表_修改_重新拧紧";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@组号", ProcessCode);
            if (DataAccess.ExecuteStoredProcedure(produceName, ref thisParms))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 测量数据项目
        /// 用于B5A项目
        /// </summary>
        /// <returns></returns>
        public static DataTable BaseData_tagTable_QualityData_Item_B5A()
        {
            DataTable dt;
            string sql;
            sql = "SELECT  TagID,0 as TagValue,TagName,工位号,数据来源,TagTypeID,TagLong,列位置,测量位置,变量类型代码,测量项目,TagAdr,TagAdrDemo,变量特点,变量排序   FROM [MES_基本变量_原始数据] "
                + " where (TagID <> 0 )  and "
            + "  ((变量类型代码 = 9)  "
            + " or (变量类型代码 = 10)  "
            + " or (变量类型代码 = 11)  "
            + " or (变量类型代码 = 80)  "
            + " or (变量类型代码 = 101) "
            + " or (变量类型代码 = 102) "

            + " or (变量类型代码 = 78) "
            + " or (变量类型代码 = 79) "
            + " or (变量类型代码 = 41) "
            + " or (变量类型代码 = 51) "
            + " or (变量类型代码 = 8) "
            + " or (变量类型代码 = 21) "

            + " or (变量类型代码 = 105)) "
            + " and (是否启用 = 1)";

            DataAccess.ExecuteDataTable(sql, out dt);
            return dt;
        }

        /// <summary>
        /// 测量数据合格索引_分线数据_删除
        /// </summary>
        /// <param name="engineID"></param>
        /// <param name="opName"></param>
        /// <returns></returns>
        public static bool BaseData_tagTable_QualityData_B5A_SubTable_Delete
                                            (string engineID, string opName)
        {
            string procedureName;
            procedureName = "测量数据合格索引_分线数据_删除";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@EngineID", engineID);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            return DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }
        /// <summary>
        /// MES_装配数据_零部件追溯_增加_不合格
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="m_ConfirmOkGo"></param>
        /// <returns></returns>
        public static bool Material_Insert_NG(string opName, int m_ConfirmOkGo)
        {
            string produceName = "MES_装配数据_零部件追溯_增加_不合格";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@放行方式", m_ConfirmOkGo);
            return DataAccess.ExecuteStoredProcedure(produceName, ref thisParms);
        }
        /// <summary>
        /// 测量数据发动机在线状态_查询_是否已工作
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="engineType">新零件号(机型)</param>
        /// <returns></returns>
        public static DataTable EngineIsTwoWork(string opName, string OrderForm, string EngineID)
        {
            DataTable dt = new DataTable();
            string produceName = "测量数据发动机在线状态_查询_是否已工作";
            SqlParameter[] thisParms = new SqlParameter[3];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@订单号", OrderForm);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@总成号", EngineID);
            DataAccess.ExecuteStoredProcedure(produceName, ref thisParms, out dt);
            return dt;
        }

        /// <summary>
        /// 测量数据发动机在线状态_增加数据
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="isOK"></param>
        static public DataTable CheckAllFinish(string opName, string EngineID, int type)
        {
            string procedureName;
            DataTable dt = new DataTable();
            procedureName = "测量数据工位_测试工位状态_修改";
            SqlParameter[] thisParms = new SqlParameter[3];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@发动机号", EngineID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@type", type);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
            return dt;
        }

        static public void GetPackByXT(string opName, string EngineID, out string pack)
        {
            pack = "";
            string procedureName;
            DataTable dt = new DataTable();
            procedureName = "测量数据工位_静止工位获取PAC_通过箱体码";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@箱体码", EngineID);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
            if(dt != null && dt.Rows.Count > 0)
            {
                pack = dt.Rows[0]["发动机号"].ToString();
            }
         
        }
        /// <summary>
        /// 测量数据工位_监控系统_MOBY_查询_PDC
        /// </summary>
        /// <param name="EngineID"></param>
        /// <param name="OrderForm"></param>
        /// <param name="EngineType"></param>
        public static void Get_NotAllowWork(string EngineID, string opName, out bool NotAllowWork)
        {
            DataTable dt = new DataTable();
            NotAllowWork = false;
            try
            {
                string procedureName;
                procedureName = "工位状态监控MOBY_不允许工作_查询";
                SqlParameter[] thisParms = new SqlParameter[2];
                thisParms[0] = new System.Data.SqlClient.SqlParameter("@发动机号", EngineID);
                thisParms[1] = new System.Data.SqlClient.SqlParameter("@工位号", opName);

                DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
                if (dt.Rows.Count > 0)
                {
                    NotAllowWork = Convert.ToBoolean(dt.Rows[0]["不允许工作"]);
                }
            }
            catch
            {
                NotAllowWork = false;
            }
        }


        /// <summary>
        /// 报警工位记录表_新增
        /// </summary>
        /// <param name="TagID"></param>
        /// <param name="opName"></param>
        /// <param name="Text"></param>
        /// <param name="Status"></param>
        /// <param name="errType"></param>
        public static void Alarm_Insert2(int TagID, string opName, string Text, int Status, string errType, string createTime)
        {
            string procedureName;
            procedureName = "报警工位记录表_新增";
            SqlParameter[] thisParms = new SqlParameter[6];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@TagId", TagID);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@Text", Text);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@状态", Status);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@报警类型", errType);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@createTime", createTime);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }

        /// <summary>
        /// 保存 OP420 静置B位开始记录，同一产品只保留最新一条。
        /// </summary>
        public static 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>
        /// 工位打码记录 增加
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="BarCode"></param>
        /// <param name="LeftTop"></param>
        /// <param name="RightTop"></param>
        /// <param name="LeftBottom"></param>
        public static void OpPrintBarCodeRecord_Add(string OpName, string BarCode, string LeftTop, string RightTop, string LeftBottom)
        {
            string procedureName;
            procedureName = "工位_打码记录_增加";
            SqlParameter[] thisParms = new SqlParameter[5];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@OpName", OpName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@BarCode", BarCode);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@LeftTop", LeftTop);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@RightTop", RightTop);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@LeftBottom", LeftBottom);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }

        /// <summary>
        /// 报警工位记录表_MOBY_查询
        /// </summary>
        public static void Alarm_Moby_Search(out DataTable dt)
        {
            string procedureName;
            procedureName = "报警工位记录表_MOBY_查询";
            SqlParameter[] thisParms = new SqlParameter[0];
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out dt);
        }

        public static bool OP_Tight_isShowAgainWork(string opName)
        {
            string procedureName;
            procedureName = "电子看板_工位机型_质量数据测量位置_临时表_查询_是否重新拧紧弹窗";
            SqlParameter[] thisParms = new SqlParameter[1];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out DataTable dt);
            if(dt.Rows.Count > 0)
            {
                return Convert.ToInt32(dt.Rows[0]["isShowAgainWork"]) == 1;
            }
            return false;
        }

        public static void Save_FristWorkData(string opName,string EngineID)
        {
            string procedureName;
            procedureName = "电子看板_质量数据_首件数据_moby_首件数据记录";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@产品编号", EngineID);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out DataTable dt);
            if(dt.Rows.Count > 0)
            {
                int result = Convert.ToInt32(dt.Rows[0]["result"]);
                string msg = dt.Rows[0]["msg"].ToString();
                if(result == 1) MisDataFun.LogRecording_OP(opName, "INFO", "MIS", msg);
                if(result == 2) MisDataFun.LogRecording_OP(opName, "ERROR", "MIS", msg);
            }
        }

        public static string GetTempValue(string opName, string name)
        {
            string procedureName;
            procedureName = "MES_持久化变量_读取";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@名称", name);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out DataTable dt);
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0]["值"].ToString();
            }
            return "";
        }

        public static bool GetPartLoadIsAutoOnLineModel(string opName)
        {
            string autoModel = GetTempValue(opName, "自动申请条码");
            if (autoModel == "1") return true;
            else
            {
                MisDataFun.LogRecording_OP(opName, "WARNING", "MIS", "条码申请失败！当前非自动申请条码模式");
                return false;
            }
        }

        public static void TightenNoOkRecord(string opName, string engineID)
        {
            string procedureName;
            procedureName = "测量数据合格_拧紧不合格放行记录_新增";
            SqlParameter[] thisParms = new SqlParameter[2];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@产品编号", engineID);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms, out DataTable dt);
        }
    }
}
