Imports System.Data.SqlClient
Imports System.Reflection

Public Class cDakosy_Statusmeldungen

    Property dySt_id As Integer
    Property dySt_dyId As Integer
    Property dySt_dyArt As String = ""
    Property dySt_BezugsNr As String = ""
    Property dySt_status As Integer = -1
    Property dySt_artMitteilung As String
    Property dySt_ergCode As String = ""
    Property dySt_ergMitteilung As String = ""
    Property dySt_ergZeitpunkt As Object = Nothing
    Property dySt_DakosyTransaktionsDatum As Object = Nothing
    Property dySt_DakosyTransaktionsID As Object = Nothing
    Property dySt_ts As Date = Now
    Property dySt_ergDetails As Object = Nothing
    Property dySt_ergZoellner As Object = Nothing
    Property dySt_pfad As Object = Nothing


    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Sub New()

    End Sub
    Sub New(dySt_id)
        Me.dySt_id = dySt_id
        LOAD()
    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("dySt_id", dySt_id, , True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_dyId", dySt_dyId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_dyArt", dySt_dyArt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_BezugsNr", dySt_BezugsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_status", dySt_status))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_artMitteilung", dySt_artMitteilung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_ergCode", dySt_ergCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_ergMitteilung", dySt_ergMitteilung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_ergZeitpunkt", dySt_ergZeitpunkt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_DakosyTransaktionsDatum", dySt_DakosyTransaktionsDatum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_DakosyTransaktionsID", dySt_DakosyTransaktionsID))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_ts", dySt_ts))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_ergDetails", dySt_ergDetails))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_ergZoellner", dySt_ergZoellner))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dySt_pfad", dySt_pfad))
        Return list
    End Function

    'Wenn true --> die Abgefragetransaktion ist die Neuste
    Shared Function isNewestTransaktion(BezugsNr As String, transID As String, ergDate As Date) As Boolean
        Try
            Dim STATUS As New cDakosy_Statusmeldungen
            Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
            'Primär wird nach erg_datum geprüft!
            'Dim cntNewerTrans As Integer = SQL.getValueTxtBySql("SELECT COUNT(*) FROM [tblDakosy_Statusmeldungen]  where dySt_BezugsNr='" & BezugsNr & "' and isnull(isnull([dySt_ergZeitpunkt],dySt_DakosyTransaktionsDatum),[dySt_ts])> '" & ergDate & "'  AND [dySt_status] > 5 ", "FMZOLL")
            'Dim cntNewerTrans As Integer = SQL.getValueTxtBySql("SELECT COUNT(*) FROM [tblDakosy_Statusmeldungen]  where dySt_BezugsNr='" & BezugsNr & "' and cast(isnull(isnull(dySt_DakosyTransaktionsDatum,[dySt_ergZeitpunkt]),[dySt_ts]) as datetime)> '" & ergDate & "'  AND [dySt_status] > 5 ", "FMZOLL")
            Dim cntNewerTrans As Integer = SQL.getValueTxtBySql("SELECT COUNT(*) FROM [tblDakosy_Statusmeldungen]  where dySt_BezugsNr='" & BezugsNr & "' and cast(dySt_ergZeitpunkt as datetime)> '" & ergDate & "'  AND [dySt_status] > 5 ", "FMZOLL")
            ' AND [dySt_status] > 5 --> Weil bei den 5er Status keine korrekte Zeitangabe vorliegt und die hoheren Staten sind immer danach.

            ' MsgBox("SELECT COUNT(*) FROM [tblDakosy_Statusmeldungen]  where dySt_BezugsNr='" & BezugsNr & "' and isnull([dySt_ergZeitpunkt],dySt_DakosyTransaktionsDatum)> '" & ergDate & "'" & "   -   " & cntNewerTrans & "   -   " & BezugsNr)


            Return cntNewerTrans = 0

        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



    Shared Function LOADByDakosyTransaktionsID(dySt_DakosyTransaktionsID As String) As cDakosy_Statusmeldungen
        Try
            Dim STATUS As New cDakosy_Statusmeldungen
            Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT TOP 1 * FROM tblDakosy_Statusmeldungen WHERE dySt_DakosyTransaktionsID=@dySt_DakosyTransaktionsID  ", conn)
                    cmd.Parameters.AddWithValue("@dySt_DakosyTransaktionsID", dySt_DakosyTransaktionsID)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each l In STATUS.getParameterList()
                            Dim propInfo As PropertyInfo = STATUS.GetType.GetProperty(l.Scalarvariable)

                            If dr.Item(l.Text) Is DBNull.Value Then
                                propInfo.SetValue(STATUS, Nothing)
                            Else
                                propInfo.SetValue(STATUS, dr.Item(l.Text))
                            End If

                        Next

                        dr.Close()
                        Return 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
        Return Nothing
    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_Statusmeldungen WITH(updlock,serializable) WHERE dySt_id=@dySt_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        Dim id = SQL.doSQLVarListID(dySt_id, sqlstr, "FMZOLL", , list)
        Me.dySt_id = id
        Return id > 0
    End Function



    Public Function updatePfad(pfad As String) As Boolean
        Me.dySt_pfad = pfad
        Return Me.SAVE
    End Function





    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblDakosy_Statusmeldungen WHERE dySt_id=@dySt_id  ", conn)
                    cmd.Parameters.AddWithValue("@dySt_id", dySt_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 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_Statusmeldungen] SET " & str & " WHERE dySt_id=@dySt_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_Statusmeldungen  (" & 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


    Shared Function InsertStatusMeldung(DY As cDakosy_Zollanmeldungen, dySt_status As Integer, Optional dySt_ergCode As String = "", Optional dySt_ergMitteilung As String = "", Optional dySt_ergZeitpunkt As Object = Nothing, Optional ByRef statusTMP As cDakosy_Statusmeldungen = Nothing)
        Return InsertStatusMeldung(DY.dy_Id, DY.dy_BezugsNr, DY.dy_ART, dySt_status, dySt_ergCode, dySt_ergMitteilung, dySt_ergZeitpunkt, statusTMP)
    End Function
    Shared Function InsertStatusMeldung(dySt_dyId As Integer, dySt_BezugsNr As String, dy_ART As String, dySt_status As Integer, Optional dySt_ergCode As String = "", Optional dySt_ergMitteilung As String = "", Optional dySt_ergZeitpunkt As Object = Nothing, Optional ByRef statusTMP As cDakosy_Statusmeldungen = Nothing)
        Try
            Dim STATUS As DAKOSY_Worker.cDakosy_Statusmeldungen = New DAKOSY_Worker.cDakosy_Statusmeldungen
            STATUS.dySt_BezugsNr = dySt_BezugsNr
            STATUS.dySt_artMitteilung = "VB"
            STATUS.dySt_dyArt = dy_ART '"NCTS" 'T1/T2
            STATUS.dySt_dyId = dySt_dyId

            STATUS.dySt_status = dySt_status
            STATUS.dySt_ergCode = If(dySt_ergCode, "")
            STATUS.dySt_ergMitteilung = If(dySt_ergMitteilung <> "", dySt_ergMitteilung, cDY_Statusliste.getTxtByCode2(dySt_status, dy_ART))
            STATUS.dySt_ergZeitpunkt = dySt_ergZeitpunkt
            'EINTRAG IN tblDakosy_Zollanmeldungen

            STATUS.SAVE()

            cDakosy_Zollanmeldungen.UPDATE_StatusById(dySt_dyId, STATUS.dySt_status)
            Select Case dy_ART
                Case "NCTS"
                    Dim NCTS As cDakosyNCTSOut = cDakosyNCTSOut.LOADByAnmIDFull(dySt_dyId, False)
                    ' cDakosyNCTSOut.UPDATE_StatusById(STATUS.dySt_status)
                    If NCTS IsNot Nothing Then NCTS.UPDATE_Status(STATUS.dySt_status)
                Case "AES"
                    Dim AES As cDakosyAES = cDakosyAES.LOADByAnmIDFull(dySt_dyId, False)
                    ' cDakosyNCTSOut.UPDATE_StatusById(STATUS.dySt_status)
                    If AES IsNot Nothing Then AES.UPDATE_Status(STATUS.dySt_status)

            End Select
            statusTMP = STATUS
            Return True

        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        End Try
        Return False
    End Function


End Class