Imports System.Data.SqlClient
Imports System.Reflection

Public Class cAvisoAnhaenge
    Property anh_id As Integer
    Property anh_AvisoId As Integer
    Property anh_SendungsId As Object = Nothing
    Property anh_Name As String
    Property anh_docId As Integer
    Property anh_Art As Object = Nothing
    Property anh_Typ As String
    Property anh_Reihenfolge As Integer = 99
    Property anh_LaufzettelDruck As Boolean = False
    Property anh_GestellungslisteAnfuegen As Boolean = False
    Property anh_markiert As Boolean = False
    Property anh_size As Double
    Property anh_cmrId As Integer


    Public hasEntry As Boolean = False

    Dim SQL As New SQL

    Sub New()
    End Sub

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

    Sub New(anh_AvisoId, anh_Name, anh_docId, anh_Art, anh_Typ, Optional anh_SendungsId = Nothing, Optional anh_Reihenfolge = 99, Optional anh_size = 0, Optional anh_cmrId = -1)
        Me.anh_AvisoId = anh_AvisoId
        Me.anh_SendungsId = anh_SendungsId
        Me.anh_Name = anh_Name
        Me.anh_docId = anh_docId
        Me.anh_Art = anh_Art
        Me.anh_Typ = anh_Typ
        Me.anh_Reihenfolge = anh_Reihenfolge
        Me.anh_size = anh_size
        Me.anh_cmrId = anh_cmrId

        If anh_Art.ToString.Equals("Auftrag") Then
            anh_markiert = True
        End If



    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("anh_id", anh_id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("anh_AvisoId", anh_AvisoId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("anh_SendungsId", anh_SendungsId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("anh_Name", anh_Name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("anh_docId", anh_docId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("anh_Art", anh_Art))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("anh_Typ", anh_Typ))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("anh_Reihenfolge", anh_Reihenfolge))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("anh_LaufzettelDruck", anh_LaufzettelDruck))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("anh_GestellungslisteAnfuegen", anh_GestellungslisteAnfuegen))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("anh_markiert", anh_markiert))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("anh_size", anh_size))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("anh_cmrId", anh_cmrId))

        Return list
    End Function


    Shared Sub LOAD_LIST_ByAviso(ByRef ANH_LIST As List(Of cAvisoAnhaenge), anh_AvisoId As Integer)
        If ANH_LIST Is Nothing Then ANH_LIST = New List(Of cAvisoAnhaenge)
        ANH_LIST.Clear()
        Dim SQL As New SQL
        For Each r In SQL.loadDgvBySql("SELECT anh_id FROM tblAvisoAnhaenge WHERE anh_AvisoId=" & anh_AvisoId & " ORDER BY anh_Reihenfolge,anh_Name,anh_id", "AVISO").Rows
            ANH_LIST.Add(New VERAG_PROG_ALLGEMEIN.cAvisoAnhaenge(r("anh_id")))
        Next
        '  If allowchangeCurrIndex Then CURRENT_INDEX = IIf(ANH_LIST.Count > 0, 0, -1)
    End Sub

    Shared Sub LOAD_LIST_BySendung(ByRef ANH_LIST As List(Of cAvisoAnhaenge), anh_SendungsId As Integer)
        If ANH_LIST Is Nothing Then ANH_LIST = New List(Of cAvisoAnhaenge)
        ANH_LIST.Clear()
        Dim SQL As New SQL
        For Each r In SQL.loadDgvBySql("SELECT anh_id FROM tblAvisoAnhaenge WHERE anh_SendungsId=" & anh_SendungsId & " ORDER BY anh_Reihenfolge,anh_Name,anh_id", "AVISO").Rows
            ANH_LIST.Add(New VERAG_PROG_ALLGEMEIN.cAvisoAnhaenge(r("anh_id")))
        Next
        '  If allowchangeCurrIndex Then CURRENT_INDEX = IIf(ANH_LIST.Count > 0, 0, -1)
    End Sub

    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 tblAvisoAnhaenge WITH(updlock,serializable) WHERE anh_id=@anh_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionAVISO()
                Using cmd As New SqlCommand("SELECT * FROM tblAvisoAnhaenge WHERE anh_id=@anh_id  ", conn)
                    cmd.Parameters.AddWithValue("@anh_id", anh_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 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 [tblAvisoAnhaenge] SET " & str & " WHERE anh_id=@anh_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 tblAvisoAnhaenge  (" & 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 [tblAvisoAnhaenge] WITH(updlock,serializable) WHERE anh_id=" & Me.anh_id
        Return SQL.doSQL(sqlstr, "AVISO")
    End Function

    Public Shared Function DELETE(id As Integer) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblAvisoAnhaenge] WITH(updlock,serializable) WHERE anh_id=" & id
        Return (New VERAG_PROG_ALLGEMEIN.SQL).doSQL(sqlstr, "AVISO")
    End Function

    Public Shared Function toggleLaufzettelDruck(id As Integer) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " UPDATE [tblAvisoAnhaenge]  SET [anh_LaufzettelDruck] = 1 - [anh_LaufzettelDruck] WHERE anh_id=" & id
        Return (New VERAG_PROG_ALLGEMEIN.SQL).doSQL(sqlstr, "AVISO")
    End Function

    Public Shared Function toggleGestellungslisteAnfuegen(id As Integer) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " UPDATE [tblAvisoAnhaenge]  SET [anh_GestellungslisteAnfuegen] = 1 - [anh_GestellungslisteAnfuegen] WHERE anh_id=" & id
        Return (New VERAG_PROG_ALLGEMEIN.SQL).doSQL(sqlstr, "AVISO")
    End Function

End Class
