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

namespace SystemX.Common.Protocol.SIA
{
    using ParamElement = Tuple<SqlDbType, int, byte, byte>;

    public class HISTTesterSummary
    {
        public string StationName { set; get; }
        public string TestType { set; get; }
        public string Version { set; get; }
        public string ProdCode { set; get; }
        public int TestListFileNo { set; get; }
        public int TestListVariantNo { set; get; }
        public string TestListCntID { set; get; }

        public int StepVersion { set; get; }
        public string Host { set; get; }
        public string Section { set; get; }
        public string ProdNoC { set; get; }
        public string ProdNoP { set; get; }
        public string TestCode { set; get; }
        public string TestListFileName { set; get; }
        public string ProductID { set; get; }
        public string Result { set; get; }
        public string Duration { set; get; }
        public DateTime TestDT { set; get; }
    }

    public class HISTLogSummary 
    {
        public Int64 AccessStart { set; get; }
        public Int64 AccessEnd { set; get; }
        public Int64 LogNo { set; get; }
        public int LogCount { set; get; }
        public string StationName { set; get; }
        public string TestType { set; get; }
        public string Version { set; get; }
        public string ProdCode { set; get; }
        public int TestListFileNo { set; get; }
        public int TestListVariantNo { set; get; }
        public string TestListCntID { set; get; }

        public int StepVersion { set; get; }
        public string Host { set; get; }
        public string Section { set; get; }
        public string ProdNoC { set; get; }
        public string ProdNoP { set; get; }
        public string TestCode { set; get; }
        public string TestListFileName { set; get; }
        public string ProductID { set; get; }
        public string Result { set; get; }
        public string Duration { set; get; }
        public DateTime TestDT { set; get; }
    }

    public static class ParamSet
    {
        public static Dictionary<string, ParamElement> fParam= new Dictionary<string, ParamElement>();

        public static void TesterSummaryField()
        {
            fParam.Clear();
            fParam = new Dictionary<string, ParamElement>();
            fParam.Add("StationName", new ParamElement(SqlDbType.NVarChar, 128, 0, 0));
            fParam.Add("TestType", new ParamElement(SqlDbType.NVarChar, 8, 0, 0));
            fParam.Add("Version", new ParamElement(SqlDbType.NVarChar, 4, 0, 0));
            fParam.Add("ProdCode", new ParamElement(SqlDbType.NVarChar, 4, 0, 0));
            fParam.Add("TestListFileNo", new ParamElement(SqlDbType.Int, 0, 0, 0));
            fParam.Add("TestListVariantNo", new ParamElement(SqlDbType.Int, 0, 0, 0));
            fParam.Add("TestListCntID", new ParamElement(SqlDbType.NVarChar, 256, 0, 0));
            fParam.Add("StepVersion", new ParamElement(SqlDbType.Int, 0, 0, 0));
            fParam.Add("HostID", new ParamElement(SqlDbType.NVarChar, 64, 0, 0));
            fParam.Add("Section", new ParamElement(SqlDbType.NVarChar, 64, 0, 0));
            fParam.Add("ProdNo_C", new ParamElement(SqlDbType.NVarChar, 32, 0, 0));
            fParam.Add("ProdNo_P", new ParamElement(SqlDbType.NVarChar, 32, 0, 0));
            fParam.Add("Testcode", new ParamElement(SqlDbType.NVarChar, 16, 0, 0));
            fParam.Add("TestListFileName", new ParamElement(SqlDbType.NVarChar, 256, 0, 0));
            fParam.Add("ProductID", new ParamElement(SqlDbType.NVarChar, 64, 0, 0));
            fParam.Add("Result", new ParamElement(SqlDbType.NVarChar, 16, 0, 0));
            fParam.Add("Duration", new ParamElement(SqlDbType.NVarChar, 16, 0, 0));
            fParam.Add("TestDT", new ParamElement(SqlDbType.DateTime2, 7, 0, 0));
        }

