
Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cEZT_ImportCodenummern
    Property ezt_id As Integer
    Property ezt_Land As String = ""
    Property ezt_Codenummer As String = ""
    Property ezt_BesMasseinheit As String = ""
    Property ezt_BesMasseinheit2 As String = ""
    Property ezt_Start As Date
    Property ezt_Ende As Date
    Property ezt_Sprache As String = ""
    Property ezt_Text As String = ""

    Dim SQL As New SQL

    Public Shared Function delWholeDB()
        Dim SQL As New SQL
        Return SQL.doSQL("DELETE FROM [tblEZT_Importcodenummern]", "FMZOLL")
    End Function


    Sub New()
    End Sub

    Sub New(ezt_id)
        Me.ezt_id = ezt_id
        LOAD()
    End Sub

    Sub New(ezt_Land, ezt_Codenummer, ezt_BesMasseinheit, ezt_BesMasseinheit2, ezt_Start, ezt_Ende, ezt_Sprache, ezt_Text)
        Me.ezt_Land = ezt_Land
        Me.ezt_BesMasseinheit = ezt_BesMasseinheit
        Me.ezt_Codenummer = ezt_Codenummer
        Me.ezt_BesMasseinheit = ezt_BesMasseinheit
        Me.ezt_BesMasseinheit2 = ezt_BesMasseinheit2
        Me.ezt_Start = ezt_Start
        Me.ezt_Ende = ezt_Ende
        Me.ezt_Sprache = ezt_Sprache
        Me.ezt_Text = ezt_Text
    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("ezt_id", ezt_id, , True, True))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ezt_Land", ezt_Land))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ezt_Codenummer", ezt_Codenummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ezt_BesMasseinheit", ezt_BesMasseinheit))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ezt_BesMasseinheit2", ezt_BesMasseinheit2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ezt_Start", ezt_Start))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ezt_Ende", ezt_Ende))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ezt_Sprache", ezt_Sprache))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ezt_Text", ezt_Text))


        Return list
    End Function



    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblEZT_Importcodenummern WHERE ezt_id=@ezt_id  ", conn)
                    cmd.Parameters.AddWithValue("@ezt_id", ezt_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 SAVE_ByID() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

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

        ezt_id = SQL.doSQLVarListID(ezt_id, sqlstr, "FMZOLL", , list)
        Return ezt_id > 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 tblEZT_Importcodenummern WITH(updlock,serializable) WHERE ezt_Codenummer=@ezt_Codenummer AND ezt_Start=@ezt_Start AND ezt_Ende=@ezt_Ende) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

    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 [tblEZT_Importcodenummern] SET " & str & " WHERE ezt_Codenummer=@ezt_Codenummer AND ezt_Start=@ezt_Start AND ezt_Ende=@ezt_Ende ")

        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 tblEZT_Importcodenummern  (" & 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 getUpdateCmd_ByID() 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 [tblEZT_Importcodenummern] SET " & str & " WHERE ezt_id=@ezt_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



End Class
