Imports System.Data.SqlClient
Imports System.Reflection

Public Class cSession

    Property sess_Id As Integer
    Property sess_system As String = ""
    Property sess_progId As Object = Nothing
    Property sess_progName As String = ""
    Property sess_progVersion As String = ""

    Property sess_maName As String = ""
    Property sess_maId As Integer

    Property sess_firma As String = ""
    Property sess_niederlassung As String = ""
    Property sess_ipAddress As String = ""
    Property sess_PCName As String = ""
    Property sess_PCUserName As String = ""


    Property sess_login As Object = Nothing = Now
    Property sess_logout As Object = Nothing
    Property sess_lastRm As Object = Nothing

    Dim SQL As New SQL

    Sub New()

    End Sub
    Sub New(sess_Id)
        Me.sess_Id = sess_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("sess_Id", sess_Id, , True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_system", sess_system))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_progId", sess_progId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_progName", sess_progName))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_progVersion", sess_progVersion))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_firma", sess_firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_niederlassung", sess_niederlassung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_ipAddress", sess_ipAddress))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_PCName", sess_PCName))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_PCUserName", sess_PCUserName))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_maName", sess_maName))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_maId", sess_maId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_login", sess_login))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_logout", sess_logout))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_lastRm", sess_lastRm))
        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 tblSessions WITH(updlock,serializable) WHERE sess_Id=@sess_Id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        Dim id = SQL.doSQLVarListID(sess_Id, sqlstr, "ADMIN", , list)
        Me.sess_Id = id
        Return id > 0
    End Function

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("SELECT * FROM tblSessions WHERE sess_Id=@sess_Id  ", conn)
                    cmd.Parameters.AddWithValue("@sess_Id", sess_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 UPDATE_TIME() As String
        If sess_Id > 0 Then
            Try
                Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
                list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_Id", sess_Id, , True))
                sess_lastRm = Now
                'list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_lastRm", sess_lastRm))

                Return SQL.doSQLVarList(" UPDATE [tblSessions] SET sess_lastRm=GETDATE() WHERE sess_Id=@sess_Id ", "ADMIN", , list)

            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 If
        Return ""
    End Function



    Public Shared Function GET_IP_FROM_MAID(sess_maId) As String
        Dim SQL As New SQL
        Return SQL.getValueTxtBySql(" SELECT TOP 1 isnull(sess_ipaddress,'') as sess_ipaddress FROM [tblSessions] where sess_lastRm > DATEADD(SECOND,-70, GETDATE()) AND sess_maId=" & sess_maId, "ADMIN")
    End Function

    Public Function LOGIN() As String
        Me.sess_system = (If(VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM, "T", "E"))
        Me.sess_login = Now
        Me.sess_maId = VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_id
        Me.sess_maName = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
        Me.sess_firma = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
        Me.sess_niederlassung = VERAG_PROG_ALLGEMEIN.cAllgemein.NIEDERLASSUNG
        Try
            Me.sess_ipAddress = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList(0).ToString()
            Me.sess_PCName = System.Environment.MachineName
            Me.sess_PCUserName = My.User.Name.ToString
        Catch ex As Exception
        End Try
        Me.sess_lastRm = Now
        Me.sess_progId = Nothing
        Me.sess_progName = VERAG_PROG_ALLGEMEIN.cAllgemein.PROGNAME
        Me.sess_progVersion = VERAG_PROG_ALLGEMEIN.cAllgemein.PROGVERSION

        Return SAVE()
    End Function

    Public Function LOGOUT() As String
        Try
            Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)

            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_Id", sess_Id, , True))
            sess_lastRm = Now
            '     list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_lastRm", sess_lastRm))
            sess_logout = Now
            '   list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sess_logout", sess_logout))

            Return SQL.doSQLVarList(" UPDATE [tblSessions] SET sess_lastRm=GETDATE(),sess_logout=GETDATE() WHERE sess_Id=@sess_Id ", "ADMIN", , list)

        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 [tblSessions] SET " & str & " WHERE sess_Id=@sess_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 tblSessions  (" & 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