        public static void LogSummaryField()
        {
            fParam.Clear();
            fParam = new Dictionary<string, ParamElement>();
            fParam.Add("AccessStart", new ParamElement(SqlDbType.BigInt, 0, 0, 0));
            fParam.Add("AccessEnd", new ParamElement(SqlDbType.BigInt, 0, 0, 0));
            fParam.Add("LogNo", new ParamElement(SqlDbType.BigInt, 0, 0, 0));
            fParam.Add("LogCount", new ParamElement(SqlDbType.Int, 0, 0, 0));
            fParam.Add("StationName", new ParamElement(SqlDbType.NVarChar, 128, 0, 0));
            fParam.Add("TestType", new ParamElement(SqlDbType.NVarChar, 8, 0, 0));
            fParam.Add("Version", new ParamElement(SqlDbType.NVarChar, 4, 0, 0));
            fParam.Add("ProdCode", new ParamElement(SqlDbType.NVarChar, 4, 0, 0));
            fParam.Add("TestListFileNo", new ParamElement(SqlDbType.Int, 0, 0, 0));
            fParam.Add("TestListVariantNo", new ParamElement(SqlDbType.Int, 0, 0, 0));
            fParam.Add("TestListCntID", new ParamElement(SqlDbType.NVarChar, 256, 0, 0));
            fParam.Add("StepVersion", new ParamElement(SqlDbType.Int, 0, 0, 0));
            fParam.Add("HostID", new ParamElement(SqlDbType.NVarChar, 64, 0, 0));
            fParam.Add("Section", new ParamElement(SqlDbType.NVarChar, 64, 0, 0));
            fParam.Add("ProdNo_C", new ParamElement(SqlDbType.NVarChar, 32, 0, 0));
            fParam.Add("ProdNo_P", new ParamElement(SqlDbType.NVarChar, 32, 0, 0));
            fParam.Add("Testcode", new ParamElement(SqlDbType.NVarChar, 16, 0, 0));
            fParam.Add("TestListFileName", new ParamElement(SqlDbType.NVarChar, 256, 0, 0));
            fParam.Add("ProductID", new ParamElement(SqlDbType.NVarChar, 64, 0, 0));
            fParam.Add("Result", new ParamElement(SqlDbType.NVarChar, 16, 0, 0));
            fParam.Add("Duration", new ParamElement(SqlDbType.NVarChar, 16, 0, 0));
            fParam.Add("TestDT", new ParamElement(SqlDbType.DateTime2, 7, 0, 0));
        }

        public static void TestResultField()
        {
            fParam.Clear();
            fParam = new Dictionary<string, ParamElement>();
            fParam.Add("AccessKey", new ParamElement(SqlDbType.BigInt, 0, 0, 0));
            fParam.Add("StepID", new ParamElement(SqlDbType.BigInt, 0, 0, 0));
            fParam.Add("MeasVal", new ParamElement(SqlDbType.Decimal, 0, 15, 5));
            fParam.Add("MeasValStr", new ParamElement(SqlDbType.NVarChar, 1024, 0, 0));
            fParam.Add("Message", new ParamElement(SqlDbType.NVarChar, 2048, 0, 0));
            fParam.Add("GlobalMin", new ParamElement(SqlDbType.NVarChar, 1024, 0, 0));
            fParam.Add("GlobalMax", new ParamElement(SqlDbType.NVarChar, 1024, 0, 0));
            fParam.Add("Result", new ParamElement(SqlDbType.NVarChar, 16, 0, 0));
            fParam.Add("SpentTime", new ParamElement(SqlDbType.NVarChar, 64, 0, 0));
            fParam.Add("DataDT", new ParamElement(SqlDbType.DateTime2, 7, 0, 0));
        }

        public static SqlParameter GetMakeSqlParameterInfo(Dictionary<string, ParamElement> refField, string strSetName, object objValue)
        {
            if (refField.ContainsKey(strSetName))
            {
                SqlParameter param = null;

                if (refField[strSetName].Item2 != 0)
                    param = new SqlParameter("@" + strSetName, refField[strSetName].Item1, refField[strSetName].Item2);
                else
                    param = new SqlParameter("@" + strSetName, refField[strSetName].Item1);

                if (refField[strSetName].Item1 == SqlDbType.Decimal)
                {
                    param.Precision = refField[strSetName].Item3;
                    param.Scale = refField[strSetName].Item4;
                }

                param.Value = objValue;
                return param;
            }
            else
                return null;
        }
    }

