
Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cNCTSGestellungsadressen
    Property nga_id As Integer
    Property nga_name As String = ""
    Property nga_strasse As Object = Nothing
    Property nga_land As Object = Nothing
    Property nga_plz As Object = Nothing
    Property nga_ort As Object = Nothing
    Property nga_EORI As Object = Nothing
    Property nga_EORI_NL As Object = Nothing
    Property nga_Ansprechpartner As Object = Nothing
    Property nga_bestZollst As Object = Nothing
    Property nga_SB As Object = Nothing
    Property nga_firma As Object = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
    Property nga_Aenderungsdatum As Date = Now


    Dim SQL As New SQL

    Sub New()
        nga_SB = If(nga_SB, VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME)
        'If sms_SachbearbeiterId <= 0 Then sms_SachbearbeiterId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID

    End Sub

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


        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nga_name", nga_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nga_strasse", nga_strasse))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nga_land", nga_land))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nga_plz", nga_plz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nga_ort", nga_ort))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nga_EORI", nga_EORI))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nga_EORI_NL", nga_EORI_NL))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nga_Ansprechpartner", nga_Ansprechpartner))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nga_bestZollst", nga_bestZollst))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nga_SB", nga_SB))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nga_firma", nga_firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nga_Aenderungsdatum", nga_Aenderungsdatum))

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

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


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



End Class
