

Imports System.Data.SqlClient
    Imports System.Reflection

Public Class cNCTS_TR_Nachrichten
    Property nctsNr_id As Integer
    Property nctsNr_nctsId As Integer
    Property nctsNr_typ As String
    Property nctsNr_GUID As Object = Nothing
    Property nctsNr_MRN As Object = Nothing
    Property nctsNr_LRN As Object = Nothing
    Property nctsNr_path As String
    Property nctsNr_tstmp As Date = Now

    Public hasEntry = False

    Dim SQL As New SQL
    Sub New()

    End Sub
    Sub New(nctsNr_id)
        Me.nctsNr_id = nctsNr_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("nctsNr_id", nctsNr_id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nctsNr_nctsId", nctsNr_nctsId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nctsNr_typ", nctsNr_typ))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nctsNr_GUID", nctsNr_GUID))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nctsNr_MRN", nctsNr_MRN))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nctsNr_LRN", nctsNr_LRN))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nctsNr_path", nctsNr_path))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("nctsNr_tstmp", nctsNr_tstmp))

        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 tblNCTS_TR_Nachrichten  WHERE nctsNr_id=@nctsNr_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        Dim ncts_IdTMP = nctsNr_id
        ncts_IdTMP = SQL.doSQLVarListID(ncts_IdTMP, sqlstr, "FMZOLL", , list, , errHinweis)
        If nctsNr_id <= 0 Then nctsNr_id = ncts_IdTMP
        Return nctsNr_id > 0

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

    Public Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblNCTS_TR_Nachrichten WHERE nctsNr_id=@nctsNr_id  ", conn)
                    cmd.Parameters.AddWithValue("@nctsNr_id", nctsNr_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
                        hasEntry = True
                    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 Shared Function updatePath(nr_id As Integer, path As String) As String
        Try
            Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
            SQL.doSQL(" UPDATE [tblNCTS_TR_Nachrichten] SET nctsNr_path='" & path & "' WHERE nctsNr_id='" & nr_id & "' ", "FMZOLL")

        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() 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 [tblNCTS_TR_Nachrichten] SET " & str & " WHERE nctsNr_id=@nctsNr_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 tblNCTS_TR_Nachrichten  (" & 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

End Class