    public static class LogParamInfo
    {
        public static readonly int MessageLength = 2048;
        public static readonly int MessageValLength = 1024;

        public static string STEP = "";
        public static string POSITION = "";
        public static string MO = "";
        public static string FNC_NAME = "";
        public static string MIN = "";
        public static string MEASURE = "";
        public static string MAX = "";
        public static string DIM = "";
        public static string CHECK = "";
        public static string SPENT_TIME = "";
        public static string INFO = "";

        public static bool GLOBAL_SPEC = false;
        public static string VRFY_MIN = "";
        public static string VRFY_MAX = "";
    }

    public class CustomProtocol_ : BASE_PROTOCOL
    {
        public CustomProtocol_() : base()
        {
        }

        public CustomProtocol_(ushort usGetCommnad, ushort usGetSubCommand) : base(usGetCommnad, usGetSubCommand)
        {
        }

        public CustomProtocol_(PROTOCOL_CODE SET_CODE) : base(SET_CODE)
        {
        }

        public string CheckTestListUpdate(string strProdC, string strTestCode, string strTestType, string strVersion, string strProdCode)
        {
            StringBuilder sbSetQuery = new StringBuilder();

            /*
            sbSetQuery.Append("SELECT A.TestListNo, A.ProdNo_C, B.ProdNo_P, B.UpdateDT, D.TestCode, " +
              "B.TestType, B.Version, B.ProdCode FROM PROD_Release AS A WITH(NOLOCK) ");
            */

            sbSetQuery.Append("SELECT A.VariantNo AS 'TestListNo', A.ProdNo_C, B.ProdNo_P, B.UpdateDT, D.TestCode, " +
              "E.TestType, E.Version, E.ProdCode, B.TestListFileNo, A.VariantNo, E.UpdateDT AS 'TestListFileUpdateDT' FROM PROD_Release AS A WITH(NOLOCK) ");

            sbSetQuery.Append("INNER JOIN(SELECT * FROM [PROD_Variant] WITH(NOLOCK)) AS B ");
            sbSetQuery.Append("ON A.VariantNo = B.No ");
            sbSetQuery.Append("INNER JOIN(SELECT * FROM [PROD_Group] WITH(NOLOCK)) AS C ");
            sbSetQuery.Append("ON B.GroupNo = C.No ");
            sbSetQuery.Append("INNER JOIN(SELECT * FROM [STAT_TestCode] WITH(NOLOCK)) AS D ");
            sbSetQuery.Append("ON A.TestCodeNo = D.No ");
            sbSetQuery.Append("INNER JOIN(SELECT * FROM [STOR_TestListFile] WITH(NOLOCK)) AS E ");
            sbSetQuery.Append("ON B.TestListFileNo = E.No ");

            sbSetQuery.Append("WHERE A.ProdNo_C = '" + strProdC + "' ");
            if (strTestCode.Length > 0)
                sbSetQuery.Append("AND D.TestCode = '" + strTestCode + "' ");
            if (strTestType.Length > 0)
                sbSetQuery.Append("AND E.TestType = '" + strTestType + "' ");
            if (strVersion.Length > 0)
                sbSetQuery.Append("AND E.Version = '" + strVersion + "' ");
            if (strProdCode.Length > 0)
                sbSetQuery.Append("AND E.ProdCode = '" + strProdCode + "' ");

            return sbSetQuery.ToString();

            /*
            string[] strGetTestListFindInfo = new string[3];
            strGetTestListFindInfo[0] = strTestType;
            strGetTestListFindInfo[1] = strVersion;
            strGetTestListFindInfo[2] = strProdCode;

            bool bFirstAppend = false;
            int iParameterCnt = 0;
            for (int i = 0; i < strGetTestListFindInfo.Length; i++)
            {
                if (strGetTestListFindInfo[i].Length > 0)
                {
                    iParameterCnt++;

                    if (bFirstAppend == false)
                    {
                        bFirstAppend = true;

                        sbSetQuery.Append("WHERE ");
                    }

                    if (iParameterCnt > 1)
                        sbSetQuery.Append("AND ");

                    switch (i)
                    {
                        case 0:
                            sbSetQuery.Append("TestType = '" + strGetTestListFindInfo[i] + "' ");
                            break;
                        case 1:
                            sbSetQuery.Append("Version = '" + strGetTestListFindInfo[i] + "' ");
                            break;
                        case 2:
                            sbSetQuery.Append("ProdCode = '" + strGetTestListFindInfo[i] + "' ");
                            break;
                    }
                }
            }
            sbSetQuery.Append(") AS B ");

            sbSetQuery.Append("ON B.No = A.TestListNo ");
            sbSetQuery.Append("INNER JOIN(SELECT * FROM PROD_Group AS Y WITH(NOLOCK)) AS C ");
            sbSetQuery.Append("ON C.No = B.GroupNo ");

            if (strTestCode.Length > 0)
                sbSetQuery.Append("INNER JOIN(SELECT * FROM STAT_TestCode AS Z WITH(NOLOCK) WHERE Z.TestCode = '" + strTestCode + "') AS D ");
            else
                sbSetQuery.Append("INNER JOIN(SELECT * FROM STAT_TestCode AS Z WITH(NOLOCK)) AS D ");

            sbSetQuery.Append("ON A.ProdNo_C = '" + strProdC + "' AND B.No = A.TestListNo AND D.No = A.TestCodeNo;");
            */

            return sbSetQuery.ToString();
        }

