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.LSU
{
    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)
        {
        }
        //REF
        public string QueryRefModelList()
        {
            string strQueryInfo = string.Empty;

            strQueryInfo = "SELECT * FROM REFS_Model ORDER BY No";

            return strQueryInfo;
        }
        public string QueryRefModelAllInfo(string strSensorID)
        {
            string strQueryInfo = string.Empty;

            //INNER JOIN REFS_Model C ON C.RefSnrsID = A.RefSnrsID
            strQueryInfo = "SELECT B.Model_Type, B.RefSnrsID, B.Name, B.Status, " +
                            "A.Pressure, A.ZeroPoint, A.SetPointPM, A.SetPointFT, A.MRG1, A.MRG3, A.MRG4, A.MRG5, A.SFG20, A.SFG45, A.RI, " +
                            "X.CountWarning, X.CountLimit " +
                            "FROM REFS_Model AS B, REFS_UsagePlan AS X, REFS_ModelChar AS A " +
                            "WHERE B.RefSnrsID = '" + strSensorID + "' AND B.RefSnrsID = A.RefSnrsID AND A.RefSnrsID = X.RefSnrsID;";


            return strQueryInfo;
        }
        public string QueryRefModelPlanInfo(string strSensorID)
        {
            string strQueryInfo = string.Empty;

            strQueryInfo = "SELECT A.Model_Type, A.RefSnrsID, A.Name, A.Status, " +
                            "X.CountWarning, X.CountLimit " +
                            "FROM REFS_Model AS A, REFS_UsagePlan AS X " +
                            "WHERE A.RefSnrsID = '" + strSensorID + "' AND A.RefSnrsID = X.RefSnrsID;";


            return strQueryInfo;
        }
        public string QueryRefModelGetCnt(string strSensroID, string strUsageID)
        {
            string strQueryInfo = string.Empty;

            strQueryInfo = "SELECT A.RefSnrsID, A.UsagePlaceID, A.LastUpdateDT, A.Count, A.AccumulateCount " +
                "FROM REFS_UsageHistory AS A " +
                "INNER JOIN(SELECT MAX(B.LastUpdateDT) AS LAST_DATE, B.RefSnrsID AS ID, B.UsagePlaceID AS UPI " +
                "FROM REFS_UsageHistory AS B WHERE B.RefSnrsID = '" + strSensroID + "' AND B.UsagePlaceID = '" + strUsageID + "' GROUP BY B.RefSnrsID, B.UsagePlaceID) AS SUB " +
                "ON SUB.LAST_DATE = A.LastUpdateDT " +
                "AND A.RefSnrsID = SUB.ID " +
                "AND A.UsagePlaceID = SUB.UPI;";

            return strQueryInfo;
        }
        public string QueryRefModelSetCnt(string strSensroID, string strUsageID)
        {
            string strQueryInfo = string.Empty;

            strQueryInfo = "UPDATE A SET A.Count = A.Count + 1, A.AccumulateCount = A.AccumulateCount + 1, A.LastUpdateDT = GETDATE() " +
                           "FROM REFS_UsageHistory A " +
                           "JOIN(SELECT MAX(B.LastUpdateDT) AS LAST_DATE, B.RefSnrsID AS ID, B.UsagePlaceID AS UPI " +
                           "FROM REFS_UsageHistory AS B WHERE B.RefSnrsID = '" + strSensroID + "' AND B.UsagePlaceID = '" + strUsageID + "' GROUP BY B.RefSnrsID, B.UsagePlaceID) AS SUB " +
                           "ON SUB.LAST_DATE = A.LastUpdateDT " +
                           "AND A.RefSnrsID = SUB.ID " +
                           "AND A.UsagePlaceID = SUB.UPI;";

            return strQueryInfo;
        }
        public string QueryRefModelChangeKnowNewPlace(string strSensroID, string strUsageID, string strNewUsageID)
        {
            string strQueryInfo = string.Empty;

            strQueryInfo = "INSERT INTO REFS_UsageHistory (RefSnrsID, UsagePlaceID, Count, AccumulateCount, Description) " +
                           "SELECT RefSnrsID, '" + strNewUsageID + "', '0', AccumulateCount, Description FROM REFS_UsageHistory AS A " +
                           "INNER JOIN(SELECT MAX(B.LastUpdateDT) AS LAST_DATE, B.RefSnrsID AS ID, B.UsagePlaceID AS UPI " +
                           "FROM REFS_UsageHistory AS B WHERE B.RefSnrsID = '" + strSensroID + "' AND B.UsagePlaceID = '" + strUsageID + "' GROUP BY B.RefSnrsID, B.UsagePlaceID) AS SUB " +
                           "ON SUB.LAST_DATE = A.LastUpdateDT " +
                           "AND A.RefSnrsID = SUB.ID " +
                           "AND A.UsagePlaceID = SUB.UPI;";

            return strQueryInfo;
        }
        public string QueryRefModelChange(string strSensroID, string strNewUsageID)
        {
            string strQueryInfo = string.Empty;

            strQueryInfo = "INSERT INTO REFS_UsageHistory(RefSnrsID, UsagePlaceID, Count, AccumulateCount, Description) " +
                           "SELECT A.RefSnrsID, '" + strNewUsageID + "', '0', A.AccumulateCount, A.Description FROM REFS_UsageHistory AS A " +
                           "INNER JOIN(SELECT * FROM REFS_UsageHistory WHERE " +
                           "LastUpdateDT = (SELECT MAX(LastUpdateDT) FROM REFS_UsageHistory B WHERE B.RefSnrsID = '" + strSensroID + "' " +
                           "GROUP BY B.RefSnrsID)) AS SUB " +
                           "ON SUB.LastUpdateDT = A.LastUpdateDT " +
                           "AND A.RefSnrsID = SUB.RefSnrsID " +
                           "AND A.UsagePlaceID = SUB.UsagePlaceID;";

            return strQueryInfo;
        }
        public string QueryRefModelRegister(string strSensroID, string strUsageID)
        {
            string strQueryInfo = string.Empty;

            strQueryInfo = "IF EXISTS(SELECT RefSnrsID FROM [REFS_Model] AS B WHERE B.RefSnrsID = '" + strSensroID + "') " +
                           "BEGIN " +
                           "IF NOT EXISTS(SELECT RefSnrsID FROM REFS_UsageHistory AS A WHERE A.RefSnrsID = '" + strSensroID + "') " +
                           "BEGIN " +
                           "INSERT INTO REFS_UsageHistory (RefSnrsID, UsagePlaceID, Count, AccumulateCount, Description) VALUES('" + strSensroID + "', '" + strUsageID + "', 0, 0, '-'); " +
                           "END " +
                           "END;";

            return strQueryInfo;
        }
        public string QueryRefModelLastSensorAtPlaceID(string strUsageID)
        {
            string strQueryInfo = string.Empty;

            strQueryInfo = "SELECT * FROM[REFS_UsageHistory] A " +
                           "WHERE LastUpdateDT = (SELECT MAX(LastUpdateDT) " +
                           "FROM[REFS_UsageHistory] B " +
                           "WHERE B.UsagePlaceID = '" + strUsageID + "' " +
                           "GROUP BY B.UsagePlaceID);";

            return strQueryInfo;
        }
        /*
         * 
         */
        /*
        public string QueryMakeModeInfo(PROCESS_PACKET getPaket)
        {
            string strQueryInfo = string.Empty;

            strQueryInfo = "SELECT ModeID FROM [CONST_WorkMode] WHERE PalletNumber = '" + getPaket.objPalletNumber[0].Data + "';";

            return strQueryInfo;
        }
        public string QueryFindTrimmingImage(int iProcNum, PROCESS_PACKET getPaket)
        {
            string strQueryInfo = string.Empty;

            int iSetType = 0;

            if (getPaket.objPalletID[0].Data.IndexOf("-") >= 0)
                iSetType = 1;
            if (getPaket.objPalletNumber[0].Data.IndexOf("-") >= 0)
                iSetType = 2;

            //Normal
            strQueryInfo = "SELECT TOP 1 No, LogData, UpdateDT FROM [HIST_TestResultData_" + iProcNum.ToString() + "] WHERE No " +
                "IN(SELECT No FROM [HIST_TestResultData_" + iProcNum.ToString() + "] WHERE " +
                "PalletNumber = '" + getPaket.objPalletNumber[0].Data + "' " +
                "AND PalletID = '" + getPaket.objPalletID[0].Data + "' " +
                "AND ProdID = '" + getPaket.objProductID[0].Data + "' " +
                "AND TestID = 'LTV' " +
                "AND Status = 50) " +
                "ORDER BY No DESC;";

            if(iSetType == 1) 
            {
                strQueryInfo = "SELECT TOP 1 No, LogData, UpdateDT FROM [HIST_TestResultData_" + iProcNum.ToString() + "] WHERE No " +
                    "IN(SELECT No FROM [HIST_TestResultData_" + iProcNum.ToString() + "] WHERE " +
                    "PalletNumber = '" + getPaket.objPalletNumber[0].Data + "' " +
                    "AND ProdID = '" + getPaket.objProductID[0].Data + "' " +
                    "AND TestID = 'LTV' " +
                    "AND Status = 50) " +
                    "ORDER BY No DESC;";
            }
            else if (iSetType == 2)
            {
                strQueryInfo = "SELECT TOP 1 No, LogData, UpdateDT FROM [HIST_TestResultData_" + iProcNum.ToString() + "] WHERE No " +
                    "IN(SELECT No FROM [HIST_TestResultData_" + iProcNum.ToString() + "] WHERE " +
                    "ProdID = '" + getPaket.objProductID[0].Data + "' " +
                    "AND TestID = 'LTV' " +
                    "AND Status = 50) " +
                    "ORDER BY No DESC;";
            }


            return strQueryInfo;
        }
        public string QueryMakePalletInfo(PROCESS_PACKET getPaket)
        {
            string strQueryInfo = string.Empty;

            //Unused Pallet Position
            if (getPaket.objProductID[0].Data.Length == 0)
                strQueryInfo = "SELECT * FROM STAT_ProdLoad A WHERE A.PalletNumber = '" + getPaket.objPalletNumber[0].Data + "' ORDER BY A.No;";
            else
                strQueryInfo = "SELECT * FROM STAT_ProdLoad A WHERE A.PalletNumber = '" + getPaket.objPalletNumber[0].Data +
                "' AND A.ProdID = '" + getPaket.objProductID[0].Data + "' ORDER BY A.No;";

            return strQueryInfo;
        }
        */
        public string QueryMakePalletInfo(string strPalletNumber, string strProductID = "")
        {
            string strQueryInfo = string.Empty;

            //Unused Pallet Position
            if (strProductID.Length == 0)
                strQueryInfo = "SELECT * FROM STAT_ProdLoad A WHERE A.PalletNumber = '" + strPalletNumber + "' ORDER BY A.No;";
            else
                strQueryInfo = "SELECT * FROM STAT_ProdLoad A WHERE A.PalletNumber = '" + strPalletNumber +
                "' AND A.ProdID = '" + strProductID + "' ORDER BY A.No;";

            return strQueryInfo;
        }
        public string QueryMakeModelInfo(int iPos, ServerInfo getServerInfo, XTableInfo getSchemaTableInfo, string strProdNo)
        {
            //int iGetProcessCode = getServerInfo.ConnectInfo[iPos].Item4;

            string strQueryInfo = string.Empty;

            /*IEnumerable<InfoProcess> queryFindProcessInfo =
            from Process in getServerInfo.ProcessInfo
            where Process.nNum == (iPos + 1)
            select Process;

            string strQueryInfo = string.Empty;

            foreach (InfoProcess fpi in queryFindProcessInfo)
            {
                //Find Process Info

                //기본 Child 및 Parent Model info Inner join Query 문 생성
                if (fpi.bParameterInfoUse == false)
                {
                    strQueryInfo = "SELECT B.Model_Type, A.PrtProdNo, A.Plug_Type AS 'C Plug Type', B.Plug_Type AS 'P Plug Type', A.ProdNo, A.Name AS 'C Name', A.CableLength, A.FillPrs_Target, A.FillPrs_USpec, A.FillPrs_LSpec," +
                    "B.Name AS 'P Name', B.RegulationType, B.ProtTubeType, B.RiSetPoint" +
                    //
                    " FROM PROD_ModelParent AS B, " +
                    //
                    "PROD_ModelChild AS A INNER JOIN PROD_ModelParent C ON C.PrtProdNo = A.PrtProdNo" +
                    " WHERE A.ProdNo = '" + strProdNo + "' AND B.PrtProdNo = A.PrtProdNo;";
                }
                else
                {
                    strQueryInfo = "SELECT B.Model_Type, A.PrtProdNo, A.Plug_Type AS 'C Plug Type', B.Plug_Type AS 'P Plug Type', A.ProdNo, A.Name AS 'C Name', A.CableLength, A.FillPrs_Target, A.FillPrs_USpec, A.FillPrs_LSpec," +
                    "B.Name AS 'P Name', B.RegulationType, B.ProtTubeType, B.RiSetPoint,";

                    string[] strParameterTableList = null;
                    strParameterTableList = fpi.strParameterInfoTableName.Split(',');

                    if (strParameterTableList.Count() > 3)
                        return "ERROR - Parameter table use max 3.";

                    string[] strSubAs = new string[3] { "X", "Y", "Z" };
                    string[] strSubTable = new string[3] { "", "", "" };

                    int iSet = 0;
                    foreach (string strName in strParameterTableList)
                    {
                        int iTp = Array.FindIndex<XTable>(getSchemaTableInfo._table.ToArray(), x => x.tableName == strName);
                        XTable xt = getSchemaTableInfo._table[iTp];

                        string[] strAddModelInfo = new string[xt.lstName.Count];
                        for (int i = 0; i < xt.lstName.Count; i++)
                        {
                            strAddModelInfo[i] = string.Empty;
                            strAddModelInfo[i] = @" " + strSubAs[iSet] + "." + xt.lstName[i] + " AS '" + strName + " " + xt.lstName[i] + "',";
                            //'SUB" + (iSet+1).ToString() + " " + xt.lstName[i] + "',";

                            if (xt.lstName[i].CompareTo("No") == 0)
                                continue;
                            else if (xt.lstName[i].CompareTo("UpdateDT") == 0)
                                continue;
                            else if (xt.lstName[i].CompareTo("Revision") == 0)
                                continue;
                            else if (xt.lstName[i].CompareTo("Description") == 0)
                                continue;

                            strQueryInfo += strAddModelInfo[i];
                        }

                        strSubTable[iSet] = strName + " AS " + strSubAs[iSet] + ",";

                        iSet++;
                    }

                    strQueryInfo = strQueryInfo.Remove(strQueryInfo.Length - 1, 1);

                    strQueryInfo +=
                    " FROM ";

                    foreach (string strSubT in strSubTable)
                    {
                        if (strSubT.Length > 0)
                            strQueryInfo += strSubT;
                    }

                    strQueryInfo +=
                    " PROD_ModelParent AS B, " +
                    "PROD_ModelChild AS A INNER JOIN PROD_ModelParent C ON C.PrtProdNo = A.PrtProdNo" +
                    " WHERE A.ProdNo = '" + strProdNo + "' AND B.PrtProdNo = A.PrtProdNo ";

                    iSet = 0;
                    foreach (string strSubT in strSubTable)
                    {
                        if (strSubT.Length > 0)
                            strQueryInfo += "AND B.PrtProdNo = " + strSubAs[iSet] + ".PrtProdNo ";

                        iSet++;
                    }

                    strQueryInfo += ";";
                }

                break;
            }
            */

            return strQueryInfo;
        }

        public string[] GetProcessPPITableList(int iPos, ServerInfo getServerInfo, XTableInfo getSchemaTableInfo)
        {
            //int iGetProcessCode = getServerInfo.ConnectInfo[iPos].Item4;

            string[] strPPITableList = null;

            /*IEnumerable<InfoProcess> queryFindProcessInfo =
            from Process in getServerInfo.ProcessInfo
            where Process.nNum == (iPos + 1)
            select Process;

            foreach (InfoProcess fpi in queryFindProcessInfo)
            {
                //Find Process Info
                strPPITableList = fpi.strPreviousProcessInquiryTableName.Split(',');

                break;
            }*/

            return strPPITableList;
        }

        public SqlCommand[] InsertLogData(
            ref string strOutLogKey,
            string strSetAddLogKey,
            Int64 SetNumber,

            string strTestID,
            int iProcNum,
            DataSet dsPalletInfo,

            string strFilePath,
            byte[] ucDataArray,

            ushort usPalletIndex = ushort.MaxValue,
            string strSetPalletNumber = "",
            string strSetPalletID = "",
            string strSetPIndex = "",
            string strSeProdNo = "",
            string strSetProdID = "",
            
            int iSetStatus = 0)
        {
            SqlCommand[] cmd = new SqlCommand[2];

            strOutLogKey = string.Empty;
            strOutLogKey = "";

            string strNullPath = "NULL";
            byte[] ucNullByte = new byte[1];
            ucNullByte[0] = 0x01;

            if (dsPalletInfo != null)
            {
                bool hasRows = dsPalletInfo.Tables.Cast<DataTable>().Any(table => table.Rows.Count != 0);

                if (hasRows)
                {
                    string strPalletNumber = string.Empty;
                    string strPalletID = string.Empty;

                    string strPIndex = string.Empty;
                    string strProdNo = string.Empty;
                    string strProdID = string.Empty;

                    if (usPalletIndex == ushort.MaxValue)
                    {
                        strPalletNumber = strSetPalletNumber;
                        strPalletID = strSetPalletID;

                        strPIndex = strSetPIndex;
                        strProdNo = strSeProdNo;
                        strProdID = strSetProdID;
                    }
                    else
                    {
                        int iAccessPos = 0;

                        if (usPalletIndex != ushort.MaxValue)
                            iAccessPos = usPalletIndex - 1;

                        strPalletNumber = dsPalletInfo.Tables[0].Rows[iAccessPos]["PalletNumber"].ToString();
                        strPalletID = dsPalletInfo.Tables[0].Rows[iAccessPos]["PalletID"].ToString();

                        strPIndex = dsPalletInfo.Tables[0].Rows[iAccessPos]["PIndex"].ToString();
                        strProdNo = dsPalletInfo.Tables[0].Rows[iAccessPos]["ProdNo"].ToString();
                        strProdID = dsPalletInfo.Tables[0].Rows[iAccessPos]["ProdID"].ToString();
                    }

                    strPalletNumber = strPalletNumber.Trim();
                    strPalletID = strPalletID.Trim();

                    strPIndex = strPIndex.Trim();
                    strProdNo = strProdNo.Trim();
                    strProdID = strProdID.Trim();

                    cmd[0] = new SqlCommand("INSERT INTO HIST_TestResultData_" + iProcNum.ToString() + " (LogAccessKey, ProcNo, PalletNumber, PalletID, PIndex, ProdNo, ProdID, TestID, LogDataPath, LogData, Status) VALUES (@LogAccessKey, @ProcNo, @PalletNumber, @PalletID, @PIndex, @ProdNo, @ProdID, @TestID, @LogDataPath, @LogData, @Status)");
                    cmd[1] = new SqlCommand("INSERT INTO HIST_TestResultData (LogAccessKey, ProcNo, PalletNumber, PalletID, PIndex, ProdNo, ProdID, TestID, LogDataPath, LogData, Status) VALUES (@LogAccessKey, @ProcNo, @PalletNumber, @PalletID, @PIndex, @ProdNo, @ProdID, @TestID, @LogDataPath, @LogData, @Status)");

                    if (strSetAddLogKey.Length == 0)
                        strOutLogKey = SetNumber.ToString() + "-" + iProcNum.ToString();
                    else
                        strOutLogKey = SetNumber.ToString() + "-" + iProcNum.ToString() + "-" + strSetAddLogKey;

                    SqlParameter param0 = new SqlParameter("@LogAccessKey", SqlDbType.NVarChar, 64);
                    param0.Value = strOutLogKey;
                    cmd[0].Parameters.Add(param0);

                    /*
                     * ProcNo : 공정 코드 180 ~ 290
                     */
                    param0 = new SqlParameter("@ProcNo", SqlDbType.BigInt);
                    param0.Value = iProcNum;
                    cmd[0].Parameters.Add(param0);

                    param0 = new SqlParameter("@PalletNumber", SqlDbType.NVarChar, 20);
                    param0.Value = strPalletNumber;
                    cmd[0].Parameters.Add(param0);

                    param0 = new SqlParameter("@PalletID", SqlDbType.NVarChar, 30);
                    param0.Value = strPalletID;
                    cmd[0].Parameters.Add(param0);

                    param0 = new SqlParameter("@PIndex", SqlDbType.TinyInt);
                    param0.Value = Convert.ToByte(strPIndex);
                    cmd[0].Parameters.Add(param0);

                    param0 = new SqlParameter("@ProdNo", SqlDbType.NVarChar, 30);
                    param0.Value = strProdNo;
                    cmd[0].Parameters.Add(param0);

                    param0 = new SqlParameter("@ProdID", SqlDbType.NVarChar, 50);
                    param0.Value = strProdID;
                    cmd[0].Parameters.Add(param0);

                    /*
                     * TestID 공정 위치별 ID
                     */
                    param0 = new SqlParameter("@TestID", SqlDbType.NVarChar, 20);
                    param0.Value = strTestID;
                    cmd[0].Parameters.Add(param0);

                    param0 = new SqlParameter("@LogDataPath", SqlDbType.NVarChar, 256);
                    param0.Value = strFilePath;
                    cmd[0].Parameters.Add(param0);

                    param0 = new SqlParameter("@LogData", SqlDbType.VarBinary, -1);
                    param0.Value = ucDataArray;  // byte[]로 지정함
                    cmd[0].Parameters.Add(param0);

                    param0 = new SqlParameter("@Status", SqlDbType.TinyInt);
                    param0.Value = iSetStatus;
                    cmd[0].Parameters.Add(param0);
                    //
                    //
                    SqlParameter param1 = new SqlParameter("@LogAccessKey", SqlDbType.NVarChar, 64);
                    param1.Value = strOutLogKey;
                    cmd[1].Parameters.Add(param1);

                    /*
                     * ProcNo : 공정 코드 180 ~ 290
                     */
                    param1 = new SqlParameter("@ProcNo", SqlDbType.BigInt);
                    param1.Value = iProcNum;
                    cmd[1].Parameters.Add(param1);

                    param1 = new SqlParameter("@PalletNumber", SqlDbType.NChar, 20);
                    param1.Value = strPalletNumber;
                    cmd[1].Parameters.Add(param1);

                    param1 = new SqlParameter("@PalletID", SqlDbType.NChar, 30);
                    param1.Value = strPalletID;
                    cmd[1].Parameters.Add(param1);

                    param1 = new SqlParameter("@PIndex", SqlDbType.TinyInt);
                    param1.Value = Convert.ToByte(strPIndex);
                    cmd[1].Parameters.Add(param1);

                    param1 = new SqlParameter("@ProdNo", SqlDbType.NChar, 30);
                    param1.Value = strProdNo;
                    cmd[1].Parameters.Add(param1);

                    param1 = new SqlParameter("@ProdID", SqlDbType.NChar, 50);
                    param1.Value = strProdID;
                    cmd[1].Parameters.Add(param1);

                    /*
                     * TestID 공정 위치별 ID
                     */
                    param1 = new SqlParameter("@TestID", SqlDbType.NChar, 20);
                    param1.Value = strTestID;
                    cmd[1].Parameters.Add(param1);

                    param1 = new SqlParameter("@LogDataPath", SqlDbType.NVarChar, 256);
                    param1.Value = strNullPath;
                    cmd[1].Parameters.Add(param1);

                    param1 = new SqlParameter("@LogData", SqlDbType.VarBinary, -1);
                    param1.Value = ucNullByte;  // byte[]로 지정함
                    cmd[1].Parameters.Add(param1);

                    param1 = new SqlParameter("@Status", SqlDbType.TinyInt);
                    param1.Value = iSetStatus;
                    cmd[1].Parameters.Add(param1);
                }
            }

            return cmd;
        }
        public string QueryMakePPIInfo(string strTableName, string strProdNo, string strPalletID)
        {
            string strQueryInfo = string.Empty;

            //오늘 부터 10일 이내 데이터만 필요시 
            strQueryInfo = "SELECT A.PalletID, A.PIndex, A.ProdID, A.Result AS RESULT, A.UpdateDT " +
                 "FROM " + strTableName + " AS A " +
                 "INNER JOIN" +
                 "(" +
                 "SELECT MAX(UpdateDT) AS LAST_DATE, B.ProdID " +
                 "FROM " + strTableName + " AS B WHERE ProdID = '" + strProdNo + "' " +
                 "GROUP BY ProdID" +
                 ") AS SUB " +
                 "ON A.UpdateDT = SUB.LAST_DATE " +
                 //"AND SUB.LAST_DATE BETWEEN CONVERT(VARCHAR, GETDATE() - 10, 112) AND CONVERT(VARCHAR, GETDATE(), 112) " +
                 "AND A.ProdID = SUB.ProdID " +
                 "AND A.PalletID = '" + strPalletID + "';";
            /*
            if (strPalletID.Length > 0 && iPalletIndex > 0)
            {
                strQueryInfo = "SELECT A.PalletID, A.PIndex, A.ProdID, A.Result AS RESULT, A.UpdateDT " +
                "FROM " + strTableName + " AS A " +
                "INNER JOIN" +
                "(" +
                "SELECT MAX(UpdateDT) AS LAST_DATE, B.ProdID " +
                "FROM " + strTableName + " AS B WHERE ProdID = '" + strProdNo + "' " +
                "GROUP BY ProdID" +
                ") AS SUB " +
                "ON A.UpdateDT = SUB.LAST_DATE " +
                "AND SUB.LAST_DATE BETWEEN CONVERT(VARCHAR, GETDATE() - 15, 112) AND CONVERT(VARCHAR, GETDATE(), 112) " +
                "AND A.ProdID = SUB.ProdID " +
                "AND A.PalletID = '" + strPalletID + "';";
            }
            else
            {
                strQueryInfo = "SELECT A.PalletID, A.PIndex, A.ProdID, A.Result AS RESULT, A.UpdateDT " +
                "FROM " + strTableName + " AS A " +
                "INNER JOIN" +
                "(" +
                "SELECT MAX(UpdateDT) AS LAST_DATE, B.ProdID " +
                "FROM " + strTableName + " AS B WHERE ProdID = '" + strProdNo + "' " +
                "GROUP BY ProdID" +
                ") AS SUB " +
                "ON A.UpdateDT = SUB.LAST_DATE " +
                "AND SUB.LAST_DATE BETWEEN CONVERT(VARCHAR, GETDATE() - 15, 112) AND CONVERT(VARCHAR, GETDATE(), 112) " +
                "AND A.ProdID = SUB.ProdID";
            }
            */

            return strQueryInfo;
        }
    }
}
