Imports System.Data.SqlClient
Imports System.Reflection
Public Enum cAPI_Status
    [NEW] = 0 ' Neu, noch nicht verarbeitet
    OK = 1 ' Erfolg! :)
    ERR = 90 ' Fehlversuch :(
    ERR_FINAL = 99 'Hier sollte kein Verarbeitungs-Versuch mehr gestartet werden
End Enum
Public Enum cAPI_ART
    UNDEFINED = -1
    AVISO_OUT_STATUSMAIL = 1
    AVISO_OUT_BELEGMAIL = 2

    'Public Const AVISO_MAIL_OUT_AVM = 5
    'Public Const AVISO_MAIL_OUT_ABD = 6
    'Public Const AVISO_MAIL_OUT_ZNV = 7
    'Public Const AVISO_MAIL_OUT_AVM = 8
    'Public Const AVISO_MAIL_OUT_AVM = 9

    'Public Const AVISO_IN_NCTS = 10
    'Public Const AVISO_IN_EZA = 11

    VERAG_OUT_NCTS = 10
    VERAG_OUT_EZA = 11

End Enum

Public Class cAPI_Type
    Public Const UNDEFINED = Nothing
    Public Const MAIL = "MAIL"
    Public Const FTP = "FTP"
    Public Const REST = "REST"
End Class
Public Class cAPI_INOUT
    Public Const UNDEFINED = Nothing
    Public Const [IN] = "IN"
    Public Const OUT = "OUT"
End Class

