Imports System.Data.SqlClient
Imports System.Reflection

Public Class cDakosy_Stammreferenzen

    Property dySr_Id As Integer
    Property dySr_Land As Object = Nothing
    Property dySr_Verfahren As Object = Nothing
    Property dySr_Stammreferenz As Object = Nothing
    Property dySr_Bezeichnung As Object = Nothing
    Property dySr_dyId As Object = Nothing
    Property dySr_dy_Mandant As Object = Nothing
    Property dySr_dy_Niederlassung As Object = Nothing
    Property dySr_dy_Standard As Object = Nothing
    Property dyStr_KdNr As Object = Nothing
    Property dySr_WarenortID As Object = Nothing


    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Sub New()

    End Sub

    Sub New(dySr_Id As Integer)
        Me.dySr_Id = dySr_Id
        LOAD()
    End Sub

    Sub New(dySr_Bez As String)
        Me.dySr_Bezeichnung = dySr_Bez
        LOADBEZ()
    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("dySr_Id", dySr_Id, , True, True))


        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySr_Land", dySr_Land))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySr_Verfahren", dySr_Verfahren))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySr_Stammreferenz", dySr_Stammreferenz))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySr_Bezeichnung", dySr_Bezeichnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySr_dyId", dySr_dyId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySr_dy_Mandant", dySr_dy_Mandant))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySr_dy_Niederlassung", dySr_dy_Niederlassung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySr_dy_Standard", dySr_dy_Standard))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dyStr_KdNr", dyStr_KdNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySr_WarenortID", dySr_WarenortID))
        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 tblDakosy_Stammreferenzen WITH(updlock,serializable) WHERE dySr_Id=@dySr_Id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        dySr_Id = SQL.doSQLVarListID(dySr_Id, sqlstr, "FMZOLL", , list)
        Return dySr_Id > 0
    End Function


    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblDakosy_Stammreferenzen WHERE dySr_Id=@dySr_Id  ", conn)
                    cmd.Parameters.AddWithValue("@dySr_Id", dySr_Id)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each l In getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(l.Scalarvariable)

                            If dr.Item(l.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(l.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 Sub LOADBEZ()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblDakosy_Stammreferenzen WHERE dySr_Bezeichnung=@dySr_Bezeichnung  ", conn)
                    cmd.Parameters.AddWithValue("@dySr_Bezeichnung", dySr_Bezeichnung)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each l In getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(l.Scalarvariable)

                            If dr.Item(l.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(l.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 [tblDakosy_Stammreferenzen] SET " & str & " WHERE dySr_Id=@dySr_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 tblDakosy_Stammreferenzen  (" & 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 [tblDakosy_Stammreferenzen] WITH(updlock,serializable) WHERE dySr_Id=" & Me.dySr_Id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function



End Class
