
Imports System.Data.SqlClient
Imports System.Reflection
Imports Org.BouncyCastle.Utilities

Public Class cParkzeiten
    Property ParkzeitID As Integer
    Property Laufende_Nr As Object = Nothing
    Property Von As DateTime
    Property Bis As DateTime
    Property Dauer As String
    Property Dauer_Minuten As Integer
    Property Kennzeichen As String
    Property TarifArt As String
    Property KartenNr As String
    Property Bereich As String
    Property KundenNr As Object = Nothing


    Public hasEntry = False

    Dim SQL As New SQL

    Sub New()

    End Sub
    Sub New(ParkzeitID)
        Me.ParkzeitID = ParkzeitID
        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("ParkzeitID", ParkzeitID,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Laufende_Nr", Laufende_Nr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Von", Von))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bis", Bis))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Dauer", Dauer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Dauer_Minuten", Dauer_Minuten))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Kennzeichen", Kennzeichen))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("TarifArt", TarifArt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KartenNr", KartenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bereich", Bereich))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("KundenNr", KundenNr))

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

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

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

        Dim sqlstr = " BEGIN TRAN IF NOT EXISTS(SELECT * FROM Parkzeiten  WHERE cast(Kennzeichen as varchar)=@Kennzeichen and Von=@Von and Bis=@Bis) " &
                         " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

    Public Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionPARKZEIT()
                Using cmd As New SqlCommand("SELECT * FROM Parkzeiten WHERE ParkzeitID=@ParkzeitID  ", conn)
                    cmd.Parameters.AddWithValue("@ParkzeitID", ParkzeitID)
                    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
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        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 [Parkzeiten] SET " & str & " WHERE ParkzeitID=@ParkzeitID ")

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        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 Parkzeiten  (" & str & ") VALUES(" & values & ")  ")
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return ""
    End Function

End Class
