using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrintBarCode
{
    public partial class PrintBarCodeForm
    {
        private DataTable PrintLogInset(string partNo, int partCount, int beginNumber, DateTime proDate)
        {
            //            ALTER PROCEDURE[dbo].[北汽福田打印条码日志_增加]
            //            @零件号 nvarchar(50),
            //@数量 int,
            //@开始号码 int,
            //@日期 date
            DataTable dt = new DataTable();
            string procedureName = "北汽福田打印条码日志_增加";
            SqlParameter[] sqlParameters = new SqlParameter[4];
            //string columnPartName = "column18";
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@零件号", partNo);
            sqlParameters[1] = new System.Data.SqlClient.SqlParameter("@数量", partCount);
            sqlParameters[2] = new System.Data.SqlClient.SqlParameter("@开始号码", beginNumber);
            sqlParameters[3] = new System.Data.SqlClient.SqlParameter("@日期", proDate);

            ExecuteStoredProcedure(procedureName, ConnectString_MES, ref sqlParameters, out dt);

            return dt;

        }
        private DataTable PrintSnInset(string sn, string partNo, int partCount, int beginNumber, DateTime proDate)
        {
            //            ALTER PROCEDURE[dbo].[北汽福田打印条码变速器序列号_增加]
            //            @变速器序列号 nvarchar(50),
            //@零件号 nvarchar(50),
            //@数量 int,
            //@开始号码 int,
            //@日期 date
            DataTable dt = new DataTable();
            string procedureName = "北汽福田打印条码变速器序列号_增加";
            SqlParameter[] sqlParameters = new SqlParameter[5];
            //string columnPartName = "column18";
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@变速器序列号", sn);
            sqlParameters[1] = new System.Data.SqlClient.SqlParameter("@零件号", partNo);
            sqlParameters[2] = new System.Data.SqlClient.SqlParameter("@数量", partCount);
            sqlParameters[3] = new System.Data.SqlClient.SqlParameter("@开始号码", beginNumber);
            sqlParameters[4] = new System.Data.SqlClient.SqlParameter("@日期", proDate);

            ExecuteStoredProcedure(procedureName, ConnectString_MES, ref sqlParameters, out dt);

            return dt;

        }
        private DataTable PrintSnUpdate(string sn, string partNo, int partCount, int beginNumber, DateTime proDate)
        {
            DataTable dt = new DataTable();
            string procedureName = "北汽福田打印条码变速器序列号_更新";
            SqlParameter[] sqlParameters = new SqlParameter[5];
            //string columnPartName = "column18";
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@变速器序列号", sn);
            sqlParameters[1] = new System.Data.SqlClient.SqlParameter("@零件号", partNo);
            sqlParameters[2] = new System.Data.SqlClient.SqlParameter("@数量", partCount);
            sqlParameters[3] = new System.Data.SqlClient.SqlParameter("@开始号码", beginNumber);
            sqlParameters[4] = new System.Data.SqlClient.SqlParameter("@日期", proDate);

            ExecuteStoredProcedure(procedureName, ConnectString_MES, ref sqlParameters, out dt);

            return dt;

        }
        /// <summary>
        /// 0:没有打印过
        /// 1：打印过
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        private int PrintSnIsPrinted(string sn)
        {
            //北汽福田打印条码变速器序列号_是否打印过
            //            @变速器序列号 nvarchar(50),

            DataTable dt = new DataTable();
            string procedureName = "北汽福田打印条码变速器序列号_是否打印过";
            SqlParameter[] sqlParameters = new SqlParameter[1];
            //string columnPartName = "column18";
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@变速器序列号", sn);


            ExecuteStoredProcedure(procedureName, ConnectString_MES, ref sqlParameters, out dt);
            int count;
            count = Convert.ToInt32(dt.Rows[0][0]);
            return count;
        }

        /// <summary>
        /// @工位号 nvarchar(50),
        /// @TagValue nvarchar(50),
        /// @变量类型代码 nvarchar(50)
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="tagValue"></param>
        /// <param name="tagTypeID"></param>
        private void OpNameMonitorInsert(string opName,string tagValue,string tagTypeID)
        {
            string procedureName = "北汽福田工位状态监控_增加";
            SqlParameter[] sqlParameters = new SqlParameter[3];
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@opName", opName);
            sqlParameters[1] = new System.Data.SqlClient.SqlParameter("@tagValue", tagValue);
            sqlParameters[2] = new System.Data.SqlClient.SqlParameter("@tagTypeID", tagTypeID);


            ExecuteStoredProcedure(procedureName, ConnectString_MES, ref sqlParameters);
        }
        public static bool ExecuteStoredProcedure(string procedureName, string connectionString, ref SqlParameter[] sqlParameters)
        {
            DataTable dt;
            return ExecuteStoredProcedure(procedureName, connectionString, ref sqlParameters, out dt);

        }
        public static bool ExecuteStoredProcedure(string procedureName, string connectionString, ref SqlParameter[] sqlParameters, out DataTable dt)
        {

            bool result = false;
            string errorMessage;
            errorMessage = "";
            dt = new DataTable();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                try
                {
                    conn.Open();
                    using (SqlDataAdapter dsCommand = new SqlDataAdapter())
                    {
                        dsCommand.SelectCommand = new SqlCommand(procedureName, conn);
                        dsCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
                        dsCommand.SelectCommand.CommandTimeout = 0;
                        if (sqlParameters != null)
                        {
                            for (int i = 0; i < sqlParameters.Length; i++)
                            {
                                dsCommand.SelectCommand.Parameters.Add(sqlParameters[i]);
                            }
                        }

                        dsCommand.Fill(dt);
                    }
                    result = true;
                }
                catch (Exception e)
                {
                    errorMessage = e.ToString();
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
            return result;
        }



    }
}
