Imports UTS_Core.Database
Imports UTS_Core.UTSModule.DbTableModel.Manage.TestPlanTipsTable

Namespace UTSModule.Production
    Public Class PlanColManager
        Private ReadOnly _planColList As List(Of PlanCol)

        ''' <summary>测试流程列管理器，全局唯一</summary>
        Private Shared _manager As PlanColManager

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


        Private Sub New()
            _planColList = New List(Of PlanCol)()

        End Sub

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


        Public Sub InitializePlanCol()
            Dim dtTable As DataTable = SearchPlanCol(UtsDb.LocalDbType, UtsDb.LocalConnString)

            _planColList.Clear()
            For Each row As DataRow In dtTable.Rows
                Dim name As String = row($"{ColNamesEnum.ColName}").ToString()
                Dim type As String = row($"{ColNamesEnum.ColType}").ToString()
                Dim desc As String = row($"{ColNamesEnum.ColDesc}").ToString()

                _planColList.Add(New PlanCol(name, type, desc))
            Next
        End Sub

        Public Function IndexOf(code As String) As Integer
            For i As Integer = 0 To _planColList.Count - 1
                If _planColList(i).NameEqual(code) Then Return i
            Next
            Return -1
        End Function

        Default Public ReadOnly Property Item(index As Integer) As PlanCol
            Get
                Return _planColList(index)
            End Get
        End Property

        Default Public ReadOnly Property Item(code As String) As PlanCol
            Get
                Return Item(IndexOf(code))
            End Get
        End Property


        Private Function Count() As Integer
            Return _planColList.Count
        End Function


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

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

    End Class
End Namespace