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
Imports System.Linq

Public Class CombindRecordCommand
    Inherits TestCommandExecutor

    Private _filedNames As New List(Of String)
    Private _dutSn As String
    Private _dutSn2 As String

    Sub New(command As TestCommand)
        MyBase.New(command)
        '合并逻辑：将源SN指定字段的值复制到目标SN，如果目标SN已经有值，则进行覆盖操作
        '如果合并字段名为 S1~S12，则对应的Result1~Result12 也进行合并，即Sx与Resultx配对进行操作
        '覆盖方向：源SN -》 目标SN’
        _dutSn = command.Parameter(0) '目标SN’
        _dutSn2 = command.Parameter(1) '源SN’

        '_filedNames.Add(command.Parameter(2)) '字段名，多个字段名之间用“:”分割，大小写不敏感，去掉前后空格

        'Dim upperArr = arr.Select(Function(x) x.ToUpper()).ToArray()
        ' upperArr = {"A", "B", "C"}

        _filedNames.AddRange(command.Parameter(2).Split(":"c).Select(Function(x) x.Trim()))
    End Sub

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

        Dim filedName As String = _filedNames(0)
        Dim updateString As String = $"t1.`{filedName}` = t2.`{filedName}`"
        For i As Integer = 1 To _filedNames.Count - 1
            filedName = _filedNames(i)
            updateString += $",t1.`{filedName}` = t2.`{filedName}` "
        Next


        Dim saveDbCmdText As String = String.Empty
        Using db As New DbExecutor(UtsDb.RemoteDbType, UtsDb.RemoteConnString)
            Dim cmdText As String = $"UPDATE `{UtsDb.RemotePrivateDb}`.`{SnListTable.TableName}` t1 JOIN `{UtsDb.RemotePrivateDb}`.`{SnListTable.TableName}` t2 On t2.`{SnListTable.ColNames.BarCode}` = '{_dutSn2}' SET {updateString} WHERE t1.`{SnListTable.ColNames.BarCode}` = '{_dutSn}';"

            Try
                db.Open()
                db.ExecuteNonQuery(cmdText)
                db.Close()
            Catch ex As Exception

                CommandReturn.ExecuteResult = False
                CommandReturn.RecordValue = "False"
                CommandReturn.ExecuteResultTipString = $"数据库更新失败,{ex.Message}"
            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 cmdText As String = $"UPDATE `{SnListTable.TableName}` t1 JOIN `{UtsDb.RemotePrivateDb}`.`{SnListTable.TableName}` t2 ON t2.`{SnListTable.ColNames.BarCode}` = '{_dutSn2}' SET {updateString} WHERE t1.`{SnListTable.ColNames.BarCode}` = '{_dutSn}';"
        '        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
