Imports System.Data.SqlClient
Imports System.Reflection

Public Class cMessenger

    Public Shared NEW_MESSGE As Boolean = False
    Public Shared UNREAD_MESAGES As Integer = 0

    Shared SQL As New SQL

    Public Shared Function GEN_NEW_CHAT(chat_name As String, MaIds As List(Of Integer), Optional ByRef CHAT As VERAG_PROG_ALLGEMEIN.cMessenger.cMessenger_Chat = Nothing) As Boolean
        CHAT = New VERAG_PROG_ALLGEMEIN.cMessenger.cMessenger_Chat
        CHAT.chat_erstelltAm = Now
        CHAT.chat_erstelltMaId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
        CHAT.chat_name = chat_name

        For Each maId In MaIds
            Dim MB As New VERAG_PROG_ALLGEMEIN.cMessenger.cMessenger_ChatMembers(CHAT.chat_id, maId)
            MB.chatMb_maId = maId
            CHAT.CHAT_MEMBERS.Add(MB)
        Next
        Return CHAT.SAVE(True)

    End Function

    Public Shared Function CHECK_EXISTS(MaId As Integer, Optional ByRef toChatID As Integer = -1) As Boolean
        Dim sqlStr = "Select TOP 1 chat_id FROM [tblMessenger_Chat]  inner join tblMessenger_ChatMembers On chatMb_chatId=chat_id  where chat_name='' AND [chat_aktiv]=1
                        And (SELECT COUNT(*) FROM tblMessenger_ChatMembers WHERE chatMb_chatId=chat_id And chatMb_maId='" & VERAG_PROG_ALLGEMEIN.cAllgemein.USRID & "' )  >0
                        And (SELECT COUNT(*) FROM tblMessenger_ChatMembers WHERE chatMb_chatId=chat_id And chatMb_maId='" & MaId & "' )  >0
                        order by chat_id DESC"

        toChatID = SQL.getValueTxtBySql(sqlStr, "ADMIN",,, -1)
        Return (toChatID > 0)
    End Function


    Public Shared Function GET_NewMSG_COUNT(Optional maId = -1) As Integer
        If maId < 0 Then maId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
        Return SQL.getValueTxtBySql("SELECT isnull(COUNT(*),0) FROM tblMessenger_ChatMessages INNER JOIN tblMessenger_ChatMessageStatus ON chatMgSt_chatMsgId=chatMg_id WHERE chatMgSt_MaId=" & VERAG_PROG_ALLGEMEIN.cAllgemein.USRID & " AND chatMgSt_gelesen=0", "ADMIN",,, 0)
    End Function

    Public Shared Function GET_CHAT_LIST() As List(Of cMessenger_Chat)
        Dim LIST As New List(Of cMessenger_Chat)
        Dim dt = SQL.loadDgvBySql("SELECT [chat_id] FROM [tblMessenger_Chat] WHERE (SELECT COUNT(*) FROM tblMessenger_ChatMembers WHERE chatMB_chatId=chat_id AND chatMB_maId=" & VERAG_PROG_ALLGEMEIN.cAllgemein.USRID & ")>0", "ADMIN")

        For Each r In dt.Rows
            Dim CHAT As New VERAG_PROG_ALLGEMEIN.cMessenger.cMessenger_Chat(r("chat_id"))
            LIST.Add(CHAT)
        Next
        Return LIST

    End Function

    Public Class cMessenger_Chat

        Property chat_id As Integer = -1
        Property chat_name As String
        Property chat_erstelltAm As Date = Now
        Property chat_erstelltMaId As Integer
        Property chat_aktiv As Boolean = True

        Public CHAT_MEMBERS As New List(Of cMessenger_ChatMembers)
        Public CHAT_MESSAGES As New List(Of cMessenger_ChatMessages)
        Dim SQL As New SQL

        Sub New()
            Me.chat_erstelltMaId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
            Me.chat_erstelltAm = Now
            LOAD()
        End Sub


        Sub New(chat_id As Integer, Optional topMax As String = "", Optional dateTmp As Object = Nothing)
            Me.chat_id = chat_id
            Me.chat_erstelltMaId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
            Me.chat_erstelltAm = Now
            LOAD(topMax, dateTmp)
        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("chat_id", chat_id,, True))

            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chat_name", chat_name))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chat_erstelltAm", chat_erstelltAm))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chat_erstelltMaId", chat_erstelltMaId))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chat_aktiv", chat_aktiv))

            Return list
        End Function



        Public Function SAVE(saveAll As Boolean) As Boolean
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
            Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblMessenger_Chat WHERE chat_id=@chat_id) " &
                       " BEGIN " & getUpdateCmd() & " End " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " End " &
                       " commit tran "

            chat_id = SQL.doSQLVarListID(chat_id, sqlstr, "ADMIN", , list)

            If saveAll Then
                SAVE_MEMBERS()
            End If

            Return chat_id > 0
        End Function

        Public Sub LOAD(Optional topMax As String = "", Optional dateTmp As Object = Nothing)
            Try
                Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                    Using cmd As New SqlCommand("Select * FROM tblMessenger_Chat WHERE chat_id=@chat_id  ", conn)
                        cmd.Parameters.AddWithValue("@chat_id", chat_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
                            Me.LOAD_MEMBERS()
                            If dateTmp Is Nothing Then
                                Me.LOAD_MESSAGES(topMax)
                            Else
                                Me.LOAD_MESSAGES_DATE(dateTmp)
                            End If

                        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_MEMBERS() As Boolean
            Try
                DELETE_MEMBERS()
                For Each MB In Me.CHAT_MEMBERS
                    MB.chatMb_chatId = Me.chat_id
                    MB.SAVE()
                Next
                Return True
            Catch ex As Exception
                MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
                Return False
            End Try
        End Function

        Public Function READ_ALL_MESSAGES() As Boolean
            Dim sqlstr = "UPDATE tblMessenger_ChatMessageStatus SET chatMgSt_gelesen=1  FROM [tblMessenger_Chat] INNER JOIN tblMessenger_ChatMessages ON [chat_id]=chatMg_chatId  INNER JOIN tblMessenger_ChatMessageStatus ON chatMgSt_chatMsgId=chatMg_id WHERE chat_id=" & Me.chat_id & " AND chatMgSt_MaId=" & VERAG_PROG_ALLGEMEIN.cAllgemein.USRID & " AND chatMgSt_gelesen=0 "
            Return SQL.doSQL(sqlstr, "ADMIN")
        End Function

        Public Function DELETE_MEMBERS() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
            Dim sqlstr = " DELETE FROM [tblMessenger_ChatMembers] WHERE chatMb_chatId=" & Me.chat_id
            Return SQL.doSQL(sqlstr, "ADMIN")
        End Function
        Public Sub LOAD_MEMBERS()
            Try
                Me.CHAT_MEMBERS.Clear()
                Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                    Using cmd As New SqlCommand("SELECT * FROM tblMessenger_ChatMembers WHERE chatMb_chatId=@chat_id  ", conn)
                        cmd.Parameters.AddWithValue("@chat_id", chat_id)
                        Dim dr = cmd.ExecuteReader()
                        While dr.Read
                            Dim MB As New cMessenger_ChatMembers(Me.chat_id, dr.Item("chatMb_maId"))
                            Me.CHAT_MEMBERS.Add(MB)
                        End While
                        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 Sub LOAD_MESSAGES(Optional top As Integer = 100)
            Try
                Me.CHAT_MESSAGES.Clear()
                Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                    Using cmd As New SqlCommand("SELECT * FROM(SELECT TOP " & top & " * FROM [tblMessenger_ChatMessages] WHERE chatMg_chatId=@chat_id  order by chatMg_datetime desc) as T order by chatMg_datetime asc", conn)
                        cmd.Parameters.AddWithValue("@chat_id", chat_id)
                        Dim dr = cmd.ExecuteReader()
                        While dr.Read
                            Dim MSG As New cMessenger_ChatMessages(dr.Item("chatMg_id")) 'Me.chat_id, dr.Item("chatMb_maId")) 'yxc
                            Me.CHAT_MESSAGES.Add(MSG)
                        End While
                        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 Sub LOAD_MESSAGES_DATE(dateTmp As Date)
            Try
                Me.CHAT_MESSAGES.Clear()
                Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                    Using cmd As New SqlCommand("SELECT * FROM [tblMessenger_ChatMessages] WHERE chatMg_chatId=@chat_id AND CAST(chatMg_datetime as date)>=@dateTmp order by chatMg_datetime asc", conn)
                        cmd.Parameters.AddWithValue("@chat_id", chat_id)
                        cmd.Parameters.AddWithValue("@dateTmp", dateTmp)
                        Dim dr = cmd.ExecuteReader()
                        While dr.Read
                            Dim MSG As New cMessenger_ChatMessages(dr.Item("chatMg_id")) 'Me.chat_id, dr.Item("chatMb_maId")) 'yxc
                            Me.CHAT_MESSAGES.Add(MSG)
                        End While
                        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 [tblMessenger_Chat] SET " & str & " WHERE chat_id=@chat_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 tblMessenger_Chat  (" & 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() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
            Dim sqlstr = " DELETE FROM [tblMessenger_Chat] WHERE chat_id=" & Me.chat_id
            Return SQL.doSQL(sqlstr, "ADMIN")
        End Function


    End Class

    Public Class cMessenger_ChatMembers

        Property chatMb_chatId As Integer = -1
        Property chatMb_maId As Integer = -1
        Property chatMb_beitritt As Date = Now

        Dim SQL As New SQL



        Sub New(chatMb_chatId, chatMb_maId)
            Me.chatMb_chatId = chatMb_chatId
            Me.chatMb_maId = chatMb_maId
            Me.chatMb_beitritt = Now
        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("chatMb_chatId", chatMb_chatId))

            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMb_maId", chatMb_maId))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMb_beitritt", chatMb_beitritt))

            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 [tblMessenger_ChatMembers] WHERE chatMb_chatId=@chatMb_chatId AND chatMb_maId=@chatMb_maId) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

        Public Sub LOAD()
            Try
                Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                    Using cmd As New SqlCommand("SELECT * FROM [tblMessenger_ChatMembers] WHERE chatMb_chatId=@chatMb_chatId AND chatMb_maId=@chatMb_maId ", conn)
                        cmd.Parameters.AddWithValue("@chatMb_chatId", chatMb_chatId)
                        cmd.Parameters.AddWithValue("@chatMb_maId", chatMb_maId)
                        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 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 [tblMessenger_ChatMembers] SET " & str & " WHERE chatMb_chatId=@chatMb_chatId AND chatMb_maId=@chatMb_maId  ")

            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 [tblMessenger_ChatMembers]  (" & 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([chatMb_maId]) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
            Dim sqlstr = " DELETE FROM [tblMessenger_ChatMembers] WHERE [chatMb_chatId]=" & Me.[chatMb_chatId] & " AND [chatMb_maId] ='" & [chatMb_maId] & "'"
            Return SQL.doSQL(sqlstr, "ADMIN")
        End Function


    End Class

    Public Class cMessenger_ChatStatus

        Property chatMgSt_chatMsgId As Integer
        Property chatMgSt_MaId As Integer
        Property chatMgSt_gelesen As Boolean = False
        Property chatMgSt_datetime As Date = Now

        Dim SQL As New SQL


        Sub New(chatMgSt_chatMsgId, chatMgSt_MaId, chatMgSt_gelesen)
            Me.chatMgSt_chatMsgId = chatMgSt_chatMsgId
            Me.chatMgSt_MaId = chatMgSt_MaId
            Me.chatMgSt_gelesen = chatMgSt_gelesen
            Me.chatMgSt_datetime = Now
        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("chatMgSt_chatMsgId", chatMgSt_chatMsgId))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMgSt_MaId", chatMgSt_MaId))

            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMgSt_gelesen", chatMgSt_gelesen))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMgSt_datetime", chatMgSt_datetime))

            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 tblMessenger_ChatMessages WHERE chatMgSt_chatMsgId=@chatMgSt_chatMsgId AND chatMgSt_MaId=@chatMgSt_MaId) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

        Public Sub LOAD()
            Try
                Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                    Using cmd As New SqlCommand("SELECT * FROM tblMessenger_ChatMessages WHERE chatMgSt_chatMsgId=@chatMgSt_chatMsgId AND chatMgSt_MaId=@chatMgSt_MaId ", conn)
                        cmd.Parameters.AddWithValue("@chatMgSt_chatMsgId", chatMgSt_chatMsgId)
                        cmd.Parameters.AddWithValue("@chatMgSt_MaId", chatMgSt_MaId)
                        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 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 tblMessenger_ChatMessages SET " & str & " WHERE chatMgSt_chatMsgId=@chatMgSt_chatMsgId AND chatMgSt_MaId=@chatMgSt_MaId  ")

            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 tblMessenger_ChatMessages  (" & 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([chatMgSt_MaId]) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
            Dim sqlstr = " DELETE FROM tblMessenger_ChatMessages WHERE [chatMgSt_chatMsgId]=" & Me.chatMgSt_chatMsgId & " AND [chatMgSt_MaId] ='" & chatMgSt_MaId & "'"
            Return SQL.doSQL(sqlstr, "ADMIN")
        End Function


    End Class


    Public Class cMessenger_ChatMessages

        Property chatMg_id As Integer ' INT Not NULL PRIMARY KEY IDENTITY, 

        Property chatMg_chatId As Integer ' INT Not NULL,
        Property chatMg_maId As Integer ' INT Not NULL,
        Property chatMg_datetime As Date = Now
        Property chatMg_maName As String = VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.Fullname
        Property chatMg_maFirma As String = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
        Property chatMg_type As String = "TEXT"
        Property chatMg_text As String = ""
        Property chatMg_anhang_docId As Integer ' int NULL,
        Property chatMg_referenz_AvisoId As Integer ' int NULL,
        Property chatMg_referenz_SendungsId As Integer ' int NULL,
        Property chatMg_referenz_KundenNr As Integer ' int NULL

        Public CHAT_MESSAGES_STATUS_LIST As New List(Of cMessenger_ChatMessageStatus)

        Dim SQL As New SQL


        Sub New(chatMg_id)
            Me.chatMg_id = chatMg_id
            LOAD()
        End Sub
        Sub New(chatMg_chatId, chatMg_maId)
            Me.chatMg_chatId = chatMg_chatId
            Me.chatMg_maId = chatMg_maId
        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("chatMg_id", chatMg_id,, True))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMg_maId", chatMg_maId))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMg_chatId", chatMg_chatId))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMg_datetime", chatMg_datetime))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMg_maName", chatMg_maName))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMg_maFirma", chatMg_maFirma))

            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMg_type", chatMg_type))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMg_text", chatMg_text))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMg_anhang_docId", chatMg_anhang_docId))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMg_referenz_AvisoId", chatMg_referenz_AvisoId))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMg_referenz_SendungsId", chatMg_referenz_SendungsId))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMg_referenz_KundenNr", chatMg_referenz_KundenNr))

            Return list
        End Function



        Public Function SAVE(saveStat As Boolean, Optional CHAT_MEMBERS As List(Of cMessenger_ChatMembers) = Nothing) As Boolean
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
            Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblMessenger_ChatMessages WHERE chatMg_id=@chatMg_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "
            Dim tmp_id = SQL.doSQLVarListID(Me.chatMg_id, sqlstr, "ADMIN", , list)
            If tmp_id > 0 Then
                Me.chatMg_id = tmp_id
                If saveStat Then SAVE_STATUS_FIRST(CHAT_MEMBERS)
                UPDATE_LASTMSG() ' Versucht einen TSTMP der letzten Nachricht einzutragen
                Return True
            Else
                Return False
            End If


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


        Public Function UPDATE_LASTMSG() As Boolean
            Dim sqlstr = " UPDATE tblMessenger_Chat SET chat_lastMsg=getDate() WHERE [chat_id]=" & Me.chatMg_chatId & "  "
            Return SQL.doSQL(sqlstr, "ADMIN")
        End Function


        Public Sub LOAD()
            Try
                Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                    Using cmd As New SqlCommand("SELECT * FROM tblMessenger_ChatMessages WHERE chatMg_id=@chatMg_id ", conn)
                        cmd.Parameters.AddWithValue("@chatMg_id", chatMg_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
                            LOAD_STATUS()
                        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 tblMessenger_ChatMessages SET " & str & " WHERE chatMg_id=@chatMg_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 tblMessenger_ChatMessages  (" & 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_MESSAGE() As Boolean
            chatMg_type = "DEL_" & chatMg_type
            Me.DELETE_STATUS() 'Alle Statuseinträge löschen
            Return SAVE(False)
        End Function

        Public Function DELETE([chatMgSt_MaId]) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
            Dim sqlstr = " DELETE FROM tblMessenger_ChatMessages WHERE [chatMg_id]=" & Me.chatMg_id & "  "
            Return SQL.doSQL(sqlstr, "ADMIN")
        End Function

        Public Sub SAVE_STATUS_FIRST(CHAT_MEMBERS As List(Of cMessenger_ChatMembers))
            Try
                If CHAT_MEMBERS IsNot Nothing Then
                    For Each MB In CHAT_MEMBERS
                        Dim gelesen = False
                        If MB.chatMb_maId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID Then
                            gelesen = True
                        End If
                        Dim STAT As New cMessenger_ChatMessageStatus(Me.chatMg_id, MB.chatMb_maId, gelesen)

                        STAT.SAVE()
                    Next
                End If
            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 DELETE_STATUS() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        '    Dim sqlstr = " DELETE FROM [tblMessenger_ChatMessageStatus] WHERE chatMb_chatId=" & Me.chatMg_chatId
        '    Return SQL.doSQL(sqlstr, "ADMIN")
        'End Function

        Public Sub LOAD_STATUS()
            Try
                Me.CHAT_MESSAGES_STATUS_LIST.Clear()
                Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                    Using cmd As New SqlCommand("SELECT * FROM tblMessenger_ChatMessageStatus WHERE chatMgSt_chatMsgId=@chatMg_id  ", conn)
                        cmd.Parameters.AddWithValue("@chatMg_id", Me.chatMg_id)
                        Dim dr = cmd.ExecuteReader()
                        While dr.Read
                            Dim MB As New cMessenger_ChatMessageStatus(Me.chatMg_id, dr.Item("chatMgSt_MaId"))
                            Me.CHAT_MESSAGES_STATUS_LIST.Add(MB)
                        End While
                        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 Sub DELETE_STATUS()
            Try
                If SQL.doSQL("DELETE FROM tblMessenger_ChatMessageStatus WHERE chatMgSt_chatMsgId='" & Me.chatMg_id & "'", "ADMIN") Then
                    Me.CHAT_MESSAGES_STATUS_LIST.Clear()
                End If
            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
    End Class




    Public Class cMessenger_ChatMessageStatus

        Property chatMgSt_chatMsgId As Integer
        Property chatMgSt_MaId As Integer
        Property chatMgSt_datetime As Date = Now
        Property chatMgSt_gelesen As Boolean = False

        Dim SQL As New SQL


        Sub New(chatMgSt_chatMsgId, chatMgSt_MaId)
            Me.chatMgSt_chatMsgId = chatMgSt_chatMsgId
            Me.chatMgSt_MaId = chatMgSt_MaId
            LOAD()
        End Sub
        Sub New(chatMgSt_chatMsgId, chatMgSt_MaId, chatMgSt_gelesen)
            Me.chatMgSt_chatMsgId = chatMgSt_chatMsgId
            Me.chatMgSt_MaId = chatMgSt_MaId
            Me.chatMgSt_gelesen = chatMgSt_gelesen
        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("chatMgSt_chatMsgId", chatMgSt_chatMsgId))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMgSt_MaId", chatMgSt_MaId))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMgSt_datetime", chatMgSt_datetime))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("chatMgSt_gelesen", chatMgSt_gelesen))

            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 tblMessenger_ChatMessageStatus WHERE chatMgSt_chatMsgId=@chatMgSt_chatMsgId AND chatMgSt_MaId=@chatMgSt_MaId) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

        Public Sub LOAD()
            Try
                Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                    Using cmd As New SqlCommand("SELECT * FROM tblMessenger_ChatMessageStatus WHERE chatMgSt_chatMsgId=@chatMgSt_chatMsgId AND chatMgSt_MaId=@chatMgSt_MaId ", conn)
                        cmd.Parameters.AddWithValue("@chatMgSt_chatMsgId", chatMgSt_chatMsgId)
                        cmd.Parameters.AddWithValue("@chatMgSt_MaId", chatMgSt_MaId)
                        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 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 tblMessenger_ChatMessageStatus SET " & str & " WHERE chatMgSt_chatMsgId=@chatMgSt_chatMsgId AND chatMgSt_MaId=@chatMgSt_MaId ")

            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 tblMessenger_ChatMessageStatus  (" & 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([chatMgSt_MaId]) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
            Dim sqlstr = " DELETE FROM tblMessenger_ChatMessageStatus WHERE [chatMgSt_chatMsgId]=" & Me.chatMgSt_chatMsgId & " AND  [chatMgSt_MaId]=" & Me.chatMgSt_MaId & "  "
            Return SQL.doSQL(sqlstr, "ADMIN")
        End Function

    End Class

End Class
