Imports UTS_Core.Database

Namespace UTSModule.Production

    Public Class TestCmdManager
        Private ReadOnly _command As Dictionary(Of String, Dictionary(Of String, TestCmd))

        ''' <summary>测试命令管理器，全局唯一</summary>
        Private Shared _manager As TestCmdManager

        ''' <summary>初始化测试器线程锁</summary>
        Private Shared ReadOnly InitLock As New Object()

        Private Sub New()
            _command = New Dictionary(Of String, Dictionary(Of String, TestCmd))()
        End Sub

        ''' <summary>
        ''' 创建单例管理器,并初始化管理器内容
        ''' </summary>
        ''' <returns></returns>
        Public Shared Function CreateManager() As TestCmdManager
            If _manager Is Nothing Then
                SyncLock InitLock
                    Threading.Thread.MemoryBarrier()
                    If _manager Is Nothing Then
                        _manager = New TestCmdManager
                        _manager.InitCommandHelper(UtsDb.LocalDbType, UtsDb.LocalConnString)
                    End If
                End SyncLock
            End If
            Return _manager
        End Function


        Public Function GetCommandTypes() As String()
            Return _command.Keys.ToArray()
        End Function

        Public Function GetCommandNames(type As String) As String()
            If _command.ContainsKey(type) Then
                Return _command(type).Keys.ToArray()
            End If
            Return New String() {}
        End Function


        Public Function GetCommandCount(type As String) As Integer
            If _command.ContainsKey(type) Then
                Return _command(type).Count
            End If

            Return 0
        End Function


        ''' <summary>
        ''' 根据命令类型与命令名索引命令
        ''' </summary>
        ''' <param name="type"></param>
        ''' <param name="index"></param>
        ''' <returns></returns>
        Public Function GetCommand(type As String, index As Integer) As TestCmd
            Dim rtn As TestCmd = Nothing
            Try
                If index < GetCommandCount(type) Then
                    rtn = _command(type).Values.ElementAt(index)
                End If
            Catch ex As Exception
                Console.WriteLine($"未查询到 type：{type} index{index} 命令存在！")
            End Try
            Return rtn
        End Function

        ''' <summary>
        ''' 根据命令类型与命令名索引命令
        ''' </summary>
        ''' <param name="type"></param>
        ''' <param name="name"></param>
        ''' <returns></returns>
        Public Function GetCommand(type As String, name As String) As TestCmd
            Dim rtn As TestCmd = Nothing
            If _command.ContainsKey(type) Then
                If _command(type).ContainsKey(name) Then
                    rtn = _command(type)(name)
                End If
            End If
            Return rtn
        End Function



        Private Function SearchCommand(type As DbExecutor.DbTypeEnum, connString As String) As DataTable
            Using db As New DbExecutor(type, connString) '查询本地
                db.Open()

                Dim tableName As String = DbTableModel.Manage.UtsCmdListTable.TableName
                Return db.ExecuteDataTable(db.CmdHelper.SearchAll(tableName))
            End Using
        End Function

        ''' <summary>
        '''  初始化版主信息
        ''' </summary>
        ''' <param name="dtCommand"></param>
        Public Sub InitCommandHelper(dtCommand As DataTable)
            _command.Clear()

            For row As Integer = 0 To dtCommand.Rows.Count - 1
                Dim planCommand As New TestCmd
                planCommand.InitCommand(dtCommand.Rows(row))

                If _command.ContainsKey(planCommand.CommandType) = False Then '添加新分组
                    _command.Add(planCommand.CommandType, New Dictionary(Of String, TestCmd))
                End If

                _command(planCommand.CommandType).Add(planCommand.CommandName, planCommand)
            Next
        End Sub


        ''' <summary>
        ''' 初始化流程站帮助信息
        ''' 从本地数据库中读取
        ''' </summary>
        ''' <param name="type">数据库类型</param>
        ''' <param name="connString">数据库连接字符串</param>
        Public Sub InitCommandHelper(type As DbExecutor.DbTypeEnum, connString As String)
            '查询数据表
            Dim dtCommand As DataTable = SearchCommand(type, connString)

            '赋值
            InitCommandHelper(dtCommand)
        End Sub
    End Class
End Namespace