Public Class CMaterialOut
    Dim m_SQL As New CSQLInterface

    Const cst_TABLE_NAME As String = "MaterialOutTable"
    Dim m_COLS_NAME() As String = {
        "出库单PN",
        "出库日期",
        "出库时间",
        "出库数量",
        "单价",
        "总金额",
        "操作员ID",
        "料号PN",
        "供应商ID"}

    Private Enum COLS
        出库单PN
        出库日期
        出库时间
        出库数量
        单价
        总金额
        操作员ID
        料号PN
        供应商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 出库日期 As String, _
                            ByVal 出库时间 As String, _
                            ByVal 出库数量 As Integer, _
                            ByVal 单价 As String, _
                            ByVal 总金额 As String, _
                            ByVal 操作员ID As String, _
                            ByVal 料号PN 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 &= "'" & 出库日期 & "'"
        strSql &= "'" & 出库时间 & "'"
        strSql &= "'" & 出库数量 & "'"
        strSql &= "'" & 单价 & "'"
        strSql &= "'" & 总金额 & "'"
        strSql &= "'" & 操作员ID & "'"
        strSql &= "'" & 料号PN & "'"
        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 出库日期 As String, _
                            ByVal 出库时间 As String, _
                            ByVal 出库数量 As Integer, _
                            ByVal 单价 As String, _
                            ByVal 总金额 As String, _
                            ByVal 操作员ID As String, _
                            ByVal 料号PN 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.出库日期) & "=" & 出库日期
        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.料号PN) & "=" & 料号PN
        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, _
                            ByVal 出库日期 As String, _
                            ByVal 出库时间 As String, _
                            ByVal 出库数量 As Integer, _
                            ByVal 单价 As String, _
                            ByVal 总金额 As String, _
                            ByVal 操作员ID As String, _
                            ByVal 料号PN As String, _
                            ByVal 供应商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
            出库单PN = rTable.Rows(0).Item(1)
            出库日期 = 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)
            操作员ID = rTable.Rows(0).Item(7)
            料号PN = 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
