Imports MySql.Data  ' 데이터베이스 연동을 위한 네임스페이스 추가
Imports MySql.Data.MySqlClient  ' MYSQL Server용 네임스페이스 추가
Module MySqlModule
    Private Const LocalHostIP As String = "192.168.0.250"
    Private Const DataBaseName As String = "jomtTesterDB"
    'Private Const DataBaseName As String = "jomtTestServerDB"
    Private Const UserDataBaseName As String = "jomtAccountDB"
    Private Const UserName As String = "root"
    Private Const Password As String = "JOMT1646db!!"
    Private Conn As String = "Server=" + LocalHostIP + ";DataBase=" + DataBaseName + ";user=" + UserName + ";password=" + Password + ";"

    Private sqlCon As New MySqlConnection(Conn)
    Private sqlCmd As New MySqlCommand()
    Private sqlData As MySqlDataReader
    Public sqlDataQueryScalar As Object
    Public sqlDataQuery() As Object
    Public StrCmd As String

    Public UseTestServer As Boolean = False

    Public IQC_GrCapStr(,) As String = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                                        {"0", "IQC_ProR1Cnt", "IQC_ProR2Cnt", "IQC_ProR3Cnt", "IQC_ProR4Cnt", "IQC_ProR5Cnt", "IQC_ProR6Cnt", "IQC_ProR7Cnt", "IQC_ProR8Cnt", "IQC_ProR9Cnt", "IQC_ProR10Cnt"},
                                        {"0", "IQC_LuxR1Cnt", "IQC_LuxR2Cnt", "IQC_LuxR3Cnt", "IQC_LuxR4Cnt", "IQC_LuxR5Cnt", "IQC_LuxR6Cnt", "IQC_LuxR7Cnt", "IQC_LuxR8Cnt", "IQC_LuxR9Cnt", "IQC_LuxR10Cnt"},
                                        {"0", "IQC_EyeR1Cnt", "IQC_EyeR2Cnt", "IQC_EyeR3Cnt", "IQC_EyeR4Cnt", "IQC_EyeR5Cnt", "IQC_EyeR6Cnt", "IQC_EyeR7Cnt", "IQC_EyeR8Cnt", "IQC_EyeR9Cnt", "IQC_EyeR10Cnt"}}
    Public Enum MODEL_NAME
        PRO = 1
        LUX
        EYE
        MAX
        MAX_MODEL_NUM
    End Enum
    Public Enum RELAY_NUM
        RL1 = 1
        RL2
        RL3
        RL4
        RL5
        RL6
        RL7
        RL8
        RL9
        RL10
        MAX_RELAY_NUM
    End Enum
    Public Enum PD1_ENUM
        STANDBY = 0
        WAIT
        WRITE
        SAVE
        COMPLETE
        MAX_NUM
    End Enum
    Public PD1_STATUS As PD1_ENUM
    Public Structure MEASURE
        Public MeasurePV_SN As String
        Public MeasureTestStep As Integer
        Public MeasureValue() As Double
        Public MeasureOkNg As String
        Public MeasureGrade As String
        Public MeasureCapacitor As String
        Public PreTestStep As Integer
        Public QC_LastDate As String

    End Structure
    Public RegMeasure As MEASURE

    Public Const LenPVSN As Integer = 10
    Public Const NamePVSN As String = "DIT"
    Public Const NamePVSN2 As String = "J"
    Public Structure TD_VALUE
        Public PV_SN As String
        Public PV_Date As String
        Public PV_Frequency As String
        Public PV_Impedance As String
        Public PV_Capacitance As String
        Public PV_Resistance As String
        Public PV_Reverved As String
    End Structure
    Public RegTD_VALUE As TD_VALUE
    Public Structure MES_IQC_CNT
        Public IQC_Date As String
        Public IQC_TotalCnt As Long
        Public IQC_OKCnt As Long
        Public IQC_NGCnt As Long
        Public IQC_GradeCnt() As Long
        Public IQC_GdCapCnt(,) As Long
        Public IQC_CapCnt() As Long
        Public IQC_ReTotalCnt As Long
        Public IQC_ReOkCnt As Long
        Public IQC_ReNgCnt As Long
        Public IQC_ReGradeCnt() As Long
        Public IQC_ReGdCapCnt(,) As Long
    End Structure
    Public RegMesIQC As MES_IQC_CNT
    Public CntRegMesIQC As MES_IQC_CNT
    Public Structure MES_PD1_CNT
        Public PD1_Date As String
        Public PD1_TotalCnt As Long
        Public PD1_OKCnt As Long
        Public PD1_NGCnt As Long
        Public PD1_GradeCnt() As Long
        Public PD1_CapCnt() As Long
    End Structure
    Public RegMesPD1 As MES_PD1_CNT
    Public CntRegMesPD1 As MES_PD1_CNT
    Public RegLocalPD1 As MES_PD1_CNT

    Public Structure MES_Tester
        Dim MesDate As String

        Dim Stock_GradeCnt() As Long
        Dim Release_GradeCnt() As Long

        Dim PV_TDCnt As Long

        Dim IQC_TotalCnt As Long
        Dim IQC_OkCnt As Long
        Dim IQC_NgCnt As Long
        Dim IQC_GradeCnt() As Long
        Dim IQC_GdCapCnt(,) As Long

        Dim P1_TotalCnt As Long
        Dim P1_OkCnt As Long
        Dim P1_NgCnt As Long
        Dim P1_GradeCnt() As Long

        Dim P2_TotalCnt As Long
        Dim P2_GradeCnt() As Long
        Dim P2_OkCnt As Long
        Dim P2_NgCnt As Long

    End Structure
    Public RegMesProduct As MES_Tester
    Private Function DBOpen() As Boolean
        Try
            If sqlCon.State <> ConnectionState.Open Then
                sqlCon.Open()
            End If
            Return True
        Catch ex As Exception
            MsgBox("MySqlCommand Error:" + vbCrLf + Err.Description)
            Return False
        End Try
    End Function

    Private Function DBClose() As Boolean
        Try
            If sqlCon.State = ConnectionState.Open Then
                sqlCon.Close()
            End If
            Return True
        Catch ex As Exception
            MsgBox("MySqlCommand Error:" + vbCrLf + Err.Description)
            Return False
        End Try
    End Function
    Public Function DBCommand(cmdStr As String) As Boolean
        Try
            sqlCon.Open()
            sqlCmd.CommandText = "USE " & DataBaseName & ";"
            sqlCmd.Connection = sqlCon
            sqlCmd.ExecuteNonQuery()

            sqlCmd.CommandText = cmdStr
            sqlCmd.Connection = sqlCon
            sqlCmd.ExecuteNonQuery()

            sqlCon.Close()

            Return True
        Catch ex As Exception
            MsgBox("MySqlCommand Error:" + vbCrLf + Err.Description)
            sqlCon.Close()
            Return False
        End Try
        End
    End Function

    Public Function DBQueryScalar(cmdStr As String) As String
        Try
            sqlCon.Open()
            sqlCmd.CommandText = "USE " & DataBaseName & ";"
            sqlCmd.Connection = sqlCon
            sqlCmd.ExecuteNonQuery()

            sqlCmd.CommandText = cmdStr
            sqlCmd.Connection = sqlCon
            sqlDataQueryScalar = sqlCmd.ExecuteScalar()

            sqlCon.Close()

            Return sqlDataQueryScalar
        Catch ex As Exception
            MsgBox("MySqlCommand Error:" + vbCrLf + Err.Description)
            sqlCon.Close()
            Return False
        End Try
        End
    End Function

    Public Function DBQueryReader(cmdStr As String) As Integer
        Dim FieldCount As Integer
        Dim QueryCount As ULong
        Dim initRun As Boolean = True
        Dim TmpData As Object
        Dim DataTypeName As String

        Try
            DBOpen()

            sqlCmd.CommandText = "USE " & DataBaseName & ";"
            sqlCmd.Connection = sqlCon
            sqlCmd.ExecuteNonQuery()

            sqlCmd.CommandText = cmdStr
            sqlCmd.Connection = sqlCon
            sqlData = sqlCmd.ExecuteReader

            FieldCount = sqlData.FieldCount

            While (sqlData.Read())
                If initRun Then
                    ReDim sqlDataQuery(FieldCount - 1)
                    initRun = False
                Else
                    ReDim Preserve sqlDataQuery(UBound(sqlDataQuery) + FieldCount)
                End If

                For cnt = 0 To FieldCount - 1
                    DataTypeName = sqlData.GetDataTypeName(cnt)

                    If DataTypeName = "DATE" Then
                        TmpData = sqlData.GetDateTime(cnt)
                    Else
                        TmpData = sqlData.GetValue(cnt)
                    End If

                    If DBNull.Value.Equals(TmpData) Or TmpData.Equals(String.Empty) Then
                        sqlDataQuery(QueryCount) = "0"
                    Else
                        sqlDataQuery(QueryCount) = TmpData
                    End If

                    QueryCount += 1
                Next
            End While

            DBClose()
            Return QueryCount

        Catch ex As Exception
            frmTD_JIG.cmdDisplay(Err.Description)
            If sqlCon.State = ConnectionState.Open Then sqlCon.Close()
            Return 0
        End Try
        End
    End Function
    Public Function UserDBCommand(cmdStr As String) As Boolean
        Try
            sqlCon.Open()
            sqlCmd.CommandText = "USE " & UserDataBaseName & ";"
            sqlCmd.Connection = sqlCon
            sqlCmd.ExecuteNonQuery()

            sqlCmd.CommandText = cmdStr
            sqlCmd.Connection = sqlCon
            sqlCmd.ExecuteNonQuery()

            sqlCon.Close()

            Return True
        Catch ex As Exception
            MsgBox("MySqlCommand Error:" + vbCrLf + Err.Description)
            sqlCon.Close()
            Return False
        End Try
        End
    End Function
    Public Function UserDBQueryReader(cmdStr As String) As Integer
        Dim FieldCount As Integer
        Dim QueryCount As ULong
        Dim initRun As Boolean = True
        Dim TmpData As Object
        Dim DataTypeName As String

        Try
            DBOpen()

            sqlCmd.CommandText = "USE " & UserDataBaseName & ";"
            sqlCmd.Connection = sqlCon
            sqlCmd.ExecuteNonQuery()

            sqlCmd.CommandText = cmdStr
            sqlCmd.Connection = sqlCon
            sqlData = sqlCmd.ExecuteReader

            FieldCount = sqlData.FieldCount

            While (sqlData.Read())
                If initRun Then
                    ReDim sqlDataQuery(FieldCount - 1)
                    initRun = False
                Else
                    ReDim Preserve sqlDataQuery(UBound(sqlDataQuery) + FieldCount)
                End If

                For cnt = 0 To FieldCount - 1
                    DataTypeName = sqlData.GetDataTypeName(cnt)

                    If DataTypeName = "DATE" Then
                        TmpData = sqlData.GetDateTime(cnt)
                    Else
                        TmpData = sqlData.GetValue(cnt)
                    End If

                    If DBNull.Value.Equals(TmpData) Or TmpData.Equals(String.Empty) Then
                        sqlDataQuery(QueryCount) = "0"
                    Else
                        sqlDataQuery(QueryCount) = TmpData
                    End If

                    QueryCount += 1
                Next
            End While

            DBClose()
            Return QueryCount

        Catch ex As Exception
            frmTD_JIG.cmdDisplay(Err.Description)
            If sqlCon.State = ConnectionState.Open Then sqlCon.Close()
            Return 0
        End Try
        End
    End Function
    Public Function getNowDateTime() As String
        Dim DateTimeStr As String

        DateTimeStr = Format(DateAndTime.Year(Now), "0000") & "-" & Format(DateAndTime.Month(Now), "00") & "-" & Format(DateAndTime.Day(Now), "00") & " " & Format(DateAndTime.Hour(Now), "00") & ":" & Format(DateAndTime.Minute(Now), "00") & ":" & Format(DateAndTime.Second(Now), "00")

        Return DateTimeStr
    End Function

    Public Function getNowDate() As String
        Dim DateTimeStr As String = Format(DateAndTime.Year(Now), "0000") & "-" & Format(DateAndTime.Month(Now), "00") & "-" & Format(DateAndTime.Day(Now), "00")
        Return DateTimeStr
    End Function

    Public Function getNowTime() As String
        Dim DateTimeStr As String

        DateTimeStr = Format(DateAndTime.Hour(Now), "00") & ":" & Format(DateAndTime.Minute(Now), "00") & ":" & Format(DateAndTime.Second(Now), "00")

        Return DateTimeStr
    End Function
    Public Sub ResetRegTD()
        With RegTD_VALUE
            .PV_SN = "N/A"
            .PV_Date = "N/A"
            .PV_Frequency = "N/A"
            .PV_Impedance = "N/A"
            .PV_Capacitance = "N/A"
            .PV_Resistance = "N/A"
            .PV_Reverved = "N/A"
        End With
    End Sub
    Public Function LoadRegTD(StrTD_SN As String) As Boolean
        Try
            If (Len(StrTD_SN) = LenPVSN Or Len(StrTD_SN) = (LenPVSN - 1) And (Mid(StrTD_SN, 1, 3) = NamePVSN Or Mid(StrTD_SN, 1, 1) = NamePVSN2)) Then

                ResetRegTD()

                RegTD_VALUE.PV_SN = StrTD_SN
                StrCmd = "select PV_Date,PV_Frequency,PV_Impedance,PV_Capacitance,PV_Resistance,TestStep from jomtCartridgeTbl where PV_SN = '" & StrTD_SN & "';"
                If DBQueryReader(StrCmd) Then
                    RegTD_VALUE.PV_Date = sqlDataQuery(0)
                    RegTD_VALUE.PV_Frequency = sqlDataQuery(1)
                    RegTD_VALUE.PV_Impedance = sqlDataQuery(2)
                    RegTD_VALUE.PV_Capacitance = sqlDataQuery(3)
                    RegTD_VALUE.PV_Resistance = sqlDataQuery(4)
                    RegMeasure.PreTestStep = sqlDataQuery(5)
                    Return False
                Else
                    RegTD_VALUE.PV_Date = "N/A"
                    RegTD_VALUE.PV_Frequency = "N/A"
                    RegTD_VALUE.PV_Impedance = "N/A"
                    RegTD_VALUE.PV_Capacitance = "N/A"
                    RegTD_VALUE.PV_Resistance = "N/A"
                    Return True
                End If
            Else
                Return True
            End If
            Return False
        Catch ex As Exception
            Return True
        End Try
    End Function
    Public Sub ResetRegMeasure()
        With RegMeasure
            .MeasurePV_SN = "N/A"
            ''.MeasureValue = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            .MeasureOkNg = "N/A"
            .MeasureGrade = "N/A"
            .MeasureCapacitor = "N/A"
            .MeasureTestStep = 1
        End With
    End Sub
    Public Sub ResetRegMeasureValue()
        With RegMeasure
            .MeasureValue = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        End With
    End Sub
    Public Sub ResetRegMesIQC()
        With RegMesIQC
            .IQC_Date = "0"
            .IQC_TotalCnt = "0"
            .IQC_OKCnt = "0"
            .IQC_NGCnt = "0"
            .IQC_ReTotalCnt = "0"
            .IQC_ReOkCnt = "0"
            .IQC_ReNgCnt = "0"
        End With

        ReDim RegMesIQC.IQC_GradeCnt(MODEL_NAME.MAX_MODEL_NUM - 1)
        ReDim RegMesIQC.IQC_GdCapCnt(MODEL_NAME.MAX_MODEL_NUM - 1, RELAY_NUM.MAX_RELAY_NUM - 1)
        ReDim RegMesIQC.IQC_CapCnt(RELAY_NUM.MAX_RELAY_NUM - 1)
        ReDim RegMesIQC.IQC_ReGradeCnt(MODEL_NAME.MAX_MODEL_NUM - 1)
        ReDim RegMesIQC.IQC_ReGdCapCnt(MODEL_NAME.MAX_MODEL_NUM - 1, RELAY_NUM.MAX_RELAY_NUM - 1)
    End Sub
    Public Sub ResetCntRegIQC()
        Try
            With CntRegMesIQC
                .IQC_Date = ""
                .IQC_TotalCnt = 0
                .IQC_OKCnt = 0
                .IQC_NGCnt = 0
                .IQC_ReTotalCnt = 0
                .IQC_ReOkCnt = 0
                .IQC_ReNgCnt = 0
            End With

            ReDim CntRegMesIQC.IQC_GradeCnt(MODEL_NAME.MAX_MODEL_NUM - 1)
            ReDim CntRegMesIQC.IQC_GdCapCnt(MODEL_NAME.MAX_MODEL_NUM - 1, RELAY_NUM.MAX_RELAY_NUM - 1)
            ReDim CntRegMesIQC.IQC_CapCnt(RELAY_NUM.MAX_RELAY_NUM - 1)
            ReDim CntRegMesIQC.IQC_ReGradeCnt(MODEL_NAME.MAX_MODEL_NUM - 1)
            ReDim CntRegMesIQC.IQC_ReGdCapCnt(MODEL_NAME.MAX_MODEL_NUM - 1, RELAY_NUM.MAX_RELAY_NUM - 1)

        Catch ex As Exception
            MsgBox(Err.Description)
        End Try
    End Sub
    Public Sub ResetRegMesPD1()
        Try
            With RegMesPD1
                .PD1_Date = ""
                .PD1_TotalCnt = 0
                .PD1_OKCnt = 0
                .PD1_NGCnt = 0
            End With

            ReDim RegMesPD1.PD1_GradeCnt(MODEL_NAME.MAX_MODEL_NUM - 1)
            ReDim RegLocalPD1.PD1_GradeCnt(MODEL_NAME.MAX_MODEL_NUM - 1)
            ReDim RegMesPD1.PD1_CapCnt(RELAY_NUM.MAX_RELAY_NUM - 1)
            ReDim RegLocalPD1.PD1_CapCnt(RELAY_NUM.MAX_RELAY_NUM - 1)

        Catch ex As Exception
            MsgBox(Err.Description)
        End Try
    End Sub
    Public Sub ResetCntRegMesPD1()
        Try
            With CntRegMesPD1
                .PD1_Date = ""
                .PD1_TotalCnt = 0
                .PD1_OKCnt = 0
                .PD1_NGCnt = 0
            End With

            ReDim CntRegMesPD1.PD1_GradeCnt(MODEL_NAME.MAX_MODEL_NUM - 1)
            ReDim CntRegMesPD1.PD1_CapCnt(RELAY_NUM.MAX_RELAY_NUM - 1)

        Catch ex As Exception
            MsgBox(Err.Description)
        End Try
    End Sub
    Public Function LoadRegIQC() As Boolean
        Dim TodayDate As String
        Dim RetCnt As String
        Dim IQCCmd As String = "IQC_Date,IQC_TotalCnt,IQC_OkCnt,IQC_NgCnt,IQC_ProCnt,IQC_LuxCnt,IQC_EyeCnt,IQC_R1Cnt,IQC_R2Cnt,IQC_R3Cnt,IQC_R4Cnt,IQC_R5Cnt,IQC_R6Cnt,IQC_R7Cnt,IQC_R8Cnt,IQC_R9Cnt,IQC_R10Cnt,IQC_ReTotalCnt,IQC_ReOkCnt,IQC_ReNgCnt"
        Dim cnt, queryCnt As Integer

        Try
            TodayDate = getNowDate()
            ResetRegMesIQC()

            RetCnt = DBQueryReader("select Number from jomtMES_IQCTbl where IQC_Date='" & TodayDate & "';")

            If RetCnt = 0 Then
                StrCmd = "insert jomtMES_IQCTbl(" & IQCCmd & ") values('" & TodayDate & "',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);"
                DBCommand(StrCmd)
            End If

            RetCnt = DBQueryReader("select " & IQCCmd & " from jomtMES_IQCTbl where IQC_Date='" & TodayDate & "';")
            If RetCnt Then
                queryCnt = 0
                RegMesIQC.IQC_Date = sqlDataQuery(queryCnt)
                queryCnt += 1
                RegMesIQC.IQC_TotalCnt = sqlDataQuery(queryCnt)
                queryCnt += 1
                RegMesIQC.IQC_OKCnt = sqlDataQuery(queryCnt)
                queryCnt += 1
                RegMesIQC.IQC_NGCnt = sqlDataQuery(queryCnt)

                For cnt = MODEL_NAME.PRO To MODEL_NAME.EYE
                    queryCnt += 1
                    RegMesIQC.IQC_GradeCnt(cnt) = sqlDataQuery(queryCnt)
                Next

                For cnt = RELAY_NUM.RL1 To RELAY_NUM.MAX_RELAY_NUM - 1
                    queryCnt += 1
                    RegMesIQC.IQC_CapCnt(cnt) = sqlDataQuery(queryCnt)
                Next cnt

                queryCnt += 1
                RegMesIQC.IQC_ReTotalCnt = sqlDataQuery(queryCnt)
                queryCnt += 1
                RegMesIQC.IQC_ReOkCnt = sqlDataQuery(queryCnt)
                queryCnt += 1
                RegMesIQC.IQC_ReNgCnt = sqlDataQuery(queryCnt)
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            Return True
        End Try
    End Function
    Public Function LoadRegPD1() As Boolean
        Dim TodayDate As String
        Dim RetCnt As String
        Dim IQCCmd As String = "PD1_Date,PD1_TotalCnt,PD1_OkCnt,PD1_NgCnt,PD1_ProCnt,PD1_LuxCnt,PD1_EyeCnt,PD1_MAXCnt,PD1_R1Cnt,PD1_R2Cnt,PD1_R3Cnt,PD1_R4Cnt,PD1_R5Cnt,PD1_R6Cnt,PD1_R7Cnt,PD1_R8Cnt,PD1_R9Cnt,PD1_R10Cnt"
        Dim cnt, queryCnt As Integer

        Try
            TodayDate = getNowDate()
            ResetRegMesPD1()

            RetCnt = DBQueryReader("select Number from jomtMES_PD1Tbl where PD1_Date='" & TodayDate & "';")

            If RetCnt = 0 Then
                StrCmd = "insert jomtMES_PD1Tbl(" & IQCCmd & ") values('" & TodayDate & "',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);"
                DBCommand(StrCmd)
            End If

            RetCnt = DBQueryReader("select " & IQCCmd & " from jomtMES_PD1Tbl where PD1_Date='" & TodayDate & "';")
            If RetCnt Then

                queryCnt = 0
                RegMesPD1.PD1_Date = sqlDataQuery(queryCnt)
                queryCnt += 1
                RegMesPD1.PD1_TotalCnt = sqlDataQuery(queryCnt)
                queryCnt += 1
                RegMesPD1.PD1_OKCnt = sqlDataQuery(queryCnt)
                queryCnt += 1
                RegMesPD1.PD1_NGCnt = sqlDataQuery(queryCnt)

                For cnt = MODEL_NAME.PRO To MODEL_NAME.MAX
                    queryCnt += 1
                    RegMesPD1.PD1_GradeCnt(cnt) = sqlDataQuery(queryCnt)
                Next

                For cnt = RELAY_NUM.RL1 To RELAY_NUM.MAX_RELAY_NUM - 1
                    queryCnt += 1
                    RegMesPD1.PD1_CapCnt(cnt) = sqlDataQuery(queryCnt)
                Next cnt

                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            Return True
        End Try
    End Function
    Public Sub ResetRegProduct()
        With RegMesProduct
            .IQC_TotalCnt = 0
            .IQC_OkCnt = 0
            .IQC_NgCnt = 0
            .P1_TotalCnt = 0
            .P2_TotalCnt = 0
            .P2_OkCnt = 0
            .P2_NgCnt = 0
        End With
        ReDim RegMesProduct.Stock_GradeCnt(MODEL_NAME.MAX_MODEL_NUM - 1)
        ReDim RegMesProduct.Release_GradeCnt(MODEL_NAME.MAX_MODEL_NUM - 1)
        ReDim RegMesProduct.IQC_GradeCnt(MODEL_NAME.MAX_MODEL_NUM - 1)
        ReDim RegMesProduct.IQC_GdCapCnt(MODEL_NAME.MAX_MODEL_NUM - 1, RELAY_NUM.MAX_RELAY_NUM - 1)
        ReDim RegMesProduct.P1_GradeCnt(MODEL_NAME.MAX_MODEL_NUM - 1)
        ReDim RegMesProduct.P2_GradeCnt(MODEL_NAME.MAX_MODEL_NUM - 1)
    End Sub
    Public Function LoadRegProduct() As Boolean
        Dim TodayDate As String
        Dim RetCnt As String
        Dim PuductCmd As String = "MesDate,Stock_ProCnt,Stock_LuxCnt,Stock_EyeCnt,Release_ProCnt,Release_LuxCnt,Release_EyeCnt,PV_TDCnt,IQC_TotalCnt,IQC_OkCnt,IQC_NgCnt,IQC_ProCnt,IQC_LuxCnt,IQC_EyeCnt,IQC_ProR1Cnt,IQC_ProR2Cnt,IQC_ProR3Cnt,IQC_ProR4Cnt,IQC_ProR5Cnt,IQC_ProR6Cnt,IQC_ProR7Cnt,IQC_ProR8Cnt,IQC_ProR9Cnt,IQC_ProR10Cnt,IQC_LuxR1Cnt,IQC_LuxR2Cnt,IQC_LuxR3Cnt,IQC_LuxR4Cnt,IQC_LuxR5Cnt,IQC_LuxR6Cnt,IQC_LuxR7Cnt,IQC_LuxR8Cnt,IQC_LuxR9Cnt,IQC_LuxR10Cnt,IQC_EyeR1Cnt,IQC_EyeR2Cnt,IQC_EyeR3Cnt,IQC_EyeR4Cnt,IQC_EyeR5Cnt,IQC_EyeR6Cnt,IQC_EyeR7Cnt,IQC_EyeR8Cnt,IQC_EyeR9Cnt,IQC_EyeR10Cnt,P1_TotalCnt,P1_OkCnt,P1_NgCnt,P1_ProCnt,P1_LuxCnt,P1_EyeCnt,P2_TotalCnt,P2_OkCnt,P2_NgCnt,P2_ProCnt,P2_LuxCnt,P2_EyeCnt"
        Dim PreNumber As Long
        Dim QueryCnt As Integer
        Dim TmpStrCmd As String = ""

        Try
            If frmTD_JIG.ShowDetailLog Then frmTD_JIG.cmdDisplay("Enter the 'LoadRegProduct' processor")

            ResetRegProduct()
            TodayDate = getNowDate()

            RetCnt = DBQueryReader("select Number from jomtMESTbl where MesDate='" & TodayDate & "';")

            If RetCnt = 0 Then
                StrCmd = "insert jomtMESTbl(" & PuductCmd & ") values('" & TodayDate & "',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);"
                DBCommand(StrCmd)

                RetCnt = DBQueryReader("select Number from jomtMESTbl where MesDate='" & TodayDate & "';")

                If RetCnt Then
                    PreNumber = sqlDataQuery(0)
                Else
                    Return True
                End If

                If PreNumber > 1 Then
                    RetCnt = DBQueryReader("select " & PuductCmd & " from jomtMESTbl where Number=" & (PreNumber - 1) & ";")
                    If RetCnt Then
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & "Stock_ProCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",Stock_LuxCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",Stock_EyeCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",Release_ProCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",Release_LuxCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",Release_EyeCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",PV_TDCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",IQC_TotalCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",IQC_OkCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",IQC_NgCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",IQC_ProCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",IQC_LuxCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",IQC_EyeCnt=" & sqlDataQuery(QueryCnt)

                        For n = 1 To 10
                            QueryCnt += 1
                            TmpStrCmd = TmpStrCmd & ",IQC_ProR" & n & "Cnt=" & sqlDataQuery(QueryCnt)
                        Next
                        For n = 1 To 10
                            QueryCnt += 1
                            TmpStrCmd = TmpStrCmd & ",IQC_LuxR" & n & "Cnt=" & sqlDataQuery(QueryCnt)
                        Next
                        For n = 1 To 10
                            QueryCnt += 1
                            TmpStrCmd = TmpStrCmd & ",IQC_EyeR" & n & "Cnt=" & sqlDataQuery(QueryCnt)
                        Next

                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",P1_TotalCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",P1_OkCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",P1_NgCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",P1_ProCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",P1_LuxCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",P1_EyeCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",P2_TotalCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",P2_OkCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",P2_NgCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",P2_ProCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",P2_LuxCnt=" & sqlDataQuery(QueryCnt)
                        QueryCnt += 1
                        TmpStrCmd = TmpStrCmd & ",P2_EyeCnt=" & sqlDataQuery(QueryCnt)
                    End If

                    StrCmd = "update jomtMESTbl set " & TmpStrCmd & " where MesDate = '" & TodayDate & "';"
                    DBCommand(StrCmd)
                End If
            End If

            RetCnt = DBQueryReader("select " & PuductCmd & " from jomtMESTbl where MesDate='" & TodayDate & "';")

            If RetCnt Then
                QueryCnt = 0
                RegMesProduct.MesDate = sqlDataQuery(QueryCnt)

                For i = MODEL_NAME.PRO To MODEL_NAME.EYE
                    QueryCnt += 1
                    RegMesProduct.Stock_GradeCnt(i) = sqlDataQuery(QueryCnt)
                Next

                For i = MODEL_NAME.PRO To MODEL_NAME.EYE
                    QueryCnt += 1
                    RegMesProduct.Release_GradeCnt(i) = sqlDataQuery(QueryCnt)
                Next

                QueryCnt += 1
                RegMesProduct.PV_TDCnt = sqlDataQuery(QueryCnt)
                QueryCnt += 1
                RegMesProduct.IQC_TotalCnt = sqlDataQuery(QueryCnt)
                QueryCnt += 1
                RegMesProduct.IQC_OkCnt = sqlDataQuery(QueryCnt)
                QueryCnt += 1
                RegMesProduct.IQC_NgCnt = sqlDataQuery(QueryCnt)

                For i = MODEL_NAME.PRO To MODEL_NAME.EYE
                    QueryCnt += 1
                    RegMesProduct.IQC_GradeCnt(i) = sqlDataQuery(QueryCnt)
                Next

                For i = MODEL_NAME.PRO To MODEL_NAME.EYE
                    For n = RELAY_NUM.RL1 To RELAY_NUM.MAX_RELAY_NUM - 1
                        QueryCnt += 1
                        RegMesProduct.IQC_GdCapCnt(i, n) = sqlDataQuery(QueryCnt)
                    Next
                Next

                QueryCnt += 1
                RegMesProduct.P1_TotalCnt = sqlDataQuery(QueryCnt)
                QueryCnt += 1
                RegMesProduct.P1_OkCnt = sqlDataQuery(QueryCnt)
                QueryCnt += 1
                RegMesProduct.P1_NgCnt = sqlDataQuery(QueryCnt)
                For i = MODEL_NAME.PRO To MODEL_NAME.EYE
                    QueryCnt += 1
                    RegMesProduct.P1_GradeCnt(i) = sqlDataQuery(QueryCnt)
                Next

                QueryCnt += 1
                RegMesProduct.P2_TotalCnt = sqlDataQuery(QueryCnt)
                QueryCnt += 1
                RegMesProduct.P2_OkCnt = sqlDataQuery(QueryCnt)
                QueryCnt += 1
                RegMesProduct.P2_NgCnt = sqlDataQuery(QueryCnt)

                For i = MODEL_NAME.PRO To MODEL_NAME.EYE
                    QueryCnt += 1
                    RegMesProduct.P2_GradeCnt(i) = sqlDataQuery(QueryCnt)
                Next

                Return False
            Else
                Return True
            End If

        Catch ex As Exception
            Return True
        End Try
    End Function
    Public Function UploadDB_IQC() As Boolean
        Dim UpdateSetDataStr As String

        Try
            If frmTD_JIG.ShowDetailLog Then frmTD_JIG.cmdDisplay("Enter the 'UploadDB_IQC' processor")
            If RegMeasure.PreTestStep <= 19 Then
                UpdateSetDataStr = "QC_LastDate='" & getNowDate() & "',TestStep='" & RegMeasure.MeasureTestStep & "',QC_Frequency='" & RegTD_VALUE.PV_Frequency & "',QC_Current='" & RegMeasure.MeasureValue(RegMeasure.MeasureValue(0)) & "',QC_Capacitor='" & RegMeasure.MeasureCapacitor & "',QC_Grade='" & RegMeasure.MeasureGrade & "',QC_R1Value='" & RegMeasure.MeasureValue(1) & "',QC_R2Value='" & RegMeasure.MeasureValue(2) & "',QC_R3Value='" & RegMeasure.MeasureValue(3) & "',QC_R4Value='" & RegMeasure.MeasureValue(4) & "',QC_R5Value='" & RegMeasure.MeasureValue(5) & "',QC_R6Value='" & RegMeasure.MeasureValue(6) & "',QC_R7Value='" & RegMeasure.MeasureValue(7) & "',QC_R8Value='" & RegMeasure.MeasureValue(8) & "',QC_R9Value='" & RegMeasure.MeasureValue(9) & "',QC_R10Value='" & RegMeasure.MeasureValue(10) & "'"

                If RegMeasure.PreTestStep = 1 Then
                    UpdateSetDataStr = UpdateSetDataStr & ",QC_Date='" & getNowDate() & "',QC_Time='" & getNowTime() & "'"
                End If

                StrCmd = "update jomtCartridgeTbl set " & UpdateSetDataStr & " where PV_SN ='" & RegTD_VALUE.PV_SN & "';"
                DBCommand(StrCmd)
                Return False
            End If
            Return True
        Catch ex As Exception
            Return True
        End Try
    End Function
    Public Function UploadRegProductIQC() As Boolean
        Dim TmpStrCmd As String

        Try
            If frmTD_JIG.ShowDetailLog Then frmTD_JIG.cmdDisplay("Enter the 'UploadRegProductIQC' processor")

            TmpStrCmd = "PV_TDCnt=" & RegMesProduct.PV_TDCnt
            TmpStrCmd = TmpStrCmd & ",IQC_TotalCnt=" & RegMesProduct.IQC_TotalCnt
            TmpStrCmd = TmpStrCmd & ",IQC_OkCnt=" & RegMesProduct.IQC_OkCnt
            TmpStrCmd = TmpStrCmd & ",IQC_NgCnt=" & RegMesProduct.IQC_NgCnt

            TmpStrCmd = TmpStrCmd & ",IQC_ProCnt=" & RegMesProduct.IQC_GradeCnt(MODEL_NAME.PRO)
            TmpStrCmd = TmpStrCmd & ",IQC_LuxCnt=" & RegMesProduct.IQC_GradeCnt(MODEL_NAME.LUX)
            TmpStrCmd = TmpStrCmd & ",IQC_EyeCnt=" & RegMesProduct.IQC_GradeCnt(MODEL_NAME.EYE)

            For i = MODEL_NAME.PRO To MODEL_NAME.EYE
                For n = RELAY_NUM.RL1 To RELAY_NUM.RL2
                    TmpStrCmd = TmpStrCmd & "," & IQC_GrCapStr(i, n) & "=" & RegMesProduct.IQC_GdCapCnt(i, n)
                Next
            Next

            StrCmd = "update jomtMESTbl set " & TmpStrCmd & " where MesDate = '" & getNowDate() & "';"
            DBCommand(StrCmd)
            Return False

        Catch ex As Exception
            Return True
        End Try
    End Function
    Public Sub CalRegProductPD1()
        Dim gCnt, rCnt As Integer

        Try
            If frmTD_JIG.ShowDetailLog Then frmTD_JIG.cmdDisplay("Enter the 'CalRegProductPD1' processor")

            RegMesProduct.P1_TotalCnt += CntRegMesPD1.PD1_TotalCnt
            RegMesProduct.P1_OkCnt += CntRegMesPD1.PD1_OKCnt
            RegMesProduct.P1_NgCnt += CntRegMesPD1.PD1_NGCnt

            For i = MODEL_NAME.PRO To MODEL_NAME.MAX
                RegMesProduct.P1_GradeCnt(i) += CntRegMesPD1.PD1_GradeCnt(i)
            Next

            RegMesProduct.IQC_TotalCnt -= CntRegMesPD1.PD1_TotalCnt

            gCnt = frmTD_JIG.GradeToValue(RegMeasure.MeasureGrade)
            For rCnt = 1 To 10
                If RegMeasure.MeasureCapacitor = frmTD_JIG.RELAY_C(rCnt) Then
                    Exit For
                End If
            Next

            RegMesProduct.IQC_GradeCnt(gCnt) -= CntRegMesPD1.PD1_GradeCnt(gCnt)
            RegMesProduct.IQC_GdCapCnt(gCnt, rCnt) -= CntRegMesPD1.PD1_CapCnt(rCnt)
        Catch ex As Exception

        End Try

    End Sub
    Public Function UploadRegProductPD1() As Boolean
        Dim TmpStrCmd As String

        Try
            If frmTD_JIG.ShowDetailLog Then frmTD_JIG.cmdDisplay("Enter the 'UploadRegProductPD1' processor")

            TmpStrCmd = "IQC_TotalCnt=" & RegMesProduct.IQC_TotalCnt
            TmpStrCmd = TmpStrCmd & ",IQC_ProCnt=" & RegMesProduct.IQC_GradeCnt(MODEL_NAME.PRO)
            TmpStrCmd = TmpStrCmd & ",IQC_LuxCnt=" & RegMesProduct.IQC_GradeCnt(MODEL_NAME.LUX)
            TmpStrCmd = TmpStrCmd & ",IQC_EyeCnt=" & RegMesProduct.IQC_GradeCnt(MODEL_NAME.EYE)

            For i = MODEL_NAME.PRO To MODEL_NAME.EYE
                For n = RELAY_NUM.RL1 To RELAY_NUM.MAX_RELAY_NUM
                    TmpStrCmd = TmpStrCmd & "," & IQC_GrCapStr(i, n) & "=" & RegMesProduct.IQC_GdCapCnt(i, n)
                Next
            Next

            TmpStrCmd = TmpStrCmd & ",P1_TotalCnt=" & RegMesProduct.P1_TotalCnt
            TmpStrCmd = TmpStrCmd & ",P1_OkCnt=" & RegMesProduct.P1_OkCnt
            TmpStrCmd = TmpStrCmd & ",P1_NgCnt=" & RegMesProduct.P1_NgCnt
            TmpStrCmd = TmpStrCmd & ",P1_ProCnt=" & RegMesProduct.P1_GradeCnt(MODEL_NAME.PRO)
            TmpStrCmd = TmpStrCmd & ",P1_LuxCnt=" & RegMesProduct.P1_GradeCnt(MODEL_NAME.LUX)
            TmpStrCmd = TmpStrCmd & ",P1_EyeCnt=" & RegMesProduct.P1_GradeCnt(MODEL_NAME.EYE)

            StrCmd = "update jomtMESTbl set " & TmpStrCmd & " where MesDate = '" & getNowDate() & "';"
            DBCommand(StrCmd)
            Return False

        Catch ex As Exception
            Return True
        End Try
    End Function
End Module
