Imports System.Text
Imports UTS_Core.UTSModule.DbTableModel.Customer
Imports UTS_Core.UTSModule.Station

Namespace UTSModule.Test
    Public Class TestResult
        ''' <summary>
        ''' 测试结果枚举集合
        ''' </summary>
        Enum TestResultEnum
            Fail
            Pass
        End Enum

        Sub New()
            UserID = -1
            ServiceID = -1
            ProductionLineID = -1
            OrderID = -1

            AppName = String.Empty
            TestPlan = String.Empty
            _dutSn = String.Empty

            TestResult = TestResultEnum.Fail

            ErrCode = String.Empty

            FailSteps = New List(Of RowNode)()
            CustomRecord = New Dictionary(Of String, String)()
        End Sub



        ''' <summary>
        ''' 当前用户索引,登陆后填充
        ''' </summary>
        ''' <returns></returns>
        Public Property UserID As Integer

        ''' <summary>
        ''' 当前服务索引，登陆后获取
        ''' </summary>
        ''' <returns></returns>
        Public Property ServiceID As Integer

        ''' <summary>
        ''' 所属产线索引，用户选择后获取
        ''' </summary>
        ''' <returns></returns>
        Public Property ProductionLineID As Integer

        ''' <summary>
        ''' 所属订单索引，用户提供条码，系统自动填充
        ''' </summary>
        ''' <returns></returns>
        Public Property OrderID() As Integer

        ''' <summary>
        ''' 测试程序的名称,登陆后填充
        ''' </summary>
        ''' <returns></returns>
        Public Property AppName As String

        ''' <summary>
        ''' 测试流程名,切换项目流程站后获取
        ''' </summary>
        ''' <returns></returns>
        Public Property TestPlan As String

        Private _dutSn As String

        ''' <summary>
        ''' 测试产品的索引序号,测试时获取,（使用大写,8字符）
        ''' </summary>
        ''' <returns></returns>
        Public Property DUT_SN As String
            Get
                Return _dutSn
            End Get
            Set(value As String)
                _dutSn = value.ToUpper
            End Set
        End Property

        ''' <summary>
        ''' 测试开始日期与时间,测试时获取
        ''' </summary>
        ''' <returns></returns>
        Public Property StartTime As DateTime

        ''' <summary>
        ''' 测试耗时,测试时获取
        ''' </summary>
        ''' <returns></returns>
        Public Property UsedTime As TimeSpan

        ''' <summary>
        ''' 测试结果,测试时获取
        ''' </summary>
        ''' <returns></returns>
        Public Property TestResult As TestResultEnum

        ''' <summary>
        ''' 错误代码,测试时获取
        ''' </summary>
        ''' <returns></returns>
        Public Property ErrCode As String


        ''' <summary>
        ''' 失败步骤集合,测试时获取
        ''' </summary>
        ''' <returns></returns>
        Public Property FailSteps As List(Of RowNode)

        ''' <summary>
        ''' 自定义记录数据,测试时获取
        ''' </summary>
        ''' <returns></returns>
        Public Property CustomRecord() As Dictionary(Of String, String)

        ''' <summary>
        ''' 切换测试站后,初始话测试结果信息
        ''' </summary>
        Public Sub InitTestResult(user As Integer)
            UserID = user

            TestResult = TestResultEnum.Fail

            FailSteps = New List(Of RowNode)()
            CustomRecord = New Dictionary(Of String, String)()
        End Sub

        ''' <summary>
        ''' 开启新一轮测试时,重置测试结果信息
        ''' </summary>
        Public Sub ResetTestResult()
            ErrCode = String.Empty

            _dutSn = String.Empty
            TestResult = TestResultEnum.Fail

            StartTime = Now
            UsedTime = New TimeSpan()

            OrderID = -1

            FailSteps.Clear()
            CustomRecord.Clear()
        End Sub

        ''' <summary>
        ''' 添加自定义需要记录的测试信息
        ''' </summary>
        ''' <param name="name">记录列名</param>
        ''' <param name="value">记录值</param>
        Public Function AddCustomRecord(name As String, value As String) As Boolean
            If String.Compare(name, $"{TestLogTable.ColNames.DUT_SN}") = 0 Then
                _dutSn = value
            Else
                If CustomRecord.ContainsKey(name) Then
                    CustomRecord(name) = value '已有字段则覆盖原值
                Else
                    CustomRecord.Add(name, value)
                End If
            End If
            Return True
        End Function


        ''' <summary>
        ''' 将当前测试信息转换为字符键值对
        ''' </summary>
        ''' <returns></returns>
        Public Function ToStringDictionary() As Dictionary(Of String, String)
            Dim dic As New Dictionary(Of String, String) From {
                {$"{TestLogTable.ColNames.UserID}", UserID.ToString()},
                {$"{TestLogTable.ColNames.ServiceID}", ServiceID.ToString()},
                {$"{TestLogTable.ColNames.DUT_SN}", _dutSn},
                {$"{TestLogTable.ColNames.AppName}", AppName},
                {$"{TestLogTable.ColNames.TestPlan}", TestPlan},
                {$"{TestLogTable.ColNames.StartTime}", StartTime.ToString("yyyy-MM-dd HH:mm:ss")},
                {$"{TestLogTable.ColNames.UsedTime}", UsedTime.TotalSeconds.ToString("F2")},
                {$"{TestLogTable.ColNames.TestResult}", IIf(TestResult = TestResultEnum.Fail, 0, 1).ToString()},
                {$"{TestLogTable.ColNames.ErrCode}", ErrCode},
                {$"{TestLogTable.ColNames.ProductionLineID}", ProductionLineID.ToString()},
                {$"{TestLogTable.ColNames.OrderID}", OrderID.ToString()}
            }

            '填充失败步骤信息(此处需要一个信息定位失败步骤)
            Dim steps As New StringBuilder
            Dim msg As New StringBuilder
            For Each node As RowNode In FailSteps
                If steps.Length > 0 Then
                    steps.Append(","c)
                    msg.Append(","c)
                End If

                steps.Append(node.RowListIndex)
                msg.Append(node.Desc4Record)
                msg.Append($" {node.TestReturn.RecordValue} ({node.TestReturn.LowerLimit},{node.TestReturn.UpperLimit})")
            Next
            dic.Add($"{TestLogTable.ColNames.FailSteps}", steps.ToString())
            dic.Add($"{TestLogTable.ColNames.FailMsg}", msg.ToString())

            '填充自定义记录数据字段信息
            For Each keyValue As KeyValuePair(Of String, String) In CustomRecord
                If dic.ContainsKey(keyValue.Key) Then
                    dic(keyValue.Key) = keyValue.Value
                Else
                    dic.Add(keyValue.Key, keyValue.Value)
                End If
            Next

            Return dic
        End Function

    End Class
End Namespace