Imports System.Data.SqlClient
Imports System.Reflection

Public Class cAvisoBenachrichtigungen


    Property ab_id As Integer = -1
    Property ab_AvisoID As Integer = -1
    Property ab_SendungID As Object = Nothing
    Property ab_Art As String
    Property ab_Datum As Date
    Property ab_Status As Integer
    Property ab_BenachrichtigungCode As Integer
    Property ab_Beschreibung As String
    Property ab_Prioritaet As Integer
    Property ab_Mitarbeiter As String
    Property ab_MitarbeiterId As Integer

    Property ab_TO_Art As Object = Nothing
    Property ab_TO_Code As Object = Nothing
    Property ab_Bestaetigt_MaId As Object = Nothing
    Property ab_Bestaetigt_Datum As Object = Nothing

    Property ab_Firma As Object = Nothing
    Property ab_Cluster As Object = Nothing
    Property ab_collAnhId As Object = Nothing
    Property ab_Vermerk As Object = Nothing
    Property ab_ReminderVermerkID As Object = Nothing
    Property ab_ReminderVermerkCode As Object = Nothing



    Shared SQL As New SQL

    Sub New()
    End Sub

    '    ab_TO_Art
    'F..irma
    'N..iederlassung
    'A..bteilung
    'T..eam
    'B..enutzer

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

    Public Shared Function INSERT_BENACHRICHTIGUNG(ab_AvisoID As Integer, ab_SendungID As Object, ab_BenachrichtigungCode As Integer, Optional ab_TO_Art As Object = Nothing, Optional ab_TO_Code As Object = Nothing, Optional ab_Beschreibung As String = "", Optional ab_Prioritaet As Integer = 0, Optional ab_collAnhId As String = "", Optional ab_Vermerk As String = "", Optional [ab_ReminderVermerkID] As Object = Nothing, Optional [ab_ReminderVermerkCode] As Object = Nothing, Optional FirmaTmp As Object = Nothing) As Boolean
        Dim AB As New cAvisoBenachrichtigungen



        AB.ab_AvisoID = ab_AvisoID
        If ab_SendungID IsNot Nothing AndAlso ab_SendungID > 0 Then
            AB.ab_SendungID = ab_SendungID
            AB.ab_Art = "S"
        Else
            AB.ab_SendungID = Nothing
            AB.ab_Art = "A"
        End If

        AB.ab_TO_Art = ab_TO_Art
        AB.ab_TO_Code = ab_TO_Code

        AB.ab_Datum = Now
        AB.ab_Status = 0
        AB.ab_BenachrichtigungCode = ab_BenachrichtigungCode
        If ab_Beschreibung = "" Then ab_Beschreibung = SQL.getValueTxtBySql("SELECT abc_Bezeichnung FROM tblAvisoBenachrichtigungenCodes WHERE abc_id=" & ab_BenachrichtigungCode, "AVISO")
        AB.ab_Beschreibung = ab_Beschreibung
        AB.ab_Prioritaet = ab_Prioritaet
        AB.ab_Mitarbeiter = If(VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME, "AUTO")
        AB.ab_MitarbeiterId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
        AB.ab_ReminderVermerkID = ab_ReminderVermerkID
        AB.ab_ReminderVermerkCode = ab_ReminderVermerkCode


        AB.ab_Firma = If(FirmaTmp, VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA)
        AB.ab_Cluster = VERAG_PROG_ALLGEMEIN.cAllgemein.CLUSTER
        AB.ab_Vermerk = ab_Vermerk
        AB.ab_collAnhId = ab_collAnhId
        Return AB.SAVE
    End Function

    Public Shared Function BESTAETIGEN_BENACHRICHTIGUNG_ALL(ab_AvisoID, onlytype) As Boolean
        Dim sqlstr = " Update [tblAvisoBenachrichtigungen] SET ab_Status = 1,ab_Bestaetigt_MaId=-1,ab_Bestaetigt_Datum=getDate() WHERE ab_AvisoID=" & ab_AvisoID & If(onlytype, " and ab_art='A' ", "")
        Return SQL.doSQL(sqlstr, "AVISO")
    End Function

    Public Shared Function BESTAETIGEN_BENACHRICHTIGUNG_SND([ab_SendungID], onlytype) As Boolean
        Dim sqlstr = " Update [tblAvisoBenachrichtigungen] SET ab_Status = 1,ab_Bestaetigt_MaId=-1,ab_Bestaetigt_Datum=getDate() WHERE [ab_SendungID]=" & [ab_SendungID]
        Return SQL.doSQL(sqlstr, "AVISO")
    End Function

    Public Function BESTAETIGEN_BENACHRICHTIGUNG() As Boolean
        Me.ab_Status = 1
        Me.ab_Bestaetigt_MaId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
        Me.ab_Bestaetigt_Datum = Now

        Return Me.SAVE
    End Function


    Public Shared Function BESTAETIGEN_BENACHRICHTIGUNG(ab_id As Integer) As Boolean
        Dim AB As New cAvisoBenachrichtigungen(ab_id)

        AB.ab_Status = 1
        AB.ab_Bestaetigt_MaId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
        AB.ab_Bestaetigt_Datum = Now

        Return AB.SAVE
    End Function

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

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_AvisoID", ab_AvisoID))     '        INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_SendungID", ab_SendungID)) '        INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_Art", ab_Art))  '        INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_Datum", ab_Datum))  '    INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_Status", ab_Status))       '          VARCHAR(200) Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_BenachrichtigungCode", ab_BenachrichtigungCode))   ' VARCHAR(40)  Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_Beschreibung", ab_Beschreibung)) '   VARCHAR(40)  NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_Prioritaet", ab_Prioritaet)) 'VARCHAR(40)  Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_Mitarbeiter", ab_Mitarbeiter)) 'VARCHAR(60)  Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_MitarbeiterId", ab_MitarbeiterId))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_TO_Art", ab_TO_Art))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_TO_Code", ab_TO_Code))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_Bestaetigt_MaId", ab_Bestaetigt_MaId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_Bestaetigt_Datum", ab_Bestaetigt_Datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_Firma", ab_Firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_Cluster", ab_Cluster))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_collAnhId", ab_collAnhId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_Vermerk", ab_Vermerk))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_ReminderVermerkCode", ab_ReminderVermerkCode))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ab_ReminderVermerkID", ab_ReminderVermerkID))


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

        ab_id = SQL.doSQLVarListID(ab_id, sqlstr, "AVISO", , list)
        Return ab_id > 0
    End Function



    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionAVISO
                Using cmd As New SqlCommand("SELECT * FROM tblAvisoBenachrichtigungen WHERE ab_id=@ab_id  ", conn)
                    cmd.Parameters.AddWithValue("@ab_id", ab_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

                    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 [tblAvisoBenachrichtigungen] SET " & str & " WHERE ab_id=@ab_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 tblAvisoBenachrichtigungen  (" & 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 [tblAvisoBenachrichtigungen] WITH(updlock,serializable) WHERE ab_id=" & Me.ab_id
        Return SQL.doSQL(sqlstr, "AVISO")
    End Function


End Class
