Namespace UTSModule.Test.Command
    ''' <summary>
    ''' UTS串口通讯返回值比较函数静态类
    ''' </summary>
    Public Class CompareFunction

#Region "String"
        ''' <summary>
        ''' 仅一个参数字符串比较,忽略大小写
        ''' </summary>
        ''' <param name="testReturn">测试命令返回集合</param>
        ''' <returns></returns>
        Public Shared Function StringCompare(testReturn As TestCommandReturn) As Boolean
            Return StringCompare(testReturn.RecordValue, testReturn.LowerLimit, testReturn.UpperLimit)
        End Function

        ''' <summary>
        ''' 判断目标字符串是否是上限或下限字符串中的某一个,忽略大小写，符合上下线中的任何一个则返回True
        ''' </summary>
        ''' <returns></returns>
        ''' Momo 2022-09-19 添加命令，等于上下限其中的一个就返回true
        Public Shared Function StringEquals(value As String, lowerLimit As String, upperLimit As String) As Boolean
            Dim result As Boolean = True

            If String.IsNullOrWhiteSpace(lowerLimit) = False Then '需要比较下限
                If String.Equals(value, lowerLimit) = False Then '不符合条件
                    result = False
                Else
                    Return True
                End If
            End If

            If String.IsNullOrWhiteSpace(upperLimit) = False Then '需要比较上限
                If String.Equals(value, upperLimit) = False Then '不符合条件
                    result = False
                Else
                    Return True
                End If
            End If

            Return result
        End Function

        ''' <summary>
        ''' 仅一个参数字符串是否包含关系,忽略大小写，下限则为目标字符串包含下限，上限则为上限字符串包含目标字符串,包含则返回False，不包含则返回True
        ''' </summary>
        ''' <returns></returns>
        ''' Momo 2022-09-15 添加命令，包含指定字符则返回false，否则返回true
        Public Shared Function StringIsNotContain(value As String, lowerLimit As String, upperLimit As String) As Boolean
            Dim result As Boolean = True

            If String.IsNullOrWhiteSpace(lowerLimit) = False Then '需要比较下限
                If value.Contains(lowerLimit) = True Then '不符合条件
                    result = False
                    Return result '失败则不用比较上限
                End If
            End If

            If String.IsNullOrWhiteSpace(upperLimit) = False Then '需要比较上限
                If upperLimit.Contains(value) = True Then '不符合条件
                    result = False
                End If
            End If

            Return result
        End Function

        ''' <summary>
        ''' 仅一个参数字符串是否包含关系,忽略大小写，下限则为目标字符串包含下限，上限则为上限字符串包含目标字符串
        ''' </summary>
        ''' <returns></returns>
        Public Shared Function StringContain(value As String, lowerLimit As String, upperLimit As String) As Boolean
            Dim result As Boolean = True

            If String.IsNullOrWhiteSpace(lowerLimit) = False Then '需要比较下限
                If value.Contains(lowerLimit) = False Then '不符合条件
                    result = False
                    Return result '失败则不用比较上限
                End If
            End If

            If String.IsNullOrWhiteSpace(upperLimit) = False Then '需要比较上限
                If upperLimit.Contains(value) = False Then '不符合条件
                    result = False
                End If
            End If
            Return result

        End Function

        ''' <summary>
        ''' 仅一个参数字符串是否包含关系,忽略大小写，返回数据包含上下限中任意一个字符串即算ok
        ''' </summary>
        ''' <returns></returns>
        Public Shared Function StringContain_Momo(value As String, lowerLimit As String, upperLimit As String) As Boolean
            Dim result As Boolean = True

            'Momo 2022-11-14 字符串比较不区分大小写
            value = Trim(value.ToUpper)
            lowerLimit = Trim(lowerLimit.ToUpper)
            upperLimit = Trim(upperLimit.ToUpper)

            'MsgBox("value = " & value & vbCrLf &
            '       "lowerLimit = " & lowerLimit & vbCrLf &
            '       "upperLimit = " & upperLimit)

            'Momo 2022-11-14 待比较字符串为空白则返回错误
            If String.IsNullOrWhiteSpace(value) = True Then
                result = False
                Return result '失败则不用比较上限
            End If


            If String.IsNullOrWhiteSpace(lowerLimit) = False Then '需要比较下限
                If value <> lowerLimit Then '不符合条件
                    If String.IsNullOrWhiteSpace(upperLimit) = False Then '需要比较上限
                        If value <> upperLimit Then '符合条件
                            result = False 'Momo 2023-06-09 字符串判断方式：包含方式 ： 包含下限则返回true’
                            Return result
                        End If
                    End If
                End If
            End If

            Return result
        End Function

        ''' <summary>
        ''' 仅一个参数字符串比较,忽略大小写
        ''' </summary>
        ''' <param name="value">当前值</param>
        ''' <param name="lowerLimit">下限</param>
        ''' <param name="upperLimit">上限</param>
        ''' <returns></returns>
        Public Shared Function StringCompare(value As String, lowerLimit As String, upperLimit As String) As Boolean
            Dim result As Boolean = True

            If String.IsNullOrWhiteSpace(lowerLimit) = False Then '需要比较下限
                If String.Compare(value, lowerLimit, True) < 0 Then '不符合条件
                    result = False
                    Return result '失败则不用比较上限
                End If
            End If


            If String.IsNullOrWhiteSpace(upperLimit) = False Then '需要比较上限
                If String.Compare(value, upperLimit, True) > 0 Then '不符合条件
                    result = False
                End If
            End If

            Return result
        End Function


        ''' <summary>
        ''' 确认字符串长度
        ''' </summary>
        ''' <param name="value">当前值</param>
        ''' <param name="lowerLimit">下限</param>
        ''' <param name="upperLimit">上限</param>
        ''' <returns></returns>
        Public Shared Function StringLengthCheck(value As String, lowerLimit As String, upperLimit As String) As Boolean
            Dim result As Boolean = True
            Dim int_lowerlimt As Integer = 0 '’
            Dim int_upperlimit As Integer = 0 '’Int(upperLimit)

            If String.IsNullOrWhiteSpace(lowerLimit) = True Then
                int_lowerlimt = 0
            Else
                int_lowerlimt = CInt(lowerLimit)
            End If

            If String.IsNullOrWhiteSpace(upperLimit) = True Then
                int_upperlimit = 0
            Else
                int_upperlimit = CInt(upperLimit)
            End If

            If String.IsNullOrWhiteSpace(lowerLimit) = False AndAlso int_lowerlimt > 0 Then '需要比较下限
                If value.Length < int_lowerlimt Then '不符合条件
                    result = False
                    Return result '失败则不用比较上限
                End If
            End If

            If String.IsNullOrWhiteSpace(upperLimit) = False AndAlso int_upperlimit > 0 Then '需要比较上限
                If value.Length > int_upperlimit Then '不符合条件
                    result = False
                    Return result
                End If
            End If

            Return result
        End Function

        ''' <summary>
        ''' 确认字符串中每个字符的格式
        ''' </summary>
        ''' <param name="value">当前值</param>
        ''' <param name="charMode">字符校验模式</param>
        ''' <returns></returns>
        Public Shared Function StringEachCharCheck(value As String, charMode As String) As Boolean
            Dim result As Boolean = True
            Dim tmpString_tobe_process As String = value
            Dim int_charMode As Integer = 0 ' CInt(charMode)
            Dim i As Integer = 0


            If String.IsNullOrWhiteSpace(charMode) = True Then
                int_charMode = 0
            Else
                int_charMode = CInt(charMode)
            End If

            If value.Length > 0 Then
                '对每个字符都做格式检查：
                '空白：不对每个字符进行检查
                '0：不对每个字符进行检查
                '1：每个字符都必须是数字0~9
                '2：每个字符都必须是十六进制， 即0~9， A~F， 包括空格
                '3：每个字符都必须是数字或字母， 用以排除乱码
                '对字符逐个进行格式检查
                Select Case int_charMode
                    Case 0 '不对每个字符进行检查

                    Case 1 '每个字符都必须是数字0~9
                        If IsNumeric(tmpString_tobe_process) = False Then
                            Return False
                        End If

                    Case 2 '每个字符都必须是十六进制， 即0~9， A~F， 包括空格
                        '0~9: 48~57
                        'A~F: 65~70
                        'space:32
                        ' -:45
                        If tmpString_tobe_process.Length > 0 Then
                            For i = 1 To tmpString_tobe_process.Length
                                Dim tmpChar As Char = CChar(Mid(tmpString_tobe_process, i, 1).ToUpper)

                                If (Asc(tmpChar) >= 48 AndAlso Asc(tmpChar) <= 57) OrElse '0~9
                                   (Asc(tmpChar) >= 65 AndAlso Asc(tmpChar) <= 70) OrElse 'A~F
                                   (Asc(tmpChar) = 32) Then 'space
                                    '合法内容
                                Else
                                    Return False
                                End If
                            Next
                        End If

                    Case 3 '每个字符都必须是数字或字母， 用以排除乱码
                        If tmpString_tobe_process.Length > 0 Then
                            For i = 1 To tmpString_tobe_process.Length
                                Dim tmpChar As Char = CChar(Mid(tmpString_tobe_process, i, 1))

                                If (Asc(tmpChar) >= 32 AndAlso Asc(tmpChar) <= 126) Then 'space
                                    '合法内容
                                Else
                                    Return False
                                End If
                            Next
                        End If

                    Case Else '其他情况

                End Select
            End If

            Return result
        End Function

        ''' <summary>
        ''' 多参数字符串列表比较上下限
        ''' </summary>
        ''' <param name="param">参数列表</param>
        ''' <param name="lowerLimit">下限字符串,切割时按冒号切割,位数不足则默认不比较</param>
        ''' <param name="upperLimit">上限字符串,切割时按冒号切割,位数不足则默认不比较</param>
        ''' <returns></returns>
        Public Shared Function ParamStringListCompare(param As List(Of String), lowerLimit As String, upperLimit As String) As Boolean
            Dim lowerList As String() = lowerLimit.Split(":"c)
            Dim upperList As String() = upperLimit.Split(":"c)
            Dim lower As String
            Dim upper As String

            If param.Count() = 0 Then
                If StringCompare("", lowerList(0), upperList(0)) = False Then Return False
            End If

            For i As Integer = 0 To param.Count - 1
                If i >= lowerList.Count() Then
                    lower = String.Empty
                Else
                    lower = lowerList(i)
                End If

                If i >= upperList.Count() Then
                    upper = String.Empty
                Else
                    upper = upperList(i)
                End If


                If StringCompare(param(i), lower, upper) = False Then Return False
            Next

            Return True
        End Function

#End Region

#Region "Double"
        ''' <summary>
        ''' 浮点型比较方式
        ''' </summary>
        ''' <param name="testReturn"></param>
        ''' <returns></returns>
        Public Shared Function DoubleCompare(testReturn As TestCommandReturn) As Boolean

            Return DoubleCompare(testReturn.RecordValue, testReturn.LowerLimit, testReturn.UpperLimit)
        End Function


        ''' <summary>
        ''' 仅一个参数浮点型比较
        ''' </summary>
        ''' <param name="value">当前值</param>
        ''' <param name="lowerLimit">下限</param>
        ''' <param name="upperLimit">上限</param>
        ''' <returns></returns>
        Public Shared Function DoubleCompare(value As String, lowerLimit As String, upperLimit As String) As Boolean

            'Momo 2023-12 15 如果上下限都为控制则默认为不需比较，返回True
            If String.IsNullOrEmpty(lowerLimit) AndAlso String.IsNullOrEmpty(upperLimit) Then Return True

            Dim result As Boolean = True

            Dim compareLower As Boolean = IsNumeric(lowerLimit)
            Dim compareUpper As Boolean = IsNumeric(upperLimit)

            If IsNumeric(value) = False Then
                If compareLower OrElse compareUpper Then result = False
            Else
                Dim val As Double = CDbl(value)

                If compareLower Then
                    If val < CDbl(lowerLimit) Then
                        result = False
                        Return result '失败则不用比较上限
                    End If
                End If

                If compareUpper Then
                    If val > CDbl(upperLimit) Then result = False
                End If
            End If

            Return result
        End Function




        ''' <summary>
        ''' 多参数浮点型列表比较上下限
        ''' </summary>
        ''' <param name="param">参数列表</param>
        ''' <param name="lowerLimit">下限字符串,切割时按冒号切割,位数不足则默认不比较</param>
        ''' <param name="upperLimit">上限字符串,切割时按冒号切割,位数不足则默认不比较</param>
        ''' <returns></returns>
        Public Shared Function ParamDoubleListCompare(param As List(Of String),
                                                      lowerLimit As String,
                                                      upperLimit As String,
                                                      Optional lowerLimit_2 As String = "",
                                                      Optional upperLimit_2 As String = "") As Boolean
            Dim lowerList As String() = lowerLimit.Split(":"c)
            Dim upperList As String() = upperLimit.Split(":"c)
            Dim lower As String
            Dim upper As String

            Dim lowerList_2 As String() = lowerLimit_2.Split(":"c)
            Dim upperList_2 As String() = upperLimit_2.Split(":"c)
            Dim lower_2 As String
            Dim upper_2 As String

            Dim reslut_1 As Boolean = False
            Dim result_2 As Boolean = False

            If param.Count = 0 Then
                If DoubleCompare("", lowerList(0), upperList(0)) = False Then
                    If DoubleCompare("", lowerLimit_2(0), upperLimit_2(0)) = False Then 'Momo 2023-12-15 增加第二个标准判定,两个标准任意一个满足则为true
                        Return False
                    End If
                End If
            End If

            For i As Integer = 0 To param.Count - 1
                If i >= lowerList.Count() Then
                    lower = String.Empty
                    lower_2 = String.Empty
                Else
                    lower = lowerList(i)
                    lower_2 = lowerList_2(i)
                End If

                If i >= upperList.Count() Then
                    upper = String.Empty
                    upper_2 = String.Empty
                Else
                    upper = upperList(i)
                    upper_2 = upperList_2(i)
                End If


                'Momo 2023-12-15 增加第二个标准判定,两个标准任意一个满足则为true
                reslut_1 = DoubleCompare(param(i), lower, upper)
                result_2 = DoubleCompare(param(i), lower_2, upper_2)

                If String.IsNullOrEmpty(lower_2) AndAlso String.IsNullOrEmpty(upper_2) Then
                    If reslut_1 = False Then Return False
                Else
                    If reslut_1 = False AndAlso result_2 = False Then Return False
                End If

            Next

            Return True
        End Function
