Imports System.Data.Common
Imports UTS_Core.Database
Imports UTS_Core.UTSModule.DbTableModel.Customer

Namespace UTSModule.Production
    Public Class ProductionLinesManager
        ''' <summary>产线管理器，全局唯一</summary>
        Private Shared _manager As ProductionLinesManager

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

        Private ReadOnly _lines As Dictionary(Of String, ProductionLineManager)


        Private Sub New()
            _lines = New Dictionary(Of String, ProductionLineManager)()

        End Sub

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


        Default Public ReadOnly Property Item(name As String) As ProductionLineManager
            Get
                Return _lines(name)
            End Get
        End Property

        Default Public ReadOnly Property Item(index As Integer) As ProductionLineManager
            Get
                Return _lines.Values(index)
            End Get
        End Property

        ''' <summary>
        ''' 获取所有产线的名字
        ''' </summary>
        ''' <returns></returns>
        Public Function GetLineNames() As String()
            Return _lines.Keys.ToArray()
        End Function


        Public Sub LoadProductionLines()
            _lines.Clear()

            Using db As New DbExecutor(UtsDb.LocalDbType, UtsDb.LocalConnString)
                db.Open()

                Dim cmdText As String = db.CmdHelper.SearchAll(ProductionLineTable.TableName)

                Using dbReader As DbDataReader = db.ExecuteReader(cmdText)
                    While dbReader.Read()
                        Dim id As Integer
                        Dim name As String
                        Dim desc As String
                        Dim remark As String

                        Dim tmpItem As Object

                        tmpItem = dbReader($"{ProductionLineTable.ColNames.ID}")
                        If IsDBNull(tmpItem) Then
                            Throw New Exception($"Load ProductionLines Error:ID is Null.")
                        Else
                            id = CInt(tmpItem)
                        End If

                        tmpItem = dbReader($"{ProductionLineTable.ColNames.Name}")
                        If IsDBNull(tmpItem) Then
                            Throw New Exception($"Load ProductionLines Error:Name is Null.")
                        Else
                            name = CStr(tmpItem)
                        End If

                        tmpItem = dbReader($"{ProductionLineTable.ColNames.Description}")
                        If IsDBNull(tmpItem) Then
                            desc = String.Empty
                        Else
                            desc = CStr(tmpItem)
                        End If

                        tmpItem = dbReader($"{ProductionLineTable.ColNames.Remark}")
                        If IsDBNull(tmpItem) Then
                            remark = String.Empty
                        Else
                            remark = CStr(tmpItem)
                        End If


                        _lines.Add(name, New ProductionLineManager(id, name, desc, remark))
                    End While
                    dbReader.Close()
                End Using

                db.Close()
            End Using
        End Sub

    End Class

End Namespace