Imports UTS_Core.UTSModule.DbTableModel.Customer.ProductTypesTable

Namespace UTSModule.Production

    Public Class ProductTypeManager
        ''' <summary>产品类型管理器，全局唯一</summary>
        Private Shared _manager As ProductTypeManager

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

        Private ReadOnly _productTypeList As List(Of ProductType)

        Private Sub New()
            _productTypeList = New List(Of ProductType)()

        End Sub

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

        ''' <summary>
        ''' 从数据库中获取所有的产品类型
        ''' </summary>
        Public Sub InitializeProductType()
            Dim dtTable As DataTable = DbConnect.DbConnector.GetAllProductTypes()
            _productTypeList.Clear()

            For Each row As DataRow In dtTable.Rows
                Dim id As Integer = CInt(row($"{ColNames.ID}"))
                Dim type As String = row($"{ColNames.ProductType}").ToString()

                _productTypeList.Add(New ProductType(id, type))
            Next
        End Sub

        Public Function GetAllProductType() As String()
            If _productTypeList.Count <= 0 Then Return New String() {}

            Dim result(_productTypeList.Count - 1) As String
            For i As Integer = 0 To _productTypeList.Count - 1
                result(i) = _productTypeList(i).ProductType
            Next
            Return result
        End Function

        Public Function GetProductType(index As Integer) As String
            Dim result As String = String.Empty
            For Each productType As ProductType In _productTypeList
                If productType.IDEqual(index) Then result = productType.ProductType : Exit For
            Next

            Return result
        End Function

        Public Function GetProductIndex(type As String) As Integer
            Dim result As Integer = -1
            For Each productType As ProductType In _productTypeList
                If productType.TypesEqual(type) Then result = productType.ID : Exit For
            Next
            Return result
        End Function
    End Class


    Public Class ProductType
        Public Property ID As Integer
        Public Property ProductType As String


        Sub New(id As Integer, type As String)
            Me.ID = id
            ProductType = type
        End Sub

        Public Function TypesEqual(type As String) As Boolean
            Return String.Compare(ProductType, type, True) = 0
        End Function

        Public Function IDEqual(index As Integer) As Boolean
            Return ID = index
        End Function
    End Class
End Namespace