Imports System.Data.SqlClient
Imports System.Reflection


Public Class cQSTeams
    Property LIST As New List(Of cQSTeam)

    Dim SQL As New SQL

    Sub New()
        LOAD_LIST()
    End Sub

    Public Sub LOAD_LIST()
        Try
            LIST.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT qstm_Id FROM [tblQS_Teams] ", conn)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cQSTeam(dr.Item("qstm_Id"))
                        LIST.Add(l)
                    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 getTeamById(id)
        For Each T In LIST
            If T.qstm_Id = id Then
                Return T
            End If
        Next
        Return Nothing
    End Function

    Public Shared Function genNewTeam(teamName) As Boolean
        If teamName = "" Then Return False
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("INSERT INTO [tblQS_Teams] ([qstm_bezeichnung]) VALUES('" & teamName & "') ", conn)
                    cmd.ExecuteNonQuery()
                    Return True
                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
        Return False
    End Function


End Class



Public Class cQSTeam

    Property qstm_Id As Integer = -1
    Property qstm_bezeichnung As String

    Property MEMBER As New List(Of cQSTeamMA)


    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("qstm_Id", qstm_Id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("qstm_bezeichnung", qstm_bezeichnung))

        Return list
    End Function

    Sub New(qstm_Id)
        Me.qstm_Id = qstm_Id
        LOAD(qstm_Id)
    End Sub

    Sub New()
    End Sub



    Public Sub LOAD(qstm_Id)
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM [tblQS_Teams] WHERE qstm_Id=@qstm_Id ", conn)
                    cmd.Parameters.AddWithValue("@qstm_Id", qstm_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_POSITIONEN()
                    End If
                    dr.Close()
                End Using
            End Using
            'Return Nothing
        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 Nothing
    End Sub




    Public Sub LOAD_POSITIONEN()
        Try
            MEMBER.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM [tblQS_TeamsMA] WHERE qtma_QstmId=@qtma_QstmId ", conn)
                    cmd.Parameters.AddWithValue("@qtma_QstmId", qstm_Id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cQSTeamMA
                        For Each i In l.getParameterList()
                            Dim propInfo As PropertyInfo = l.GetType.GetProperty(i.Scalarvariable)
                            If dr.Item(i.Text) Is DBNull.Value Then
                                propInfo.SetValue(l, Nothing)
                            Else
                                propInfo.SetValue(l, dr.Item(i.Text))
                            End If
                        Next
                        MEMBER.Add(l)
                    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


    

End Class


Public Class cQSTeamMA

    Property qtma_Id As Integer = -1
    Property qtma_QstmId As Integer
    Property qtma_MaId As Integer
    Property qtma_name As String = ""
    Property qtma_funktion As Object = Nothing

    Dim SQL As New SQL
    Sub New()
    End Sub

    Sub New(qtma_Id)
        Me.qtma_Id = qtma_Id
        LOAD(qtma_Id)
    End Sub

   


    Public Sub LOAD(qtma_Id)
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM [tblQS_TeamsMA] WHERE qtma_Id=@qtma_Id ", conn)
                    cmd.Parameters.AddWithValue("@qtma_Id", qtma_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
            'Return Nothing
        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 Nothing
    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("qtma_Id", qtma_Id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("qtma_QstmId", qtma_QstmId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("qtma_MaId", qtma_MaId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("qtma_name", qtma_name))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("qtma_funktion", qtma_funktion))

        Return list
    End Function

    Public Function INSERT() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        Return SQL.doSQLVarList(getInsertCmd, "FMZOLL", , list)
    End Function


    Public Function DELETE() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblQS_TeamsMA] WITH(updlock,serializable) WHERE qtma_Id=" & Me.qtma_Id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    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 tblQS_TeamsMA  (" & 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
