Imports System.Data.SqlClient
Imports System.Reflection

Public Class cEZB_Waehrungskurse

    '  Property ezb_id As Integer
    Property ezb_waehrungscode As String = ""
    Property ezb_waehrungsschluessel As Object = Nothing
    Property ezb_datum As Date
    Property ezb_kurs As Double



    Dim SQL As New SQL

    Sub New()

    End Sub

    '  Sub New(ezb_id As Integer)
    ' Me.ezb_id = ezb_id
    '    LOAD_ById(ezb_id)
    ' End Sub

    Sub New(ezb_waehrungscode As String)
        Me.ezb_waehrungscode = ezb_waehrungscode
        LOAD_ByWaehrungscode(ezb_waehrungscode)

    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("ezb_id", ezb_id, , True, True))


        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ezb_waehrungscode", ezb_waehrungscode))
        If Me.ezb_waehrungsschluessel Is Nothing Then
            Me.ezb_waehrungsschluessel = SQL.getValueTxtBySql(" Select TOP 1 isnull([Währungsschlüssel] ,'')  FROM [Währungstabelle] where [Währungscode]='" & Me.ezb_waehrungscode.Replace("TRY", "TRL") & "' ", "FMZOLL")
        End If

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ezb_waehrungsschluessel", ezb_waehrungsschluessel))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ezb_datum", ezb_datum))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ezb_kurs", ezb_kurs))

        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 tblEZB_Waehrungskurse WITH(updlock,serializable) WHERE ezb_waehrungscode=@ezb_waehrungscode AND ezb_datum=@ezb_datum) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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



    Public Sub LOAD_ByWaehrungscode(ezb_waehrungscode As String)
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblEZB_Waehrungskurse WHERE ezb_waehrungscode=@ezb_waehrungscode ORDER BY ezb_datum DeSC ", conn)
                    cmd.Parameters.AddWithValue("@ezb_waehrungscode", ezb_waehrungscode)
                    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 [tblEZB_Waehrungskurse] SET " & str & " WHERE ezb_waehrungscode=@ezb_waehrungscode AND ezb_datum=@ezb_datum ")

        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 tblEZB_Waehrungskurse  (" & 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 [tblEZB_Waehrungskurse] WITH(updlock,serializable) WHERE  ezb_waehrungscode='" & ezb_waehrungscode & "' AND ezb_datum='" & ezb_datum & "' "
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function


    Shared Function UPDATE_Waehrungstabelle(datum As Date) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " UPDATE [Währungstabelle] SET [Währungstabelle].Eurokurs=tblEZB_Waehrungskurse.ezb_kurs
                        FROM [Währungstabelle] inner join tblEZB_Waehrungskurse on [Währungstabelle].Währungsschlüssel=tblEZB_Waehrungskurse.ezb_waehrungsschluessel where  tblEZB_Waehrungskurse.ezb_datum='" & datum.ToShortDateString & "' "
        Return (New SQL).doSQL(sqlstr, "FMZOLL")
    End Function

    Shared Function GET_KURS(ezb_waehrungscode As String, datum As Date) As Double
        Dim sqlstr = " SELECT TOP 1 [ezb_kurs]  FROM [tblEZB_Waehrungskurse]  where ezb_waehrungscode='" & ezb_waehrungscode & "' and ezb_datum<='" & datum.ToShortTimeString & "' order by ezb_datum desc"
        Return (New SQL).getValueTxtBySql(sqlstr, "FMZOLL",,, 0)
    End Function

    Shared Function EXCHANGE(betrag As Double, waehrungscode As String, datum As Date) As Double
        Dim sqlstr = " SELECT TOP 1 [ezb_kurs]  FROM [tblEZB_Waehrungskurse]  where ezb_waehrungscode='" & waehrungscode & "' and ezb_datum<='" & datum.ToShortTimeString & "' order by ezb_datum desc"
        Return (New SQL).getValueTxtBySql(sqlstr, "FMZOLL",,, 0) * betrag
    End Function

End Class