        public string QueryFindTestList(PROCESS_PACKET getPaket, bool bJustCheck = false)
        {
            StringBuilder sbSetQuery = new StringBuilder();

            /*
            if (bJustCheck)
            {
                sbSetQuery.Append("SELECT A.ProdNo_C, B.ProdNo_P, B.UpdateDT, D.TestCode, D.Gate1, D.Gate2, " +
                    "B.FileName, A.RegUserComment, E.Description, C.GroupName, C.ModelName, " +
                    "A.TestListNo, A.Config, " +
                    "B.TestType, B.Version, B.ProdCode, B.UseTLPosition FROM PROD_Release AS A ");
            }
            else
            {
                sbSetQuery.Append("SELECT A.ProdNo_C, B.ProdNo_P, B.UpdateDT, D.TestCode, D.Gate1, D.Gate2, " +
                    "B.FileName, A.RegUserComment, E.Description, C.GroupName, C.ModelName, " +
                    "A.TestListNo, A.Config, " +
                    "B.TestType, B.Version, B.ProdCode, B.TestListData, B.UseTLPosition FROM PROD_Release AS A ");
            }
            */

            if (bJustCheck)
            {
                sbSetQuery.Append("SELECT A.ProdNo_C, B.ProdNo_P, B.UpdateDT, D.TestCode, D.Gate1, D.Gate2, " +
                    "E.FileName, A.RegUserComment, E.Description, C.GroupName, C.ModelName, " +
                    "A.VariantNo AS 'TestListNo', A.Config, " +
                    "E.TestType, E.Version, E.ProdCode, B.UseTLPosition, B.TestListFileNo, E.UpdateDT AS 'TestListFileUpdateDT', A.VariantNo FROM [PROD_Release] AS A WITH(NOLOCK) ");
            }
            else
            {
                sbSetQuery.Append("SELECT A.ProdNo_C, B.ProdNo_P, B.UpdateDT, D.TestCode, D.Gate1, D.Gate2, " +
                    "E.FileName, A.RegUserComment, E.Description, C.GroupName, C.ModelName, " +
                    "A.VariantNo AS 'TestListNo', A.Config, " +
                    "E.TestType, E.Version, E.ProdCode, E.TestListData, B.UseTLPosition, B.TestListFileNo, E.UpdateDT AS 'TestListFileUpdateDT', A.VariantNo FROM [PROD_Release] AS A WITH(NOLOCK) ");
            }

            sbSetQuery.Append("INNER JOIN(SELECT * FROM [PROD_Variant] WITH(NOLOCK)) AS B ");
            sbSetQuery.Append("ON A.VariantNo = B.No ");
            sbSetQuery.Append("INNER JOIN(SELECT * FROM [PROD_Group] WITH(NOLOCK)) AS C ");
            sbSetQuery.Append("ON B.GroupNo = C.No ");
            sbSetQuery.Append("INNER JOIN(SELECT * FROM [STAT_TestCode] WITH(NOLOCK)) AS D ");
            sbSetQuery.Append("ON A.TestCodeNo = D.No ");
            sbSetQuery.Append("INNER JOIN(SELECT * FROM [STOR_TestListFile] WITH(NOLOCK)) AS E ");
            sbSetQuery.Append("ON B.TestListFileNo = E.No ");
            sbSetQuery.Append("WHERE A.ProdNo_C = '" + getPaket.objProdNo_C[0].Data + "' ");

            if (getPaket.objTestCode[0].Data.Length > 0)
                sbSetQuery.Append("AND D.TestCode = '" + getPaket.objTestCode[0].Data + "' ");
            if (getPaket.objTestType[0].Data.Length > 0)
                sbSetQuery.Append("AND E.TestType = '" + getPaket.objTestType[0].Data + "' ");
            if (getPaket.objTestListFileVersion[0].Data.Length > 0)
                sbSetQuery.Append("AND E.Version = '" + getPaket.objTestListFileVersion[0].Data + "' ");
            if (getPaket.objProductionCode[0].Data.Length > 0)
                sbSetQuery.Append("AND E.ProdCode = '" + getPaket.objProductionCode[0].Data + "' ");

            return sbSetQuery.ToString();

            /*
            string[] strGetTestListFindInfo = new string[3];
            strGetTestListFindInfo[0] = getPaket.objTestType[0].Data;
            strGetTestListFindInfo[1] = getPaket.objTestListFileVersion[0].Data;
            strGetTestListFindInfo[2] = getPaket.objProductionCode[0].Data;

            bool bFirstAppend = false;
            int iParameterCnt = 0;
            for (int i = 0; i < strGetTestListFindInfo.Length; i++)
            {
                if (strGetTestListFindInfo[i].Length > 0)
                {
                    iParameterCnt++;

                    if (bFirstAppend == false)
                    {
                        bFirstAppend = true;

                        sbSetQuery.Append("WHERE ");
                    }

                    if (iParameterCnt > 1)
                        sbSetQuery.Append("AND ");

                    switch (i)
                    {
                        case 0:
                            sbSetQuery.Append("TestType = '" + strGetTestListFindInfo[i] + "' ");
                            break;
                        case 1:
                            sbSetQuery.Append("Version = '" + strGetTestListFindInfo[i] + "' ");
                            break;
                        case 2:
                            sbSetQuery.Append("ProdCode = '" + strGetTestListFindInfo[i] + "' ");
                            break;
                    }
                }
            }
            */
        }

