Public Module mdl_SQLAccessManage
    Public m_SQL As New CMSQL_Interface
    Dim m_Log As New CLogManage

    Public Enum COL_RIGHTS
        登录
        系统记录
        用户管理
        料号管理
        原料库存管理
        成品库存管理
        BOM管理
        供应商管理
        ECN管理
        公司管理
        部门管理
        仓库管理
        库位管理
        财务管理
        max
    End Enum

    Public AccessTableName() As String =
    {
        "",
        "系统记录表",
        "帐号表",
        "料号表",
        "原料库存表",
        "成品库存表",
        "BOM表",
        "供应商信息表",
        "",
        "公司信息表",
        "部门信息表",
        "仓库表",
        "库位表",
        "收支明细表"
    }

    Public Current_UserName As String
    Public Current_Rights(COL_RIGHTS.max - 1) As RIGHTS

    Public Function SQL_ConnectionSetting(ByVal strHostIp As String, ByVal DataBaseName As String, ByVal user As String, ByVal password As String) As Boolean
        Return m_SQL.ConnectionSetting(strHostIp, DataBaseName, user, password)
    End Function

    Public Function SQL_Query(ByVal RightID As RIGHTS, ByVal strQuery As String, ByRef r_Table As DataTable) As Boolean
        If CBool(Current_Rights(RightID) And RIGHTS.READ) Then
            Return m_SQL.Query(strQuery, r_Table)
        End If
        Return False
    End Function

    Private Function GetUpdateLogString(ByRef tbl As DataTable) As String
        Dim rString As String = ""
        For row As Integer = 0 To tbl.Rows.Count - 1
            Dim rowString As String = ""
            Dim tStr As String = ""
            For col As Integer = 0 To tbl.Columns.Count - 1
                Try
                    tStr = tbl.Rows.Item(row).Item(col)
                Catch ex As Exception
                    tStr = ""
                End Try

                If rowString.Length > 0 Then
                    rowString &= ","
                End If
                rowString &= tStr
            Next

            If rString.Length > 0 Then
                rString &= ";"
            End If
            rString &= rowString
        Next

        Return rString
    End Function

    Public Function SQL_Update(ByVal RightID As RIGHTS, ByRef upTable As DataTable) As Boolean
        If CBool(Current_Rights(RightID) And RIGHTS.WRITE) Then
            If m_SQL.Update(upTable) = True Then
                m_Log.AddItem(AccessTableName(RightID), "UPDATE: " & GetUpdateLogString(upTable), Current_UserName)
                Return True
            End If
        End If
        Return False
    End Function

    Public Function SQL_ExeCommand(ByVal RightID As RIGHTS, ByVal strExe As String) As Boolean
        If CBool(Current_Rights(RightID) And RIGHTS.WRITE) Then
            If m_SQL.ExeCommand(strExe) = True Then
                m_Log.AddItem(AccessTableName(RightID), "EXECUTE:" & strExe, Current_UserName)
                Return True
            End If
        End If
        Return False
    End Function

    Public Function SQL_ExeCommandWithParamters(ByVal RightID As RIGHTS, ByVal strExe As String, ByRef ParamName() As String, ByRef ParamValue() As Object) As Boolean
        If CBool(Current_Rights(RightID) And RIGHTS.WRITE) Then
            If m_SQL.ExeCommandWithParamters(strExe, ParamName, ParamValue) = True Then
                m_Log.AddItem(AccessTableName(RightID), "EXECUTE:" & strExe, Current_UserName)
                Return True
            End If
        End If
        Return False
    End Function

    Public Function SQL_DataReader(ByVal RightID As RIGHTS, ByVal strSelect As String, ByRef valueTable() As Object) As Boolean
        If CBool(Current_Rights(RightID) And RIGHTS.READ) Then
            Return m_SQL.DataReader(strSelect, valueTable)
        End If
        Return False
    End Function
End Module
