Imports MySql.Data  ' 데이터베이스 연동을 위한 네임스페이스 추가
Imports MySql.Data.MySqlClient  ' MYSQL Server용 네임스페이스 추가
Module MySqlModule
    Private Const ServerIP As String = "192.168.0.250"
    Private Const DataBaseName As String = "jomtTesterDB"
    'Private Const DataBaseName As String = "jomtTesterServerDB"
    Private Const UserDataBaseName As String = "jomtAccountDB"
    Private Const UserName As String = "root"
    Private Const Password As String = "JOMT1646db!!"
    Private Conn As String = "Server=" + ServerIP + ";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

    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
            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
            If sqlCon.State = ConnectionState.Open Then sqlCon.Close()
            Return 0
        End Try
        End
    End Function

    Public Function CheckExistTD_SN(strPV_SN As String) As String
        StrCmd = "select PV_SN, Number from jomtCartridgeTbl where PV_SN = '" & strPV_SN & "';"
        If DBQueryReader(StrCmd) Then
            If sqlDataQuery(0) = strPV_SN.ToUpper() Then
                Return sqlDataQuery(1)
            Else
                Return False
            End If
        End If
        Return False
    End Function
    Public Function CheckExistCT_SN(strCT_SN As String) As String
        StrCmd = "select Product_SN,Number from jomtCartridgeTbl where Product_SN = '" & strCT_SN & "';"
        If DBQueryReader(StrCmd) Then
            If sqlDataQuery(0) = strCT_SN.ToUpper() Then
                Return sqlDataQuery(1)
            Else
                Return False
            End If
        End If
        Return False
    End Function
    Public Function CheckExistDEV_SN(strDEV_SN As String) As String
        StrCmd = "select Product_SN,Number from jomtDeviceTbl where Product_SN = '" & strDEV_SN & "';"
        If DBQueryReader(StrCmd) Then
            If sqlDataQuery(0) = strDEV_SN.ToUpper() Then
                Return sqlDataQuery(1)
            Else
                Return False
            End If
        End If
        Return False
    End Function
    Public Function CheckExistAssy_SN(strItem As String, strDEV_SN As String) As Long
        StrCmd = "select " & strItem & ",Number from jomtProductTbl where " & strItem & " = '" & strDEV_SN & "';"
        If DBQueryReader(StrCmd) Then
            If sqlDataQuery(0) = strDEV_SN.ToUpper() Then
                Return Val(sqlDataQuery(1))
            Else
                Return 0
            End If
        End If
        Return 0
    End Function
    Public Function CheckExistCountDate(strDate As String) As String

        StrCmd = "select Date from jomtMES_ProductTbl where Date='" & strDate & "';"
        If DBQueryReader(StrCmd) Then
            If sqlDataQuery(0) = strDate Then
                Return sqlDataQuery(0)
            Else
                Return 0
            End If
        End If
        Return 0
    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

        DateTimeStr = 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

End Module
