

Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cGrenzstelle
    Property grz_Grenzstelle As String

    Property grz_BezeichnungCBO As String
    Property grz_Bezeichnung As String
    Property grz_Firma As String
    Property grz_Cluster As Object = Nothing
    Property grz_Reihenfolge As Integer
    Property grz_Warenort As Boolean
    Property grz_Zollamt As Boolean
    Property grz_Aktiv As Boolean
    Property grz_ZollamtDST As Object = Nothing
    Property grz_ZollamtBezeichnung As Object = Nothing



    Public hasEntry As Boolean = False

    Dim SQL As New SQL

    Sub New()

    End Sub
    Sub New(grz_Grenzstelle As String)
        Me.grz_Grenzstelle = grz_Grenzstelle
        LOAD()
    End Sub


    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("grz_Grenzstelle", grz_Grenzstelle,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("grz_BezeichnungCBO", grz_BezeichnungCBO))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("grz_Bezeichnung", grz_Bezeichnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("grz_Firma", grz_Firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("grz_Cluster", grz_Cluster))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("grz_Reihenfolge", grz_Reihenfolge))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("grz_Warenort", grz_Warenort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("grz_Zollamt", grz_Zollamt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("grz_Aktiv", grz_Aktiv))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("grz_ZollamtDST", grz_ZollamtDST))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("grz_ZollamtBezeichnung", grz_ZollamtBezeichnung))

        Return list
    End Function



    'Public Shared Function LOADByBezeichnung(grz_Bezeichnung) As cGrenzstelle
    '    Try
    '        Dim GRZ As New cGrenzstelle

    '        Using conn As SqlConnection = SQL.GetNewOpenConnectionAVISO()
    '            MsgBox("SELECT * FROM tblGrenzstelle WHERE grz_Bezeichnung=@grz_Bezeichnung  " & grz_Bezeichnung)
    '            Using cmd As New SqlCommand("SELECT * FROM tblGrenzstelle WHERE grz_Bezeichnung=@grz_Bezeichnung  ", conn)
    '                cmd.Parameters.AddWithValue("@grz_Bezeichnung", grz_Bezeichnung)
    '                Dim dr = cmd.ExecuteReader()
    '                If dr.Read Then
    '                    For Each li In GRZ.getParameterList()
    '                        Dim propInfo As PropertyInfo = GRZ.GetType.GetProperty(li.Scalarvariable)

    '                        If dr.Item(li.Text) Is DBNull.Value Then
    '                            propInfo.SetValue(GRZ, Nothing)
    '                        Else
    '                            propInfo.SetValue(GRZ, dr.Item(li.Text))
    '                        End If

    '                    Next
    '                    GRZ.hasEntry = True
    '                End If
    '                dr.Close()
    '            End Using
    '            Return GRZ
    '        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
    '    Return Nothing
    'End Function
    Public Function SAVE() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblGrenzstelle  WHERE grz_Grenzstelle=@grz_Grenzstelle) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        Return SQL.doSQLVarList(sqlstr, "AVISO", , list)
    End Function

    Public Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionAVISO()
                Using cmd As New SqlCommand("SELECT * FROM tblGrenzstelle WHERE grz_Grenzstelle=@grz_Grenzstelle  ", conn)
                    cmd.Parameters.AddWithValue("@grz_Grenzstelle", grz_Grenzstelle)
                    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

                        Next
                        hasEntry = True
                    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 [tblGrenzstelle] SET " & str & " WHERE grz_Grenzstelle=@grz_Grenzstelle ")

        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 tblGrenzstelle  (" & 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

End Class
