Imports System.Data.SqlClient
Imports System.Reflection

Public Class cKundenDatenblatt
    Property kdb_KundenNr As Integer
    Property kdb_route As Object = Nothing
    Property kdb_buergschaftsgeber As Object = Nothing
    Property kdb_anzahlLKW As Object = Nothing
    Property kdb_anzahlMA As Object = Nothing
    Property kdb_bestimmungslandEU As Object = Nothing
    Property kdb_grosskunden As Object = Nothing
    Property kdb_gueter As Object = Nothing
    Property kdb_grenzen As Object = Nothing
    Property kdb_verhaltenAvisierung As Object = Nothing
    Property kdb_sonstiges As Object = Nothing

    Dim SQL As New SQL

    Sub New(kdb_KundenNr)
        Me.kdb_KundenNr = kdb_KundenNr
        LOAD()
    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("kdb_KundenNr", kdb_KundenNr))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_route", kdb_route))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_buergschaftsgeber", kdb_buergschaftsgeber))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_anzahlLKW", kdb_anzahlLKW))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_anzahlMA", kdb_anzahlMA))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_bestimmungslandEU", kdb_bestimmungslandEU))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_grosskunden", kdb_grosskunden))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_gueter", kdb_gueter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_grenzen", kdb_grenzen))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_verhaltenAvisierung", kdb_verhaltenAvisierung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_sonstiges", kdb_sonstiges))


        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 tblKundenDatenblatt WHERE kdb_KundenNr=@kdb_KundenNr) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblKundenDatenblatt WHERE kdb_KundenNr=@kdb_KundenNr  ", conn)
                    cmd.Parameters.AddWithValue("@kdb_KundenNr", kdb_KundenNr)
                    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 [tblKundenDatenblatt] SET " & str & " WHERE kdb_KundenNr=@kdb_KundenNr ")

        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 tblKundenDatenblatt  (" & 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 [tblKundenDatenblatt] WHERE kdb_KundenNr=" & Me.kdb_KundenNr
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function



End Class
