Imports System.Text
Imports UTS_Core.Database

Namespace UTSModule.DbTableModel.Customer
    ''' <summary>
    ''' 测试记录表,不同项目不同站表名不同
    ''' </summary>
    Public Class TestLogTable
        Enum ColNames
            ID
            ''' <summary>
            ''' 用户唯一标识
            ''' </summary>
            UserID

            ''' <summary>
            ''' 设备唯一索引
            ''' </summary>
            ServiceID

            ''' <summary>
            ''' 生产线索引
            ''' </summary>
            ProductionLineID

            ''' <summary>
            ''' 内部订单索引
            ''' </summary>
            OrderID

            ''' <summary>
            ''' 应用程序名
            ''' </summary>
            AppName

            ''' <summary>
            ''' 测试流程名
            ''' </summary>
            TestPlan

            ''' <summary>
            ''' 产品SN唯一索引
            ''' </summary>
            DUT_SN

            ''' <summary>
            ''' 测试起始时间
            ''' </summary>
            StartTime

            ''' <summary>
            ''' 测试耗时
            ''' </summary>
            UsedTime

            ''' <summary>
            ''' 测试结果
            ''' </summary>
            TestResult

            ''' <summary>
            ''' 错误代码
            ''' </summary>
            ErrCode

            ''' <summary>
            ''' 失败步骤集合
            ''' </summary>
            FailSteps

            ''' <summary>
            ''' 失败步骤集合
            ''' </summary>
            FailMsg

            ''' <summary>
            ''' 自定义字段,由实际使用中添加字段
            ''' </summary>
            CustomFiled
        End Enum


        ''' <summary>
        ''' 数据表名
        ''' </summary>
        ''' <param name="projectIndex">项目索引</param>
        ''' <param name="stationIndex">工艺站索引</param>
        ''' <returns></returns>
        Public Shared Function TableName(projectIndex As Integer, stationIndex As Integer) As String
            Return $"TBL_{projectIndex}_{stationIndex}_TestLog"
        End Function

        Public Shared ReadOnly CustomFiledType As String = $"varchar(16)"

        ''' <summary>
        ''' 建表语句
        ''' </summary>
        ''' <returns>建表语句</returns>
        Public Shared Function CreateTableString(dbName As String, tableName As String, Optional dbType As DbExecutor.DbTypeEnum = DbExecutor.DbTypeEnum.Mysql) As String
            Dim createStr As New StringBuilder
            If dbType = DbExecutor.DbTypeEnum.Sqlite Then
                createStr.Append($"Create Table If Not Exists `{tableName}`")
                createStr.Append(" (")
                createStr.Append($"`{ColNames.ID}` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,")
                createStr.Append($"`{ColNames.UserID}` INTEGER,")
                createStr.Append($"`{ColNames.ServiceID}` INTEGER,")
                createStr.Append($"`{ColNames.AppName}` varchar(64) DEFAULT NULL,")
                createStr.Append($"`{ColNames.TestPlan}` varchar(128) DEFAULT NULL,")
                createStr.Append($"`{ColNames.DUT_SN}` varchar(32) DEFAULT NULL,")
                createStr.Append($"`{ColNames.StartTime}` datetime DEFAULT NULL,")
                createStr.Append($"`{ColNames.UsedTime}` double DEFAULT NULL,")
                createStr.Append($"`{ColNames.TestResult}` int DEFAULT NULL,")
                createStr.Append($"`{ColNames.ErrCode}` varchar(16) DEFAULT NULL,")
                createStr.Append($"`{ColNames.FailSteps}` varchar(128) DEFAULT NULL,")
                createStr.Append($"`{ColNames.ProductionLineID}` int DEFAULT NULL,")
                createStr.Append($"`{ColNames.OrderID}` int DEFAULT NULL,")
                createStr.Append($"`{ColNames.FailMsg}` varchar(254) DEFAULT NULL")
                createStr.Append(" );")
            ElseIf dbType = DbExecutor.DbTypeEnum.Mysql Then
                createStr.Append($"Create Table If Not Exists `{dbName}`.`{tableName}`")
                createStr.Append(" (")
                createStr.Append($"`{ColNames.ID}` int(11) NOT NULL AUTO_INCREMENT ,")
                createStr.Append($"`{ColNames.UserID}` int,")
                createStr.Append($"`{ColNames.ServiceID}` int,")
                createStr.Append($"`{ColNames.AppName}` varchar(64) DEFAULT NULL,")
                createStr.Append($"`{ColNames.TestPlan}` varchar(128) DEFAULT NULL,")
                createStr.Append($"`{ColNames.DUT_SN}` varchar(32) DEFAULT NULL,")
                createStr.Append($"`{ColNames.StartTime}` datetime DEFAULT NULL,")
                createStr.Append($"`{ColNames.UsedTime}` double DEFAULT NULL,")
                createStr.Append($"`{ColNames.TestResult}` tinyint(1) DEFAULT NULL,")
                createStr.Append($"`{ColNames.ErrCode}` varchar(16) DEFAULT NULL,")
                createStr.Append($"`{ColNames.FailSteps}` varchar(128) DEFAULT NULL,")
                createStr.Append($"`{ColNames.ProductionLineID}` int DEFAULT NULL,")
                createStr.Append($"`{ColNames.OrderID}` int DEFAULT NULL,")
                createStr.Append($"`{ColNames.FailMsg}` varchar(254) DEFAULT NULL,")
                createStr.Append($"PRIMARY KEY (`{ColNames.ID}`)")
                createStr.Append(" );")
            Else
                Throw New Exception($"Unknown Type{dbType}!")
            End If

            Return createStr.ToString()
        End Function

    End Class
End Namespace