Imports System.Data.SqlClient
Imports System.Reflection

Public Class cGestellungsgarantien
    Property gg_Id As Integer
    Property gg_KundenNr As Integer
    Property gg_erhalten_Datum As Date
    Property gg_Zollamt As String
    Property gg_Info As Object = Nothing
    Property gg_datenarchivId As Object = Nothing

    Dim SQL As New SQL

    Sub New()

    End Sub
    Sub New(gg_Id)
        Me.gg_Id = gg_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("gg_Id", gg_Id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gg_KundenNr", gg_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gg_erhalten_Datum", gg_erhalten_Datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gg_Zollamt", gg_Zollamt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gg_Info", gg_Info))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gg_datenarchivId", gg_datenarchivId))

        Return list
    End Function

    Public Shared Function LOAD_LIST(KundenNr As Integer) As DataTable
        Return (New SQL).loadDgvBySql("SELECT * FROM tblGestellungsgarantien WHERE gg_KundenNr=" & KundenNr, "FMZOLL")
    End Function

    Public Shared Function ENTRY_Exists(KundenNr As Integer, Zollamt As String) As Boolean
        Return (CInt((New SQL).getValueTxtBySql("SELECT count(*) FROM tblGestellungsgarantien WHERE gg_KundenNr=" & KundenNr & " AND gg_Zollamt='" & Zollamt & "'", "FMZOLL")) > 0)
    End Function
    Public Shared Function ENTRY_Exists(KundenNr As Integer, Zollamt As String, gg_erhalten_Datum As Date) As Boolean
        Return (CInt((New SQL).getValueTxtBySql("SELECT count(*) FROM tblGestellungsgarantien WHERE gg_KundenNr=" & KundenNr & " AND gg_Zollamt='" & Zollamt & "' AND gg_erhalten_Datum='" & gg_erhalten_Datum.ToShortDateString & "'", "FMZOLL")) > 0)
    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 tblGestellungsgarantien With(updlock, serializable) WHERE gg_Id=@gg_Id) " &
                       " BEGIN " & getUpdateCmd() & " End " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " End " &
                       " commit tran "

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

    Public Function SAVEOBJECT() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN If EXISTS(Select * FROM tblGestellungsgarantien With(updlock, serializable) WHERE gg_Id=@gg_Id) " &
                       " BEGIN " & getUpdateCmd() & " End " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " End " &
                       " commit tran "

        gg_Id = SQL.doSQLVarListID(gg_Id, sqlstr, "FMZOLL", , list)
        Return gg_Id
    End Function

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("Select * FROM tblGestellungsgarantien WHERE gg_Id=@gg_Id  ", conn)
                    cmd.Parameters.AddWithValue("@gg_Id", gg_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

                    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 [tblGestellungsgarantien] SET " & str & " WHERE gg_Id=@gg_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 tblGestellungsgarantien  (" & 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 [tblGestellungsgarantien] WITH(updlock,serializable) WHERE gg_Id=" & Me.gg_Id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function


End Class
