Imports System.Threading
Imports UTS_Core.UTSModule.DbTableModel.Customer

Namespace UTSModule.Production
    ''' <summary>
    ''' 内部订单信息
    ''' </summary>
    Public Class InternalOrderManager


        Private Sub New()
            ID = -1
            OrderID = -1
        End Sub

        ''' <summary>
        ''' 内部订单索引
        ''' </summary>
        Public Property ID As Integer

        ''' <summary>
        ''' 客户订单索引
        ''' </summary>
        Public Property OrderID As Integer

        ''' <summary>
        ''' 内部订单号字符串
        ''' </summary>
        Public Property InternalNo As String

        ''' <summary>
        ''' 公司索引
        ''' </summary>
        Public Property CompanyID As Integer

        ''' <summary>
        ''' 产品索引
        ''' </summary>
        Public Property ProductID As Integer

        ''' <summary>
        ''' 产品类型索引
        ''' </summary>
        Public Property ProductTypeID As Integer


        ''' <summary>
        ''' 内部订单创建时间
        ''' </summary>
        Public Property CreateTime As Date

        ''' <summary>
        ''' 订单交期
        ''' </summary>
        Public Property DeliveryTime As Date


        ''' <summary>
        ''' 目标产能
        ''' </summary>
        Public Property ObjectiveYield As Integer

        ''' <summary>
        ''' 当前总产能
        ''' </summary>
        Public Property ObjectiveYieldTotal As Integer

        ''' <summary>
        ''' 内部订单状态
        ''' </summary>
        Public Property OrderStatus As Integer

        ''' <summary>内部订单管理器</summary>
        Private Shared _manager As InternalOrderManager

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

        ''' <summary>
        ''' 创建内部订单管理器单例
        ''' </summary>
        ''' <returns></returns>
        Public Shared Function CreateManager() As InternalOrderManager
            If _manager Is Nothing Then
                SyncLock InitLock
                    Thread.MemoryBarrier()
                    If _manager Is Nothing Then
                        _manager = New InternalOrderManager()
                    End If
                End SyncLock
            End If

            Return _manager
        End Function

        ''' <summary>
        ''' 初始化指定内部订单索引的订单详情
        ''' </summary>
        ''' <param name="internalOrderID"></param>
        Public Sub Initialize(internalOrderID As Integer)
            Dim dtTable As DataTable = DbConnect.DbConnector.GetInternalOrder(internalOrderID)
            If dtTable.Rows.Count = 0 Then Return

            ID = internalOrderID

            If Integer.TryParse(dtTable(0)($"{OrderInternalTable.ColNames.OrderID}").ToString, OrderID) = False Then
                OrderID = -1
            End If
            ' OrderID = CInt(dtTable(0)($"{OrderInternalTable.ColNames.OrderID}"))

            If Integer.TryParse(dtTable(0)($"{OrderInternalTable.ColNames.OrderStatus}").ToString, OrderStatus) = False Then
                OrderStatus = 4
            End If
            'OrderStatus = CInt(dtTable(0)($"{OrderInternalTable.ColNames.OrderStatus}"))

            If Integer.TryParse(dtTable(0)($"{OrderInternalTable.ColNames.CompanyID}").ToString, CompanyID) = False Then
                CompanyID = -1
            End If
            ' CompanyID = CInt(dtTable(0)($"{OrderInternalTable.ColNames.CompanyID}"))

            If Integer.TryParse(dtTable(0)($"{OrderInternalTable.ColNames.ProductID}").ToString, ProductID) = False Then
                ProductID = -1
            End If
            ' ProductID = CInt(dtTable(0)($"{OrderInternalTable.ColNames.ProductID}"))

            If Integer.TryParse(dtTable(0)($"{OrderInternalTable.ColNames.ProductTypeID}").ToString, ProductTypeID) = False Then
                ProductTypeID = -1
            End If
            'ProductTypeID = CInt(dtTable(0)($"{OrderInternalTable.ColNames.ProductTypeID}"))

            If Integer.TryParse(dtTable(0)($"{OrderInternalTable.ColNames.ObjectiveYield}").ToString, ObjectiveYield) = False Then
                ObjectiveYield = -1
            End If
            'ObjectiveYield = CInt(dtTable(0)($"{OrderInternalTable.ColNames.ObjectiveYield}"))

            If Integer.TryParse(dtTable(0)($"{OrderInternalTable.ColNames.ObjectiveYieldTotal}").ToString, ObjectiveYieldTotal) = False Then
                ObjectiveYieldTotal = -1
            End If
            'ObjectiveYieldTotal = CInt(dtTable(0)($"{OrderInternalTable.ColNames.ObjectiveYieldTotal}"))

            If Date.TryParse(dtTable(0)($"{OrderInternalTable.ColNames.CreateTime}").ToString, CreateTime) = False Then
                CreateTime = Now
            End If
            'CreateTime = CDate(dtTable(0)($"{OrderInternalTable.ColNames.CreateTime}"))

            If Date.TryParse(dtTable(0)($"{OrderInternalTable.ColNames.DeliveryTime}").ToString, DeliveryTime) = False Then
                DeliveryTime = Now
            End If
            ' DeliveryTime = CDate(dtTable(0)($"{OrderInternalTable.ColNames.DeliveryTime}"))

            InternalNo = CStr(dtTable(0)($"{OrderInternalTable.ColNames.InternalNo}"))
        End Sub

    End Class

End Namespace