
Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cVermerkeCodes_LIST
    Dim LIST As New List(Of cVermerkeCodes)

    Sub New()
        LOAD_LIST()
    End Sub
    Public Sub LOAD_LIST()
        Try
            LIST.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionAVISO()
                Using cmd As New SqlCommand("SELECT * FROM VermerkeCodes ", conn)

                    '  cmd.Parameters.AddWithValue("@OffertenNr", OffertenNr)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cVermerkeCodes
                        For Each i In l.getParameterList()
                            Dim propInfo As PropertyInfo = l.GetType.GetProperty(i.Scalarvariable)
                            If dr.Item(i.Text) Is DBNull.Value Then
                                propInfo.SetValue(l, Nothing)
                            Else
                                propInfo.SetValue(l, dr.Item(i.Text))
                            End If
                        Next
                        LIST.Add(l)

                    End While
                    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 getTxt(VermerkCodeId, language) As String
        For Each li In LIST
            If li.VermerkCodeId = VermerkCodeId Then
                Select Case language
                    Case "EN" : If li.Beschreibung_EN <> "" Then Return li.Beschreibung_EN
                    Case "TR" : If li.Beschreibung_TR <> "" Then Return li.Beschreibung_TR
                    Case "SRB", "YU" : If li.Beschreibung_SRB <> "" Then Return li.Beschreibung_SRB
                    Case "RO" : If li.Beschreibung_RO <> "" Then Return li.Beschreibung_RO
                    Case "BG" : If li.Beschreibung_BG <> "" Then Return li.Beschreibung_BG
                    Case "HU" : If li.Beschreibung_HU <> "" Then Return li.Beschreibung_HU
                    Case Else : If li.Beschreibung_DE <> "" Then Return li.Beschreibung_DE
                End Select
                Return li.Bezeichnung
            End If
        Next
        Return ""
    End Function

End Class




Public Class cVermerkeCodes
    Property VermerkCodeId As Integer
    Property Bezeichnung As String = ""
    Property LKW_Vermerk As Boolean
    Property Kunde_Vermerk As Boolean
    Property Beschreibung_DE As String = ""
    Property Beschreibung_EN As String = ""
    Property Beschreibung_TR As String = ""
    Property Beschreibung_SRB As String = ""
    Property Beschreibung_RO As String = ""
    Property Beschreibung_BG As String = ""
    Property Beschreibung_HU As String = ""


    Dim SQL As New SQL

    Sub New()

    End Sub
    Sub New(VermerkCodeId)
        Me.VermerkCodeId = VermerkCodeId
        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("VermerkCodeId", VermerkCodeId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bezeichnung", Bezeichnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LKW_Vermerk", LKW_Vermerk))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Kunde_Vermerk", Kunde_Vermerk))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Beschreibung_DE", Beschreibung_DE))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Beschreibung_EN", Beschreibung_EN))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Beschreibung_TR", Beschreibung_TR))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Beschreibung_SRB", Beschreibung_SRB))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Beschreibung_RO", Beschreibung_RO))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Beschreibung_BG", Beschreibung_BG))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Beschreibung_HU", Beschreibung_HU))

        Return list
    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 VermerkeCodes  WHERE VermerkCodeId=@VermerkCodeId) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

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

                    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 [VermerkeCodes] SET " & str & " WHERE VermerkCodeId=@VermerkCodeId ")

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