Public Class CMaterialManage
    Dim m_SQL As New CSQLInterface

    Const cst_TABLE_NAME As String = "MaterialManageTable"
    Dim m_COLS_NAME() As String = {"料号PN", "仓库ID", "库位ID", "数量", "物料名称", "规格描述", "单位", "单重", "供应商ID", "图片"}
    Private Enum COLS
        料号PN
        仓库ID
        库位ID
        数量
        物料名称
        规格描述
        单位
        单重
        供应商ID
        图片ID
    End Enum

    '权限
    Private m_AccessRight As Integer = RIGHTS.NONE
    Public Property AccessRight() As Integer
        Get
            Return m_AccessRight
        End Get
        Set(ByVal value As Integer)
            m_AccessRight = value
        End Set
    End Property


    Public Sub New()
    End Sub

    Public Function AddItem(ByVal 料号PN As String, _
                            ByVal 仓库ID As String, _
                            ByVal 库位ID As String, _
                            ByVal 数量 As Integer, _
                            ByVal 物料名称 As String, _
                            ByVal 规格描述 As String, _
                            ByVal 单位 As String, _
                            ByVal 单重 As String, _
                            ByVal 供应商ID As String, _
                            ByVal 图片ID As String) As ERROR_CODE
        If CBool(m_AccessRight And RIGHTS.WRITE) = False Then
            Return ERROR_CODE.NORIGHT
        End If
        '"INSERT INTO [dbo].[TABLE1] ([11],[22],[33],[44],[55]) VALUES ('11','2','3','4','5')"
        Dim strSql As String = "INSERT INTO " & cst_TABLE_NAME & "("
        '加入列名
        For i As Integer = 0 To m_COLS_NAME.Length - 1
            If i > 0 Then
                strSql &= ","
            End If
            strSql &= "[" & m_COLS_NAME(i) & "]"
        Next
        strSql &= ") VALUES ("
        strSql &= "'" & 料号PN & "'"
        strSql &= "'" & 仓库ID & "'"
        strSql &= "'" & 库位ID & "'"
        strSql &= "'" & 数量 & "'"
        strSql &= "'" & 物料名称 & "'"
        strSql &= "'" & 规格描述 & "'"
        strSql &= "'" & 单位 & "'"
        strSql &= "'" & 单重 & "'"
        strSql &= "'" & 供应商ID & "'"
        strSql &= "'" & 图片ID & "'"
        strSql &= ")"

        If SQL_ExeCommand(strSql) = True Then
            Return ERROR_CODE.SUCCESS
        End If

        Return ERROR_CODE.ACCESS
    End Function

    Public Function UpdateItem(ByVal 料号PN As String, _
                                ByVal 仓库ID As String, _
                                ByVal 库位ID As String, _
                                ByVal 数量 As Integer, _
                                ByVal 物料名称 As String, _
                                ByVal 规格描述 As String, _
                                ByVal 单位 As String, _
                                ByVal 单重 As String, _
                                ByVal 供应商ID As String, _
                                ByVal 图片ID As String) As ERROR_CODE
        If CBool(m_AccessRight And RIGHTS.WRITE) = False Then
            Return ERROR_CODE.NORIGHT
        End If

        Dim strSql As String = "UPDATE " & cst_TABLE_NAME & " SET"
        strSql &= " " & m_COLS_NAME(COLS.仓库ID) & "=" & 仓库ID
        strSql &= ", " & m_COLS_NAME(COLS.库位ID) & "=" & 库位ID
        strSql &= ", " & m_COLS_NAME(COLS.数量) & "=" & 数量
        strSql &= ", " & m_COLS_NAME(COLS.物料名称) & "=" & 物料名称
        strSql &= ", " & m_COLS_NAME(COLS.规格描述) & "=" & 规格描述
        strSql &= ", " & m_COLS_NAME(COLS.单位) & "=" & 单位
        strSql &= ", " & m_COLS_NAME(COLS.单重) & "=" & 单重
        strSql &= ", " & m_COLS_NAME(COLS.供应商ID) & "=" & 供应商ID
        strSql &= ", " & m_COLS_NAME(COLS.图片ID) & "=" & 图片ID
        strSql &= " WHERE "
        strSql &= m_COLS_NAME(COLS.料号PN) & "=" & 料号PN

        If SQL_ExeCommand(strSql) = True Then
            Return ERROR_CODE.SUCCESS
        End If

        Return ERROR_CODE.ACCESS
    End Function

    Public Function DeleteItem(ByVal 料号PN As String) As ERROR_CODE
        If CBool(m_AccessRight And RIGHTS.WRITE) = False Then
            Return ERROR_CODE.NORIGHT
        End If

        Dim strSql As String = "DELETE FROM " & cst_TABLE_NAME & " WHERE "
        strSql &= m_COLS_NAME(COLS.料号PN) & "=" & 料号PN

        If SQL_ExeCommand(strSql) = True Then
            Return ERROR_CODE.SUCCESS
        End If

        Return ERROR_CODE.ACCESS
    End Function

    Public Function QueryItem(ByVal 料号PN As String, _
                            ByRef 仓库ID As String, _
                            ByRef 库位ID As String, _
                            ByRef 数量 As Integer, _
                            ByRef 物料名称 As String, _
                            ByRef 规格描述 As String, _
                            ByRef 单位 As String, _
                            ByRef 单重 As String, _
                            ByRef 供应商ID As String, _
                            ByRef 图片ID As String) As ERROR_CODE
        If CBool(m_AccessRight And RIGHTS.READ) = False Then
            Return ERROR_CODE.NORIGHT
        End If

        Dim strSql As String = "SELECT * FROM " & cst_TABLE_NAME & " WHERE "
        strSql &= m_COLS_NAME(COLS.料号PN) & "=" & 料号PN

        Dim rTable As New System.Data.DataTable
        If SQL_Query(strSql, rTable) = True Then
            仓库ID = rTable.Rows(0).Item(1)
            库位ID = rTable.Rows(0).Item(2)
            数量 = rTable.Rows(0).Item(3)
            物料名称 = rTable.Rows(0).Item(4)
            规格描述 = rTable.Rows(0).Item(5)
            单位 = rTable.Rows(0).Item(6)
            单重 = rTable.Rows(0).Item(7)
            供应商ID = rTable.Rows(0).Item(8)
            图片ID = rTable.Rows(0).Item(9)
            Return ERROR_CODE.SUCCESS
        End If

        Return ERROR_CODE.ACCESS
    End Function
End Class
