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

Public Class SetRecordCommand
    Inherits TestCommandExecutor

    Private _filedName As String
    Private _filedValue As String
    Private _dutSn As String


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

        _dutSn = command.Parameter(0)
        _filedName = command.Parameter(1)
        _filedValue = command.Parameter(2)

        'todo：SetRecord 只能对自定义字段进行写入，保护字段不执行’
    End Sub

    Public Overrides Function Execute() As TestCommandReturn
        CommandReturn.ExecuteResult = True
        CommandReturn.RecordValue = "True"

        '优先储存云端
        Dim saveDbCmdText As String = String.Empty
        Using db As New DbExecutor(UtsDb.RemoteDbType, UtsDb.RemoteConnString)
            Dim updateString As String = $"`{_filedName}` = {_filedValue}"
            Dim condition As String = $"`{SnListTable.ColNames.BarCode}` = '{_dutSn}'"
            Dim cmdText As String = db.CmdHelper.DbUpdate(UtsDb.RemotePrivateDb, SnListTable.TableName, updateString, condition)

            Try
                db.Open()
                db.ExecuteNonQuery(cmdText)
                db.Close()
            Catch ex As Exception
                saveDbCmdText = cmdText '云端存储失败，转存本地
            End Try
        End Using

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

            Try
                Dim updateString As String = $"`{_filedName}` = {_filedValue}"
                ' 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.Update(SnListTable.TableName, updateString, condition)
                db.ExecuteNonQuery(cmdText)
            Catch ex As Exception
                CommandReturn.ExecuteResult = False
                CommandReturn.RecordValue = "False"
                CommandReturn.ExecuteResultTipString = $"本地数据库保存失败,{ex.Message}"
            End Try


            '本地缓存
            Try
                If Not String.IsNullOrEmpty(saveDbCmdText) Then
                    DbConnector.SaveCmdStringToCacheTable(db, saveDbCmdText)
                    CommandReturn.ExecuteResultTipString = "本地缓存成功"
                End If
            Catch ex As Exception
                CommandReturn.ExecuteResult = False
                CommandReturn.RecordValue = "False"
                CommandReturn.ExecuteResultTipString = $"本地数据库缓存失败,{ex.Message}"
            End Try


            db.Close()
        End Using

        Return CommandReturn
    End Function
End Class