        public string QuerySelectTestListData(PROCESS_PACKET getPaket, int nLoadPosition)
        {
            StringBuilder sbSetQuery = new StringBuilder();

            sbSetQuery.Append("SELECT A.ProdNo_C, B.ProdNo_P, B.UpdateDT, E.UpdateDT AS 'TestListFileUpdateDT', D.TestCode, D.Gate1, D.Gate2, " +
                "E.FileName, A.RegUserComment, E.Description, C.GroupName, C.ModelName, " +
                "A.VariantNo, A.VariantNo AS 'TestListNo', A.Config, " +
                "E.TestType, E.Version, E.ProdCode, ");

            switch (nLoadPosition)
            {
                case 1: sbSetQuery.Append("F.TestListData1 AS 'TestListData', "); break;
                case 2: sbSetQuery.Append("F.TestListData2 AS 'TestListData', "); break;
                case 3: sbSetQuery.Append("F.TestListData3 AS 'TestListData', "); break;
                case 4: sbSetQuery.Append("F.TestListData4 AS 'TestListData', "); break;
                case 5: sbSetQuery.Append("F.TestListData5 AS 'TestListData', "); break;
                case 6: sbSetQuery.Append("F.TestListData6 AS 'TestListData', "); break;
                case 7: sbSetQuery.Append("F.TestListData7 AS 'TestListData', "); break;
                case 8: sbSetQuery.Append("F.TestListData8 AS 'TestListData', "); break;
                case 9: sbSetQuery.Append("F.TestListData9 AS 'TestListData', "); break;
                case 10: sbSetQuery.Append("F.TestListData10 AS 'TestListData', "); break;
            }

            sbSetQuery.Append("B.TestListFileNo, B.UseTLPosition FROM [PROD_Release] AS A WITH(NOLOCK) ");

            sbSetQuery.Append("INNER JOIN(SELECT * FROM [PROD_Variant] WITH(NOLOCK)) AS B ");
            sbSetQuery.Append("ON A.VariantNo = B.No ");
            sbSetQuery.Append("INNER JOIN(SELECT * FROM [PROD_Group] WITH(NOLOCK)) AS C ");
            sbSetQuery.Append("ON B.GroupNo = C.No ");
            sbSetQuery.Append("INNER JOIN(SELECT * FROM [STAT_TestCode] WITH(NOLOCK)) AS D ");
            sbSetQuery.Append("ON A.TestCodeNo = D.No ");
            sbSetQuery.Append("INNER JOIN(SELECT * FROM [STOR_TestListFile] WITH(NOLOCK)) AS E ");
            sbSetQuery.Append("ON B.TestListFileNo = E.No ");
            sbSetQuery.Append("INNER JOIN(SELECT * FROM [HIST_TestListFile] WITH(NOLOCK)) AS F ");
            sbSetQuery.Append("ON B.TestListFileNo = F.TestListFileNo ");
            sbSetQuery.Append("WHERE A.ProdNo_C = '" + getPaket.objProdNo_C[0].Data + "' ");

            if (getPaket.objTestCode[0].Data.Length > 0)
                sbSetQuery.Append("AND D.TestCode = '" + getPaket.objTestCode[0].Data + "' ");
            if (getPaket.objTestType[0].Data.Length > 0)
                sbSetQuery.Append("AND E.TestType = '" + getPaket.objTestType[0].Data + "' ");
            if (getPaket.objTestListFileVersion[0].Data.Length > 0)
                sbSetQuery.Append("AND E.Version = '" + getPaket.objTestListFileVersion[0].Data + "' ");
            if (getPaket.objProductionCode[0].Data.Length > 0)
                sbSetQuery.Append("AND E.ProdCode = '" + getPaket.objProductionCode[0].Data + "' ");

            /*
            string[] strGetTestListFindInfo = new string[3];
            strGetTestListFindInfo[0] = getPaket.objTestType[0].Data;
            strGetTestListFindInfo[1] = getPaket.objTestListFileVersion[0].Data;
            strGetTestListFindInfo[2] = getPaket.objProductionCode[0].Data;

            bool bFirstAppend = false;
            int iParameterCnt = 0;
            for (int i = 0; i < strGetTestListFindInfo.Length; i++)
            {
                if (strGetTestListFindInfo[i].Length > 0)
                {
                    iParameterCnt++;

                    if (bFirstAppend == false)
                    {
                        bFirstAppend = true;

                        sbSetQuery.Append("WHERE ");
                    }

                    if (iParameterCnt > 1)
                        sbSetQuery.Append("AND ");

                    switch (i)
                    {
                        case 0:
                            sbSetQuery.Append("TestType = '" + strGetTestListFindInfo[i] + "' ");
                            break;
                        case 1:
                            sbSetQuery.Append("Version = '" + strGetTestListFindInfo[i] + "' ");
                            break;
                        case 2:
                            sbSetQuery.Append("ProdCode = '" + strGetTestListFindInfo[i] + "' ");
                            break;
                    }
                }
            }
            sbSetQuery.Append(") AS B ");

            sbSetQuery.Append("ON B.No = A.TestListNo ");
            sbSetQuery.Append("INNER JOIN(SELECT * FROM PROD_Group AS Y) AS C ");
            sbSetQuery.Append("ON C.No = B.GroupNo ");

            sbSetQuery.Append("INNER JOIN(SELECT* FROM HIST_TestList) AS K ON K.TestListNo = A.TestListNo ");

            if (getPaket.objTestCode[0].Data.Length > 0)
                sbSetQuery.Append("INNER JOIN(SELECT * FROM STAT_TestCode AS Z WHERE Z.TestCode = '" + getPaket.objTestCode[0].Data + "') AS D ");
            else
                sbSetQuery.Append("INNER JOIN(SELECT * FROM STAT_TestCode AS Z) AS D ");

            sbSetQuery.Append("ON A.ProdNo_C = '" + getPaket.objProdNo_C[0].Data + "' AND B.No = A.TestListNo AND D.No = A.TestCodeNo;");
            */

            return sbSetQuery.ToString();
        }

