Imports System.Data.SqlClient
Imports System.Reflection

Public Class cDienstSettings
    Property dpset_id As Integer
    Property dpset_niederlassung As String
    Property dpset_Filiale As String
    Property dpset_AbfArt As String
    Property dpset_AbfNL As String
    Property dpset_WoAuslVon As Integer
    Property dpset_WoAuslBis As Integer
    Property dpset_dgvRowHeight As Integer
    Property dpset_showTime As Boolean
    Property dpset_startJahr As Integer
    Property dpset_startKW As Integer
    Property dpset_AbfAuswertung As Boolean
    Property dpset_StudenAuswertung As Boolean
    Property dpset_AvisoAuswertung As Boolean
    Property dpset_land As String



    Dim SQL As New cDienstplan

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


    Function getParameterList() As List(Of SQLVariable)
        Dim list As New List(Of SQLVariable)
        list.Add(New SQLVariable("dpset_id", dpset_id, , True))
        list.Add(New SQLVariable("dpset_niederlassung", dpset_niederlassung))
        list.Add(New SQLVariable("dpset_Filiale", dpset_Filiale))
        list.Add(New SQLVariable("dpset_AbfArt", dpset_AbfArt))
        list.Add(New SQLVariable("dpset_AbfNL", dpset_AbfNL))
        list.Add(New SQLVariable("dpset_WoAuslVon", dpset_WoAuslVon))
        list.Add(New SQLVariable("dpset_WoAuslBis", dpset_WoAuslBis))
        list.Add(New SQLVariable("dpset_dgvRowHeight", dpset_dgvRowHeight))
        list.Add(New SQLVariable("dpset_showTime", dpset_showTime))
        list.Add(New SQLVariable("dpset_startJahr", dpset_startJahr))
        list.Add(New SQLVariable("dpset_startKW", dpset_startKW))
        list.Add(New SQLVariable("dpset_AbfAuswertung", dpset_AbfAuswertung))
        list.Add(New SQLVariable("dpset_StudenAuswertung", dpset_StudenAuswertung))
        list.Add(New SQLVariable("dpset_AvisoAuswertung", dpset_AvisoAuswertung))
        list.Add(New SQLVariable("dpset_land", dpset_land))
        Return list
    End Function



    Public Function SAVE() As Boolean
        Dim list As List(Of SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblDienstSettings WITH(updlock,serializable) WHERE dpset_niederlassung=@dpset_niederlassung) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT * FROM tblDienstSettings WHERE dpset_niederlassung=@dpset_niederlassung  ", conn)
                    cmd.Parameters.AddWithValue("@dpset_niederlassung", dpset_niederlassung)
                    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



    Shared Function getLand(dpset_niederlassung) As String
        getLand = "AT" 'Defaut
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT dpset_land FROM tblDienstSettings WHERE dpset_niederlassung=@dpset_niederlassung  ", conn)
                    cmd.Parameters.AddWithValue("@dpset_niederlassung", dpset_niederlassung)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        getLand = dr.Item(0)
                    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 Function



    Public Function getUpdateCmd() As String
        Try
            Dim list As List(Of 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 [tblDienstSettings] SET " & str & " WHERE dpset_niederlassung=@dpset_niederlassung ")

        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 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 tblDienstSettings  (" & 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
