Imports System.Data.SqlClient
Imports System.Reflection

Public Class cWarenbezeichnungBegriffe
    Property wb_id As Integer
    Property wb_begriff As String
    Property wb_beispiele As String
    Property wb_datum As Date = Now
    Property wb_Sachbearbeiter As String
    Property wb_SachbearbeiterId As Integer


    Public hasEntry As Boolean = False

    Dim SQL As New SQL

    Sub New()
    End Sub

    Sub New(wb_id)
        If wb_id IsNot Nothing Then
            Me.wb_id = wb_id
            LOAD()
        End If
    End Sub

    Public Function getParameterList() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wb_id", wb_id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wb_begriff", wb_begriff))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wb_beispiele", wb_beispiele))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wb_datum", wb_datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wb_Sachbearbeiter", wb_Sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("wb_SachbearbeiterId", wb_SachbearbeiterId))

        Return list
    End Function



    Public Function SAVE() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        Me.wb_Sachbearbeiter = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
        Me.wb_SachbearbeiterId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblWarenbezeichnungBegriffe WITH(updlock,serializable) WHERE wb_id=@wb_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        wb_id = SQL.doSQLVarListID(wb_id, sqlstr, "FMZOLL", , list)
        Return wb_id > 0
    End Function

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblWarenbezeichnungBegriffe WHERE wb_id=@wb_id  ", conn)
                    cmd.Parameters.AddWithValue("@wb_id", wb_id)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(li.Text))
                            End If
                            hasEntry = True
                        Next

                    End If
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
    End Sub


    Public Function getUpdateCmd() As String
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

            Dim str As String = ""
            For Each i In list
                If Not i.isPrimaryParam Then
                    str &= "[" & i.Text & "] = @" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & ","
                End If
            Next
            str = str.Substring(0, str.Length - 1) 'wg. ','
            Return (" UPDATE [tblWarenbezeichnungBegriffe] SET " & str & " WHERE wb_id=@wb_id ")

        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
        Return ""
    End Function


    Public Function getInsertCmd() As String
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
            Dim str As String = ""
            Dim values As String = ""
            For Each i In list
                If Not i.isPrimaryParam Then
                    str &= "[" & i.Text & "],"
                    values &= "@" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & ","
                End If
            Next
            str = str.Substring(0, str.Length - 1) 'wg. ','
            values = values.Substring(0, values.Length - 1) 'wg. ','
            Return (" INSERT INTO tblWarenbezeichnungBegriffe  (" & str & ") VALUES(" & values & ")  ")
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
        Return ""
    End Function

    Public Function DELETE() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblWarenbezeichnungBegriffe] WITH(updlock,serializable) WHERE wb_id=" & Me.wb_id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function

    Public Shared Function DELETE(id As Integer) As Boolean
        Dim SQL As New SQL
        Dim sqlstr = " DELETE FROM [tblWarenbezeichnungBegriffe]  WHERE wb_id=" & id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function
End Class