Public Class cAPI
    Property api_id As Integer
    Property api_apiArt As cAPI_ART = -1
    Property api_apiUArt As Object = Nothing
    Property api_apiInfo As Object = Nothing
    Property api_apiZollStatus As Object = Nothing


    Property api_apiVersion As String = "1.0.0.0"
    Property api_InOut As String = cAPI_INOUT.UNDEFINED
    Property api_datetime As DateTime = Now
    Property api_status As cAPI_Status = cAPI_Status.[NEW]
    Property api_fehlversuch As Integer = 0
    Property api_progId As Integer = VERAG_PROG_ALLGEMEIN.cAllgemein.PROGID
    Property api_progName As String = VERAG_PROG_ALLGEMEIN.cAllgemein.PROGNAME
    Property api_progVersion As String = VERAG_PROG_ALLGEMEIN.cAllgemein.PROGVERSION
    Property api_Testsystem As Boolean = VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM
    Property api_Type As String = cAPI_Type.UNDEFINED
    Property api_EMailFrom As Object = Nothing
    Property api_EMail As Object = Nothing
    Property api_EMailCC As Object = Nothing
    Property api_EMailBCC As Object = Nothing
    Property api_EMailSubject As Object = Nothing
    Property api_EMailText As Object = Nothing
    Property api_EMailAttCnt As Object = Nothing

    Property api_filepathSource As Object = Nothing
    Property api_filepathDestination As Object = Nothing

    Property api_Firma As String = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
    Property api_MaId As String = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
    Property api_Username As String = If(VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME, "AUTO")
    Property api_Partnersystem As Object = Nothing
    Property api_Vorsystem As Object = Nothing

    Property api_KundenNr As Object = Nothing
    Property api_AvisoId As Object = Nothing
    Property api_SendungsId As Object = Nothing
    Property api_FilialenNr As Object = Nothing
    Property api_AbfertigungsNr As Object = Nothing
    Property api_BezugsNr As Object = Nothing
    Property api_Zollbelegnummer As Object = Nothing
    Property api_finTstmp As Object = Nothing
    Property api_errTstmp As Object = Nothing
    Property api_errStack As Object = Nothing
    Property api_errMessage As Object = Nothing
    Property api_errMethodName As Object = Nothing



    Public hasEntry = False

    Dim SQL As New SQL

    Sub New()
        api_progId = VERAG_PROG_ALLGEMEIN.cAllgemein.PROGID
        api_progName = VERAG_PROG_ALLGEMEIN.cAllgemein.PROGNAME
        api_progVersion = VERAG_PROG_ALLGEMEIN.cAllgemein.PROGVERSION
        api_Testsystem = VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM
        api_Firma = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
        api_MaId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
        api_Username = If(VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME, "AUTO")
    End Sub

    Sub New(api_id)
        Me.api_id = api_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("api_id", api_id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_apiArt", api_apiArt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_apiUArt", api_apiUArt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_apiInfo", api_apiInfo))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_apiZollStatus", api_apiZollStatus))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_apiVersion", api_apiVersion))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_InOut", api_InOut))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_datetime", api_datetime))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_status", api_status))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_fehlversuch", api_fehlversuch))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_progId", api_progId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_progName", api_progName))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_progVersion", api_progVersion))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_Testsystem", api_Testsystem))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_Type", api_Type)) 'MAIL; FTP; REST
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_EMailFrom", api_EMailFrom))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_EMail", api_EMail))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_EMailCC", api_EMailCC))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_EMailBCC", api_EMailBCC))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_EMailSubject", api_EMailSubject))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_EMailText", api_EMailText))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_EMailAttCnt", api_EMailAttCnt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_filepathSource", api_filepathSource))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_filepathDestination", api_filepathDestination))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_Firma", api_Firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_MaId", api_MaId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_Partnersystem", api_Partnersystem))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_Vorsystem", api_Vorsystem))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_KundenNr", api_KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_AvisoId", api_AvisoId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_SendungsId", api_SendungsId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_FilialenNr", api_FilialenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_AbfertigungsNr", api_AbfertigungsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_BezugsNr", api_BezugsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_Zollbelegnummer", api_Zollbelegnummer))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_finTstmp", api_finTstmp))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_errTstmp", api_errTstmp))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_errStack", api_errStack))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_errMessage", api_errMessage))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_errMethodName", api_errMethodName))
        Return list
    End Function

    Public Shared Function INSERT_API(api_InOut As String, api_Type As String, api_apiArt As cAPI_ART, Optional api_apiUArt As Object = Nothing,
                                      Optional api_Partnersystem As Object = Nothing, Optional api_Vorsystem As Object = Nothing, Optional api_KundenNr As Object = Nothing,
                                      Optional api_AvisoId As Object = Nothing, Optional api_SendungsId As Object = Nothing, Optional api_FilialenNr As Object = Nothing,
                                      Optional api_AbfertigungsNr As Object = Nothing, Optional api_BezugsNr As Object = Nothing, Optional api_Zollbelegnummer As Object = Nothing) As cAPI

        Dim API As New cAPI()
        API.api_apiArt = api_apiArt
        API.api_apiUArt = api_apiUArt
        API.api_InOut = api_InOut
        API.api_status = cAPI_Status.[NEW]
        API.api_Type = api_Type

        API.api_Partnersystem = api_Partnersystem
        API.api_Vorsystem = api_Vorsystem
        API.api_KundenNr = api_KundenNr

        API.api_AvisoId = api_AvisoId
        API.api_SendungsId = api_SendungsId
        API.api_FilialenNr = api_FilialenNr
        API.api_AbfertigungsNr = api_AbfertigungsNr
        API.api_BezugsNr = api_BezugsNr
        API.api_Zollbelegnummer = api_Zollbelegnummer

        API.SAVE()

        Return API
    End Function


    Public Sub UPDTAE_ERR(Optional api_errStack = Nothing, Optional api_errMessage = Nothing, Optional api_errMethodName = Nothing)
        Me.api_status = cAPI_Status.ERR
        Me.api_errTstmp = Now
        Me.api_errStack = If(api_errStack, Me.api_errStack)
        Me.api_errMessage = If(api_errMessage, Me.api_errMessage)
        Me.api_errMethodName = If(api_errMethodName, Me.api_errMethodName)
        Me.api_fehlversuch += 1
        Me.SAVE()
    End Sub
    Public Sub UPDTAE_OK(Optional api_status = 1, Optional api_filepathDestination = Nothing)
        Me.api_status = cAPI_Status.OK
        Me.api_finTstmp = Now
        Me.api_filepathDestination = If(api_filepathDestination, Me.api_filepathDestination)
        Me.api_EMail = If(api_EMail, Me.api_EMail)
        Me.api_EMailCC = If(api_EMailCC, Me.api_EMailCC)
        Me.api_EMailBCC = If(api_EMailBCC, Me.api_EMailBCC)
        Me.api_EMailSubject = If(api_EMailSubject, Me.api_EMailSubject)
        Me.api_EMailText = If(api_EMailText, Me.api_EMailText)
        Me.api_EMailAttCnt = If(api_EMailAttCnt, Me.api_EMailAttCnt)
        Me.SAVE()
    End Sub

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

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblAPI  WHERE api_id=@api_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        api_id = SQL.doSQLVarListID(api_id, sqlstr, "ADMIN", , list)
        hasEntry = api_id > 0
        Return api_id > 0

    End Function

    Public Sub LOAD()
        Try
            hasEntry = False
            Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("SELECT * FROM tblAPI WHERE api_id=@api_id  ", conn)
                    cmd.Parameters.AddWithValue("@api_id", api_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
                        hasEntry = True
                    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 [tblAPI] SET " & str & " WHERE api_id=@api_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 tblAPI  (" & 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