        public SqlCommand LoginInfoSummaryInsert(
            string strHostID,
            string strSection,
            bool bConnectState)
        {
            string columns = "TestType,Version,ProdCode,TestListFileNo,TestListVariantNo,StepVersion,HostID,Section,ProdNo_C,ProdNo_P,Testcode,TestListFileName,ProductID,Result,Duration";
            string values = string.Join(",", columns.Split(',').Select(c => string.Format("@{0}", c)));
            string sqlCommand = string.Format("INSERT INTO [HIST_TesterSummary] ({0}) VALUES ({1})", columns, values);

            ParamSet.TesterSummaryField();

            SqlCommand cmd = new SqlCommand(sqlCommand);

            SqlParameter[] setParams = new SqlParameter[columns.Split(',').Count()];

            setParams[0] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestType", "-");
            setParams[1] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Version", "-");
            setParams[2] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProdCode", "-");
            setParams[3] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListFileNo", -1);
            setParams[4] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListVariantNo", -1);
            setParams[5] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "StepVersion", -1);
            setParams[6] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "HostID", strHostID);
            setParams[7] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Section", strSection);
            setParams[8] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProdNo_C", "-");
            setParams[9] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProdNo_P", "-");
            setParams[10] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Testcode", "-");
            setParams[11] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListFileName", "-");

