-- 训练任务相关数据库表结构
-- 创建时间: 2026-04-01

-- 训练任务表
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[training_tasks]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[training_tasks] (
        [Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
        [Name] NVARCHAR(200) NOT NULL,
        [Description] NVARCHAR(1000) NULL,
        [DatasetPath] NVARCHAR(500) NULL,
        [ModelPath] NVARCHAR(500) NULL,
        [ConfigJson] NVARCHAR(MAX) NULL,
        [Status] INT NOT NULL DEFAULT 0, -- 0:Draft, 1:Pending, 2:Running, 3:Paused, 4:Completed, 5:Failed, 6:Cancelled
        [Priority] INT NOT NULL DEFAULT 0, -- 0:Low, 1:Normal, 2:High
        [AssignedToId] UNIQUEIDENTIFIER NULL,
        [AssignedToName] NVARCHAR(100) NULL,
        [AssignedBy] NVARCHAR(100) NULL,
        [CurrentEpoch] INT NOT NULL DEFAULT 0,
        [TotalEpochs] INT NOT NULL DEFAULT 100,
        [CurrentLoss] FLOAT NOT NULL DEFAULT 0,
        [CurrentMap] FLOAT NOT NULL DEFAULT 0,
        [Progress] FLOAT NOT NULL DEFAULT 0, -- 0-100
        [StartedAtUtc] DATETIME2 NULL,
        [CompletedAtUtc] DATETIME2 NULL,
        [OutputModelPath] NVARCHAR(500) NULL,
        [FinalLoss] FLOAT NULL,
        [FinalMap] FLOAT NULL,
        [ErrorMessage] NVARCHAR(MAX) NULL,
        [CreatedAtUtc] DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
        [UpdatedAtUtc] DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
        [CreatedBy] NVARCHAR(100) NOT NULL,
        [UpdatedBy] NVARCHAR(100) NOT NULL,
        [Remark] NVARCHAR(500) NULL
    );
    
    CREATE INDEX [IX_training_tasks_Name] ON [dbo].[training_tasks] ([Name]);
    CREATE INDEX [IX_training_tasks_Status] ON [dbo].[training_tasks] ([Status]);
    CREATE INDEX [IX_training_tasks_AssignedToId] ON [dbo].[training_tasks] ([AssignedToId]);
    CREATE INDEX [IX_training_tasks_CreatedAtUtc] ON [dbo].[training_tasks] ([CreatedAtUtc]);
    CREATE INDEX [IX_training_tasks_Priority] ON [dbo].[training_tasks] ([Priority]);
END

-- 训练任务历史记录表
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[training_task_history]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[training_task_history] (
        [Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
        [TaskId] UNIQUEIDENTIFIER NOT NULL,
        [Action] NVARCHAR(50) NOT NULL, -- Created, Updated, StatusChanged, ProgressUpdated, Assigned, Completed, Failed, Cancelled
        [OldStatus] INT NULL,
        [NewStatus] INT NULL,
        [OldProgress] FLOAT NULL,
        [NewProgress] FLOAT NULL,
        [OldAssignedToId] UNIQUEIDENTIFIER NULL,
        [NewAssignedToId] UNIQUEIDENTIFIER NULL,
        [DetailsJson] NVARCHAR(MAX) NULL,
        [OperatedBy] NVARCHAR(100) NOT NULL,
        [OperatedAtUtc] DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
        [IpAddress] NVARCHAR(45) NULL,
        [UserAgent] NVARCHAR(500) NULL,
        CONSTRAINT [FK_training_task_history_training_tasks] FOREIGN KEY ([TaskId]) REFERENCES [dbo].[training_tasks] ([Id]) ON DELETE CASCADE
    );
    
    CREATE INDEX [IX_training_task_history_TaskId] ON [dbo].[training_task_history] ([TaskId]);
    CREATE INDEX [IX_training_task_history_Action] ON [dbo].[training_task_history] ([Action]);
    CREATE INDEX [IX_training_task_history_OperatedAtUtc] ON [dbo].[training_task_history] ([OperatedAtUtc]);
END

-- 训练任务标签表
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[training_task_tags]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[training_task_tags] (
        [Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
        [TaskId] UNIQUEIDENTIFIER NOT NULL,
        [Tag] NVARCHAR(50) NOT NULL,
        [CreatedAtUtc] DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
        [CreatedBy] NVARCHAR(100) NOT NULL,
        CONSTRAINT [FK_training_task_tags_training_tasks] FOREIGN KEY ([TaskId]) REFERENCES [dbo].[training_tasks] ([Id]) ON DELETE CASCADE,
        CONSTRAINT [UX_training_task_tags_TaskId_Tag] UNIQUE ([TaskId], [Tag])
    );
    
    CREATE INDEX [IX_training_task_tags_Tag] ON [dbo].[training_task_tags] ([Tag]);
END

-- 训练任务附件表
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[training_task_attachments]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[training_task_attachments] (
        [Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
        [TaskId] UNIQUEIDENTIFIER NOT NULL,
        [FileName] NVARCHAR(255) NOT NULL,
        [FilePath] NVARCHAR(500) NOT NULL,
        [FileSize] BIGINT NOT NULL,
        [FileType] NVARCHAR(50) NOT NULL,
        [Description] NVARCHAR(500) NULL,
        [CreatedAtUtc] DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
        [CreatedBy] NVARCHAR(100) NOT NULL,
        CONSTRAINT [FK_training_task_attachments_training_tasks] FOREIGN KEY ([TaskId]) REFERENCES [dbo].[training_tasks] ([Id]) ON DELETE CASCADE
    );
    
    CREATE INDEX [IX_training_task_attachments_TaskId] ON [dbo].[training_task_attachments] ([TaskId]);
    CREATE INDEX [IX_training_task_attachments_FileType] ON [dbo].[training_task_attachments] ([FileType]);
END

-- 插入示例训练任务数据
IF NOT EXISTS (SELECT * FROM [dbo].[training_tasks])
BEGIN
    INSERT INTO [dbo].[training_tasks] (
        [Id], [Name], [Description], [Status], [Priority], [CurrentEpoch], [TotalEpochs],
        [CurrentLoss], [CurrentMap], [Progress], [CreatedBy], [UpdatedBy]
    ) VALUES
        (NEWID(), 'YOLOv8目标检测训练', '使用YOLOv8训练工业零件目标检测模型', 4, 1, 100, 100, 0.125, 0.892, 100, 'SYSTEM', 'SYSTEM'),
        (NEWID(), 'ResNet分类训练', '使用ResNet50训练零件分类模型', 1, 2, 0, 50, 0, 0, 0, 'SYSTEM', 'SYSTEM'),
        (NEWID(), '缺陷检测模型训练', '训练表面缺陷检测模型', 2, 1, 25, 100, 0.234, 0.756, 25, 'SYSTEM', 'SYSTEM'),
        (NEWID(), '尺寸测量模型训练', '训练零件尺寸测量模型', 0, 0, 0, 80, 0, 0, 0, 'SYSTEM', 'SYSTEM'),
        (NEWID(), '颜色识别训练', '训练颜色识别分类模型', 5, 1, 80, 60, 0.456, 0.623, 100, 'SYSTEM', 'SYSTEM');
END

-- 插入示例标签数据
IF NOT EXISTS (SELECT * FROM [dbo].[training_task_tags])
BEGIN
    DECLARE @taskIds TABLE (Id UNIQUEIDENTIFIER);
    INSERT INTO @taskIds SELECT TOP(3) Id FROM [dbo].[training_tasks];
    
    INSERT INTO [dbo].[training_task_tags] ([Id], [TaskId], [Tag], [CreatedBy])
    SELECT NEWID(), t.Id, '目标检测', 'SYSTEM' FROM @taskIds t;
    
    INSERT INTO [dbo].[training_task_tags] ([Id], [TaskId], [Tag], [CreatedBy])
    SELECT NEWID(), t.Id, '工业零件', 'SYSTEM' FROM @taskIds t WHERE ROW_NUMBER() OVER (ORDER BY t.Id) <= 2;
    
    INSERT INTO [dbo].[training_task_tags] ([Id], [TaskId], [Tag], [CreatedBy])
    SELECT NEWID(), t.Id, '高优先级', 'SYSTEM' FROM @taskIds t WHERE ROW_NUMBER() OVER (ORDER BY t.Id) = 1;
END
