Imports UTS_Core.Database
Imports UTS_Core.UTSModule
Imports UTS_Core.UTSModule.DbTableModel.Customer
Imports UTS_Core.UTSModule.Test.Command

Public Class GetRecordCommand
    Inherits TestCommandExecutor

    Private _filedName As String
    Private _dutSn As String
    Private _mode As String

    Private _LowerLimit As String = ""
    Private _UpperLimit As String = ""
    Private _stringLen_LowwerLimit As String = ""
    Private _stringLen_UpperLimit As String = ""
    Private _Char_VerfiyMode As String = ""

    Sub New(command As TestCommand)
        MyBase.New(command)

        _dutSn = command.Parameter(0)
        _filedName = command.Parameter(1)
        _mode = command.Parameter(2) 'Local:只查询本地   Remote：只查询云端   Both:查询本地和云端（云端优先）


    End Sub

    Private Sub GetByLocal()
        Using db As New DbExecutor(UtsDb.LocalDbType, UtsDb.LocalConnString)
            Try
                db.Open()
            Catch ex As Exception
                CommandReturn.ExecuteResult = False
                CommandReturn.RecordValue = ""
                CommandReturn.ExecuteResultTipString = $"本地数据库连接失败,{ex.Message}"
            End Try

            Try
                ' Dim condition As String = $"`{SnListTable.ColNames.ProductID}` = '{Station.ParentProject.Index}' and `{SnListTable.ColNames.BarCode}` = '{_dutSn}'"
                Dim condition As String = $"`{SnListTable.ColNames.BarCode}` = '{_dutSn}'"
                Dim cmdText As String = db.CmdHelper.Search(_filedName, SnListTable.TableName, condition)
                CommandReturn.RecordValue = db.ExecuteScalar(cmdText).ToString()
                CommandReturn.ExecuteResult = True
                CommandReturn.ExecuteResultTipString = $"本地数据库查询成功"
            Catch ex As Exception
                CommandReturn.ExecuteResult = False
                CommandReturn.RecordValue = ""
                CommandReturn.ExecuteResultTipString = $"本地数据库查询失败,{ex.Message}"
            End Try

            db.Close()
        End Using

    End Sub


    Private Function GetByRemote() As Boolean
        Dim useLocalSearch As Boolean = False


        Using db As New DbExecutor(UtsDb.RemoteDbType, UtsDb.RemoteConnString)
            Dim condition As String = $"`{SnListTable.ColNames.BarCode}` = '{_dutSn}'"
            Dim cmdText As String = db.CmdHelper.DbSearch(UtsDb.RemotePrivateDb, _filedName, SnListTable.TableName, condition)

            Try
                db.Open()
                CommandReturn.RecordValue = db.ExecuteScalar(cmdText).ToString()
                CommandReturn.ExecuteResult = True
                db.Close()
            Catch ex As Exception
                useLocalSearch = True '云端查询失败，使用本地查询
            End Try
        End Using

        Return useLocalSearch
    End Function


    Public Overrides Function Execute() As TestCommandReturn
        Select Case _mode
            Case "Local", "0"
                GetByLocal()
            Case "Both", "2"
                If GetByRemote() = False Then GetByLocal()

            Case "Remote", "1"
                GetByRemote()
            Case Else
                GetByRemote()
        End Select

        'todo:数据验证




        ''优先查询云端
        'Dim useLocalSearch As Boolean = False
        'Using db As New DbExecutor(UtsDb.RemoteDbType, UtsDb.RemoteConnString)
        '    Dim condition As String = $"`{SnListTable.ColNames.BarCode}` = '{_dutSn}'"
        '    Dim cmdText As String = db.CmdHelper.DbSearch(UtsDb.RemotePrivateDb, _filedName, SnListTable.TableName, condition)

        '    Try
        '        db.Open()
        '        CommandReturn.RecordValue = db.ExecuteScalar(cmdText).ToString()
        '        CommandReturn.ExecuteResult = True
        '        db.Close()
        '    Catch ex As Exception
        '        useLocalSearch = True '云端查询失败，使用本地查询
        '    End Try
        'End Using


        ''本地存储
        'If useLocalSearch Then
        '    Using db As New DbExecutor(UtsDb.LocalDbType, UtsDb.LocalConnString)
        '        Try
        '            db.Open()
        '        Catch ex As Exception
        '            CommandReturn.ExecuteResult = False
        '            CommandReturn.RecordValue = ""
        '            CommandReturn.ExecuteResultTipString = $"本地数据库连接失败,{ex.Message}"
        '        End Try

        '        Try
        '            ' Dim condition As String = $"`{SnListTable.ColNames.ProductID}` = '{Station.ParentProject.Index}' and `{SnListTable.ColNames.BarCode}` = '{_dutSn}'"
        '            Dim condition As String = $"`{SnListTable.ColNames.BarCode}` = '{_dutSn}'"
        '            Dim cmdText As String = db.CmdHelper.Search(_filedName, SnListTable.TableName, condition)
        '            CommandReturn.RecordValue = db.ExecuteScalar(cmdText).ToString()
        '            CommandReturn.ExecuteResult = True
        '            CommandReturn.ExecuteResultTipString = $"本地数据库查询成功"
        '        Catch ex As Exception
        '            CommandReturn.ExecuteResult = False
        '            CommandReturn.RecordValue = ""
        '            CommandReturn.ExecuteResultTipString = $"本地数据库查询失败,{ex.Message}"
        '        End Try

        '        db.Close()
        '    End Using
        'End If

        Return CommandReturn
    End Function
End Class