            if (bConnectState)
                setParams[12] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProductID", "PowerON");
            else
                setParams[12] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProductID", "PowerOFF");

            setParams[13] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Result", "-");
            setParams[14] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Duration", "-");

            cmd.Parameters.AddRange(setParams);

            return cmd;
        }

        public SqlCommand QueryTestListInfoSummaryInsert(
        //Int64 No,
        string strHostID,
        string strSection,

        string strTestListCntID,

        int nResultType,

        string strProdNo_C = "-",
        string strTestType = "-",
        string strVersion = "-",
        string strProdCode = "-",
        string strTestcode = "-",

        int nTestListFileNo = -1,
        int nTestListVariantNo = -1,
        string strProdNo_P = "-",
        string strTestlistFile = "-")
        {
            string columns = "TestType,Version,ProdCode,TestListFileNo,TestListVariantNo,TestListCntID,StepVersion,HostID,Section,ProdNo_C,ProdNo_P,Testcode,TestListFileName,ProductID,Result,Duration";
            string values = string.Join(",", columns.Split(',').Select(c => string.Format("@{0}", c)));
            string sqlCommand = string.Format("INSERT INTO [HIST_TesterSummary] ({0}) VALUES ({1})", columns, values);

            if (strProdNo_C.Length <= 0) strProdNo_C = "-";
            if (strTestType.Length <= 0) strTestType = "-";
            if (strVersion.Length <= 0) strVersion = "-";
            if (strProdCode.Length <= 0) strProdCode = "-";
            if (strTestcode.Length <= 0) strTestcode = "-";
            if (strProdNo_P.Length <= 0) strProdNo_P = "-";
            if (strTestlistFile.Length <= 0) strTestlistFile = "-";

            ParamSet.TesterSummaryField();

            SqlCommand cmd = new SqlCommand(sqlCommand);
            SqlParameter[] setParams = new SqlParameter[columns.Split(',').Count()];
            //setParams[0] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestID", No);
            setParams[0] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestType", strTestType);
            setParams[1] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Version", strVersion);
            setParams[2] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProdCode", strProdCode);
            setParams[3] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListFileNo", nTestListFileNo);
            setParams[4] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListVariantNo", nTestListVariantNo);
            setParams[5] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListCntID", strTestListCntID);
            setParams[6] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "StepVersion", -1);
            setParams[7] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "HostID", strHostID);
            setParams[8] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Section", strSection);
            setParams[9] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProdNo_C", strProdNo_C);
            setParams[10] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProdNo_P", strProdNo_P);
            setParams[11] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Testcode", strTestcode);
            setParams[12] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListFileName", strTestlistFile);
            setParams[13] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProductID", "QUERY");

            switch (nResultType)
            {
                case 0: setParams[14] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Result", "FAIL"); break;
                case 1: setParams[14] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Result", "FIND"); break;
                case 2: setParams[14] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Result", "CHECK"); break;
                case 3: setParams[14] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Result", "ERROR"); break;
                default: setParams[14] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Result", "-"); break;
            }

            setParams[15] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Duration", "-");

            cmd.Parameters.AddRange(setParams);

            return cmd;
        }

        public SqlCommand LogDataSummaryInsert(HISTLogSummary itemSummary)
        {
            string columns = string.Empty;
            if(itemSummary.TestListCntID == string.Empty)
                columns = "StationName,TestType,Version,ProdCode,TestListFileNo,TestListVariantNo,StepVersion,HostID,Section,ProdNo_C,ProdNo_P,Testcode,TestListFileName,ProductID,Result,Duration,TestDT";
            else
                columns = "StationName,TestType,Version,ProdCode,TestListFileNo,TestListVariantNo,TestListCntID,StepVersion,HostID,Section,ProdNo_C,ProdNo_P,Testcode,TestListFileName,ProductID,Result,Duration,TestDT";

            string values = string.Join(",", columns.Split(',').Select(c => string.Format("@{0}", c)));
            string sqlCommand = string.Format("INSERT INTO [HIST_LogSummary] ({0}) VALUES ({1})", columns, values);

            ParamSet.LogSummaryField();

            SqlCommand cmd = new SqlCommand(sqlCommand);

            SqlParameter[] setParams = new SqlParameter[columns.Split(',').Count()];

            if (itemSummary.TestListCntID == string.Empty)
            {
                setParams[0] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "StationName", itemSummary.StationName);
                setParams[1] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestType", itemSummary.TestType);
                setParams[2] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Version", itemSummary.Version);
                setParams[3] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProdCode", itemSummary.ProdCode);
                setParams[4] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListFileNo", itemSummary.TestListFileNo);
                setParams[5] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListVariantNo", itemSummary.TestListVariantNo);
                setParams[6] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "StepVersion", itemSummary.StepVersion);
                setParams[7] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "HostID", itemSummary.Host);
                setParams[8] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Section", itemSummary.Section);
                setParams[9] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProdNo_C", itemSummary.ProdNoC);
                setParams[10] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProdNo_P", itemSummary.ProdNoP);
                setParams[11] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Testcode", itemSummary.TestCode);
                setParams[12] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListFileName", itemSummary.TestListFileName);
                setParams[13] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProductID", itemSummary.ProductID);
                setParams[14] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Result", itemSummary.Result);
                setParams[15] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Duration", itemSummary.Duration);
                setParams[16] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestDT", itemSummary.TestDT);
            }
            else
            {
                setParams[0] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "StationName", itemSummary.StationName);
                setParams[1] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestType", itemSummary.TestType);
                setParams[2] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Version", itemSummary.Version);
                setParams[3] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProdCode", itemSummary.ProdCode);
                setParams[4] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListFileNo", itemSummary.TestListFileNo);
                setParams[5] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListVariantNo", itemSummary.TestListVariantNo);
                setParams[6] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListCntID", itemSummary.TestListCntID);
                setParams[7] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "StepVersion", itemSummary.StepVersion);
                setParams[8] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "HostID", itemSummary.Host);
                setParams[9] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Section", itemSummary.Section);
                setParams[10] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProdNo_C", itemSummary.ProdNoC);
                setParams[11] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProdNo_P", itemSummary.ProdNoP);
                setParams[12] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Testcode", itemSummary.TestCode);
                setParams[13] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestListFileName", itemSummary.TestListFileName);
                setParams[14] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "ProductID", itemSummary.ProductID);
                setParams[15] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Result", itemSummary.Result);
                setParams[16] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "Duration", itemSummary.Duration);
                setParams[17] = ParamSet.GetMakeSqlParameterInfo(ParamSet.fParam, "TestDT", itemSummary.TestDT);
            }

            cmd.Parameters.AddRange(setParams);

            return cmd;
        }
    }
}
