Imports UTS_Core.Database
Imports UTS_Core.UTSModule
Imports UTS_Core.UTSModule.DbTableModel.Customer

Public Class CheckRecorder

    Private ReadOnly _failTipDictionary As Dictionary(Of String, Integer)

    Sub New()
        _failTipDictionary = New Dictionary(Of String, Integer)()
    End Sub

    Public Property TestLogTableName As String


    Public Sub Commit(failTip As String)
        If _failTipDictionary.ContainsKey(failTip) Then
            _failTipDictionary(failTip) += 1
        Else
            _failTipDictionary.Add(failTip, 1)
        End If
    End Sub


    ''' <summary>
    ''' 获取当前倒叙排序的结果信息
    ''' </summary>
    ''' <returns></returns>
    Public Function UpdateFailStrings() As String()
        Dim result As New List(Of String)
        '排序
        For Each valuePair As KeyValuePair(Of String, Integer) In _failTipDictionary.OrderByDescending(Function(x) x.Value)
            result.Add(valuePair.Key)
        Next

        Return result.ToArray()
    End Function


    Public Sub ReloadFailString()
        If String.IsNullOrWhiteSpace(TestLogTableName) Then Return

        '以本地库为准
        Using db As New DbExecutor(UtsDb.LocalDbType, UtsDb.LocalConnString)
            db.Open()

            Dim cmdText As String

            '统计日志表分组信息
            cmdText = $"select `{TestLogTable.ColNamesEnum.QA_CheckResult}`,count(*) as count from `{TestLogTableName}` where `TestResult` = '0' group by `{TestLogTable.ColNamesEnum.QA_CheckResult}` order by count"
            Dim dtTable As DataTable = db.ExecuteDataTable(cmdText)

            For Each row As DataRow In dtTable.Rows
                _failTipDictionary.Add(row($"{TestLogTable.ColNamesEnum.QA_CheckResult}").ToString(), CInt(row("count")))
            Next

            db.Close()
        End Using
    End Sub

End Class
