Imports System.Data.SqlClient
Imports System.Reflection

Public Class cGesamtsicherheitsPositionen

    Property gsp_gspPosId As Integer
    Property gsp_gsId As Object = Nothing
    Property gsp_gsNr As Object = Nothing
    Property gsp_warenwert As Object = Nothing
    Property gsp_sicherheitsbetrag As Object = Nothing
    Property gsp_freitext As Object = Nothing
    Property gsp_art As Object = Nothing
    Property gsp_avisoId As Object = Nothing
    Property gsp_ust As Object = Nothing
    Property gsp_filialenNr As Object = Nothing
    Property gsp_abfertigungsNr As Object = Nothing
    Property gsp_ATCNr As String
    Property gsp_MRNNr As String
    Property gsp_erstellungsdatum As DateTime
    Property gsp_isPosRundungsdiff As Boolean
    Property gsp_zuletztgeandert As Object = Nothing
    Property gsp_systemuser As Object = Nothing
    Property gsp_datum As Object = Nothing
    Property gsp_EH_Anzahl As Object = Nothing

    Public hasEntry = False

    Dim SQL As New SQL

    Sub New(gsp_gsId, gsp_gspPosId)
        Me.gsp_gsId = gsp_gsId
        Me.gsp_gspPosId = gsp_gspPosId
        LOAD()
    End Sub

    Sub New()

    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("gsp_gspPosId", gsp_gspPosId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_gsId", gsp_gsId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_gsNr", gsp_gsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_warenwert", gsp_warenwert))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_sicherheitsbetrag", gsp_sicherheitsbetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_freitext", gsp_freitext))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_art", gsp_art))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_avisoId", gsp_avisoId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_ust", gsp_ust))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_filialenNr", gsp_filialenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_abfertigungsNr", gsp_abfertigungsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_ATCNr", gsp_ATCNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_erstellungsdatum", gsp_erstellungsdatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_datum", gsp_datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_isPosRundungsdiff", gsp_isPosRundungsdiff))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_systemuser", gsp_systemuser))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_zuletztgeandert", gsp_zuletztgeandert))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_MRNNr", gsp_MRNNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gsp_EH_Anzahl", gsp_EH_Anzahl))

        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 tblGesamtsicherheitsPositionen WHERE gsp_gspPosId=@gsp_gspPosId AND gsp_gsId=@gsp_gsId ) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

    Public Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionAVISO()
                Using cmd As New SqlCommand("SELECT * FROM tblGesamtsicherheitsPositionen WHERE gsp_gspPosId=@gsp_gspPosId AND gsp_gsId=@gsp_gsId ", conn)
                    cmd.Parameters.AddWithValue("@gsp_gspPosId", gsp_gspPosId)
                    cmd.Parameters.AddWithValue("@gsp_gsId", gsp_gsId)
                    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 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 [tblGesamtsicherheitsPositionen] SET " & str & " WHERE gsp_gspPosId=@gsp_gspPosId AND gsp_gsId=@gsp_gsId")

        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 [tblGesamtsicherheitsPositionen]  (" & 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()
        SQL.doSQL("delete from [tblGesamtsicherheitsPositionen] where gsp_gsId = '" & gsp_gsId & "' AND gsp_gspPosId = '" & gsp_gspPosId & "' ", "AVISO")
    End Function


End Class