#End Region

#Region "Integer"
        ''' <summary>
        ''' 整数型比较方式
        ''' </summary>
        ''' <param name="testReturn"></param>
        ''' <returns></returns>
        Public Shared Function IntegerCompare(testReturn As TestCommandReturn) As Boolean

            Return IntegerCompare(testReturn.RecordValue, testReturn.LowerLimit, testReturn.UpperLimit)
        End Function


        ''' <summary>
        ''' 仅一个参数整数比较,忽略大小写
        ''' </summary>
        ''' <param name="value">当前值</param>
        ''' <param name="lowerLimit">下限</param>
        ''' <param name="upperLimit">上限</param>
        ''' <returns></returns>
        Public Shared Function IntegerCompare(value As String, lowerLimit As String, upperLimit As String) As Boolean
            Dim result As Boolean = True

            Dim compareLower As Boolean = IsNumeric(lowerLimit)
            Dim compareUpper As Boolean = IsNumeric(upperLimit)

            If IsNumeric(value) = False Then
                If compareLower OrElse compareUpper Then result = False
            Else
                Dim val As Double = CInt(value)

                If compareLower Then
                    If val < CInt(lowerLimit) Then
                        result = False
                        Return result '失败则不用比较上限
                    End If
                End If

                If compareUpper Then
                    If val > CInt(upperLimit) Then result = False
                End If
            End If

            Return result
        End Function




        ''' <summary>
        ''' 多参数整数型列表比较上下限
        ''' </summary>
        ''' <param name="param">参数列表</param>
        ''' <param name="lowerLimit">下限字符串,切割时按冒号切割,位数不足则默认不比较</param>
        ''' <param name="upperLimit">上限字符串,切割时按冒号切割,位数不足则默认不比较</param>
        ''' <returns></returns>
        Public Shared Function ParamIntegerListCompare(param As List(Of String), lowerLimit As String, upperLimit As String) As Boolean
            Dim lowerList As String() = lowerLimit.Split(":"c)
            Dim upperList As String() = upperLimit.Split(":"c)
            Dim lower As String
            Dim upper As String

            If param.Count = 0 Then
                If IntegerCompare("", lowerList(0), upperList(0)) = False Then Return False
            End If

            For i As Integer = 0 To param.Count - 1
                If i >= lowerList.Count() Then
                    lower = String.Empty
                Else
                    lower = lowerList(i)
                End If

                If i >= upperList.Count() Then
                    upper = String.Empty
                Else
                    upper = upperList(i)
                End If


                If IntegerCompare(param(i), lower, upper) = False Then Return False
            Next

            Return True
        End Function
#End Region

    End Class
End Namespace