Imports System.Data.SqlClient
Imports System.Reflection

Public Class cDHF_Anhaenge

    Property dhfanh_id As Integer
    Property LizenzNr As String
    Property Operatorid As Integer
    Property AnmID As Integer
    Property MsgTyp As String
    Property ErstelltAm As DateTime
    Property pfad As String
    Property LRN As String
    Property CRN As String
    Property docID As Integer

    Public hasEntry As Boolean = False

    Dim SQL As New SQL

    Sub New()
    End Sub

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

    Sub New(AnmID, MsgTyp)
        LOAD(AnmID, MsgTyp)
    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("dhfanh_id", dhfanh_id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LizenzNr", LizenzNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Operatorid", Operatorid))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AnmID", AnmID))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("MsgTyp", MsgTyp))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ErstelltAm", ErstelltAm))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("pfad", pfad))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LRN", LRN))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("CRN", CRN))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("docID", docID))
        Return list
    End Function



    Public Function SAVE(Optional errHinweis = "") As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

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

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

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblDHF_Anhaenge WHERE dhfanh_id=@dhfanh_id  ", conn)
                    cmd.Parameters.AddWithValue("@dhfanh_id", dhfanh_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 Sub LOAD(AnmID, MsgTyp)
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblDHF_Anhaenge WHERE AnmID=@AnmID AND MsgTyp=@MsgTyp ", conn)
                    cmd.Parameters.AddWithValue("@AnmID", AnmID)
                    cmd.Parameters.AddWithValue("@MsgTyp", MsgTyp)
                    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 tblDHF_Anhaenge SET " & str & " WHERE dhfanh_id=@dhfanh_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 tblDHF_Anhaenge  (" & 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 tblDHF_Anhaenge WITH(updlock,serializable) WHERE dhfanh_id=" & Me.dhfanh_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 tblDHF_Anhaenge  WHERE dhfanh_id=" & id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function
End Class
