Imports System.Data.SqlClient
Imports System.Reflection

Public Class cKundenBesonderheiten_LIST
    Sub New(KundenNr, Optional hervorheben = Nothing, Optional kdb_kategorie_in = Nothing)
        LOAD(KundenNr, hervorheben, kdb_kategorie_in)
    End Sub
    Public LIST As New List(Of cKundenBesonderheiten)
    Public Sub LOAD(KundenNr, Optional hervorheben = Nothing, Optional kdb_kategorie_in = Nothing)
        LIST.Clear()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblKundenBesonderheiten WHERE kdb_KundenNr=@kdb_KundenNr  " & If(hervorheben IsNot Nothing, "  AND [kdb_hervorheben]=@kdb_hervorheben ", "") & If(kdb_kategorie_in IsNot Nothing, " AND [kdb_kategorie] IN (" & kdb_kategorie_in & ") ", ""), conn)
                    cmd.Parameters.AddWithValue("@kdb_KundenNr", KundenNr)
                    If hervorheben IsNot Nothing Then cmd.Parameters.AddWithValue("@kdb_hervorheben", CBool(hervorheben))
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim BH As New cKundenBesonderheiten
                        For Each li In BH.getParameterList()
                            Dim propInfo As PropertyInfo = BH.GetType.GetProperty(li.Scalarvariable)

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

                        Next
                        LIST.Add(BH)
                    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

End Class
Public Class cKundenBesonderheiten
    Property kdb_id As Integer
    Property kdb_KundenNr As Integer
    Property kdb_kategorie As String
    Property kdb_text As String
    Property kdb_mitId As Integer
    Property kdb_mitName As String
    Property kdb_EingetragenAm As Date
    Property kdb_AenderungAm As Date
    Property kdb_visible As Boolean = True
    Property kdb_hervorheben As Boolean = False
    Property kdb_history As Boolean = False

    Public hasEntry = False

    Dim SQL As New SQL

    Sub New()

    End Sub
    Sub New(kdb_id)
        Me.kdb_id = kdb_id
        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("kdb_id", kdb_id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_KundenNr", kdb_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_kategorie", kdb_kategorie))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_text", kdb_text))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_mitId", kdb_mitId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_mitName", kdb_mitName))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_EingetragenAm", kdb_EingetragenAm))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_AenderungAm", kdb_AenderungAm))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_visible", kdb_visible))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_hervorheben", kdb_hervorheben))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("kdb_history", kdb_history))

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

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

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

                        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 Shared Function DELETE_ALL_KD(kdb_KundenNr As Integer) As Boolean
        Dim SQL As New SQL
        Dim sqlstr = " DELETE FROM tblKundenBesonderheiten  WHERE kdb_KundenNr=" & kdb_KundenNr
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function



    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 [tblKundenBesonderheiten] SET " & str & " WHERE kdb_id=@kdb_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 tblKundenBesonderheiten  (" & 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
