Imports System.Text
Imports UTS_Core.Database

Namespace UTSModule.DbTableModel.Customer
    ''' <summary>
    ''' 客户订单表
    ''' </summary>
    Public Class OrdersTable
        Enum ColNames
            ''' <summary>
            ''' 订单索引
            ''' </summary>
            ID

            ''' <summary>
            ''' 公司索引
            ''' </summary>
            CompanyID

            ''' <summary>
            ''' 订单生产产品索引
            ''' </summary>
            ProductID

            ''' <summary>
            ''' 订单号
            ''' </summary>
            OrderNo

            ''' <summary>
            ''' 生产数量
            ''' </summary>
            OrderCount

            ''' <summary>
            ''' 交货日期
            ''' </summary>
            DeliveryTime

            ''' <summary>
            ''' 订单生成日期
            ''' </summary>
            CreateTime

            ''' <summary>
            ''' 修改日期
            ''' </summary>
            UpdateTime

            ''' <summary>
            ''' 成本单价
            ''' </summary>
            CostPrice

            ''' <summary>
            ''' 销售单价
            ''' </summary>
            TransactPrice

            ''' <summary>
            ''' 订单状态
            ''' </summary>
            OrderStatus
        End Enum

        Public Shared Property TableName() As String = "TBL_Orders"

        ''' <summary>
        ''' 建表语句
        ''' </summary>
        ''' <returns>建表语句</returns>
        Public Shared Function CreateTableString(dbName As String, Optional dbType As DbExecutor.DbTypeEnum = DbExecutor.DbTypeEnum.Mysql) As String
            Dim builder As New StringBuilder
            If dbType = DbExecutor.DbTypeEnum.Mysql Then
                builder.Append($"CREATE TABLE If Not Exists `{dbName}`.`{TableName}` ")
                builder.Append("(")
                builder.Append($"`{ColNames.ID}` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单索引',")
                builder.Append($"`{ColNames.CompanyID}` int(11) NOT NULL COMMENT '所属厂商索引',")
                builder.Append($"`{ColNames.ProductID}` int(11) NOT NULL COMMENT '订单生产产品索引',")
                builder.Append($"`{ColNames.OrderNo}` varchar(64) NOT NULL DEFAULT '' COMMENT '订单号',")
                builder.Append($"`{ColNames.OrderCount}` int(11) NOT NULL DEFAULT 0 COMMENT '生产数量',")
                builder.Append($"`{ColNames.DeliveryTime}` datetime DEFAULT NULL COMMENT '交货日期',")
                builder.Append($"`{ColNames.CreateTime}` datetime NOT NULL DEFAULT current_timestamp() COMMENT '订单生成日期',")
                builder.Append($"`{ColNames.UpdateTime}` datetime NOT NULL DEFAULT current_timestamp() COMMENT '更新时间',")
                builder.Append($"`{ColNames.CostPrice}` float DEFAULT NULL COMMENT '成本单价',")
                builder.Append($"`{ColNames.TransactPrice}` float DEFAULT NULL COMMENT '销售单价',")
                builder.Append($"`{ColNames.OrderStatus}` int(11) DEFAULT NULL COMMENT '订单状态',")
                builder.Append($"PRIMARY KEY (`{ColNames.ID}`) ")
                builder.Append(")")
                builder.Append("ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")
            Else
                Throw New Exception($"{TableName}-Invalid db type :{dbType}")
            End If

            Return builder.ToString()
        End Function
    End Class
End NameSpace